Subversion Repositories eFlore/Applications.del

Rev

Rev 1893 | Rev 1897 | Go to most recent revision | 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
 
1227 gduche 155
	/**
1284 jpm 156
	 * Retrouver les derniers votes image
157
	 * */
158
	private function getDerniersVotesImage() {
1821 jpm 159
		$clauseWhere = $this->chargerClauseWhere();
1284 jpm 160
		$requete =  'SELECT DISTINCT id_vote, ce_image, valeur, divo.date AS date_vote, '.
161
				'	duo.prenom AS observateur_prenom, duo.nom AS observateur_nom, '.
162
				'	duv.prenom AS votant_prenom, duv.nom AS votant_nom, '.
1292 jpm 163
				'	do.id_observation, do.nom_sel, do.zone_geo, do.date_observation, dip.intitule '.
1284 jpm 164
				'FROM del_image_vote AS divo '.
1893 jpm 165
				'	INNER JOIN del_image AS di '.
166
				'		ON divo.ce_image = di.id_image '.
1284 jpm 167
				'	INNER JOIN del_observation AS do '.
1893 jpm 168
				'		ON di.ce_observation = do.id_observation '.
1284 jpm 169
				'	INNER JOIN del_image_protocole AS dip '.
170
				'		ON ce_protocole = id_protocole '.
171
				'	LEFT JOIN del_utilisateur AS duo '.
172
				'		ON do.ce_utilisateur = duo.id_utilisateur '.
173
				'	LEFT JOIN del_utilisateur AS duv '.
1309 jpm 174
				'		ON if((CHAR_LENGTH(divo.ce_utilisateur) <> 32),CAST(divo.ce_utilisateur AS unsigned),0) '.
175
				'			= duv.id_utilisateur '.
1821 jpm 176
				($clauseWhere != '' ? "WHERE $clauseWhere " : '').
1284 jpm 177
				'ORDER BY divo.date DESC '.
1821 jpm 178
				'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().
179
				' -- '.__FILE__.' : '.__LINE__;
1284 jpm 180
 
1793 jpm 181
		$elements = $this->bdd->recupererTous($requete);
1284 jpm 182
		return $elements;
183
	}
184
 
1227 gduche 185
	private function chargerClauseWhere() {
186
		$where = array();
1821 jpm 187
		$filtres = $this->navigation->getFiltre();
188
		if (!empty($filtres)) {
189
			$filtrePossibles = $this->conteneur->getParametreTableau('syndication.vote_filtres');
190
			foreach ($filtres as $cleFiltre => $valeur) {
191
				if (in_array($cleFiltre, $filtrePossibles)) {
192
					$champ = $this->mapping[$cleFiltre];
193
					$valeurP = $this->bdd->proteger($valeur);
194
					$where[] = " $champ = $valeurP ";
1227 gduche 195
				}
1253 jpm 196
			}
1227 gduche 197
		}
1253 jpm 198
 
1821 jpm 199
		return (!empty($where)) ? implode('AND', $where) : '';
1227 gduche 200
	}
1821 jpm 201
}