Subversion Repositories eFlore/Applications.del

Rev

Rev 1309 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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();
1253 jpm 66
 
1227 gduche 67
		if (empty($this->mappingFiltre)) {
68
			$erreurs[] = '- le fichier de configuration ne contient pas le tableau [mapping_masque] ou celui-ci est vide ;';
69
		} else {
70
			$champsMappingFiltre = array('image', 'protocole');
71
			foreach ($champsMappingFiltre as $champ) {
72
				if (!isset($this->mappingFiltre[$champ])) {
73
					$erreurs[] = '- le mapping du champ "'.$champ.'" pour le commentaire est manquant ;';
74
				}
75
			}
76
		}
1253 jpm 77
 
1227 gduche 78
		if (!empty($erreurs)) {
79
			$e = 'Erreur lors de la configuration : '."\n";
80
			$e .= implode("\n", $erreurs);
81
			throw new Exception($e, RestServeur::HTTP_CODE_ERREUR);
82
		}
83
	}
1253 jpm 84
 
1227 gduche 85
	/**
86
	 * Verifier si le flux admin est demandé
87
	 */
88
	private function fluxAdminDemande() {
1253 jpm 89
		return $this->conteneur->getParametre('admin') != null && $this->conteneur->getParametre('admin') == 1;
1227 gduche 90
	}
1253 jpm 91
 
1227 gduche 92
	/**
93
	 * Si le flux est un flux admin, demander un mot de passe
94
	 * */
95
	private function demanderAutorisationAdmin() {
96
		$verification = new ControleAcces($this->conteneur);
97
		$verification->demanderAuthentificationAdmin();
98
	}
1253 jpm 99
 
1227 gduche 100
	/**
101
	 * Formater les données pour mettre en page le RSS
102
	 * */
103
	private function formaterPourRss($elements) {
104
		$donnees = $this->construireDonneesCommunesAuFlux($elements);
105
		foreach ($elements as $element) {
1229 gduche 106
			$identifiants[$element['id_vote']] = $element['id_vote'];
1227 gduche 107
		}
108
		foreach ($elements as $element) {
109
			$donnees['items'][] = $this->construireDonneesCommunesAuxItems($element);
110
		}
111
		return $donnees;
112
	}
1253 jpm 113
 
1227 gduche 114
	/**
115
	 * Générer les métadonnées du flux (titre, dates, editeur etc.)
116
	 * */
117
	private function construireDonneesCommunesAuFlux($infos) {
118
		$donnees = array();
1307 jpm 119
		$donnees['guid'] = htmlspecialchars($this->creerUrlService());
1284 jpm 120
		$donnees['titre'] = 'pictoFlora : votes';
121
		$donnees['description'] = 'Ce flux regroupe les derniers votes sur les images de pictoFlora';
1307 jpm 122
		$donnees['lien_service'] = htmlspecialchars($this->creerUrlService());
1227 gduche 123
		$donnees['lien_del'] = $this->conteneur->getParametre('pictoAppliLien');
124
		$donnees['editeur'] = $this->conteneur->getParametre('editeur');
125
		$derniere_info_en_date = reset($infos);
1229 gduche 126
		$date_modification_timestamp = strtotime($derniere_info_en_date['date_vote']);
1227 gduche 127
		$donnees['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
128
		$donnees['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
129
		$donnees['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
130
		$donnees['annee_courante'] = date('Y');
131
		$donnees['generateur'] = 'DEL - SyndicationCommentaire';
132
		$donnees['generateur_version'] = (preg_match('/([0-9]+)/', '$Revision$', $match)) ?  $match[1] : '0';
1253 jpm 133
		return $donnees;
1227 gduche 134
	}
1253 jpm 135
 
1227 gduche 136
	/**
137
	 * Générer le lien du flux RSS
138
	 * */
139
	private function creerUrlService() {
1292 jpm 140
		$url_service = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
1227 gduche 141
		return $url_service;
142
	}
1253 jpm 143
 
1227 gduche 144
	/**
145
	 * Générer les données communes & spécifiques à chaque item
146
	 * */
147
	private function construireDonneesCommunesAuxItems($info) {
148
		$item = array();
1229 gduche 149
		$date_modification_timestamp = strtotime($info['date_vote']);
1227 gduche 150
		$item['date_maj_simple'] = strftime('%A %d %B %Y à %H:%M', $date_modification_timestamp);
151
		$item['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
152
		$item['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
153
		$item['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
154
		$item['titre'] = $this->creerTitre($info);
155
		$item['guid'] = $this->creerGuidItem($info);
156
		$item['lien'] = $this->creerLienItem($info);
157
		$item['categorie'] = $this->creerCategorie($item);
158
		$item['description'] = $this->creerDescription($info, $item);
159
		$item['description_encodee'] = htmlspecialchars($this->creerDescription($info, $item));
1294 jpm 160
		$item['modifier_par'] = $this->creerVotant($info);
1227 gduche 161
		return $item;
162
	}
1253 jpm 163
 
1227 gduche 164
	private function creerCategorie($element) {
165
		$categorie = 'Vote protocole';
166
		$categorie = htmlentities($categorie);
167
		return $categorie;
168
	}
1253 jpm 169
 
1227 gduche 170
	private function creerGuidItem($element) {
171
		$guid = sprintf($this->conteneur->getParametre('voteParProtocole'), $element['id_vote']);
172
		return $guid;
173
	}
174
 
175
	private function creerLienItem($element) {
1285 jpm 176
		$lien = sprintf($this->conteneur->getParametre('pictofloraFicheObsTpl'), $element['id_observation']);
1227 gduche 177
		return $lien;
178
	}
1253 jpm 179
 
1227 gduche 180
	private function creerTitre($element) {
1253 jpm 181
		$noteVote = $element['valeur'];
182
		$nomSci = htmlspecialchars($element['nom_sel']);
1292 jpm 183
		$votant = htmlspecialchars($this->creerVotant($element));
184
		$observateur = htmlspecialchars($this->creerObservateur($element));
1253 jpm 185
 
1292 jpm 186
		$titre = "Vote $noteVote par $votant pour $nomSci de $observateur";
1227 gduche 187
		return $titre;
188
	}
1253 jpm 189
 
1227 gduche 190
	private function creerDescription($donnees, $item) {
1285 jpm 191
		$idVote = htmlspecialchars($donnees['id_vote']);
192
		$idObs = htmlspecialchars($donnees['id_observation']);
1284 jpm 193
		$idImg = htmlspecialchars($donnees['ce_image']);
194
		$urlImg = $this->getUrlImage($donnees['ce_image']);
1292 jpm 195
		$miniatureUrl = $this->getUrlImage($donnees['ce_image'], 'CRS');
196
		$nomSelActuel = htmlspecialchars($donnees['nom_sel']);
197
		$dateObs = htmlspecialchars(str_replace(' 00:00:00', '', $donnees['date_observation']));
198
		$lieuObs = htmlspecialchars($donnees['zone_geo']);
1284 jpm 199
		$protocole = htmlspecialchars($donnees['intitule']);
1292 jpm 200
		$votant = htmlspecialchars($this->creerVotant($donnees));
201
		$dateVote = htmlspecialchars(strftime('%A %d %B %Y à %H:%M', strtotime($donnees['date_vote'])));
202
		$observateur = htmlspecialchars($this->creerObservateur($donnees));
1253 jpm 203
 
1292 jpm 204
		$description = '<style>.champ{color:grey} .gauche{float:left;padding:0 20px 0 0;} ul{list-style-type:none;padding:0;}</style>'.
205
			'<h2>'."Vote pictoFlora #$idVote pour l'image #$idImg de l'observation #$idObs".'</h2>'.
206
			'<div class="gauche">'.
1284 jpm 207
			'	<a href="'.$urlImg.'">'.
208
			'		<img src="'.$miniatureUrl.'" alt="Img #'.$idImg.'"/>'.
209
			'	</a>'.
1292 jpm 210
			'</div>'.
211
			'<div class="gauche">'.
212
			"	<h3>Image #$idImg de l'observation #$idObs</h3>".
213
			'	<ul>'.
214
			'		<li><span class="champ">'."Auteur de l'image :</span> $observateur</li>".
215
			'		<li><span class="champ">'."Nom saisi actuel :</span> <em>$nomSelActuel</em></li>".
216
			'		<li><span class="champ">'."Lieu :</span> $lieuObs</li>".
217
			'		<li><span class="champ">'."Date :</span> $dateObs</li>".
218
			'	</ul>'.
219
			'</div>'.
220
			'<div class="gauche">'.
221
			"	<h3>Vote #$idVote</h3>".
222
			'	<ul>'.
223
			'		<li><span class="champ">'."Protocole :</span> <strong>$protocole</strong></li>".
224
			'		<li><span class="champ">'."Valeur :</span> <strong>{$donnees['valeur']}</strong>/5</li>".
225
			'		<li><span class="champ">'."Votant :</span> $votant</li>".
226
			'		<li><span class="champ">'."À voté le :</span> $dateVote</li>".
227
			'	</ul>'.
228
			'</div>';
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
 
1292 jpm 239
	private function creerVotant($info) {
240
		$votant = 'Anonyme';
241
		if (isset($info['votant_prenom']) && isset($info['votant_nom'])) {
242
			$votant = $info['votant_prenom'].' '.$info['votant_nom'];
1248 gduche 243
		}
1292 jpm 244
		return $votant;
1227 gduche 245
	}
1253 jpm 246
 
1292 jpm 247
	private function creerObservateur($info) {
248
		$observateur = 'Anonyme';
249
		if ($info['observateur_prenom'] != '' && $info['observateur_nom'] != '') {
250
			$observateur = $info['observateur_prenom'].' '.$info['observateur_nom'];
251
		}
252
		return $observateur;
253
	}
254
 
1227 gduche 255
	/**
1284 jpm 256
	 * Retrouver les derniers votes image
257
	 * */
258
	private function getDerniersVotesImage() {
259
		$requete =  'SELECT DISTINCT id_vote, ce_image, valeur, divo.date AS date_vote, '.
260
				'	duo.prenom AS observateur_prenom, duo.nom AS observateur_nom, '.
261
				'	duv.prenom AS votant_prenom, duv.nom AS votant_nom, '.
1292 jpm 262
				'	do.id_observation, do.nom_sel, do.zone_geo, do.date_observation, dip.intitule '.
1284 jpm 263
				'FROM del_image_vote AS divo '.
264
				'	INNER JOIN del_obs_image AS doi '.
265
				'		ON divo.ce_image = doi.id_image '.
266
				'	INNER JOIN del_observation AS do '.
267
				'		ON do.id_observation = doi.id_observation '.
268
				'	INNER JOIN del_image_protocole AS dip '.
269
				'		ON ce_protocole = id_protocole '.
270
				'	LEFT JOIN del_utilisateur AS duo '.
271
				'		ON do.ce_utilisateur = duo.id_utilisateur '.
272
				'	LEFT JOIN del_utilisateur AS duv '.
1309 jpm 273
				'		ON if((CHAR_LENGTH(divo.ce_utilisateur) <> 32),CAST(divo.ce_utilisateur AS unsigned),0) '.
274
				'			= duv.id_utilisateur '.
1284 jpm 275
				$this->chargerClauseWhere().' '.
276
				'ORDER BY divo.date DESC '.
277
				'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite();
278
 
279
		$elements = $this->gestionBdd->getBdd()->recupererTous($requete);
280
		return $elements;
281
	}
282
 
283
	/**
1227 gduche 284
	* Charger la clause WHERE en fonction des paramètres de masque
285
	* */
286
	private function chargerClauseWhere() {
287
		$where = array();
288
		$tableauMasque = $this->masque->getMasque();
289
		if (!empty($tableauMasque)) {
290
			foreach($tableauMasque as $idMasque => $valeurMasque) {
291
				$idMasque = str_replace('masque.', '', $idMasque);
292
				switch ($idMasque) {
293
					case 'image':
1253 jpm 294
						$where[] = ' '.$this->mappingFiltre[$idMasque].' = '.$this->gestionBdd->getBdd()->proteger($valeurMasque);
1227 gduche 295
					break;
296
					case 'protocole':
1253 jpm 297
						$where[] = ' '.$this->mappingFiltre[$idMasque].' = '.$this->gestionBdd->getBdd()->proteger($valeurMasque).' ';
1227 gduche 298
					break;
299
					default:
300
						$where[] = ' '.$this->mappingFiltre[$idMasque].' = '.$this->gestionBdd->getBdd()->proteger($valeurMasque);
301
					break;
302
				}
1253 jpm 303
			}
1227 gduche 304
		}
305
		if (!empty($where)) {
306
			return ' WHERE '.implode('AND', $where);
307
		} else {
308
			return;
309
		}
310
	}
1253 jpm 311
 
1227 gduche 312
	private function creerFiltreAuteur($valeurMasque) {
313
		$masque = '';
314
		$auteurId = $valeurMasque;
315
		if (is_numeric($auteurId)) {
316
			$masque = ' dc.ce_utilisateur = '.$auteurId;
317
		} else {
318
			if (strpos($auteurId, '@') === false) {
319
				$tableauNomPrenom = explode(' ',$auteurId, 2);
320
				if(count($tableauNomPrenom) == 2) {
321
					// on teste potentiellement un nom prenom ou bien un prénom nom
322
					$masque = '('.
1284 jpm 323
						'(dc.utilisateur_nom LIKE '.$this->gestionBdd->getBdd()->proteger($tableauNomPrenom[0].'%').' AND '.
324
							'dc.utilisateur_prenom LIKE '.$this->gestionBdd->getBdd()->proteger($tableauNomPrenom[1].'%').') OR '.
325
							'(dc.utilisateur_nom LIKE '.$this->gestionBdd->getBdd()->proteger($tableauNomPrenom[1].'%').' AND '.
326
							'dc.utilisateur_prenom LIKE '.$this->gestionBdd->getBdd()->proteger($tableauNomPrenom[0].'%').')'.
327
					')';
1227 gduche 328
				} else {
329
					$masque = '(
1284 jpm 330
						(dc.utilisateur_nom LIKE '.$this->gestionBdd->getBdd()->proteger($auteurId.'%').' OR '.
331
						'dc.utilisateur_prenom LIKE '.$this->gestionBdd->getBdd()->proteger($auteurId.'%').')'.
332
					')';
1227 gduche 333
				}
334
			} else {
1284 jpm 335
				$masque = " do.utilisateur_courriel LIKE ".$this->gestionBdd->getBdd()->proteger($valeurMasque.'%')." ";
1227 gduche 336
			}
337
		}
338
		return $masque;
339
	}
1253 jpm 340
}
1284 jpm 341
?>