Subversion Repositories eFlore/Applications.del

Rev

Rev 1793 | Go to most recent revision | 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 $mappingFiltre = array();
19
	private $conteneur = null;
1793 jpm 20
	private $bdd = null;
1227 gduche 21
	private $navigation = null;
22
	private $type_rss = null;
1253 jpm 23
 
1227 gduche 24
	/**
25
	 * Constructeur
26
	 * Initialiser les configurations
27
	 * */
28
	public function __construct(Conteneur $conteneur = null) {
29
		$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
30
		$this->conteneur->chargerConfiguration('config_syndication_votesparprotocole.ini');
31
		$this->mappingFiltre = $this->conteneur->getParametre('mapping_masque');
1806 jpm 32
		$this->bdd = $this->conteneur->getBdd();
33
		$this->navigation = $this->conteneur->getNavigation();
1227 gduche 34
	}
1253 jpm 35
 
1227 gduche 36
	/**
1253 jpm 37
	 * Consulter
1227 gduche 38
	 * Méthode par défaut pour récupérer l'ensemble des votes.
39
	 * Vérifie la configuration et retourne les derniers votes formatés
40
	 * */
1247 gduche 41
	public function consulter($params = array()) {
42
		$this->verifierConfiguration();
1227 gduche 43
		$this->type_rss = $params[1];
44
		if ($this->fluxAdminDemande()) {
45
			$this->demanderAutorisationAdmin();
1253 jpm 46
		}
47
 
1227 gduche 48
		$donnees_brutes = $this->getDerniersVotesImage();
1253 jpm 49
		$commentaires_formates = $this->formaterPourRss($donnees_brutes) ;
1227 gduche 50
		return $commentaires_formates;
51
	}
1253 jpm 52
 
1227 gduche 53
	/**
54
	* Vérifier que le service est bien configuré
55
	* */
56
	public function verifierConfiguration() {
57
		$erreurs = array();
1253 jpm 58
 
1227 gduche 59
		if (empty($this->mappingFiltre)) {
60
			$erreurs[] = '- le fichier de configuration ne contient pas le tableau [mapping_masque] ou celui-ci est vide ;';
61
		} else {
62
			$champsMappingFiltre = array('image', 'protocole');
63
			foreach ($champsMappingFiltre as $champ) {
64
				if (!isset($this->mappingFiltre[$champ])) {
65
					$erreurs[] = '- le mapping du champ "'.$champ.'" pour le commentaire est manquant ;';
66
				}
67
			}
68
		}
1253 jpm 69
 
1227 gduche 70
		if (!empty($erreurs)) {
71
			$e = 'Erreur lors de la configuration : '."\n";
72
			$e .= implode("\n", $erreurs);
73
			throw new Exception($e, RestServeur::HTTP_CODE_ERREUR);
74
		}
75
	}
1253 jpm 76
 
1227 gduche 77
	/**
78
	 * Verifier si le flux admin est demandé
79
	 */
80
	private function fluxAdminDemande() {
1253 jpm 81
		return $this->conteneur->getParametre('admin') != null && $this->conteneur->getParametre('admin') == 1;
1227 gduche 82
	}
1253 jpm 83
 
1227 gduche 84
	/**
85
	 * Si le flux est un flux admin, demander un mot de passe
86
	 * */
87
	private function demanderAutorisationAdmin() {
1602 jpm 88
		$verification = $this->conteneur->getControleAcces();
1227 gduche 89
		$verification->demanderAuthentificationAdmin();
90
	}
1253 jpm 91
 
1227 gduche 92
	/**
93
	 * Formater les données pour mettre en page le RSS
94
	 * */
95
	private function formaterPourRss($elements) {
96
		$donnees = $this->construireDonneesCommunesAuFlux($elements);
97
		foreach ($elements as $element) {
1229 gduche 98
			$identifiants[$element['id_vote']] = $element['id_vote'];
1227 gduche 99
		}
100
		foreach ($elements as $element) {
101
			$donnees['items'][] = $this->construireDonneesCommunesAuxItems($element);
102
		}
103
		return $donnees;
104
	}
1253 jpm 105
 
1227 gduche 106
	/**
107
	 * Générer les métadonnées du flux (titre, dates, editeur etc.)
108
	 * */
109
	private function construireDonneesCommunesAuFlux($infos) {
110
		$donnees = array();
1307 jpm 111
		$donnees['guid'] = htmlspecialchars($this->creerUrlService());
1284 jpm 112
		$donnees['titre'] = 'pictoFlora : votes';
113
		$donnees['description'] = 'Ce flux regroupe les derniers votes sur les images de pictoFlora';
1307 jpm 114
		$donnees['lien_service'] = htmlspecialchars($this->creerUrlService());
1227 gduche 115
		$donnees['lien_del'] = $this->conteneur->getParametre('pictoAppliLien');
116
		$donnees['editeur'] = $this->conteneur->getParametre('editeur');
117
		$derniere_info_en_date = reset($infos);
1229 gduche 118
		$date_modification_timestamp = strtotime($derniere_info_en_date['date_vote']);
1227 gduche 119
		$donnees['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
120
		$donnees['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
121
		$donnees['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
122
		$donnees['annee_courante'] = date('Y');
123
		$donnees['generateur'] = 'DEL - SyndicationCommentaire';
124
		$donnees['generateur_version'] = (preg_match('/([0-9]+)/', '$Revision$', $match)) ?  $match[1] : '0';
1253 jpm 125
		return $donnees;
1227 gduche 126
	}
1253 jpm 127
 
1227 gduche 128
	/**
129
	 * Générer le lien du flux RSS
130
	 * */
131
	private function creerUrlService() {
1292 jpm 132
		$url_service = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
1227 gduche 133
		return $url_service;
134
	}
1253 jpm 135
 
1227 gduche 136
	/**
137
	 * Générer les données communes & spécifiques à chaque item
138
	 * */
139
	private function construireDonneesCommunesAuxItems($info) {
140
		$item = array();
1229 gduche 141
		$date_modification_timestamp = strtotime($info['date_vote']);
1227 gduche 142
		$item['date_maj_simple'] = strftime('%A %d %B %Y à %H:%M', $date_modification_timestamp);
143
		$item['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
144
		$item['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
145
		$item['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
146
		$item['titre'] = $this->creerTitre($info);
147
		$item['guid'] = $this->creerGuidItem($info);
148
		$item['lien'] = $this->creerLienItem($info);
149
		$item['categorie'] = $this->creerCategorie($item);
150
		$item['description'] = $this->creerDescription($info, $item);
151
		$item['description_encodee'] = htmlspecialchars($this->creerDescription($info, $item));
1294 jpm 152
		$item['modifier_par'] = $this->creerVotant($info);
1227 gduche 153
		return $item;
154
	}
1253 jpm 155
 
1227 gduche 156
	private function creerCategorie($element) {
157
		$categorie = 'Vote protocole';
158
		$categorie = htmlentities($categorie);
159
		return $categorie;
160
	}
1253 jpm 161
 
1227 gduche 162
	private function creerGuidItem($element) {
1661 jpm 163
		$guid = sprintf($this->conteneur->getParametre('voteGuidTpl'), $element['id_vote']);
1227 gduche 164
		return $guid;
165
	}
166
 
167
	private function creerLienItem($element) {
1677 jpm 168
		$lien = sprintf($this->conteneur->getParametre('imgFicheTpl'), $element['ce_image']);
1227 gduche 169
		return $lien;
170
	}
1253 jpm 171
 
1227 gduche 172
	private function creerTitre($element) {
1253 jpm 173
		$noteVote = $element['valeur'];
174
		$nomSci = htmlspecialchars($element['nom_sel']);
1292 jpm 175
		$votant = htmlspecialchars($this->creerVotant($element));
176
		$observateur = htmlspecialchars($this->creerObservateur($element));
1253 jpm 177
 
1292 jpm 178
		$titre = "Vote $noteVote par $votant pour $nomSci de $observateur";
1227 gduche 179
		return $titre;
180
	}
1253 jpm 181
 
1227 gduche 182
	private function creerDescription($donnees, $item) {
1285 jpm 183
		$idVote = htmlspecialchars($donnees['id_vote']);
184
		$idObs = htmlspecialchars($donnees['id_observation']);
1284 jpm 185
		$idImg = htmlspecialchars($donnees['ce_image']);
186
		$urlImg = $this->getUrlImage($donnees['ce_image']);
1292 jpm 187
		$miniatureUrl = $this->getUrlImage($donnees['ce_image'], 'CRS');
188
		$nomSelActuel = htmlspecialchars($donnees['nom_sel']);
189
		$dateObs = htmlspecialchars(str_replace(' 00:00:00', '', $donnees['date_observation']));
190
		$lieuObs = htmlspecialchars($donnees['zone_geo']);
1284 jpm 191
		$protocole = htmlspecialchars($donnees['intitule']);
1292 jpm 192
		$votant = htmlspecialchars($this->creerVotant($donnees));
193
		$dateVote = htmlspecialchars(strftime('%A %d %B %Y à %H:%M', strtotime($donnees['date_vote'])));
194
		$observateur = htmlspecialchars($this->creerObservateur($donnees));
1253 jpm 195
 
1292 jpm 196
		$description = '<style>.champ{color:grey} .gauche{float:left;padding:0 20px 0 0;} ul{list-style-type:none;padding:0;}</style>'.
197
			'<h2>'."Vote pictoFlora #$idVote pour l'image #$idImg de l'observation #$idObs".'</h2>'.
198
			'<div class="gauche">'.
1284 jpm 199
			'	<a href="'.$urlImg.'">'.
200
			'		<img src="'.$miniatureUrl.'" alt="Img #'.$idImg.'"/>'.
201
			'	</a>'.
1292 jpm 202
			'</div>'.
203
			'<div class="gauche">'.
204
			"	<h3>Image #$idImg de l'observation #$idObs</h3>".
205
			'	<ul>'.
206
			'		<li><span class="champ">'."Auteur de l'image :</span> $observateur</li>".
207
			'		<li><span class="champ">'."Nom saisi actuel :</span> <em>$nomSelActuel</em></li>".
208
			'		<li><span class="champ">'."Lieu :</span> $lieuObs</li>".
209
			'		<li><span class="champ">'."Date :</span> $dateObs</li>".
210
			'	</ul>'.
211
			'</div>'.
212
			'<div class="gauche">'.
213
			"	<h3>Vote #$idVote</h3>".
214
			'	<ul>'.
215
			'		<li><span class="champ">'."Protocole :</span> <strong>$protocole</strong></li>".
216
			'		<li><span class="champ">'."Valeur :</span> <strong>{$donnees['valeur']}</strong>/5</li>".
217
			'		<li><span class="champ">'."Votant :</span> $votant</li>".
218
			'		<li><span class="champ">'."À voté le :</span> $dateVote</li>".
219
			'	</ul>'.
220
			'</div>';
1227 gduche 221
		return $description;
222
	}
1253 jpm 223
 
1284 jpm 224
	private function getUrlImage($id, $format = 'L') {
225
		$url_tpl = $this->conteneur->getParametre('celImgUrlTpl');
226
		$id = sprintf('%09s', $id).$format;
227
		$url = sprintf($url_tpl, $id);
228
		return $url;
229
	}
230
 
1292 jpm 231
	private function creerVotant($info) {
232
		$votant = 'Anonyme';
233
		if (isset($info['votant_prenom']) && isset($info['votant_nom'])) {
234
			$votant = $info['votant_prenom'].' '.$info['votant_nom'];
1248 gduche 235
		}
1292 jpm 236
		return $votant;
1227 gduche 237
	}
1253 jpm 238
 
1292 jpm 239
	private function creerObservateur($info) {
240
		$observateur = 'Anonyme';
241
		if ($info['observateur_prenom'] != '' && $info['observateur_nom'] != '') {
242
			$observateur = $info['observateur_prenom'].' '.$info['observateur_nom'];
243
		}
244
		return $observateur;
245
	}
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, '.
1292 jpm 254
				'	do.id_observation, do.nom_sel, do.zone_geo, do.date_observation, 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 '.
1309 jpm 265
				'		ON if((CHAR_LENGTH(divo.ce_utilisateur) <> 32),CAST(divo.ce_utilisateur AS unsigned),0) '.
266
				'			= duv.id_utilisateur '.
1284 jpm 267
				$this->chargerClauseWhere().' '.
268
				'ORDER BY divo.date DESC '.
269
				'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite();
270
 
1793 jpm 271
		$elements = $this->bdd->recupererTous($requete);
1284 jpm 272
		return $elements;
273
	}
274
 
275
	/**
1227 gduche 276
	* Charger la clause WHERE en fonction des paramètres de masque
277
	* */
278
	private function chargerClauseWhere() {
279
		$where = array();
1806 jpm 280
		$tableauMasque = $this->navigation->getFiltre();
1227 gduche 281
		if (!empty($tableauMasque)) {
282
			foreach($tableauMasque as $idMasque => $valeurMasque) {
283
				$idMasque = str_replace('masque.', '', $idMasque);
284
				switch ($idMasque) {
285
					case 'image':
1793 jpm 286
						$where[] = ' '.$this->mappingFiltre[$idMasque].' = '.$this->bdd->proteger($valeurMasque);
1227 gduche 287
					break;
288
					case 'protocole':
1793 jpm 289
						$where[] = ' '.$this->mappingFiltre[$idMasque].' = '.$this->bdd->proteger($valeurMasque).' ';
1227 gduche 290
					break;
291
					default:
1793 jpm 292
						$where[] = ' '.$this->mappingFiltre[$idMasque].' = '.$this->bdd->proteger($valeurMasque);
1227 gduche 293
					break;
294
				}
1253 jpm 295
			}
1227 gduche 296
		}
297
		if (!empty($where)) {
298
			return ' WHERE '.implode('AND', $where);
299
		} else {
300
			return;
301
		}
302
	}
1253 jpm 303
 
1227 gduche 304
	private function creerFiltreAuteur($valeurMasque) {
305
		$masque = '';
306
		$auteurId = $valeurMasque;
307
		if (is_numeric($auteurId)) {
308
			$masque = ' dc.ce_utilisateur = '.$auteurId;
309
		} else {
310
			if (strpos($auteurId, '@') === false) {
311
				$tableauNomPrenom = explode(' ',$auteurId, 2);
312
				if(count($tableauNomPrenom) == 2) {
313
					// on teste potentiellement un nom prenom ou bien un prénom nom
314
					$masque = '('.
1793 jpm 315
						'(dc.utilisateur_nom LIKE '.$this->bdd->proteger($tableauNomPrenom[0].'%').' AND '.
316
							'dc.utilisateur_prenom LIKE '.$this->bdd->proteger($tableauNomPrenom[1].'%').') OR '.
317
							'(dc.utilisateur_nom LIKE '.$this->bdd->proteger($tableauNomPrenom[1].'%').' AND '.
318
							'dc.utilisateur_prenom LIKE '.$this->bdd->proteger($tableauNomPrenom[0].'%').')'.
1284 jpm 319
					')';
1227 gduche 320
				} else {
321
					$masque = '(
1793 jpm 322
						(dc.utilisateur_nom LIKE '.$this->bdd->proteger($auteurId.'%').' OR '.
323
						'dc.utilisateur_prenom LIKE '.$this->bdd->proteger($auteurId.'%').')'.
1284 jpm 324
					')';
1227 gduche 325
				}
326
			} else {
1793 jpm 327
				$masque = " do.utilisateur_courriel LIKE ".$this->bdd->proteger($valeurMasque.'%')." ";
1227 gduche 328
			}
329
		}
330
		return $masque;
331
	}
1253 jpm 332
}
1284 jpm 333
?>