Subversion Repositories eFlore/Applications.del

Rev

Details | Last modification | View Log | RSS feed

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