1227 |
gduche |
1 |
<?php
|
|
|
2 |
/**
|
1253 |
jpm |
3 |
* Service fournissant des informations concernant les votes sur les images de DEL en fonction d'un protocole
|
1227 |
gduche |
4 |
* au format RSS1, RSS2 ou ATOM.
|
|
|
5 |
* Encodage en entrée : utf8
|
|
|
6 |
* Encodage en sortie : utf8
|
1253 |
jpm |
7 |
*
|
1227 |
gduche |
8 |
* @author Grégoire DUCHE <gregoire@tela-botanica.org>
|
|
|
9 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
10 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
11 |
* @version $Id$
|
|
|
12 |
* @copyright 2010
|
|
|
13 |
*/
|
|
|
14 |
class SyndicationVotesParProtocole {
|
|
|
15 |
/**
|
|
|
16 |
* Paramètres du service
|
|
|
17 |
* */
|
|
|
18 |
private $ressources = null;
|
|
|
19 |
private $parametres = null;
|
|
|
20 |
private $format = null;
|
|
|
21 |
private $service = null;
|
|
|
22 |
private $squelette = null;
|
|
|
23 |
private $squelette_dossier = null;
|
|
|
24 |
private $masque = array();
|
|
|
25 |
private $mappingFiltre = array();
|
|
|
26 |
private $conteneur = null;
|
|
|
27 |
private $gestionBdd = null;
|
|
|
28 |
private $navigation = null;
|
|
|
29 |
private $type_rss = null;
|
1253 |
jpm |
30 |
|
1227 |
gduche |
31 |
/**
|
|
|
32 |
* Constructeur
|
|
|
33 |
* Initialiser les configurations
|
|
|
34 |
* */
|
|
|
35 |
public function __construct(Conteneur $conteneur = null) {
|
|
|
36 |
$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
|
|
|
37 |
$this->conteneur->chargerConfiguration('config_syndication_votesparprotocole.ini');
|
|
|
38 |
$this->mappingFiltre = $this->conteneur->getParametre('mapping_masque');
|
|
|
39 |
$this->masque = $conteneur->getMasque();
|
|
|
40 |
$this->gestionBdd = $conteneur->getGestionBdd();
|
|
|
41 |
$this->navigation = $conteneur->getNavigation();
|
|
|
42 |
}
|
1253 |
jpm |
43 |
|
1227 |
gduche |
44 |
/**
|
1253 |
jpm |
45 |
* Consulter
|
1227 |
gduche |
46 |
* Méthode par défaut pour récupérer l'ensemble des votes.
|
|
|
47 |
* Vérifie la configuration et retourne les derniers votes formatés
|
|
|
48 |
* */
|
1247 |
gduche |
49 |
public function consulter($params = array()) {
|
|
|
50 |
$this->verifierConfiguration();
|
1227 |
gduche |
51 |
$this->type_rss = $params[1];
|
|
|
52 |
if ($this->fluxAdminDemande()) {
|
|
|
53 |
$this->demanderAutorisationAdmin();
|
1253 |
jpm |
54 |
}
|
|
|
55 |
|
1227 |
gduche |
56 |
$donnees_brutes = $this->getDerniersVotesImage();
|
1253 |
jpm |
57 |
$commentaires_formates = $this->formaterPourRss($donnees_brutes) ;
|
1227 |
gduche |
58 |
return $commentaires_formates;
|
|
|
59 |
}
|
1253 |
jpm |
60 |
|
1227 |
gduche |
61 |
/**
|
|
|
62 |
* Vérifier que le service est bien configuré
|
|
|
63 |
* */
|
|
|
64 |
public function verifierConfiguration() {
|
|
|
65 |
$erreurs = array();
|
|
|
66 |
$tableauImages = $this->conteneur->getParametre('mapping_masque');
|
1253 |
jpm |
67 |
|
1227 |
gduche |
68 |
if (empty($this->mappingFiltre)) {
|
|
|
69 |
$erreurs[] = '- le fichier de configuration ne contient pas le tableau [mapping_masque] ou celui-ci est vide ;';
|
|
|
70 |
} else {
|
|
|
71 |
$champsMappingFiltre = array('image', 'protocole');
|
|
|
72 |
foreach ($champsMappingFiltre as $champ) {
|
|
|
73 |
if (!isset($this->mappingFiltre[$champ])) {
|
|
|
74 |
$erreurs[] = '- le mapping du champ "'.$champ.'" pour le commentaire est manquant ;';
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
}
|
1253 |
jpm |
78 |
|
1227 |
gduche |
79 |
if (!empty($erreurs)) {
|
|
|
80 |
$e = 'Erreur lors de la configuration : '."\n";
|
|
|
81 |
$e .= implode("\n", $erreurs);
|
|
|
82 |
throw new Exception($e, RestServeur::HTTP_CODE_ERREUR);
|
|
|
83 |
}
|
|
|
84 |
}
|
1253 |
jpm |
85 |
|
1227 |
gduche |
86 |
/**
|
|
|
87 |
* Verifier si le flux admin est demandé
|
|
|
88 |
*/
|
|
|
89 |
private function fluxAdminDemande() {
|
1253 |
jpm |
90 |
return $this->conteneur->getParametre('admin') != null && $this->conteneur->getParametre('admin') == 1;
|
1227 |
gduche |
91 |
}
|
1253 |
jpm |
92 |
|
1227 |
gduche |
93 |
/**
|
|
|
94 |
* Si le flux est un flux admin, demander un mot de passe
|
|
|
95 |
* */
|
|
|
96 |
private function demanderAutorisationAdmin() {
|
|
|
97 |
$verification = new ControleAcces($this->conteneur);
|
|
|
98 |
$verification->demanderAuthentificationAdmin();
|
|
|
99 |
}
|
1253 |
jpm |
100 |
|
1227 |
gduche |
101 |
/**
|
|
|
102 |
* Formater les données pour mettre en page le RSS
|
|
|
103 |
* */
|
|
|
104 |
private function formaterPourRss($elements) {
|
|
|
105 |
$donnees = $this->construireDonneesCommunesAuFlux($elements);
|
|
|
106 |
foreach ($elements as $element) {
|
1229 |
gduche |
107 |
$identifiants[$element['id_vote']] = $element['id_vote'];
|
1227 |
gduche |
108 |
}
|
|
|
109 |
foreach ($elements as $element) {
|
|
|
110 |
$donnees['items'][] = $this->construireDonneesCommunesAuxItems($element);
|
|
|
111 |
}
|
|
|
112 |
return $donnees;
|
|
|
113 |
}
|
1253 |
jpm |
114 |
|
1227 |
gduche |
115 |
/**
|
|
|
116 |
* Générer les métadonnées du flux (titre, dates, editeur etc.)
|
|
|
117 |
* */
|
|
|
118 |
private function construireDonneesCommunesAuFlux($infos) {
|
|
|
119 |
$donnees = array();
|
|
|
120 |
$donnees['guid'] = '';
|
1284 |
jpm |
121 |
$donnees['titre'] = 'pictoFlora : votes';
|
|
|
122 |
$donnees['description'] = 'Ce flux regroupe les derniers votes sur les images de pictoFlora';
|
1227 |
gduche |
123 |
$donnees['lien_service'] = $this->creerUrlService();
|
|
|
124 |
$donnees['lien_del'] = $this->conteneur->getParametre('pictoAppliLien');
|
|
|
125 |
$donnees['editeur'] = $this->conteneur->getParametre('editeur');
|
|
|
126 |
$derniere_info_en_date = reset($infos);
|
1229 |
gduche |
127 |
$date_modification_timestamp = strtotime($derniere_info_en_date['date_vote']);
|
1227 |
gduche |
128 |
$donnees['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
|
|
|
129 |
$donnees['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
|
|
|
130 |
$donnees['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
|
|
|
131 |
$donnees['annee_courante'] = date('Y');
|
|
|
132 |
$donnees['generateur'] = 'DEL - SyndicationCommentaire';
|
|
|
133 |
$donnees['generateur_version'] = (preg_match('/([0-9]+)/', '$Revision$', $match)) ? $match[1] : '0';
|
1253 |
jpm |
134 |
return $donnees;
|
1227 |
gduche |
135 |
}
|
1253 |
jpm |
136 |
|
1227 |
gduche |
137 |
/**
|
|
|
138 |
* Générer le lien du flux RSS
|
|
|
139 |
* */
|
|
|
140 |
private function creerUrlService() {
|
|
|
141 |
$url_service = $this->conteneur->getParametre('url_service');
|
|
|
142 |
$url_service .= '/'.$this->type_rss;
|
|
|
143 |
//$url_service = '';
|
|
|
144 |
if (count($_GET) > 0) {
|
|
|
145 |
$parametres_get = array();
|
|
|
146 |
foreach ($_GET as $cle => $valeur) {
|
|
|
147 |
$parametres_get[] = $cle.'='.$valeur;
|
|
|
148 |
}
|
|
|
149 |
$url_service .= '?'.implode('&', $parametres_get);
|
|
|
150 |
}
|
|
|
151 |
return $url_service;
|
|
|
152 |
}
|
1253 |
jpm |
153 |
|
1227 |
gduche |
154 |
/**
|
|
|
155 |
* Générer les données communes & spécifiques à chaque item
|
|
|
156 |
* */
|
|
|
157 |
private function construireDonneesCommunesAuxItems($info) {
|
|
|
158 |
$item = array();
|
1229 |
gduche |
159 |
$date_modification_timestamp = strtotime($info['date_vote']);
|
1227 |
gduche |
160 |
$item['date_maj_simple'] = strftime('%A %d %B %Y à %H:%M', $date_modification_timestamp);
|
|
|
161 |
$item['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
|
|
|
162 |
$item['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
|
|
|
163 |
$item['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
|
|
|
164 |
$item['titre'] = $this->creerTitre($info);
|
|
|
165 |
$item['guid'] = $this->creerGuidItem($info);
|
|
|
166 |
$item['lien'] = $this->creerLienItem($info);
|
|
|
167 |
$item['categorie'] = $this->creerCategorie($item);
|
|
|
168 |
$item['description'] = $this->creerDescription($info, $item);
|
|
|
169 |
$item['description_encodee'] = htmlspecialchars($this->creerDescription($info, $item));
|
|
|
170 |
$item['modifie_par'] = $this->creerAuteur($info);
|
|
|
171 |
return $item;
|
|
|
172 |
}
|
1253 |
jpm |
173 |
|
1227 |
gduche |
174 |
private function creerCategorie($element) {
|
|
|
175 |
$categorie = 'Vote protocole';
|
|
|
176 |
$categorie = htmlentities($categorie);
|
|
|
177 |
return $categorie;
|
|
|
178 |
}
|
1253 |
jpm |
179 |
|
1227 |
gduche |
180 |
private function creerGuidItem($element) {
|
|
|
181 |
$guid = sprintf($this->conteneur->getParametre('voteParProtocole'), $element['id_vote']);
|
|
|
182 |
return $guid;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
private function creerLienItem($element) {
|
1285 |
jpm |
186 |
$lien = sprintf($this->conteneur->getParametre('pictofloraFicheObsTpl'), $element['id_observation']);
|
1227 |
gduche |
187 |
return $lien;
|
|
|
188 |
}
|
1253 |
jpm |
189 |
|
1227 |
gduche |
190 |
private function creerTitre($element) {
|
1253 |
jpm |
191 |
$noteVote = $element['valeur'];
|
|
|
192 |
$nomSci = htmlspecialchars($element['nom_sel']);
|
1285 |
jpm |
193 |
$votant = array('prenom' => $element['votant_prenom'], 'nom' => $element['votant_nom']);
|
|
|
194 |
$votantTxt = htmlspecialchars($this->creerAuteur($votant));
|
1247 |
gduche |
195 |
$observateur = array('prenom' => $element['observateur_prenom'], 'nom' => $element['observateur_nom']);
|
1253 |
jpm |
196 |
$observateurTxt = htmlspecialchars($this->creerAuteur($observateur));
|
|
|
197 |
|
1285 |
jpm |
198 |
$titre = "Vote $noteVote par $votantTxt pour $nomSci de $observateurTxt";
|
1227 |
gduche |
199 |
return $titre;
|
|
|
200 |
}
|
1253 |
jpm |
201 |
|
1227 |
gduche |
202 |
private function creerDescription($donnees, $item) {
|
1285 |
jpm |
203 |
$idVote = htmlspecialchars($donnees['id_vote']);
|
|
|
204 |
$idObs = htmlspecialchars($donnees['id_observation']);
|
1284 |
jpm |
205 |
$idImg = htmlspecialchars($donnees['ce_image']);
|
|
|
206 |
$urlImg = $this->getUrlImage($donnees['ce_image']);
|
|
|
207 |
$miniatureUrl = $this->getUrlImage($donnees['ce_image'], 'CRX2S');
|
|
|
208 |
$proposition = htmlspecialchars($donnees['nom_sel']);
|
|
|
209 |
$protocole = htmlspecialchars($donnees['intitule']);
|
|
|
210 |
$votant = array('prenom' => $donnees['votant_prenom'], 'nom' => $donnees['votant_nom']);
|
|
|
211 |
$votantTxt = htmlspecialchars($this->creerAuteur($votant));
|
1247 |
gduche |
212 |
$observateur = array('prenom' => $donnees['observateur_prenom'], 'nom' => $donnees['observateur_nom']);
|
|
|
213 |
$observateurTxt = htmlspecialchars($this->creerAuteur($observateur));
|
1253 |
jpm |
214 |
|
1247 |
gduche |
215 |
$description = '<ul>'.
|
1285 |
jpm |
216 |
"<li>Vote pictoFlora #$idVote</li>".
|
1284 |
jpm |
217 |
'<li>'.
|
|
|
218 |
' <a href="'.$urlImg.'">'.
|
|
|
219 |
' <img src="'.$miniatureUrl.'" alt="Img #'.$idImg.'"/>'.
|
|
|
220 |
' Image #'.$idImg.
|
|
|
221 |
' </a>'.
|
|
|
222 |
'</li>'.
|
|
|
223 |
"<li>Auteur de l'image : $observateurTxt</li>".
|
1285 |
jpm |
224 |
"<li>Observation #$idObs : <em>$proposition</em></li>".
|
1284 |
jpm |
225 |
"<li>Protocole : $protocole</li>".
|
|
|
226 |
'<li>Valeur : <strong>'.$donnees['valeur'].'</strong>/5</li>'.
|
|
|
227 |
'<li>Votant : '.$votantTxt.'</li>'.
|
1247 |
gduche |
228 |
'</ul>';
|
1227 |
gduche |
229 |
return $description;
|
|
|
230 |
}
|
1253 |
jpm |
231 |
|
1284 |
jpm |
232 |
private function getUrlImage($id, $format = 'L') {
|
|
|
233 |
$url_tpl = $this->conteneur->getParametre('celImgUrlTpl');
|
|
|
234 |
$id = sprintf('%09s', $id).$format;
|
|
|
235 |
$url = sprintf($url_tpl, $id);
|
|
|
236 |
return $url;
|
|
|
237 |
}
|
|
|
238 |
|
1227 |
gduche |
239 |
private function creerAuteur($info) {
|
1284 |
jpm |
240 |
$intitule = 'Anonyme';
|
1248 |
gduche |
241 |
if (isset($info['prenom']) && isset($info['nom'])) {
|
|
|
242 |
$intitule = $info['prenom'].' '.$info['nom'];
|
|
|
243 |
}
|
|
|
244 |
return $intitule;
|
1227 |
gduche |
245 |
}
|
1253 |
jpm |
246 |
|
1227 |
gduche |
247 |
/**
|
1284 |
jpm |
248 |
* Retrouver les derniers votes image
|
|
|
249 |
* */
|
|
|
250 |
private function getDerniersVotesImage() {
|
|
|
251 |
$requete = 'SELECT DISTINCT id_vote, ce_image, valeur, divo.date AS date_vote, '.
|
|
|
252 |
' duo.prenom AS observateur_prenom, duo.nom AS observateur_nom, '.
|
|
|
253 |
' duv.prenom AS votant_prenom, duv.nom AS votant_nom, '.
|
1285 |
jpm |
254 |
' do.id_observation, do.nom_sel, dip.intitule '.
|
1284 |
jpm |
255 |
'FROM del_image_vote AS divo '.
|
|
|
256 |
' INNER JOIN del_obs_image AS doi '.
|
|
|
257 |
' ON divo.ce_image = doi.id_image '.
|
|
|
258 |
' INNER JOIN del_observation AS do '.
|
|
|
259 |
' ON do.id_observation = doi.id_observation '.
|
|
|
260 |
' INNER JOIN del_image_protocole AS dip '.
|
|
|
261 |
' ON ce_protocole = id_protocole '.
|
|
|
262 |
' LEFT JOIN del_utilisateur AS duo '.
|
|
|
263 |
' ON do.ce_utilisateur = duo.id_utilisateur '.
|
|
|
264 |
' LEFT JOIN del_utilisateur AS duv '.
|
1285 |
jpm |
265 |
' ON CAST(divo.ce_utilisateur AS UNSIGNED) = duv.id_utilisateur '.
|
1284 |
jpm |
266 |
$this->chargerClauseWhere().' '.
|
|
|
267 |
'ORDER BY divo.date DESC '.
|
|
|
268 |
'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite();
|
|
|
269 |
|
|
|
270 |
$elements = $this->gestionBdd->getBdd()->recupererTous($requete);
|
|
|
271 |
return $elements;
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
/**
|
1227 |
gduche |
275 |
* Charger la clause WHERE en fonction des paramètres de masque
|
|
|
276 |
* */
|
|
|
277 |
private function chargerClauseWhere() {
|
|
|
278 |
$where = array();
|
|
|
279 |
$tableauMasque = $this->masque->getMasque();
|
|
|
280 |
if (!empty($tableauMasque)) {
|
|
|
281 |
foreach($tableauMasque as $idMasque => $valeurMasque) {
|
|
|
282 |
$idMasque = str_replace('masque.', '', $idMasque);
|
|
|
283 |
switch ($idMasque) {
|
|
|
284 |
case 'image':
|
1253 |
jpm |
285 |
$where[] = ' '.$this->mappingFiltre[$idMasque].' = '.$this->gestionBdd->getBdd()->proteger($valeurMasque);
|
1227 |
gduche |
286 |
break;
|
|
|
287 |
case 'protocole':
|
1253 |
jpm |
288 |
$where[] = ' '.$this->mappingFiltre[$idMasque].' = '.$this->gestionBdd->getBdd()->proteger($valeurMasque).' ';
|
1227 |
gduche |
289 |
break;
|
|
|
290 |
default:
|
|
|
291 |
$where[] = ' '.$this->mappingFiltre[$idMasque].' = '.$this->gestionBdd->getBdd()->proteger($valeurMasque);
|
|
|
292 |
break;
|
|
|
293 |
}
|
1253 |
jpm |
294 |
}
|
1227 |
gduche |
295 |
}
|
|
|
296 |
if (!empty($where)) {
|
|
|
297 |
return ' WHERE '.implode('AND', $where);
|
|
|
298 |
} else {
|
|
|
299 |
return;
|
|
|
300 |
}
|
|
|
301 |
}
|
1253 |
jpm |
302 |
|
1227 |
gduche |
303 |
private function creerFiltreAuteur($valeurMasque) {
|
|
|
304 |
$masque = '';
|
|
|
305 |
$auteurId = $valeurMasque;
|
|
|
306 |
if (is_numeric($auteurId)) {
|
|
|
307 |
$masque = ' dc.ce_utilisateur = '.$auteurId;
|
|
|
308 |
} else {
|
|
|
309 |
if (strpos($auteurId, '@') === false) {
|
|
|
310 |
$tableauNomPrenom = explode(' ',$auteurId, 2);
|
|
|
311 |
if(count($tableauNomPrenom) == 2) {
|
|
|
312 |
// on teste potentiellement un nom prenom ou bien un prénom nom
|
|
|
313 |
$masque = '('.
|
1284 |
jpm |
314 |
'(dc.utilisateur_nom LIKE '.$this->gestionBdd->getBdd()->proteger($tableauNomPrenom[0].'%').' AND '.
|
|
|
315 |
'dc.utilisateur_prenom LIKE '.$this->gestionBdd->getBdd()->proteger($tableauNomPrenom[1].'%').') OR '.
|
|
|
316 |
'(dc.utilisateur_nom LIKE '.$this->gestionBdd->getBdd()->proteger($tableauNomPrenom[1].'%').' AND '.
|
|
|
317 |
'dc.utilisateur_prenom LIKE '.$this->gestionBdd->getBdd()->proteger($tableauNomPrenom[0].'%').')'.
|
|
|
318 |
')';
|
1227 |
gduche |
319 |
} else {
|
|
|
320 |
$masque = '(
|
1284 |
jpm |
321 |
(dc.utilisateur_nom LIKE '.$this->gestionBdd->getBdd()->proteger($auteurId.'%').' OR '.
|
|
|
322 |
'dc.utilisateur_prenom LIKE '.$this->gestionBdd->getBdd()->proteger($auteurId.'%').')'.
|
|
|
323 |
')';
|
1227 |
gduche |
324 |
}
|
|
|
325 |
} else {
|
1284 |
jpm |
326 |
$masque = " do.utilisateur_courriel LIKE ".$this->gestionBdd->getBdd()->proteger($valeurMasque.'%')." ";
|
1227 |
gduche |
327 |
}
|
|
|
328 |
}
|
|
|
329 |
return $masque;
|
|
|
330 |
}
|
1253 |
jpm |
331 |
}
|
1284 |
jpm |
332 |
?>
|