Subversion Repositories eFlore/Applications.del

Rev

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

Rev Author Line No. Line
1047 aurelien 1
<?php
1820 jpm 2
// declare(encoding='UTF-8');
1047 aurelien 3
/**
4
 * Service fournissant des informations concernant les commentaire de DEL au format RSS1, RSS2 ou ATOM.
1284 jpm 5
 *
1820 jpm 6
 * @category   DEL
7
 * @package    Services
8
 * @subpackage Syndication
9
 * @version    0.1
10
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
11
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
12
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
13
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
14
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
15
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
1047 aurelien 16
 */
1821 jpm 17
class Commentaires {
1284 jpm 18
 
1821 jpm 19
	private $categorie = 'Commentaires';
20
	private $conteneur;
21
	private $contexte;
22
	private $bdd;
23
	private $navigation;
24
	private $mapping = array();
25
	private $syndication;
1284 jpm 26
 
1047 aurelien 27
	public function __construct(Conteneur $conteneur = null) {
28
		$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
1821 jpm 29
		$this->contexte = $this->conteneur->getContexte();
1806 jpm 30
		$this->bdd = $this->conteneur->getBdd();
31
		$this->navigation = $this->conteneur->getNavigation();
1821 jpm 32
		$this->syndication = $this->conteneur->getSyndicationOutils();
33
		$this->mapping = $this->conteneur->getParametreTableau('syndication.mapping');
1047 aurelien 34
	}
1284 jpm 35
 
1821 jpm 36
	public function consulter() {
37
		if ($this->syndication->fluxAdminDemande()) {
38
			$this->syndication->demanderAutorisationAdmin();
1284 jpm 39
		}
40
 
1047 aurelien 41
		$donnees_brutes = $this->getDerniersCommentaires();
1284 jpm 42
		$commentaires_formates = $this->formaterPourRss($donnees_brutes) ;
1047 aurelien 43
		return $commentaires_formates;
44
	}
1284 jpm 45
 
1047 aurelien 46
	private function formaterPourRss($elements) {
1821 jpm 47
		$derniere_info_en_date = reset($elements);
48
		$donnees = $this->syndication->construireDonneesCommunesAuFlux('commentaire', $derniere_info_en_date['date']);
1047 aurelien 49
		foreach ($elements as $element) {
50
			$donnees['items'][] = $this->construireDonneesCommunesAuxItems($element);
51
		}
52
		return $donnees;
53
	}
1284 jpm 54
 
1047 aurelien 55
	private function construireDonneesCommunesAuxItems($info) {
56
		$item = array();
1821 jpm 57
		$date_modification_timestamp = $this->syndication->convertirDateHeureMysqlEnTimestamp($info['date']);
1047 aurelien 58
		$item['date_maj_simple'] = strftime('%A %d %B %Y à %H:%M', $date_modification_timestamp);
59
		$item['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
60
		$item['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
61
		$item['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
62
		$item['titre'] = $this->creerTitre($info);
63
		$item['guid'] = $this->creerGuidItem($info);
64
		$item['lien'] = $this->creerLienItem($info);
1821 jpm 65
		$item['categorie'] = htmlentities($this->categorie);
1047 aurelien 66
		$item['description'] = $this->creerDescription($info, $item);
67
		$item['description_encodee'] = htmlspecialchars($this->creerDescription($info, $item));
1294 jpm 68
		$item['modifier_par'] = $this->creerAuteur($info);
1047 aurelien 69
		return $item;
70
	}
1284 jpm 71
 
1047 aurelien 72
	private function creerTitre($element) {
1290 jpm 73
		$nomPropose = htmlspecialchars($element['nom_sel']);
74
		$intitule = ($element['nom_sel'] != '') ? "Proposition $nomPropose" : 'Commentaire';
75
		$auteur = htmlspecialchars($this->creerAuteur($element));
76
		$nomSelActuel = htmlspecialchars($element['dob_nom_sel']);
1286 jpm 77
		$zoneGeo = htmlspecialchars((($element['dob_zone_geo'] != '') ? $element['dob_zone_geo'] : '?'));
78
		$dateObs = htmlspecialchars(strftime('%d %B %Y', strtotime($element['dob_date_observation'])));
79
 
1290 jpm 80
		$titre = "$intitule par $auteur pour $nomSelActuel à $zoneGeo le $dateObs";
1047 aurelien 81
		return $titre;
82
	}
1284 jpm 83
 
1292 jpm 84
	private function creerAuteur($info) {
1294 jpm 85
		$auteur = 'Anonyme';
86
		if ($info['utilisateur_prenom'] != '' && $info['utilisateur_nom'] != '') {
87
			$auteur = $info['utilisateur_prenom'].' '.$info['utilisateur_nom'];
88
		}
89
		return $auteur;
1292 jpm 90
	}
91
 
92
	private function creerGuidItem($element) {
1821 jpm 93
		$guid = sprintf($this->conteneur->getParametre('syndication.commentaire_guid_tpl'), $element['id_commentaire']);
1292 jpm 94
		return $guid;
95
	}
96
 
97
	private function creerLienItem($element) {
1821 jpm 98
		$lien = sprintf($this->conteneur->getParametre('obs_fiche_tpl'), $element['dob_id_observation']);
1292 jpm 99
		return $lien;
100
	}
101
 
1047 aurelien 102
	private function creerDescription($donnees, $item) {
1286 jpm 103
		$idCommentaire = $donnees['id_commentaire'];
1290 jpm 104
		$idObs = $donnees['dob_id_observation'];
105
		$nomPropose = ($donnees['nom_sel'] != '') ? htmlspecialchars($donnees['nom_sel']) : '';
1821 jpm 106
		$dateCommentaire = htmlspecialchars(strftime('%A %d %B %Y à %H:%M', $this->syndication->convertirDateHeureMysqlEnTimestamp($donnees['date'])));
1290 jpm 107
		$nomSelActuel = htmlspecialchars($donnees['dob_nom_sel']);
108
		$etreProposition = ($nomPropose != '') ? true : false;
1292 jpm 109
		$intitule = ($etreProposition) ? 'Proposition' : 'Commentaire';
1284 jpm 110
		$txt = ($donnees['texte'] != '') ? htmlspecialchars($donnees['texte']) : '';
111
		$auteur = htmlspecialchars($this->creerAuteur($donnees)).
1821 jpm 112
			($this->syndication->fluxAdminDemande() ? ' ('.$donnees['utilisateur_courriel'].')' : '');
1292 jpm 113
		$lieuObs = htmlspecialchars((($donnees['dob_zone_geo'] != '') ? $donnees['dob_zone_geo'] : '?'));
114
		$dateObs = htmlspecialchars(str_replace(' 00:00:00', '', $donnees['dob_date_observation']));
115
		$observateur = htmlspecialchars($this->creerObservateur($donnees));
1284 jpm 116
 
1292 jpm 117
		$contenuCommentaire = '';
1285 jpm 118
		if ($etreProposition) {
1292 jpm 119
			$contenuCommentaire =
120
				'<li><span class="champ">'."Nom proposé :</span> <em>$nomPropose</em></li>".
121
				((!empty($txt)) ? '<li><span class="champ">'."Argumentaire :</span> $txt</li>" : '').
122
				'<li><span class="champ">'."Auteur de la proposition :</span> $auteur</li>".
123
				'<li><span class="champ">'."Proposé le :</span> $dateCommentaire</li>";
1285 jpm 124
		} else {
1292 jpm 125
			$contenuCommentaire =
126
				'<li><span class="champ">'."Commentaire #$idCommentaire :</span> <pre>$txt</pre></li>".
127
				'<li><span class="champ">'."Auteur du commentaire :</span> $auteur</li>".
128
				'<li><span class="champ">'."Commenté le :</span> $dateCommentaire</li>";
1285 jpm 129
		}
1292 jpm 130
 
131
		$description = '<style>.champ{color:grey} .gauche{float:left;padding:0 20px 0 0;} ul{list-style-type:none;padding:0;}</style>'.
132
			'<h2>'."$intitule identiPlante #$idCommentaire pour l'observation #$idObs".'</h2>'.
133
			'<div class="gauche">'.
134
			"	<h3>Observation #$idObs</h3>".
135
			'	<ul>'.
136
			'		<li><span class="champ">'."Nom saisi actuel :</span> <em>$nomSelActuel</em></li>".
137
			'		<li><span class="champ">'."Lieu :</span> $lieuObs</li>".
138
			'		<li><span class="champ">'."Date :</span> $dateObs</li>".
139
			'		<li><span class="champ">'."Auteur :</span> $observateur</li>".
140
			'	</ul>'.
141
			'</div>'.
142
			'<div class="gauche">'.
143
			"	<h3>$intitule #$idCommentaire</h3>".
144
			"	<ul>$contenuCommentaire</ul>".
145
			'</div>';
1047 aurelien 146
		return $description;
147
	}
1284 jpm 148
 
1292 jpm 149
	private function creerObservateur($info) {
150
		$observateur = 'Anonyme';
151
		if ($info['observateur_prenom'] != '' && $info['observateur_nom'] != '') {
152
			$observateur = $info['observateur_prenom'].' '.$info['observateur_nom'];
153
		}
154
		return $observateur;
1047 aurelien 155
	}
1284 jpm 156
 
157
	private function getDerniersCommentaires() {
1922 jpm 158
		$commentaires = array();
159
		$idsCommentaires = $this->getIdsDerniersCommentaires();
160
		if (!empty($idsCommentaires)) {
161
			$idsCommentairesconcat = implode(', ', $idsCommentaires);
162
			$requete = 'SELECT DISTINCT dc.*, '.
163
				'	dob.id_observation AS dob_id_observation, dob.ce_zone_geo AS dob_ce_zone_geo, '.
164
				'	dob.zone_geo AS dob_zone_geo, dob.date_observation AS dob_date_observation, dob.nom_sel AS dob_nom_sel, '.
165
				'	duo.prenom AS observateur_prenom, duo.nom AS observateur_nom, duo.courriel AS observateur_courriel '.
166
				'FROM del_commentaire AS dc '.
167
				'	INNER JOIN del_observation AS dob '.
168
				'		ON dob.id_observation = dc.ce_observation '.
169
				'	LEFT JOIN del_utilisateur AS duo '.
170
				'		ON dob.ce_utilisateur = duo.id_utilisateur '.
171
				"WHERE id_commentaire IN ($idsCommentairesconcat) ".
172
				'ORDER BY dc.date DESC '.
173
				'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().' '.
174
				' -- '.__FILE__.' : '.__LINE__;
175
			$commentaires = $this->bdd->recupererTous($requete);
176
		}
177
		return $commentaires;
178
	}
179
 
180
	private function getIdsDerniersCommentaires() {
1821 jpm 181
		$clauseWhere = $this->chargerClauseWhere();
1922 jpm 182
		$requete = 'SELECT DISTINCT dc.id_commentaire '.
1284 jpm 183
			'FROM del_commentaire AS dc '.
1922 jpm 184
			'	INNER JOIN del_observation AS dob ON (dc.ce_observation = dob.id_observation) '.
185
			'	LEFT JOIN del_utilisateur AS duo ON (dob.ce_utilisateur = duo.id_utilisateur) '.
1309 jpm 186
			'WHERE proposition_initiale != 1 '.
1821 jpm 187
			($clauseWhere != '' ? "AND $clauseWhere " : '').
1284 jpm 188
			'ORDER BY dc.date DESC '.
1821 jpm 189
			'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().' '.
190
			' -- '.__FILE__.' : '.__LINE__;
1922 jpm 191
		//echo $requete;
192
		$resultats = $this->bdd->recupererTous($requete);
1284 jpm 193
 
1922 jpm 194
		$idsCommentaires = array();
195
		if ($resultats != false && is_array($resultats)) {
196
			foreach ($resultats as $infos) {
197
				$idsCommentaires[] = $infos['id_commentaire'];
198
			}
199
		}
200
		return $idsCommentaires;
1047 aurelien 201
	}
1284 jpm 202
 
1049 aurelien 203
	/**
204
	* Charger la clause WHERE en fonction des paramètres de masque
205
	* */
206
	private function chargerClauseWhere() {
207
		$where = array();
1821 jpm 208
		$filtres = $this->navigation->getFiltre();
209
		if (!empty($filtres)) {
210
			$filtrePossibles = $this->conteneur->getParametreTableau('syndication.commentaire_filtres');
211
			foreach ($filtres as $cleFiltre => $valeur) {
212
				if (in_array($cleFiltre, $filtrePossibles)) {
213
					if (isset($this->mapping[$cleFiltre])) {
214
						$champ = $this->mapping[$cleFiltre];
215
					}
216
					switch ($cleFiltre) {
217
						case 'espece':
218
							$valeurP = $this->bdd->proteger($valeur.'%');
1922 jpm 219
							$where[] = " (dob.$champ LIKE $valeurP OR dc.$champ LIKE $valeurP) ";
1821 jpm 220
							break;
221
						case 'auteur':
222
							$where[] = $this->creerFiltreAuteur($valeur);
223
							break;
224
						default:
225
							$valeurP = $this->bdd->proteger($valeur);
226
							$where[] = " $champ = $valeurP ";
227
					}
1049 aurelien 228
				}
1284 jpm 229
			}
1049 aurelien 230
		}
1922 jpm 231
		return (!empty($where)) ? implode(' AND ', $where) : '';
1049 aurelien 232
	}
1284 jpm 233
 
234
	private function creerFiltreAuteur($auteurId) {
1307 jpm 235
		$whereAuteur = '';
1049 aurelien 236
		if (is_numeric($auteurId)) {
1307 jpm 237
			$whereAuteur = " dc.ce_utilisateur = $auteurId ";
1049 aurelien 238
		} else {
1793 jpm 239
			$auteurIdMotif = $this->bdd->proteger($auteurId.'%');
1284 jpm 240
 
1821 jpm 241
			if (strpos($auteurId, '@') !== false) {
242
				$whereAuteur = " dc.utilisateur_courriel LIKE $auteurIdMotif ";
243
			} else {
1284 jpm 244
				$tableauNomPrenom = explode(' ', $auteurId, 2);
1922 jpm 245
				if (count($tableauNomPrenom) == 1) {
246
					$whereAuteur = " (dc.utilisateur_nom LIKE $auteurIdMotif OR dc.utilisateur_prenom LIKE $auteurIdMotif) ";
247
				} else if (count($tableauNomPrenom) == 2) {
1049 aurelien 248
					// on teste potentiellement un nom prenom ou bien un prénom nom
1793 jpm 249
					$nomMotif = $this->bdd->proteger($tableauNomPrenom[0].'%');
250
					$prenomMotif = $this->bdd->proteger($tableauNomPrenom[1].'%');
1284 jpm 251
 
1307 jpm 252
					$whereAuteur = ' ('.
1284 jpm 253
						"(dc.utilisateur_nom LIKE $nomMotif AND dc.utilisateur_prenom LIKE $prenomMotif) ".
254
						'OR '.
1922 jpm 255
						"(dc.utilisateur_nom LIKE $prenomMotif AND dc.utilisateur_prenom LIKE $nomMotif) ".
1307 jpm 256
					') ';
1049 aurelien 257
				}
258
			}
259
		}
1307 jpm 260
		return $whereAuteur;
1049 aurelien 261
	}
1821 jpm 262
}