1020 |
aurelien |
1 |
<?php
|
|
|
2 |
// declare(encoding='UTF-8');
|
|
|
3 |
/**
|
|
|
4 |
* Le script récupère tous les commentaires et déterminations fait ce jour et envoie un mail
|
|
|
5 |
* aux auteurs des observations récapitulant celle-ci
|
|
|
6 |
*
|
|
|
7 |
* @category php 5.2
|
|
|
8 |
* @package del
|
|
|
9 |
* @subpackage scripts
|
|
|
10 |
* @author Aurélien PERONNET <aurelien@tela-botanica.org>
|
|
|
11 |
* @copyright Copyright (c) 2012, Tela Botanica (accueil@tela-botanica.org)
|
|
|
12 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
|
|
|
13 |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
|
|
|
14 |
* @version $Id$
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
class AlerteMailCommentaires {
|
|
|
18 |
|
|
|
19 |
private $parametres;
|
|
|
20 |
private $conteneur;
|
|
|
21 |
|
|
|
22 |
private $observations_concernees = array();
|
|
|
23 |
private $id_observation_commentees = array();
|
|
|
24 |
private $correspondance_id_obs_auteurs = array();
|
|
|
25 |
|
|
|
26 |
public function __construct($parametres, $conteneur) {
|
|
|
27 |
$this->parametres = $parametres;
|
|
|
28 |
$this->conteneur = $conteneur;
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
public function executer() {
|
|
|
32 |
try {
|
|
|
33 |
$observations = $this->chargerObservationsCommentees();
|
|
|
34 |
$this->formaterObservations($observations);
|
|
|
35 |
|
|
|
36 |
$commentaires = $this->chargerCommentairesAjourdhui();
|
|
|
37 |
$this->formaterCommentaires($commentaires);
|
|
|
38 |
$this->parcourirListeCommentairesEtEnvoyerMessage($this->observations_concernees);
|
|
|
39 |
} catch (Exception $e) {
|
|
|
40 |
$this->traiterErreur($e->getMessage());
|
|
|
41 |
}
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
protected function chargerCommentairesAjourdhui() {
|
|
|
45 |
//TODO: optimiser la requête si beaucoup de mails sont envoyés
|
|
|
46 |
$requete = "SELECT * ".
|
|
|
47 |
"FROM del_commentaire dc ".
|
|
|
48 |
"WHERE dc.ce_observation != '0' ".
|
|
|
49 |
"AND dc.id_commentaire NOT IN ".
|
|
|
50 |
"( ".
|
|
|
51 |
" SELECT dc.id_commentaire ".
|
|
|
52 |
" FROM del_commentaire dc, del_observation dob".
|
|
|
53 |
" WHERE dc.nom_sel = dob.nom_sel ".
|
|
|
54 |
" AND dc.ce_utilisateur = dob.ce_utilisateur ".
|
|
|
55 |
" AND dc.ce_observation = dob.id_observation ".
|
|
|
56 |
") ".
|
|
|
57 |
"AND DATE_FORMAT( dc.date, '%Y-%m-%d' ) = CURDATE()".
|
|
|
58 |
"ORDER BY dc.ce_observation";
|
|
|
59 |
$commentaires = $this->conteneur->getBdd()->recupererTous($requete);
|
|
|
60 |
return $commentaires;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
protected function chargerObservationsCommentees() {
|
|
|
64 |
//TODO: optimiser la requête si beaucoup de mails sont envoyés
|
|
|
65 |
$requete = "SELECT * FROM del_observation do ".
|
|
|
66 |
"WHERE do.id_observation IN ".
|
|
|
67 |
"( ".
|
|
|
68 |
"SELECT dc.ce_observation ".
|
|
|
69 |
"FROM del_commentaire dc ".
|
|
|
70 |
"WHERE dc.ce_observation != '0' ".
|
|
|
71 |
"AND dc.id_commentaire NOT IN ".
|
|
|
72 |
"( ".
|
|
|
73 |
" SELECT dc.id_commentaire ".
|
|
|
74 |
" FROM del_commentaire dc, del_observation dob".
|
|
|
75 |
" WHERE dc.nom_sel = dob.nom_sel ".
|
|
|
76 |
" AND dc.ce_utilisateur = dob.ce_utilisateur ".
|
|
|
77 |
" AND dc.ce_observation = dob.id_observation ".
|
|
|
78 |
") ".
|
|
|
79 |
"AND DATE_FORMAT( dc.date, '%Y-%m-%d' ) = CURDATE()".
|
|
|
80 |
") ".
|
|
|
81 |
"ORDER BY do.ce_utilisateur, do.id_observation ";
|
|
|
82 |
$observations = $this->conteneur->getBdd()->recupererTous($requete);
|
|
|
83 |
return $observations;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
protected function formaterObservations($liste_obs) {
|
|
|
87 |
foreach ($liste_obs as $obs) {
|
|
|
88 |
$id_obs = $obs['id_observation'];
|
|
|
89 |
$auteur_obs = $obs['ce_utilisateur'];
|
|
|
90 |
$obs['date'] = $this->formaterDateObs($obs['date_observation']);
|
1036 |
aurelien |
91 |
$obs['lien'] = $this->obtenirLienFicheObs($obs['id_observation']);
|
1020 |
aurelien |
92 |
$this->correspondance_id_obs_auteurs[$id_obs] = $auteur_obs;
|
|
|
93 |
$this->observations_concernees[$auteur_obs][$id_obs] = $obs;
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
protected function formaterCommentaires($liste_commentaires) {
|
|
|
98 |
$liste_commentaires_formatee = array();
|
|
|
99 |
foreach ($liste_commentaires as $commentaire) {
|
|
|
100 |
$id_obs = $commentaire['ce_observation'];
|
|
|
101 |
$auteur_obs = $this->correspondance_id_obs_auteurs[$commentaire['ce_observation']];
|
|
|
102 |
$commentaire['auteur'] = $this->formaterAuteur($commentaire);
|
|
|
103 |
$this->observations_concernees[$auteur_obs][$id_obs]['commentaires'][] = $commentaire;
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
protected function parcourirListeCommentairesEtEnvoyerMessage($liste_obs_a_auteur) {
|
|
|
108 |
foreach ($liste_obs_a_auteur as $auteur => $liste_obs) {
|
|
|
109 |
$message = $this->formaterMessagePourAuteur($liste_obs,$auteur);
|
|
|
110 |
$this->envoyerMessage($message, $auteur);
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
private function formaterMessagePourAuteur($liste_obs, $auteur) {
|
|
|
115 |
$donnees = array();
|
|
|
116 |
$donnees['liste_observations'] = $liste_obs;
|
|
|
117 |
$donnees['auteur'] = $auteur;
|
|
|
118 |
$this->squelette_dossier = dirname(__FILE__).DIRECTORY_SEPARATOR.'squelettes'.DIRECTORY_SEPARATOR;
|
|
|
119 |
return $this->conteneur->traiterSquelettePhp($this->squelette_dossier.'commentaires.tpl.html', $donnees);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
protected function formaterDateObs($date) {
|
|
|
123 |
$time = strtotime($date);
|
|
|
124 |
return date('d/m/Y', $time);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
private function formaterAuteur($commentaire) {
|
|
|
128 |
return $commentaire['utilisateur_prenom'].' '.$commentaire['utilisateur_nom'];
|
|
|
129 |
}
|
|
|
130 |
|
1036 |
aurelien |
131 |
private function obtenirLienFicheObs($id_obs) {
|
|
|
132 |
return $this->conteneur->formaterUrl(Config::get('url_fiche_observation'), array('id' => $id_obs));
|
|
|
133 |
}
|
|
|
134 |
|
1020 |
aurelien |
135 |
protected function envoyerMessage($message, $auteur) {
|
|
|
136 |
|
|
|
137 |
$url_requete = $this->conteneur->formaterUrl(Config::get('url_service_messagerie'), array('id' => $auteur));
|
|
|
138 |
$post = array(
|
|
|
139 |
'sujet' => Config::get('titre_message_recapitulatif'),
|
|
|
140 |
'message' => $message,
|
|
|
141 |
'utilisateur_courriel' => Config::get('adresse_expediteur_recapitulatif'),
|
|
|
142 |
'format' => 'html'
|
|
|
143 |
);
|
|
|
144 |
|
|
|
145 |
$ch = curl_init();
|
|
|
146 |
$options = array(CURLOPT_URL => $url_requete,
|
|
|
147 |
CURLOPT_HEADER => false,
|
|
|
148 |
CURLOPT_POST => 1,
|
|
|
149 |
CURLOPT_POSTFIELDS => $post,
|
|
|
150 |
CURLOPT_RETURNTRANSFER => true
|
|
|
151 |
);
|
|
|
152 |
curl_setopt_array($ch, $options);
|
|
|
153 |
$envoi = curl_exec($ch);
|
|
|
154 |
curl_close($ch);
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
?>
|