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
1047 aurelien 1
<?php
2
/**
3
 * Service fournissant des informations concernant les commentaire de DEL au format RSS1, RSS2 ou ATOM.
4
 * Encodage en entrée : utf8
5
 * Encodage en sortie : utf8
1284 jpm 6
 *
1047 aurelien 7
 * @author Aurélien PERONNET <aurelien@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 SyndicationCommentaires {
1284 jpm 14
 
1049 aurelien 15
	private $mappingFiltre = array();
1047 aurelien 16
	private $conteneur = null;
1793 jpm 17
	private $bdd = null;
1055 aurelien 18
	private $navigation = null;
1284 jpm 19
 
1047 aurelien 20
	public function __construct(Conteneur $conteneur = null) {
21
		$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
1049 aurelien 22
		$this->conteneur->chargerConfiguration('config_syndication_commentaires.ini');
23
		$this->mappingFiltre = $this->conteneur->getParametre('mapping_masque');
1806 jpm 24
		$this->bdd = $this->conteneur->getBdd();
25
		$this->navigation = $this->conteneur->getNavigation();
1047 aurelien 26
	}
1284 jpm 27
 
28
	public function consulter($params = array()) {
29
		$this->verifierConfiguration();
30
 
1055 aurelien 31
		if ($this->fluxAdminDemande()) {
32
			$this->demanderAutorisationAdmin();
1284 jpm 33
		}
34
 
1047 aurelien 35
		$donnees_brutes = $this->getDerniersCommentaires();
1284 jpm 36
		$commentaires_formates = $this->formaterPourRss($donnees_brutes) ;
1047 aurelien 37
		return $commentaires_formates;
38
	}
1284 jpm 39
 
1049 aurelien 40
	/**
41
	* Vérifier que le service est bien configuré
42
	* */
43
	public function verifierConfiguration() {
44
		$erreurs = array();
1284 jpm 45
 
1049 aurelien 46
		if (empty($this->mappingFiltre)) {
1307 jpm 47
			$erreurs[] = 'Le fichier de configuration ne contient pas le tableau [mapping_masque] ou celui-ci est vide.';
1049 aurelien 48
		}
1284 jpm 49
 
1049 aurelien 50
		if (!empty($erreurs)) {
51
			$e = 'Erreur lors de la configuration : '."\n";
52
			$e .= implode("\n", $erreurs);
53
			throw new Exception($e, RestServeur::HTTP_CODE_ERREUR);
54
		}
55
	}
1284 jpm 56
 
1055 aurelien 57
	/**
58
	 * Verifier si le flux admin est demande
59
	 */
60
	private function fluxAdminDemande() {
1284 jpm 61
		return $this->conteneur->getParametre('admin') != null && $this->conteneur->getParametre('admin') == 1;
1055 aurelien 62
	}
1284 jpm 63
 
1055 aurelien 64
	private function demanderAutorisationAdmin() {
1602 jpm 65
		$verification = $this->conteneur->getControleAcces();
1055 aurelien 66
		$verification->demanderAuthentificationAdmin();
67
	}
1284 jpm 68
 
1047 aurelien 69
	private function formaterPourRss($elements) {
70
		$donnees = $this->construireDonneesCommunesAuFlux($elements);
71
		foreach ($elements as $element) {
72
			$identifiants[$element['id_commentaire']] = $element['id_commentaire'];
73
		}
74
		foreach ($elements as $element) {
75
			$donnees['items'][] = $this->construireDonneesCommunesAuxItems($element);
76
		}
77
		return $donnees;
78
	}
1284 jpm 79
 
1047 aurelien 80
	private function construireDonneesCommunesAuFlux($infos) {
81
		$donnees = array();
1307 jpm 82
		$donnees['guid'] = htmlspecialchars($this->creerUrlService());
1284 jpm 83
		$donnees['titre'] = 'identiPlante : commentaires et propositions';
84
		$donnees['description'] = 'Ce flux regroupe les dernières déterminations et commentaires rédigés dans l\'application identiPlante';
1307 jpm 85
		$donnees['lien_service'] = htmlspecialchars($this->creerUrlService());
1661 jpm 86
		$donnees['lien_del'] = $this->conteneur->getParametre('obsAppliLien');
1047 aurelien 87
		$donnees['editeur'] = $this->conteneur->getParametre('editeur');
88
		$derniere_info_en_date = reset($infos);
1304 jpm 89
		$date_modification_timestamp = $this->convertirDateHeureMysqlEnTimestamp($derniere_info_en_date['date']);
1047 aurelien 90
		$donnees['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
91
		$donnees['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
92
		$donnees['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
93
		$donnees['annee_courante'] = date('Y');
94
		$donnees['generateur'] = 'DEL - SyndicationCommentaire';
95
		$donnees['generateur_version'] = (preg_match('/([0-9]+)/', '$Revision$', $match)) ?  $match[1] : '0';
1284 jpm 96
		return $donnees;
1047 aurelien 97
	}
1284 jpm 98
 
1292 jpm 99
	private function creerUrlService() {
100
		$url_service = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
101
		return $url_service;
102
	}
103
 
1047 aurelien 104
	private function construireDonneesCommunesAuxItems($info) {
105
		$item = array();
1304 jpm 106
		$date_modification_timestamp = $this->convertirDateHeureMysqlEnTimestamp($info['date']);
1047 aurelien 107
		$item['date_maj_simple'] = strftime('%A %d %B %Y à %H:%M', $date_modification_timestamp);
108
		$item['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
109
		$item['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
110
		$item['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
111
		$item['titre'] = $this->creerTitre($info);
112
		$item['guid'] = $this->creerGuidItem($info);
113
		$item['lien'] = $this->creerLienItem($info);
114
		$item['categorie'] = $this->creerCategorie($item);
115
		$item['description'] = $this->creerDescription($info, $item);
116
		$item['description_encodee'] = htmlspecialchars($this->creerDescription($info, $item));
1294 jpm 117
		$item['modifier_par'] = $this->creerAuteur($info);
1047 aurelien 118
		return $item;
119
	}
1284 jpm 120
 
1304 jpm 121
	protected function convertirDateHeureMysqlEnTimestamp($date_heure_mysql){
1318 jpm 122
		$timestamp = 0;
123
		// Le date de 1970-01-01 pose problème dans certains lecteur de Flux, on met donc la date de création de Tela
124
		$date_heure_mysql = ($date_heure_mysql == '0000-00-00 00:00:00') ? '1999-12-14 00:00:00' : $date_heure_mysql;
1304 jpm 125
		if ($date_heure_mysql != '0000-00-00 00:00:00') {
126
			$val = explode(' ', $date_heure_mysql);
127
			$date = explode('-', $val[0]);
128
			$heure = explode(':', $val[1]);
129
			$timestamp = mktime((int) $heure[0], (int) $heure[1], (int) $heure[2], (int) $date[1], (int) $date[2], (int) $date[0]);
130
		}
131
		return $timestamp;
132
	}
133
 
1047 aurelien 134
	private function creerTitre($element) {
1290 jpm 135
		$nomPropose = htmlspecialchars($element['nom_sel']);
136
		$intitule = ($element['nom_sel'] != '') ? "Proposition $nomPropose" : 'Commentaire';
137
		$auteur = htmlspecialchars($this->creerAuteur($element));
138
		$nomSelActuel = htmlspecialchars($element['dob_nom_sel']);
1286 jpm 139
		$zoneGeo = htmlspecialchars((($element['dob_zone_geo'] != '') ? $element['dob_zone_geo'] : '?'));
140
		$dateObs = htmlspecialchars(strftime('%d %B %Y', strtotime($element['dob_date_observation'])));
141
 
1290 jpm 142
		$titre = "$intitule par $auteur pour $nomSelActuel à $zoneGeo le $dateObs";
1047 aurelien 143
		return $titre;
144
	}
1284 jpm 145
 
1292 jpm 146
	private function creerAuteur($info) {
1294 jpm 147
		$auteur = 'Anonyme';
148
		if ($info['utilisateur_prenom'] != '' && $info['utilisateur_nom'] != '') {
149
			$auteur = $info['utilisateur_prenom'].' '.$info['utilisateur_nom'];
150
		}
151
		return $auteur;
1292 jpm 152
	}
153
 
154
	private function creerGuidItem($element) {
1661 jpm 155
		$guid = sprintf($this->conteneur->getParametre('commentaireGuidTpl'), $element['id_commentaire']);
1292 jpm 156
		return $guid;
157
	}
158
 
159
	private function creerLienItem($element) {
1661 jpm 160
		$lien = sprintf($this->conteneur->getParametre('obsFicheTpl'), $element['dob_id_observation']);
1292 jpm 161
		return $lien;
162
	}
163
 
164
	private function creerCategorie($element) {
165
		$categorie = 'Commentaires';
166
		$categorie = htmlentities($categorie);
167
		return $categorie;
168
	}
169
 
1047 aurelien 170
	private function creerDescription($donnees, $item) {
1286 jpm 171
		$idCommentaire = $donnees['id_commentaire'];
1290 jpm 172
		$idObs = $donnees['dob_id_observation'];
173
		$nomPropose = ($donnees['nom_sel'] != '') ? htmlspecialchars($donnees['nom_sel']) : '';
1304 jpm 174
		$dateCommentaire = htmlspecialchars(strftime('%A %d %B %Y à %H:%M', $this->convertirDateHeureMysqlEnTimestamp($donnees['date'])));
1290 jpm 175
		$nomSelActuel = htmlspecialchars($donnees['dob_nom_sel']);
176
		$etreProposition = ($nomPropose != '') ? true : false;
1292 jpm 177
		$intitule = ($etreProposition) ? 'Proposition' : 'Commentaire';
1284 jpm 178
		$txt = ($donnees['texte'] != '') ? htmlspecialchars($donnees['texte']) : '';
179
		$auteur = htmlspecialchars($this->creerAuteur($donnees)).
180
			($this->fluxAdminDemande() ? ' ('.$donnees['utilisateur_courriel'].')' : '');
1292 jpm 181
		$lieuObs = htmlspecialchars((($donnees['dob_zone_geo'] != '') ? $donnees['dob_zone_geo'] : '?'));
182
		$dateObs = htmlspecialchars(str_replace(' 00:00:00', '', $donnees['dob_date_observation']));
183
		$observateur = htmlspecialchars($this->creerObservateur($donnees));
1284 jpm 184
 
1292 jpm 185
		$contenuCommentaire = '';
1285 jpm 186
		if ($etreProposition) {
1292 jpm 187
			$contenuCommentaire =
188
				'<li><span class="champ">'."Nom proposé :</span> <em>$nomPropose</em></li>".
189
				((!empty($txt)) ? '<li><span class="champ">'."Argumentaire :</span> $txt</li>" : '').
190
				'<li><span class="champ">'."Auteur de la proposition :</span> $auteur</li>".
191
				'<li><span class="champ">'."Proposé le :</span> $dateCommentaire</li>";
1285 jpm 192
		} else {
1292 jpm 193
			$contenuCommentaire =
194
				'<li><span class="champ">'."Commentaire #$idCommentaire :</span> <pre>$txt</pre></li>".
195
				'<li><span class="champ">'."Auteur du commentaire :</span> $auteur</li>".
196
				'<li><span class="champ">'."Commenté le :</span> $dateCommentaire</li>";
1285 jpm 197
		}
1292 jpm 198
 
199
		$description = '<style>.champ{color:grey} .gauche{float:left;padding:0 20px 0 0;} ul{list-style-type:none;padding:0;}</style>'.
200
			'<h2>'."$intitule identiPlante #$idCommentaire pour l'observation #$idObs".'</h2>'.
201
			'<div class="gauche">'.
202
			"	<h3>Observation #$idObs</h3>".
203
			'	<ul>'.
204
			'		<li><span class="champ">'."Nom saisi actuel :</span> <em>$nomSelActuel</em></li>".
205
			'		<li><span class="champ">'."Lieu :</span> $lieuObs</li>".
206
			'		<li><span class="champ">'."Date :</span> $dateObs</li>".
207
			'		<li><span class="champ">'."Auteur :</span> $observateur</li>".
208
			'	</ul>'.
209
			'</div>'.
210
			'<div class="gauche">'.
211
			"	<h3>$intitule #$idCommentaire</h3>".
212
			"	<ul>$contenuCommentaire</ul>".
213
			'</div>';
1047 aurelien 214
		return $description;
215
	}
1284 jpm 216
 
1292 jpm 217
	private function creerObservateur($info) {
218
		$observateur = 'Anonyme';
219
		if ($info['observateur_prenom'] != '' && $info['observateur_nom'] != '') {
220
			$observateur = $info['observateur_prenom'].' '.$info['observateur_nom'];
221
		}
222
		return $observateur;
1047 aurelien 223
	}
1284 jpm 224
 
225
	private function getDerniersCommentaires() {
226
		$requete = 'SELECT DISTINCT dc.*, '.
227
			'	dob.id_observation AS dob_id_observation, dob.ce_zone_geo AS dob_ce_zone_geo, '.
1292 jpm 228
			'	dob.zone_geo AS dob_zone_geo, dob.date_observation AS dob_date_observation, dob.nom_sel AS dob_nom_sel, '.
229
			'	duo.prenom AS observateur_prenom, duo.nom AS observateur_nom, duo.courriel AS observateur_courriel '.
1284 jpm 230
			'FROM del_commentaire AS dc '.
231
			'	INNER JOIN del_observation AS dob '.
232
			'		ON dob.id_observation = dc.ce_observation '.
1292 jpm 233
			'	LEFT JOIN del_utilisateur AS duo '.
234
			'		ON dob.ce_utilisateur = duo.id_utilisateur '.
1309 jpm 235
			'WHERE proposition_initiale != 1 '.
1284 jpm 236
			$this->chargerClauseWhere().' '.
237
			'ORDER BY dc.date DESC '.
238
			'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().' ';
239
 
1793 jpm 240
		$elements = $this->bdd->recupererTous($requete);
1047 aurelien 241
		return $elements;
242
	}
1284 jpm 243
 
1049 aurelien 244
	/**
245
	* Charger la clause WHERE en fonction des paramètres de masque
246
	* */
247
	private function chargerClauseWhere() {
248
		$where = array();
1307 jpm 249
 
1806 jpm 250
		$tableauMasque = $this->navigation->getFiltre();
1049 aurelien 251
		if (!empty($tableauMasque)) {
1307 jpm 252
			foreach ($tableauMasque as $idMasque => $valeurMasque) {
1049 aurelien 253
				$idMasque = str_replace('masque.', '', $idMasque);
1307 jpm 254
				$champ = $this->mappingFiltre[$idMasque];
1793 jpm 255
				$valeurMasquePattern = $this->bdd->proteger($valeurMasque.'%');
256
				$valeurMasqueProtegee = $this->bdd->proteger($valeurMasque);
1307 jpm 257
 
1049 aurelien 258
				switch ($idMasque) {
259
					case 'espece':
1307 jpm 260
						$where[] = " dob.$champ LIKE $valeurMasquePattern OR dc.$champ LIKE $valeurMasquePattern ";
1284 jpm 261
						break;
1049 aurelien 262
					case 'auteur':
1307 jpm 263
						$where[] = $this->creerFiltreAuteur($valeurMasque);
1284 jpm 264
						break;
1049 aurelien 265
					default:
1307 jpm 266
						$where[] = " $champ = $valeurMasqueProtegee ";
1049 aurelien 267
				}
1284 jpm 268
			}
1049 aurelien 269
		}
1307 jpm 270
 
271
		$whereSql = '';
1049 aurelien 272
		if (!empty($where)) {
1307 jpm 273
			$whereSql = ' AND '.implode('AND', $where);
1049 aurelien 274
		}
1307 jpm 275
		return $whereSql;
1049 aurelien 276
	}
1284 jpm 277
 
278
	private function creerFiltreAuteur($auteurId) {
1307 jpm 279
		$whereAuteur = '';
1049 aurelien 280
		if (is_numeric($auteurId)) {
1307 jpm 281
			$whereAuteur = " dc.ce_utilisateur = $auteurId ";
1049 aurelien 282
		} else {
1793 jpm 283
			$auteurIdMotif = $this->bdd->proteger($auteurId.'%');
1284 jpm 284
 
1049 aurelien 285
			if (strpos($auteurId, '@') === false) {
1284 jpm 286
				$tableauNomPrenom = explode(' ', $auteurId, 2);
287
				if (count($tableauNomPrenom) == 2) {
1049 aurelien 288
					// on teste potentiellement un nom prenom ou bien un prénom nom
1793 jpm 289
					$nomMotif = $this->bdd->proteger($tableauNomPrenom[0].'%');
290
					$prenomMotif = $this->bdd->proteger($tableauNomPrenom[1].'%');
1284 jpm 291
 
1307 jpm 292
					$whereAuteur = ' ('.
1284 jpm 293
						"(dc.utilisateur_nom LIKE $nomMotif AND dc.utilisateur_prenom LIKE $prenomMotif) ".
294
						'OR '.
295
						"(dc.utilisateur_nom LIKE $nomMotif AND dc.utilisateur_prenom LIKE $prenomMotif) ".
1307 jpm 296
					') ';
1049 aurelien 297
				} else {
1307 jpm 298
					$whereAuteur = "(dc.utilisateur_nom LIKE $auteurIdMotif OR dc.utilisateur_prenom LIKE $auteurIdMotif) ";
1049 aurelien 299
				}
300
			} else {
1307 jpm 301
				$whereAuteur = " dob.utilisateur_courriel LIKE $auteurIdMotif ";
1049 aurelien 302
			}
303
		}
1307 jpm 304
		return $whereAuteur;
1049 aurelien 305
	}
1284 jpm 306
}
307
?>