Subversion Repositories eFlore/Applications.del

Rev

Rev 1820 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1820 Rev 1821
Line 13... Line 13...
13
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
13
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
14
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
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>
15
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
16
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
16
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
17
 */
17
 */
18
class SyndicationVotesParProtocole {
18
class VotesParProtocole {
19
	/**
-
 
20
	 * Paramètres du service
-
 
21
	 * */
-
 
22
	private $mappingFiltre = array();
-
 
23
	private $conteneur = null;
-
 
24
	private $bdd = null;
-
 
25
	private $navigation = null;
-
 
26
	private $type_rss = null;
-
 
Line -... Line 19...
-
 
19
 
-
 
20
	private $categorie = 'Vote protocole';
27
 
21
	private $conteneur;
28
	/**
22
	private $bdd;
29
	 * Constructeur
23
	private $navigation;
-
 
24
	private $syndication;
30
	 * Initialiser les configurations
25
	private $mapping = array();
31
	 * */
26
 
32
	public function __construct(Conteneur $conteneur = null) {
27
	public function __construct(Conteneur $conteneur) {
33
		$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
-
 
34
		$this->conteneur->chargerConfiguration('config_syndication_votesparprotocole.ini');
-
 
35
		$this->mappingFiltre = $this->conteneur->getParametre('mapping_masque');
28
		$this->conteneur = $conteneur;
36
		$this->bdd = $this->conteneur->getBdd();
29
		$this->bdd = $this->conteneur->getBdd();
-
 
30
		$this->navigation = $this->conteneur->getNavigation();
-
 
31
		$this->syndication = $this->conteneur->getSyndicationOutils();
37
		$this->navigation = $this->conteneur->getNavigation();
32
		$this->mapping = $this->conteneur->getParametreTableau('syndication.mapping');
Line 38... Line -...
38
	}
-
 
39
 
-
 
40
	/**
-
 
41
	 * Consulter
-
 
42
	 * Méthode par défaut pour récupérer l'ensemble des votes.
-
 
43
	 * Vérifie la configuration et retourne les derniers votes formatés
33
	}
44
	 * */
-
 
45
	public function consulter($params = array()) {
-
 
46
		$this->verifierConfiguration();
34
 
47
		$this->type_rss = $params[1];
35
	public function consulter() {
48
		if ($this->fluxAdminDemande()) {
36
		if ($this->syndication->fluxAdminDemande()) {
Line 49... Line 37...
49
			$this->demanderAutorisationAdmin();
37
			$this->syndication->demanderAutorisationAdmin();
50
		}
38
		}
51
 
39
 
52
		$donnees_brutes = $this->getDerniersVotesImage();
40
		$donnees_brutes = $this->getDerniersVotesImage();
Line 53... Line 41...
53
		$commentaires_formates = $this->formaterPourRss($donnees_brutes) ;
41
		$commentaires_formates = $this->formaterPourRss($donnees_brutes) ;
54
		return $commentaires_formates;
-
 
55
	}
-
 
56
 
-
 
57
	/**
-
 
58
	* Vérifier que le service est bien configuré
-
 
59
	* */
-
 
60
	public function verifierConfiguration() {
-
 
61
		$erreurs = array();
-
 
62
 
-
 
63
		if (empty($this->mappingFiltre)) {
-
 
64
			$erreurs[] = '- le fichier de configuration ne contient pas le tableau [mapping_masque] ou celui-ci est vide ;';
-
 
65
		} else {
-
 
66
			$champsMappingFiltre = array('image', 'protocole');
-
 
67
			foreach ($champsMappingFiltre as $champ) {
-
 
68
				if (!isset($this->mappingFiltre[$champ])) {
-
 
69
					$erreurs[] = '- le mapping du champ "'.$champ.'" pour le commentaire est manquant ;';
-
 
70
				}
-
 
71
			}
-
 
72
		}
-
 
73
 
-
 
74
		if (!empty($erreurs)) {
-
 
75
			$e = 'Erreur lors de la configuration : '."\n";
-
 
76
			$e .= implode("\n", $erreurs);
-
 
77
			throw new Exception($e, RestServeur::HTTP_CODE_ERREUR);
-
 
78
		}
-
 
79
	}
-
 
80
 
-
 
81
	/**
-
 
82
	 * Verifier si le flux admin est demandé
-
 
83
	 */
-
 
84
	private function fluxAdminDemande() {
-
 
85
		return $this->conteneur->getParametre('admin') != null && $this->conteneur->getParametre('admin') == 1;
-
 
86
	}
-
 
87
 
-
 
88
	/**
-
 
89
	 * Si le flux est un flux admin, demander un mot de passe
-
 
90
	 * */
-
 
91
	private function demanderAutorisationAdmin() {
-
 
92
		$verification = $this->conteneur->getControleAcces();
-
 
93
		$verification->demanderAuthentificationAdmin();
42
		return $commentaires_formates;
94
	}
43
	}
95
 
44
 
96
	/**
-
 
97
	 * Formater les données pour mettre en page le RSS
45
	/**
98
	 * */
46
	 * Formater les données pour mettre en page le RSS
99
	private function formaterPourRss($elements) {
-
 
100
		$donnees = $this->construireDonneesCommunesAuFlux($elements);
47
	 * */
101
		foreach ($elements as $element) {
48
	private function formaterPourRss($elements) {
102
			$identifiants[$element['id_vote']] = $element['id_vote'];
49
		$derniere_info_en_date = reset($elements);
103
		}
50
		$donnees = $this->syndication->construireDonneesCommunesAuFlux('vote', $derniere_info_en_date['date_vote']);
104
		foreach ($elements as $element) {
51
		foreach ($elements as $element) {
Line 105... Line 52...
105
			$donnees['items'][] = $this->construireDonneesCommunesAuxItems($element);
52
			$donnees['items'][] = $this->construireDonneesCommunesAuxItems($element);
106
		}
-
 
107
		return $donnees;
-
 
108
	}
-
 
109
 
-
 
110
	/**
-
 
111
	 * Générer les métadonnées du flux (titre, dates, editeur etc.)
-
 
112
	 * */
-
 
113
	private function construireDonneesCommunesAuFlux($infos) {
-
 
114
		$donnees = array();
-
 
115
		$donnees['guid'] = htmlspecialchars($this->creerUrlService());
-
 
116
		$donnees['titre'] = 'pictoFlora : votes';
-
 
117
		$donnees['description'] = 'Ce flux regroupe les derniers votes sur les images de pictoFlora';
-
 
118
		$donnees['lien_service'] = htmlspecialchars($this->creerUrlService());
-
 
119
		$donnees['lien_del'] = $this->conteneur->getParametre('pictoAppliLien');
-
 
120
		$donnees['editeur'] = $this->conteneur->getParametre('editeur');
-
 
121
		$derniere_info_en_date = reset($infos);
-
 
122
		$date_modification_timestamp = strtotime($derniere_info_en_date['date_vote']);
-
 
123
		$donnees['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
-
 
124
		$donnees['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
-
 
125
		$donnees['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
-
 
126
		$donnees['annee_courante'] = date('Y');
-
 
127
		$donnees['generateur'] = 'DEL - SyndicationCommentaire';
-
 
128
		$donnees['generateur_version'] = (preg_match('/([0-9]+)/', '$Revision$', $match)) ?  $match[1] : '0';
-
 
129
		return $donnees;
-
 
130
	}
-
 
131
 
-
 
132
	/**
-
 
133
	 * Générer le lien du flux RSS
-
 
134
	 * */
-
 
135
	private function creerUrlService() {
-
 
136
		$url_service = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
53
		}
137
		return $url_service;
54
		return $donnees;
138
	}
55
	}
139
 
56
 
140
	/**
57
	/**
Line 148... Line 65...
148
		$item['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
65
		$item['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
149
		$item['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
66
		$item['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
150
		$item['titre'] = $this->creerTitre($info);
67
		$item['titre'] = $this->creerTitre($info);
151
		$item['guid'] = $this->creerGuidItem($info);
68
		$item['guid'] = $this->creerGuidItem($info);
152
		$item['lien'] = $this->creerLienItem($info);
69
		$item['lien'] = $this->creerLienItem($info);
153
		$item['categorie'] = $this->creerCategorie($item);
70
		$item['categorie'] = htmlentities($this->categorie);
154
		$item['description'] = $this->creerDescription($info, $item);
71
		$item['description'] = $this->creerDescription($info, $item);
155
		$item['description_encodee'] = htmlspecialchars($this->creerDescription($info, $item));
72
		$item['description_encodee'] = htmlspecialchars($this->creerDescription($info, $item));
156
		$item['modifier_par'] = $this->creerVotant($info);
73
		$item['modifier_par'] = $this->creerVotant($info);
157
		return $item;
74
		return $item;
158
	}
75
	}
Line 159... Line -...
159
 
-
 
160
	private function creerCategorie($element) {
-
 
161
		$categorie = 'Vote protocole';
-
 
162
		$categorie = htmlentities($categorie);
-
 
163
		return $categorie;
-
 
164
	}
-
 
165
 
76
 
166
	private function creerGuidItem($element) {
77
	private function creerGuidItem($element) {
167
		$guid = sprintf($this->conteneur->getParametre('voteGuidTpl'), $element['id_vote']);
78
		$guid = sprintf($this->conteneur->getParametre('syndication.vote_guid_tpl'), $element['id_vote']);
168
		return $guid;
79
		return $guid;
Line 169... Line 80...
169
	}
80
	}
170
 
81
 
171
	private function creerLienItem($element) {
82
	private function creerLienItem($element) {
172
		$lien = sprintf($this->conteneur->getParametre('imgFicheTpl'), $element['ce_image']);
83
		$lien = sprintf($this->conteneur->getParametre('img_fiche_tpl'), $element['ce_image']);
Line 173... Line 84...
173
		return $lien;
84
		return $lien;
174
	}
85
	}
Line 185... Line 96...
185
 
96
 
186
	private function creerDescription($donnees, $item) {
97
	private function creerDescription($donnees, $item) {
187
		$idVote = htmlspecialchars($donnees['id_vote']);
98
		$idVote = htmlspecialchars($donnees['id_vote']);
188
		$idObs = htmlspecialchars($donnees['id_observation']);
99
		$idObs = htmlspecialchars($donnees['id_observation']);
189
		$idImg = htmlspecialchars($donnees['ce_image']);
100
		$idImg = htmlspecialchars($donnees['ce_image']);
190
		$urlImg = $this->getUrlImage($donnees['ce_image']);
101
		$urlImg = $this->syndication->getUrlImage($donnees['ce_image']);
191
		$miniatureUrl = $this->getUrlImage($donnees['ce_image'], 'CRS');
102
		$miniatureUrl = $this->syndication->getUrlImage($donnees['ce_image'], 'CRS');
192
		$nomSelActuel = htmlspecialchars($donnees['nom_sel']);
103
		$nomSelActuel = htmlspecialchars($donnees['nom_sel']);
193
		$dateObs = htmlspecialchars(str_replace(' 00:00:00', '', $donnees['date_observation']));
104
		$dateObs = htmlspecialchars(str_replace(' 00:00:00', '', $donnees['date_observation']));
194
		$lieuObs = htmlspecialchars($donnees['zone_geo']);
105
		$lieuObs = htmlspecialchars($donnees['zone_geo']);
195
		$protocole = htmlspecialchars($donnees['intitule']);
106
		$protocole = htmlspecialchars($donnees['intitule']);
Line 223... Line 134...
223
			'	</ul>'.
134
			'	</ul>'.
224
			'</div>';
135
			'</div>';
225
		return $description;
136
		return $description;
226
	}
137
	}
Line 227... Line -...
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
 
138
 
235
	private function creerVotant($info) {
139
	private function creerVotant($info) {
236
		$votant = 'Anonyme';
140
		$votant = 'Anonyme';
237
		if (isset($info['votant_prenom']) && isset($info['votant_nom'])) {
141
		if (isset($info['votant_prenom']) && isset($info['votant_nom'])) {
238
			$votant = $info['votant_prenom'].' '.$info['votant_nom'];
142
			$votant = $info['votant_prenom'].' '.$info['votant_nom'];
Line 250... Line 154...
250
 
154
 
251
	/**
155
	/**
252
	 * Retrouver les derniers votes image
156
	 * Retrouver les derniers votes image
253
	 * */
157
	 * */
-
 
158
	private function getDerniersVotesImage() {
254
	private function getDerniersVotesImage() {
159
		$clauseWhere = $this->chargerClauseWhere();
255
		$requete =  'SELECT DISTINCT id_vote, ce_image, valeur, divo.date AS date_vote, '.
160
		$requete =  'SELECT DISTINCT id_vote, ce_image, valeur, divo.date AS date_vote, '.
256
				'	duo.prenom AS observateur_prenom, duo.nom AS observateur_nom, '.
161
				'	duo.prenom AS observateur_prenom, duo.nom AS observateur_nom, '.
257
				'	duv.prenom AS votant_prenom, duv.nom AS votant_nom, '.
162
				'	duv.prenom AS votant_prenom, duv.nom AS votant_nom, '.
258
				'	do.id_observation, do.nom_sel, do.zone_geo, do.date_observation, dip.intitule '.
163
				'	do.id_observation, do.nom_sel, do.zone_geo, do.date_observation, dip.intitule '.
Line 266... Line 171...
266
				'	LEFT JOIN del_utilisateur AS duo '.
171
				'	LEFT JOIN del_utilisateur AS duo '.
267
				'		ON do.ce_utilisateur = duo.id_utilisateur '.
172
				'		ON do.ce_utilisateur = duo.id_utilisateur '.
268
				'	LEFT JOIN del_utilisateur AS duv '.
173
				'	LEFT JOIN del_utilisateur AS duv '.
269
				'		ON if((CHAR_LENGTH(divo.ce_utilisateur) <> 32),CAST(divo.ce_utilisateur AS unsigned),0) '.
174
				'		ON if((CHAR_LENGTH(divo.ce_utilisateur) <> 32),CAST(divo.ce_utilisateur AS unsigned),0) '.
270
				'			= duv.id_utilisateur '.
175
				'			= duv.id_utilisateur '.
271
				$this->chargerClauseWhere().' '.
176
				($clauseWhere != '' ? "WHERE $clauseWhere " : '').
272
				'ORDER BY divo.date DESC '.
177
				'ORDER BY divo.date DESC '.
273
				'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite();
178
				'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().
-
 
179
				' -- '.__FILE__.' : '.__LINE__;
Line 274... Line 180...
274
 
180
 
275
		$elements = $this->bdd->recupererTous($requete);
181
		$elements = $this->bdd->recupererTous($requete);
276
		return $elements;
182
		return $elements;
Line 277... Line -...
277
	}
-
 
278
 
-
 
279
	/**
-
 
280
	* Charger la clause WHERE en fonction des paramètres de masque
183
	}
281
	* */
184
 
282
	private function chargerClauseWhere() {
185
	private function chargerClauseWhere() {
283
		$where = array();
186
		$where = array();
284
		$tableauMasque = $this->navigation->getFiltre();
187
		$filtres = $this->navigation->getFiltre();
285
		if (!empty($tableauMasque)) {
188
		if (!empty($filtres)) {
286
			foreach($tableauMasque as $idMasque => $valeurMasque) {
189
			$filtrePossibles = $this->conteneur->getParametreTableau('syndication.vote_filtres');
287
				$idMasque = str_replace('masque.', '', $idMasque);
190
			foreach ($filtres as $cleFiltre => $valeur) {
288
				switch ($idMasque) {
191
				if (in_array($cleFiltre, $filtrePossibles)) {
289
					case 'image':
-
 
290
						$where[] = ' '.$this->mappingFiltre[$idMasque].' = '.$this->bdd->proteger($valeurMasque);
192
					$champ = $this->mapping[$cleFiltre];
291
					break;
-
 
292
					case 'protocole':
-
 
293
						$where[] = ' '.$this->mappingFiltre[$idMasque].' = '.$this->bdd->proteger($valeurMasque).' ';
-
 
294
					break;
-
 
295
					default:
-
 
296
						$where[] = ' '.$this->mappingFiltre[$idMasque].' = '.$this->bdd->proteger($valeurMasque);
193
					$valeurP = $this->bdd->proteger($valeur);
297
					break;
194
					$where[] = " $champ = $valeurP ";
298
				}
195
				}
299
			}
-
 
300
		}
-
 
301
		if (!empty($where)) {
-
 
302
			return ' WHERE '.implode('AND', $where);
-
 
303
		} else {
-
 
304
			return;
-
 
Line 305... Line -...
305
		}
-
 
306
	}
-
 
307
 
-
 
308
	private function creerFiltreAuteur($valeurMasque) {
-
 
309
		$masque = '';
-
 
310
		$auteurId = $valeurMasque;
-
 
311
		if (is_numeric($auteurId)) {
-
 
312
			$masque = ' dc.ce_utilisateur = '.$auteurId;
196
			}
313
		} else {
-
 
314
			if (strpos($auteurId, '@') === false) {
-
 
315
				$tableauNomPrenom = explode(' ',$auteurId, 2);
-
 
316
				if(count($tableauNomPrenom) == 2) {
-
 
317
					// on teste potentiellement un nom prenom ou bien un prénom nom
-
 
318
					$masque = '('.
-
 
319
						'(dc.utilisateur_nom LIKE '.$this->bdd->proteger($tableauNomPrenom[0].'%').' AND '.
-
 
320
							'dc.utilisateur_prenom LIKE '.$this->bdd->proteger($tableauNomPrenom[1].'%').') OR '.
-
 
321
							'(dc.utilisateur_nom LIKE '.$this->bdd->proteger($tableauNomPrenom[1].'%').' AND '.
-
 
322
							'dc.utilisateur_prenom LIKE '.$this->bdd->proteger($tableauNomPrenom[0].'%').')'.
-
 
323
					')';
-
 
324
				} else {
-
 
325
					$masque = '(
-
 
326
						(dc.utilisateur_nom LIKE '.$this->bdd->proteger($auteurId.'%').' OR '.
-
 
327
						'dc.utilisateur_prenom LIKE '.$this->bdd->proteger($auteurId.'%').')'.
-
 
328
					')';
-
 
329
				}
-
 
330
			} else {
-
 
331
				$masque = " do.utilisateur_courriel LIKE ".$this->bdd->proteger($valeurMasque.'%')." ";
-
 
332
			}
197
		}
333
		}
-
 
334
		return $masque;
-
 
335
	}
198
 
-
 
199
		return (!empty($where)) ? implode('AND', $where) : '';
336
}
200
	}