Subversion Repositories eFlore/Applications.cel

Rev

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

Rev Author Line No. Line
416 aurelien 1
<?php
2
/**
522 jpm 3
 * Service fournissant des informations concernant les images du CEL au format RSS1, RSS2 ou ATOM.
416 aurelien 4
 * Encodage en entrée : utf8
5
 * Encodage en sortie : utf8
6
 *
7
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
8
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
9
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
10
 * @version $Id$
11
 * @copyright 2010
12
 */
13
class CelSyndicationImage extends Cel {
14
 
15
	private $format = null;
16
	private $service = null;
17
	private $squelette = null;
18
	private $squelette_dossier = null;
19
	private $flux = array();
20
 
21
	private $format_image = 'L';
22
 
23
	/**
24
	 * Méthode appelée avec une requête de type GET.
25
	 */
524 jpm 26
	public function getElement($params = array()) {
416 aurelien 27
		// Initialisation des variables
28
		$info = array();
29
		$contenu = '';
522 jpm 30
 
523 jpm 31
		if (! $this->etreFluxAdmin() || $this->authentifier()) {
522 jpm 32
			// Pré traitement des paramêtres
33
			$pour_bdd = false;
524 jpm 34
			$p = $this->traiterParametres(array('service', 'format'), $params, $pour_bdd);
522 jpm 35
			extract($p);
524 jpm 36
			$this->parametres = $params;
37
			$this->squelette_dossier = dirname(__FILE__).DIRECTORY_SEPARATOR.'squelettes'.DIRECTORY_SEPARATOR;
416 aurelien 38
 
522 jpm 39
			// Récupération de la liste des flux
40
			$this->chargerListeDesFlux();
41
 
42
			// Chargement du bon type de service demandé
43
			if (isset($service)) {
524 jpm 44
				$this->service = $this->traiterNomService($service);
522 jpm 45
				$methode = $this->getNomMethodeService();
46
				if (method_exists($this, $methode)) {
524 jpm 47
					if (isset($format) && preg_match('/^(?:rss1|rss2|atom)$/i', $format)) {
48
						// Mise en minuscule de l'indication du format
49
						$this->format = strtolower($format);
50
						// Définition du fichier squelette demandé
51
						$this->squelette = $this->squelette_dossier.$this->format.'.tpl.xml';
52
					} else if (isset($this->flux[$this->service])) {
53
						$this->format = '';
54
						$this->messages[] = "Le service CEL Syndication nécessite d'indiquer en second paramètre le format : rss1, rss2 ou atom.";
416 aurelien 55
					}
524 jpm 56
 
57
					if (!isset($this->flux[$this->service]) || isset($this->format)) {
58
						// Suppression des paramêtres inutile pour le reste des méthodes
59
						array_shift($this->parametres);
60
						array_shift($this->parametres);
61
 
62
						// Récupération du contenu à renvoyer
63
						$contenu = $this->$methode();
64
					}
522 jpm 65
				} else {
66
					$this->messages[] = "Le type d'information demandé '$this->service' n'est pas disponible.";
416 aurelien 67
				}
68
			} else {
522 jpm 69
				$this->messages[] = "Le service CEL Syndication Image nécessite d'indiquer en premier paramètre le type d'information demandé.";
416 aurelien 70
			}
71
		}
72
 
73
		// Envoie sur la sortie standard
74
		$encodage = 'utf-8';
75
		$mime = $this->getTypeMime();
76
		$formatage_json = $this->getFormatageJson();
77
		$this->envoyer($contenu, $mime, $encodage, $formatage_json);
78
	}
79
 
524 jpm 80
	private function chargerListeDesFlux() {
81
		$this->setFlux('simple', 'Nouvelles images liées à une observation dans le CEL',
82
			"Ce flux fournit l'url des nouvelles images du CEL liées à une observation.");
83
		$this->setFlux('complet', 'Nouvelles images liées à une observation dans le CEL (détails)',
84
			"Ce flux fournit les informations sur les nouvelles images du CEL liées à une observation.");
416 aurelien 85
	}
86
 
524 jpm 87
	private function setFlux($nom, $titre, $description) {
88
		$url_base = $this->config['settings']['baseURLAbsoluDyn'].'CoelSyndicationImage/';
89
		$formats = array('atom', 'rss2', 'rss1');
90
		$flux = array();
91
		foreach ($formats as $format) {
92
			$url = $url_base.$nom.'/'.$format;
93
			$flux[$format] = $url;
94
		}
95
		$this->flux[$nom] = array('titre' => $titre, 'description' => $description, 'urls' => $flux);
96
	}
97
 
98
	private function getFlux($nom) {
99
		return isset($this->flux[$nom]) ? $this->flux[$nom] : array();
100
	}
101
 
102
	private function traiterNomService($nom) {
103
		$nom = strtolower($nom);
104
		return $nom;
105
	}
106
 
416 aurelien 107
	private function getNomMethodeService() {
108
		$methode = '';
524 jpm 109
		$service_formate = str_replace(' ', '', ucwords(implode(' ', explode('-', $this->service))));
416 aurelien 110
		$methode = 'getService'.$service_formate;
111
		return $methode;
112
	}
113
 
524 jpm 114
	private function getUrlBase() {
115
		$url_base = sprintf($this->config['settings']['baseURLAbsoluDyn'], get_class($this).'/');
116
		return $url_base;
117
	}
118
 
119
	private function getUrlServiceBase() {
120
		$url_service = $this->getUrlBase().$this->service.'/'.$this->format;
121
		return $url_service;
122
	}
123
 
416 aurelien 124
	private function getTypeMime() {
125
		$mime = '';
126
		switch ($this->format) {
127
			case 'atom' :
128
				$mime = 'application/atom+xml';
129
				break;
130
			case 'rss1' :
131
			case 'rss2' :
132
				$mime = 'application/rss+xml';
133
				break;
524 jpm 134
			case 'opml' :
135
				$mime = 'text/x-opml';
136
				break;
416 aurelien 137
			default:
138
				$mime = 'text/html';
139
		}
140
		return $mime;
141
	}
142
 
143
	private function getFormatageJson() {
144
		$json = false;
145
		switch ($this->service) {
524 jpm 146
			case 'liste-des-flux' :
416 aurelien 147
				$json = true;
148
				break;
149
			default:
150
				$json = false;
151
		}
152
		return $json;
153
	}
524 jpm 154
 
522 jpm 155
	private function etreFluxAdmin() {
156
		return ($_GET['admin'] == '1') ? true : false;
416 aurelien 157
	}
158
 
522 jpm 159
	private function creerUrlService() {
160
		$url_service = $this->getUrlServiceBase();
161
		if (isset($this->start) || isset($this->limit)) {
162
			$arguments = array();
163
			if (isset($this->start) && isset($_GET['start'])) {
164
				$arguments[] = 'start='.$this->start;
165
			}
166
			if (isset($this->limit) && isset($_GET['limit'])) {
167
				$arguments[] = 'limit='.$this->limit;
168
			}
169
			if (count($arguments) > 0) {
170
				$url_service .= '?'.implode('&', $arguments);
171
			}
416 aurelien 172
		}
522 jpm 173
		return $url_service;
416 aurelien 174
	}
175
 
176
	protected function executerRequete($requete) {
177
		try {
178
			$infos = $this->bdd->query($requete)->fetchAll(PDO::FETCH_ASSOC);
179
			if ($infos === false) {
180
				$this->messages[] = "La requête suivante a retourné aucun résultat :\n$requete";
181
			}
182
		} catch (PDOException $e) {
183
			$this->messages[] = sprintf($this->getTxt('sql_erreur'), $e->getFile(), $e->getLine(), $e->getMessage());
184
		}
185
		return $infos;
186
	}
187
 
188
	private function executerService($elements) {
189
		// Prétraitement des données
190
		$donnees = $this->construireDonneesCommunesAuFlux($elements);
191
 
192
		foreach ($elements as $element) {
193
			$donnees['items'][] = $this->construireDonneesCommunesAuxItems($element);
194
		}
195
 
196
		// Création du contenu à partir d'un template PHP
197
		$contenu = Cel::traiterSquelettePhp($this->squelette, $donnees);
198
 
199
		return $contenu;
200
	}
201
 
202
	private function construireDonneesCommunesAuFlux($infos) {
203
		$donnees = $this->getFlux($this->service);
204
		$donnees['guid'] = $this->getUrlServiceBase();
205
		$donnees['lien_service'] = $this->creerUrlService();
206
		$donnees['lien_cel'] = sprintf($this->config['settings']['efloreUrlTpl'], $infos['num_nom_sel']);
207
		$donnees['editeur'] = $this->config['settings']['editeur'];
208
		$derniere_info_en_date = reset($infos);
209
		$date_modification_timestamp = strtotime($derniere_info_en_date['ci_meta_date_ajout']);
210
		$donnees['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
211
		$donnees['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
212
		$donnees['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
213
		$donnees['annee_courante'] = date('Y');
214
		$donnees['generateur'] = 'CEL - Jrest - CelSyndicationImage';
215
		preg_match('/([0-9]+)/', '$Revision$', $match);
216
		$donnees['generateur_version'] = $match[1];
217
		return $donnees;
218
	}
219
 
220
	private function construireDonneesCommunesAuxItems($info) {
221
		$item = array();
222
		$date_modification_timestamp = strtotime($info['ci_meta_date_ajout']);
223
		$item['date_maj_simple'] = strftime('%A %d %B %Y à %H:%M', $date_modification_timestamp);
224
		$item['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
225
		$item['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
226
		$item['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
227
		$item['titre'] = $this->creerTitre($info);
228
		$item['guid'] = $this->creerGuidItem($info);
229
		$item['lien'] = $this->creerLienItem($info);
523 jpm 230
		$item['description'] = $this->creerDescription($info, $item);
416 aurelien 231
		$item['description_encodee'] = htmlspecialchars($item['description']);
232
		return $item;
233
	}
234
 
235
	private function creerGuidItem($element) {
236
		$guid = sprintf($this->config['settings']['guidImgTpl'], $element['ci_id_image']);
237
		return $guid;
238
	}
239
 
522 jpm 240
	private function creerTitre($element) {
241
		$methode = 'creerTitre'.$this->service;
242
		$methode = (method_exists($this, $methode)) ? $methode : 'creerTitreSimple';
243
		$titre = $this->$methode($element);
244
		$titre = $this->nettoyerTexte($titre);
245
		return $titre;
416 aurelien 246
	}
247
 
523 jpm 248
	private function creerDescription($donnees, $item) {
522 jpm 249
		$methode = 'creerDescription'.$this->service;
250
		$methode = (method_exists($this, $methode)) ? $methode : 'creerDescriptionSimple';
523 jpm 251
		$description = $this->$methode($donnees, $item);
416 aurelien 252
		$description = $this->nettoyerTexte($description);
253
		return $description;
254
	}
522 jpm 255
 
256
	private function creerLienItem($element) {
257
		$lien = $this->getUrlImage($element['ci_id_image'], $this->format_image);
258
		return $lien;
416 aurelien 259
	}
260
 
524 jpm 261
	private function getServiceListeDesFlux() {
262
		return $this->flux;
263
	}
264
 
265
	private function getServiceOpml() {
266
		$donnees = array();
267
		$id = 1;
268
		foreach ($this->flux as $flux_nom => $flux){
269
			$info = array();
270
			$info['type'] = 'atom';
271
			$info['titre'] = $flux['titre'];
272
			$info['texte'] = "CEL - Images - $flux_nom";
273
			$info['description'] = $flux['description'];
274
			$info['url_xml'] = $this->getUrlBase().$flux_nom.'/atom';
275
			$info['url_html'] = $this->config['settings']['aideCelUrl'].'FluxSyndication';
276
			$donnees['liste_flux'][] = $info;
522 jpm 277
		}
278
 
524 jpm 279
		$this->squelette = $this->squelette_dossier.'opml.tpl.xml';
280
		$contenu = Cel::traiterSquelettePhp($this->squelette, $donnees);
281
		return $contenu;
282
	}
283
 
284
	private function getServiceSimple() {
285
		if (isset($this->parametres[0])) {
286
			$this->format_image = $this->parametres[0];
287
		}
522 jpm 288
 
289
		// Construction de la requête
290
		$requete = 	(($this->distinct) ? 'SELECT DISTINCT' : 'SELECT').' ci.*, ci_id_image, ci_nom_original '.
291
			'FROM cel_obs_images AS coi '.
292
			'LEFT JOIN cel_inventory AS ci '.
293
				'ON (coi.coi_ce_observation = ci.ordre AND coi.coi_ce_utilisateur = ci.identifiant) '.
294
			'LEFT JOIN cel_images AS cim '.
295
				'ON (coi.coi_ce_image = cim.ci_id_image AND coi.coi_ce_utilisateur = cim.ci_ce_utilisateur) '.
296
			'WHERE ci.transmission = 1 '.
297
			'	AND ci.identifiant = cim.ci_ce_utilisateur '.
298
			'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby  : 'ci_meta_date_ajout DESC').' '.
299
			"LIMIT $this->start, $this->limit ";
300
 
301
		$elements = $this->executerRequete($requete);
302
 
303
		// Création du contenu
304
		$contenu = $this->executerService($elements);
305
		return $contenu;
416 aurelien 306
	}
307
 
522 jpm 308
	private function creerTitreSimple($element) {
309
		$titre = $element['nom_sel'].' [nn'.$element['num_nom_sel'].'] par '.$this->creerAuteur($element['identifiant'], $this->etreFluxAdmin());
310
		return $titre;
416 aurelien 311
	}
312
 
523 jpm 313
	private function creerDescriptionSimple($donnees, $item) {
314
		$description = sprintf($this->config['settings']['efloreUrlTpl'], urlencode($donnees['num_nom_sel']));
522 jpm 315
		return $description;
416 aurelien 316
	}
317
 
524 jpm 318
	private function getServiceComplet() {
522 jpm 319
		// Construction de la requête
523 jpm 320
		$requete = 	(($this->distinct) ? 'SELECT DISTINCT' : 'SELECT').' ci.*, ci_id_image, ci_nom_original, ci_meta_date_ajout, ci_meta_user_comment, ci_note_image '.
522 jpm 321
			'FROM cel_obs_images AS coi '.
322
			'LEFT JOIN cel_inventory AS ci '.
323
				'ON (coi.coi_ce_observation = ci.ordre AND coi.coi_ce_utilisateur = ci.identifiant) '.
324
			'LEFT JOIN cel_images AS cim '.
325
				'ON (coi.coi_ce_image = cim.ci_id_image AND coi.coi_ce_utilisateur = cim.ci_ce_utilisateur) '.
326
			'WHERE ci.transmission = 1 '.
327
			'	AND ci.identifiant = cim.ci_ce_utilisateur '.
328
			'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby  : 'ci_meta_date_ajout DESC').' '.
329
			"LIMIT $this->start, $this->limit ";
330
 
331
		$elements = $this->executerRequete($requete);
332
 
333
		// Création du contenu
334
		$contenu = $this->executerService($elements);
335
		return $contenu;
516 aurelien 336
	}
522 jpm 337
 
523 jpm 338
	private function creerDescriptionComplet($donnees, $item) {
339
		$url_img = $this->getUrlImage($donnees['ci_id_image'], 'M');
340
		$description .= '<style>.champ{color:grey} img{float:left;padding:0 20px 0 0;}</style>';
341
		$description .= '<img src="'.$url_img.'" alt="'.$donnees['ci_nom_original'].'" />';
522 jpm 342
		$description .= '<ul>';
523 jpm 343
		$description .= '<li>'."Image liée à l'observation ".$donnees['id'].'</li>';
344
		$description .= '<li>'.'<span class="champ">Importée le :</span> '.$item['date_maj_simple'].'</li>';
345
		$description .= '<li>'.'<span class="champ">Nom du fichier :</span> '.$donnees['ci_nom_original'].'</li>';
346
		$description .= '<li>'.'<span class="champ">Note qualité image :</span> '.($donnees['ci_note_image'] +1).'/5</li>';
347
		// TODO :traiter le problème du champ commentaire.
348
		//$description .= '<li>'.'<span class="champ">Commentaire :</span> '.$donnees['ci_meta_user_comment'].'</li>';
349
 
350
		$description .= '<li>'.'<span class="champ">Nom saisi :</span> '.$donnees['nom_sel'].'</li>';
351
		$description .= '<li>'.'<span class="champ">Nom retenu :</span> '.$donnees['nom_ret'].'</li>';
352
		$description .= '<li>'.'<span class="champ">Observée le :</span> '.strftime('%A %d %B %Y', strtotime($donnees['date_observation'])).'</li>';
353
		$description .= '<li>'.'<span class="champ">Lieu :</span> '.$donnees['location'].' ('.$donnees['id_location'].') '.$donnees['station'].' '.$donnees['lieudit'].'</li>';
522 jpm 354
		$description .= '</ul>';
355
		$description = $this->nettoyerTexte($description);
356
		return $description;
416 aurelien 357
	}
358
}