Subversion Repositories eFlore/Applications.del

Rev

Rev 1806 | Rev 1821 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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