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