Subversion Repositories eFlore/Applications.del

Rev

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

Rev Author Line No. Line
1231 gduche 1
<?php
2
/**
1284 jpm 3
 * Service fournissant des informations concernant les tags sur les images de DEL en fonction d'un protocole
1231 gduche 4
 * au format RSS1, RSS2 ou ATOM.
5
 * Encodage en entrée : utf8
6
 * Encodage en sortie : utf8
1284 jpm 7
 *
1231 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 SyndicationTagsParProtocole {
1284 jpm 15
 
1231 gduche 16
	/**
17
	 * Paramètres du service
18
	 * */
19
	private $mappingFiltre = array();
20
	private $conteneur = null;
1793 jpm 21
	private $bdd = null;
1231 gduche 22
	private $navigation = null;
23
	private $type_rss = null;
1284 jpm 24
 
1231 gduche 25
	/**
26
	 * Constructeur
27
	 * Initialiser les configurations
28
	 * */
29
	public function __construct(Conteneur $conteneur = null) {
30
		$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
31
		$this->conteneur->chargerConfiguration('config_syndication_tagsparprotocole.ini');
32
		$this->mappingFiltre = $this->conteneur->getParametre('mapping_masque');
1806 jpm 33
		$this->bdd = $this->conteneur->getBdd();
34
		$this->navigation = $this->conteneur->getNavigation();
1231 gduche 35
	}
1284 jpm 36
 
1231 gduche 37
	/**
1284 jpm 38
	 * Consulter
1231 gduche 39
	 * Méthode par défaut pour récupérer l'ensemble des tags.
40
	 * Vérifie la configuration et retourne les derniers tags formatés
41
	 * */
1284 jpm 42
	public function consulter($params = array()) {
43
		$this->verifierConfiguration();
1231 gduche 44
		$this->type_rss = $params[1];
45
		if ($this->fluxAdminDemande()) {
46
			$this->demanderAutorisationAdmin();
1284 jpm 47
		}
48
 
1231 gduche 49
		$donnees_brutes = $this->getDerniersVotesImage();
1284 jpm 50
		$commentaires_formates = $this->formaterPourRss($donnees_brutes) ;
1231 gduche 51
		return $commentaires_formates;
52
	}
1284 jpm 53
 
1231 gduche 54
	/**
55
	* Vérifier que le service est bien configuré
56
	* */
57
	public function verifierConfiguration() {
58
		$erreurs = array();
1284 jpm 59
 
1231 gduche 60
		if (empty($this->mappingFiltre)) {
61
			$erreurs[] = '- le fichier de configuration ne contient pas le tableau [mapping_masque] ou celui-ci est vide ;';
62
		} else {
63
			$champsMappingFiltre = array('image', 'protocole');
64
			foreach ($champsMappingFiltre as $champ) {
65
				if (!isset($this->mappingFiltre[$champ])) {
66
					$erreurs[] = '- le mapping du champ "'.$champ.'" pour le commentaire est manquant ;';
67
				}
68
			}
69
		}
1284 jpm 70
 
1231 gduche 71
		if (!empty($erreurs)) {
72
			$e = 'Erreur lors de la configuration : '."\n";
73
			$e .= implode("\n", $erreurs);
74
			throw new Exception($e, RestServeur::HTTP_CODE_ERREUR);
75
		}
76
	}
1284 jpm 77
 
1231 gduche 78
	/**
79
	 * Verifier si le flux admin est demandé
80
	 */
81
	private function fluxAdminDemande() {
1284 jpm 82
		return $this->conteneur->getParametre('admin') != null && $this->conteneur->getParametre('admin') == 1;
1231 gduche 83
	}
1284 jpm 84
 
1231 gduche 85
	/**
86
	 * Si le flux est un flux admin, demander un mot de passe
87
	 * */
88
	private function demanderAutorisationAdmin() {
1602 jpm 89
		$verification = $this->conteneur->getControleAcces();
1231 gduche 90
		$verification->demanderAuthentificationAdmin();
91
	}
1284 jpm 92
 
1231 gduche 93
	/**
94
	 * Formater les données pour mettre en page le RSS
95
	 * */
96
	private function formaterPourRss($elements) {
97
		$donnees = $this->construireDonneesCommunesAuFlux($elements);
98
		foreach ($elements as $element) {
1232 gduche 99
			$identifiants[$element['id_tag']] = $element['id_tag'];
1231 gduche 100
		}
101
		foreach ($elements as $element) {
102
			$donnees['items'][] = $this->construireDonneesCommunesAuxItems($element);
103
		}
104
		return $donnees;
105
	}
1284 jpm 106
 
1231 gduche 107
	/**
108
	 * Générer les métadonnées du flux (titre, dates, editeur etc.)
109
	 * */
110
	private function construireDonneesCommunesAuFlux($infos) {
111
		$donnees = array();
1307 jpm 112
		$donnees['guid'] = htmlspecialchars($this->creerUrlService());
1284 jpm 113
		$donnees['titre'] = 'pictoFlora : tags';
114
		$donnees['description'] = 'Ce flux regroupe les derniers tags des images de pictoFlora';
1307 jpm 115
		$donnees['lien_service'] = htmlspecialchars($this->creerUrlService());
1231 gduche 116
		$donnees['lien_del'] = $this->conteneur->getParametre('pictoAppliLien');
117
		$donnees['editeur'] = $this->conteneur->getParametre('editeur');
118
		$derniere_info_en_date = reset($infos);
119
		$date_modification_timestamp = strtotime($derniere_info_en_date['date']);
120
		$donnees['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
121
		$donnees['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
122
		$donnees['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
123
		$donnees['annee_courante'] = date('Y');
124
		$donnees['generateur'] = 'DEL - SyndicationCommentaire';
125
		$donnees['generateur_version'] = (preg_match('/([0-9]+)/', '$Revision$', $match)) ?  $match[1] : '0';
1284 jpm 126
		return $donnees;
1231 gduche 127
	}
1284 jpm 128
 
1231 gduche 129
	/**
130
	 * Générer le lien du flux RSS
131
	 * */
132
	private function creerUrlService() {
1292 jpm 133
		$url_service = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
1231 gduche 134
		return $url_service;
135
	}
1284 jpm 136
 
1231 gduche 137
	/**
138
	 * Générer les données communes & spécifiques à chaque item
139
	 * */
140
	private function construireDonneesCommunesAuxItems($info) {
141
		$item = array();
142
		$date_modification_timestamp = strtotime($info['date']);
143
		$item['date_maj_simple'] = strftime('%A %d %B %Y à %H:%M', $date_modification_timestamp);
144
		$item['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
145
		$item['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
146
		$item['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
147
		$item['titre'] = $this->creerTitre($info);
148
		$item['guid'] = $this->creerGuidItem($info);
149
		$item['lien'] = $this->creerLienItem($info);
150
		$item['categorie'] = $this->creerCategorie($item);
151
		$item['description'] = $this->creerDescription($info, $item);
152
		$item['description_encodee'] = htmlspecialchars($this->creerDescription($info, $item));
1294 jpm 153
		$item['modifier_par'] = $this->formaterNomTagueur($info);
1231 gduche 154
		return $item;
155
	}
1284 jpm 156
 
1231 gduche 157
	private function creerCategorie($element) {
158
		$categorie = 'Tag protocole';
159
		$categorie = htmlentities($categorie);
160
		return $categorie;
161
	}
1284 jpm 162
 
1231 gduche 163
	private function creerGuidItem($element) {
1661 jpm 164
		$guid = sprintf($this->conteneur->getParametre('tagGuidTpl'), $element['id_tag']);
1231 gduche 165
		return $guid;
166
	}
167
 
168
	private function creerLienItem($element) {
1232 gduche 169
		// TODO : ajouter un lien vers la plateforme validation de picto lorsqu'elle sera dispo
1661 jpm 170
		$lien = sprintf($this->conteneur->getParametre('imgFicheTpl'), $element['id_image']);
1231 gduche 171
		return $lien;
172
	}
1284 jpm 173
 
1231 gduche 174
	private function creerTitre($element) {
1284 jpm 175
		$tag = $element['tag'];
176
		$nomSel = htmlspecialchars($element['nom_sel']);
1296 jpm 177
		$tagueur = htmlspecialchars($this->formaterNomTagueur($element));
178
		$auteurImg = $this->formaterNomAuteurImg($element);
179
		$titre = "Tag «{$tag}» par $tagueur pour $nomSel de $auteurImg";
1231 gduche 180
		return $titre;
181
	}
1284 jpm 182
 
1231 gduche 183
	private function creerDescription($donnees, $item) {
1285 jpm 184
		$idTag = htmlspecialchars($donnees['id_tag']);
185
		$idObs = htmlspecialchars($donnees['id_observation']);
1284 jpm 186
		$idImg = htmlspecialchars($donnees['id_image']);
187
		$urlImg = $this->getUrlImage($donnees['id_image']);
1292 jpm 188
		$miniatureUrl = $this->getUrlImage($donnees['id_image'], 'CRS');
189
		$nomSelActuel = htmlspecialchars($donnees['nom_sel']);
190
		$dateObs = htmlspecialchars(str_replace(' 00:00:00', '', $donnees['date_observation']));
191
		$lieuObs = htmlspecialchars($donnees['zone_geo']);
192
		$tag = htmlspecialchars($donnees['tag']);
193
		$dateTag = htmlspecialchars(strftime('%A %d %B %Y à %H:%M', strtotime($donnees['date'])));
194
		$auteurImg = htmlspecialchars($this->creerAuteurImg($donnees));
1284 jpm 195
		$tagueur = htmlspecialchars($this->creerTagueur($donnees));
196
 
1292 jpm 197
		$description = '<style>.champ{color:grey} .gauche{float:left;padding:0 20px 0 0;} ul{list-style-type:none;padding:0;}</style>'.
198
			'<h2>'."Tag pictoFlora #$idTag pour l'image #$idImg de l'observation #$idObs".'</h2>'.
199
			'<div class="gauche">'.
200
			'	<a href="'.$urlImg.'">'.
201
			'		<img src="'.$miniatureUrl.'" alt="Img #'.$idImg.'"/>'.
202
			'	</a>'.
203
			'</div>'.
204
			'<div class="gauche">'.
205
			"	<h3>Image #$idImg de l'observation #$idObs</h3>".
206
			'	<ul>'.
207
			'		<li><span class="champ">'."Auteur de l'image :</span> $auteurImg</li>".
208
			'		<li><span class="champ">'."Nom saisi actuel :</span> <em>$nomSelActuel</em></li>".
209
			'		<li><span class="champ">'."Lieu :</span> $lieuObs</li>".
210
			'		<li><span class="champ">'."Date :</span> $dateObs</li>".
211
			'	</ul>'.
212
			'</div>'.
213
			'<div class="gauche">'.
214
			"	<h3>Tag #$idTag</h3>".
215
			'	<ul>'.
216
			'		<li><span class="champ">'."Tag :</span> <strong>$tag</strong></li>".
217
			'		<li><span class="champ">'."Auteur :</span> $tagueur</li>".
218
			'		<li><span class="champ">'."Taguée le :</span> $dateTag</li>".
219
			'	</ul>'.
220
			'</div>';
1231 gduche 221
		return $description;
222
	}
1284 jpm 223
 
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
 
231
	private function creerAuteurImg($info) {
1296 jpm 232
		$auteur = $this->formaterNomAuteurImg($info).
233
			($this->fluxAdminDemande() ? ' ('.$info['auteur_courriel'].')' : '');
234
		return $auteur;
235
	}
236
 
237
	private function formaterNomAuteurImg($info) {
1284 jpm 238
		$auteur = 'Anonyme';
239
		if ($info['auteur_prenom'] != '' && $info['auteur_nom'] != '') {
1296 jpm 240
			$auteur = $info['auteur_prenom'].' '.$info['auteur_nom'];
1231 gduche 241
		}
1284 jpm 242
		return $auteur;
1231 gduche 243
	}
1284 jpm 244
 
245
	private function creerTagueur($info) {
246
		$tagueur = $this->formaterNomTagueur($info).
1296 jpm 247
			($this->fluxAdminDemande() ? ' ('.$info['tagueur_courriel'].')' : '');
1284 jpm 248
		return $tagueur;
249
	}
250
 
251
	private function formaterNomTagueur($info) {
252
		$tagueur = 'Anonyme';
253
		if ($info['tagueur_prenom'] != '' && $info['tagueur_nom'] != '') {
254
			$tagueur = $info['tagueur_prenom'].' '.$info['tagueur_nom'];
255
		}
256
		return $tagueur;
257
	}
258
 
1231 gduche 259
	/**
1284 jpm 260
	 * Retrouver les derniers votes image
261
	 * */
262
	private function getDerniersVotesImage() {
1292 jpm 263
		$requete = 'SELECT DISTINCT id_tag, tag, date, '.
264
				'	do.id_observation, do.nom_sel, do.zone_geo, do.date_observation, doi.id_image, '.
1296 jpm 265
				'	duo.prenom AS auteur_prenom, duo.nom AS auteur_nom, duo.courriel AS auteur_courriel, '.
266
				'	du.prenom AS tagueur_prenom, du.nom AS tagueur_nom, du.courriel AS tagueur_courriel '.
1284 jpm 267
				'FROM del_image_tag AS dit '.
268
				'	INNER JOIN del_obs_image AS doi '.
269
				'		ON ce_image = id_image '.
270
				'	INNER JOIN del_observation AS do '.
271
				'			ON doi.id_observation = do.id_observation '.
272
				'	LEFT JOIN del_utilisateur AS duo '.
273
				'		ON do.ce_utilisateur = duo.id_utilisateur '.
274
				'	LEFT JOIN del_utilisateur AS du '.
1309 jpm 275
				'		ON if((CHAR_LENGTH(dit.ce_utilisateur) <> 32),CAST(dit.ce_utilisateur AS unsigned),0) '.
276
				'			= du.id_utilisateur '.
1284 jpm 277
				'WHERE actif = 1 '.
278
				$this->chargerClauseWhere().' '.
279
				'ORDER BY date DESC '.
280
				'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite();
1793 jpm 281
		$elements = $this->bdd->recupererTous($requete);
1284 jpm 282
		return $elements;
283
	}
284
 
285
	/**
1231 gduche 286
	* Charger la clause WHERE en fonction des paramètres de masque
287
	* */
288
	private function chargerClauseWhere() {
289
		$where = array();
1806 jpm 290
		$tableauMasque = $this->navigation->getFiltre();
1231 gduche 291
		if (!empty($tableauMasque)) {
1284 jpm 292
			foreach ($tableauMasque as $idMasque => $valeurMasque) {
1231 gduche 293
				$idMasque = str_replace('masque.', '', $idMasque);
1284 jpm 294
				$champMasque = $this->mappingFiltre[$idMasque];
1793 jpm 295
				$masqueMotif = $this->bdd->proteger($valeurMasque);
1284 jpm 296
 
1231 gduche 297
				switch ($idMasque) {
298
					case 'image':
1284 jpm 299
					//TODO : ajouter le protocole ? case 'protocole':
1231 gduche 300
					default:
1284 jpm 301
						$where[] = " $champMasque = $masqueMotif ";
1231 gduche 302
				}
1284 jpm 303
			}
1231 gduche 304
		}
1284 jpm 305
 
306
		$conditionsTxt = (!empty($where)) ? ' AND '.implode('AND', $where) : '';
307
		return $conditionsTxt;
1231 gduche 308
	}
1284 jpm 309
}
310
?>