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 votes sur les images de DEL en fonction d'un protocole
5
 * au format RSS1, RSS2 ou ATOM.
6
 *
7
 * @category   DEL
8
 * @package    Services
9
 * @subpackage Syndication
10
 * @version    0.1
11
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
12
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
13
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
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>
16
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
17
 */
18
class Votes {
19
 
20
	private $categorie = 'Vote protocole';
21
	private $conteneur;
22
	private $bdd;
23
	private $navigation;
24
	private $syndication;
25
	private $mapping = array();
26
 
27
	public function __construct(Conteneur $conteneur) {
28
		$this->conteneur = $conteneur;
29
		$this->bdd = $this->conteneur->getBdd();
30
		$this->navigation = $this->conteneur->getNavigation();
31
		$this->syndication = $this->conteneur->getSyndicationOutils();
32
		$this->mapping = $this->conteneur->getParametreTableau('syndication.mapping');
33
	}
34
 
35
	public function consulter() {
36
		if ($this->syndication->fluxAdminDemande()) {
37
			$this->syndication->demanderAutorisationAdmin();
38
		}
39
 
40
		$donnees_brutes = $this->getDerniersVotesImage();
41
		$commentaires_formates = $this->formaterPourRss($donnees_brutes) ;
42
		return $commentaires_formates;
43
	}
44
 
45
	/**
46
	 * Formater les données pour mettre en page le RSS
47
	 * */
48
	private function formaterPourRss($elements) {
49
		$derniere_info_en_date = reset($elements);
50
		$donnees = $this->syndication->construireDonneesCommunesAuFlux('vote', $derniere_info_en_date['date_vote']);
51
		foreach ($elements as $element) {
52
			$donnees['items'][] = $this->construireDonneesCommunesAuxItems($element);
53
		}
54
		return $donnees;
55
	}
56
 
57
	/**
58
	 * Générer les données communes & spécifiques à chaque item
59
	 * */
60
	private function construireDonneesCommunesAuxItems($info) {
61
		$item = array();
62
		$date_modification_timestamp = strtotime($info['date_vote']);
63
		$item['date_maj_simple'] = strftime('%A %d %B %Y à %H:%M', $date_modification_timestamp);
64
		$item['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
65
		$item['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
66
		$item['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
67
		$item['titre'] = $this->creerTitre($info);
68
		$item['guid'] = $this->creerGuidItem($info);
69
		$item['lien'] = $this->creerLienItem($info);
70
		$item['categorie'] = htmlentities($this->categorie);
71
		$item['description'] = $this->creerDescription($info, $item);
72
		$item['description_encodee'] = htmlspecialchars($this->creerDescription($info, $item));
73
		$item['modifier_par'] = $this->creerVotant($info);
74
		return $item;
75
	}
76
 
77
	private function creerGuidItem($element) {
78
		$guid = sprintf($this->conteneur->getParametre('syndication.vote_guid_tpl'), $element['id_vote']);
79
		return $guid;
80
	}
81
 
82
	private function creerLienItem($element) {
83
		$lien = sprintf($this->conteneur->getParametre('img_fiche_tpl'), $element['ce_image']);
84
		return $lien;
85
	}
86
 
87
	private function creerTitre($element) {
88
		$noteVote = $element['valeur'];
89
		$nomSci = htmlspecialchars($element['nom_sel']);
90
		$votant = htmlspecialchars($this->creerVotant($element));
91
		$observateur = htmlspecialchars($this->creerObservateur($element));
92
 
93
		$titre = "Vote $noteVote par $votant pour $nomSci de $observateur";
94
		return $titre;
95
	}
96
 
97
	private function creerDescription($donnees, $item) {
98
		$idVote = htmlspecialchars($donnees['id_vote']);
99
		$idObs = htmlspecialchars($donnees['id_observation']);
100
		$idImg = htmlspecialchars($donnees['ce_image']);
101
		$urlImg = $this->syndication->getUrlImage($donnees['ce_image']);
102
		$miniatureUrl = $this->syndication->getUrlImage($donnees['ce_image'], 'CRS');
103
		$nomSelActuel = htmlspecialchars($donnees['nom_sel']);
104
		$dateObs = htmlspecialchars(str_replace(' 00:00:00', '', $donnees['date_observation']));
105
		$lieuObs = htmlspecialchars($donnees['zone_geo']);
106
		$protocole = htmlspecialchars($donnees['intitule']);
107
		$votant = htmlspecialchars($this->creerVotant($donnees));
108
		$dateVote = htmlspecialchars(strftime('%A %d %B %Y à %H:%M', strtotime($donnees['date_vote'])));
109
		$observateur = htmlspecialchars($this->creerObservateur($donnees));
110
 
111
		$description = '<style>.champ{color:grey} .gauche{float:left;padding:0 20px 0 0;} ul{list-style-type:none;padding:0;}</style>'.
112
			'<h2>'."Vote pictoFlora #$idVote pour l'image #$idImg de l'observation #$idObs".'</h2>'.
113
			'<div class="gauche">'.
114
			'	<a href="'.$urlImg.'">'.
115
			'		<img src="'.$miniatureUrl.'" alt="Img #'.$idImg.'"/>'.
116
			'	</a>'.
117
			'</div>'.
118
			'<div class="gauche">'.
119
			"	<h3>Image #$idImg de l'observation #$idObs</h3>".
120
			'	<ul>'.
121
			'		<li><span class="champ">'."Auteur de l'image :</span> $observateur</li>".
122
			'		<li><span class="champ">'."Nom saisi actuel :</span> <em>$nomSelActuel</em></li>".
123
			'		<li><span class="champ">'."Lieu :</span> $lieuObs</li>".
124
			'		<li><span class="champ">'."Date :</span> $dateObs</li>".
125
			'	</ul>'.
126
			'</div>'.
127
			'<div class="gauche">'.
128
			"	<h3>Vote #$idVote</h3>".
129
			'	<ul>'.
130
			'		<li><span class="champ">'."Protocole :</span> <strong>$protocole</strong></li>".
131
			'		<li><span class="champ">'."Valeur :</span> <strong>{$donnees['valeur']}</strong>/5</li>".
132
			'		<li><span class="champ">'."Votant :</span> $votant</li>".
133
			'		<li><span class="champ">'."À voté le :</span> $dateVote</li>".
134
			'	</ul>'.
135
			'</div>';
136
		return $description;
137
	}
138
 
139
	private function creerVotant($info) {
140
		$votant = 'Anonyme';
141
		if (isset($info['votant_prenom']) && isset($info['votant_nom'])) {
142
			$votant = $info['votant_prenom'].' '.$info['votant_nom'];
143
		}
144
		return $votant;
145
	}
146
 
147
	private function creerObservateur($info) {
148
		$observateur = 'Anonyme';
149
		if ($info['observateur_prenom'] != '' && $info['observateur_nom'] != '') {
150
			$observateur = $info['observateur_prenom'].' '.$info['observateur_nom'];
151
		}
152
		return $observateur;
153
	}
154
 
155
 
156
 
157
	/**
158
	 * Retrouver les derniers votes image
159
	 * */
160
	private function getDerniersVotesImage() {
161
		$elements = array();
162
		$idsVotes = $this->getIdsDerniersVotesImage();
163
		if (!empty($idsVotes)) {
164
			$idsVotesConcat = implode(', ', $idsVotes);
165
			$requete =  'SELECT DISTINCT id_vote, ce_image, valeur, divo.date AS date_vote, '.
166
					'	duo.prenom AS observateur_prenom, duo.nom AS observateur_nom, '.
167
					'	duv.prenom AS votant_prenom, duv.nom AS votant_nom, '.
168
					'	do.id_observation, do.nom_sel, do.zone_geo, do.date_observation, dip.intitule '.
169
					'FROM del_image_vote AS divo '.
170
					'	INNER JOIN del_image AS di '.
171
					'		ON divo.ce_image = di.id_image '.
172
					'	INNER JOIN del_observation AS do '.
173
					'		ON di.ce_observation = do.id_observation '.
174
					'	INNER JOIN del_image_protocole AS dip '.
175
					'		ON ce_protocole = id_protocole '.
176
					'	LEFT JOIN del_utilisateur_infos AS duo '.
177
					'		ON do.ce_utilisateur = duo.id_utilisateur '.
178
					'	LEFT JOIN del_utilisateur_infos AS duv '.
179
					'		ON if((CHAR_LENGTH(divo.ce_utilisateur) <> 32),CAST(divo.ce_utilisateur AS unsigned),0) '.
180
					'			= duv.id_utilisateur '.
181
					"WHERE id_vote IN ($idsVotesConcat) ".
182
					'ORDER BY divo.date DESC '.
183
					'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().
184
					' -- '.__FILE__.' : '.__LINE__;
185
			$elements = $this->bdd->recupererTous($requete);
186
		}
187
		return $elements;
188
	}
189
 
190
	/**
191
	 * Retrouver les ids des derniers votes image
192
	 * */
193
	private function getIdsDerniersVotesImage() {
194
		$clauseWhere = $this->chargerClauseWhere();
195
		$requete =  'SELECT DISTINCT id_vote '.
196
				'FROM del_image_vote '.
197
				($clauseWhere != '' ? "WHERE $clauseWhere " : '').
198
				'ORDER BY date DESC '.
199
				'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().
200
				' -- '.__FILE__.' : '.__LINE__;
201
		$resultats = $this->bdd->recupererTous($requete);
202
 
203
		$idsVotes = array();
204
		if ($resultats != false && is_array($resultats)) {
205
			foreach ($resultats as $infos) {
206
				$idsVotes[] = $infos['id_vote'];
207
			}
208
		}
209
		return $idsVotes;
210
	}
211
 
212
	private function chargerClauseWhere() {
213
		$where = array();
214
		$filtres = $this->navigation->getFiltre();
215
		if (!empty($filtres)) {
216
			$filtrePossibles = $this->conteneur->getParametreTableau('syndication.vote_filtres');
217
			foreach ($filtres as $cleFiltre => $valeur) {
218
				if (in_array($cleFiltre, $filtrePossibles)) {
219
					$champ = $this->mapping[$cleFiltre];
220
					$valeurP = $this->bdd->proteger($valeur);
221
					$where[] = " $champ = $valeurP ";
222
				}
223
			}
224
		}
225
 
226
		return (!empty($where)) ? implode('AND', $where) : '';
227
	}
228
}