Subversion Repositories eFlore/Applications.del

Rev

Rev 1903 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1227 gduche 1
<?php
1820 jpm 2
// declare(encoding='UTF-8');
1227 gduche 3
/**
1253 jpm 4
 * Service fournissant des informations concernant les votes sur les images de DEL en fonction d'un protocole
1227 gduche 5
 * au format RSS1, RSS2 ou ATOM.
1253 jpm 6
 *
1820 jpm 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>
1227 gduche 17
 */
1894 jpm 18
class Votes {
1253 jpm 19
 
1821 jpm 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;
1806 jpm 29
		$this->bdd = $this->conteneur->getBdd();
30
		$this->navigation = $this->conteneur->getNavigation();
1821 jpm 31
		$this->syndication = $this->conteneur->getSyndicationOutils();
32
		$this->mapping = $this->conteneur->getParametreTableau('syndication.mapping');
1227 gduche 33
	}
1253 jpm 34
 
1821 jpm 35
	public function consulter() {
36
		if ($this->syndication->fluxAdminDemande()) {
37
			$this->syndication->demanderAutorisationAdmin();
1253 jpm 38
		}
39
 
1227 gduche 40
		$donnees_brutes = $this->getDerniersVotesImage();
1253 jpm 41
		$commentaires_formates = $this->formaterPourRss($donnees_brutes) ;
1227 gduche 42
		return $commentaires_formates;
43
	}
1253 jpm 44
 
1227 gduche 45
	/**
46
	 * Formater les données pour mettre en page le RSS
47
	 * */
48
	private function formaterPourRss($elements) {
1821 jpm 49
		$derniere_info_en_date = reset($elements);
50
		$donnees = $this->syndication->construireDonneesCommunesAuFlux('vote', $derniere_info_en_date['date_vote']);
1227 gduche 51
		foreach ($elements as $element) {
52
			$donnees['items'][] = $this->construireDonneesCommunesAuxItems($element);
53
		}
54
		return $donnees;
55
	}
1253 jpm 56
 
1227 gduche 57
	/**
58
	 * Générer les données communes & spécifiques à chaque item
59
	 * */
60
	private function construireDonneesCommunesAuxItems($info) {
61
		$item = array();
1229 gduche 62
		$date_modification_timestamp = strtotime($info['date_vote']);
1227 gduche 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);
1821 jpm 70
		$item['categorie'] = htmlentities($this->categorie);
1227 gduche 71
		$item['description'] = $this->creerDescription($info, $item);
72
		$item['description_encodee'] = htmlspecialchars($this->creerDescription($info, $item));
1294 jpm 73
		$item['modifier_par'] = $this->creerVotant($info);
1227 gduche 74
		return $item;
75
	}
1253 jpm 76
 
1227 gduche 77
	private function creerGuidItem($element) {
1821 jpm 78
		$guid = sprintf($this->conteneur->getParametre('syndication.vote_guid_tpl'), $element['id_vote']);
1227 gduche 79
		return $guid;
80
	}
81
 
82
	private function creerLienItem($element) {
1821 jpm 83
		$lien = sprintf($this->conteneur->getParametre('img_fiche_tpl'), $element['ce_image']);
1227 gduche 84
		return $lien;
85
	}
1253 jpm 86
 
1227 gduche 87
	private function creerTitre($element) {
1253 jpm 88
		$noteVote = $element['valeur'];
89
		$nomSci = htmlspecialchars($element['nom_sel']);
1292 jpm 90
		$votant = htmlspecialchars($this->creerVotant($element));
91
		$observateur = htmlspecialchars($this->creerObservateur($element));
1253 jpm 92
 
1292 jpm 93
		$titre = "Vote $noteVote par $votant pour $nomSci de $observateur";
1227 gduche 94
		return $titre;
95
	}
1253 jpm 96
 
1227 gduche 97
	private function creerDescription($donnees, $item) {
1285 jpm 98
		$idVote = htmlspecialchars($donnees['id_vote']);
99
		$idObs = htmlspecialchars($donnees['id_observation']);
1284 jpm 100
		$idImg = htmlspecialchars($donnees['ce_image']);
1821 jpm 101
		$urlImg = $this->syndication->getUrlImage($donnees['ce_image']);
102
		$miniatureUrl = $this->syndication->getUrlImage($donnees['ce_image'], 'CRS');
1292 jpm 103
		$nomSelActuel = htmlspecialchars($donnees['nom_sel']);
104
		$dateObs = htmlspecialchars(str_replace(' 00:00:00', '', $donnees['date_observation']));
105
		$lieuObs = htmlspecialchars($donnees['zone_geo']);
1284 jpm 106
		$protocole = htmlspecialchars($donnees['intitule']);
1292 jpm 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));
1253 jpm 110
 
1292 jpm 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">'.
1284 jpm 114
			'	<a href="'.$urlImg.'">'.
115
			'		<img src="'.$miniatureUrl.'" alt="Img #'.$idImg.'"/>'.
116
			'	</a>'.
1292 jpm 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>';
1227 gduche 136
		return $description;
137
	}
1253 jpm 138
 
1292 jpm 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'];
1248 gduche 143
		}
1292 jpm 144
		return $votant;
1227 gduche 145
	}
1253 jpm 146
 
1292 jpm 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
 
1897 jpm 155
 
156
 
1227 gduche 157
	/**
1284 jpm 158
	 * Retrouver les derniers votes image
159
	 * */
160
	private function getDerniersVotesImage() {
1904 jpm 161
		$elements = array();
1897 jpm 162
		$idsVotes = $this->getIdsDerniersVotesImage();
1904 jpm 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 AS duo '.
177
					'		ON do.ce_utilisateur = duo.id_utilisateur '.
178
					'	LEFT JOIN del_utilisateur 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
		}
1284 jpm 187
		return $elements;
188
	}
189
 
1897 jpm 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();
1904 jpm 204
		if ($resultats != false && is_array($resultats)) {
1897 jpm 205
			foreach ($resultats as $infos) {
206
				$idsVotes[] = $infos['id_vote'];
207
			}
208
		}
209
		return $idsVotes;
210
	}
211
 
1227 gduche 212
	private function chargerClauseWhere() {
213
		$where = array();
1821 jpm 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 ";
1227 gduche 222
				}
1253 jpm 223
			}
1227 gduche 224
		}
1253 jpm 225
 
1821 jpm 226
		return (!empty($where)) ? implode('AND', $where) : '';
1227 gduche 227
	}
1821 jpm 228
}