Subversion Repositories eFlore/Applications.del

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2211 arthur 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
 * Service fournissant des informations concernant les tags 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 Tags {
19
 
20
	private $categorie = 'Tag';
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('tag', $derniere_info_en_date['date']);
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']);
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->formaterNomTagueur($info);
74
		return $item;
75
	}
76
 
77
	private function creerGuidItem($element) {
78
		$guid = sprintf($this->conteneur->getParametre('syndication.tag_guid_tpl'), $element['id_tag']);
79
		return $guid;
80
	}
81
 
82
	private function creerLienItem($element) {
83
		// TODO : ajouter un lien vers la plateforme validation de picto lorsqu'elle sera dispo
84
		$lien = sprintf($this->conteneur->getParametre('img_fiche_tpl'), $element['id_image']);
85
		return $lien;
86
	}
87
 
88
	private function creerTitre($element) {
89
		$tag = $element['tag'];
90
		$nomSel = htmlspecialchars($element['nom_sel']);
91
		$tagueur = htmlspecialchars($this->formaterNomTagueur($element));
92
		$auteurImg = $this->formaterNomAuteurImg($element);
93
		$titre = "Tag «{$tag}» par $tagueur pour $nomSel de $auteurImg";
94
		return $titre;
95
	}
96
 
97
	private function creerDescription($donnees, $item) {
98
		$idTag = htmlspecialchars($donnees['id_tag']);
99
		$idObs = htmlspecialchars($donnees['id_observation']);
100
		$idImg = htmlspecialchars($donnees['id_image']);
101
		$urlImg = $this->syndication->getUrlImage($donnees['id_image']);
102
		$miniatureUrl = $this->syndication->getUrlImage($donnees['id_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
		$tag = htmlspecialchars($donnees['tag']);
107
		$dateTag = htmlspecialchars(strftime('%A %d %B %Y à %H:%M', strtotime($donnees['date'])));
108
		$auteurImg = htmlspecialchars($this->creerAuteurImg($donnees));
109
		$tagueur = htmlspecialchars($this->creerTagueur($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>'."Tag pictoFlora #$idTag 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> $auteurImg</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>Tag #$idTag</h3>".
129
			'	<ul>'.
130
			'		<li><span class="champ">'."Tag :</span> <strong>$tag</strong></li>".
131
			'		<li><span class="champ">'."Auteur :</span> $tagueur</li>".
132
			'		<li><span class="champ">'."Taguée le :</span> $dateTag</li>".
133
			'	</ul>'.
134
			'</div>';
135
		return $description;
136
	}
137
 
138
	private function creerAuteurImg($info) {
139
		$auteur = $this->formaterNomAuteurImg($info).
140
			($this->syndication->fluxAdminDemande() ? ' ('.$info['auteur_courriel'].')' : '');
141
		return $auteur;
142
	}
143
 
144
	private function formaterNomAuteurImg($info) {
145
		$auteur = 'Anonyme';
146
		if ($info['auteur_prenom'] != '' && $info['auteur_nom'] != '') {
147
			$auteur = $info['auteur_prenom'].' '.$info['auteur_nom'];
148
		}
149
		return $auteur;
150
	}
151
 
152
	private function creerTagueur($info) {
153
		$tagueur = $this->formaterNomTagueur($info).
154
			($this->syndication->fluxAdminDemande() ? ' ('.$info['tagueur_courriel'].')' : '');
155
		return $tagueur;
156
	}
157
 
158
	private function formaterNomTagueur($info) {
159
		$tagueur = 'Anonyme';
160
		if ($info['tagueur_prenom'] != '' && $info['tagueur_nom'] != '') {
161
			$tagueur = $info['tagueur_prenom'].' '.$info['tagueur_nom'];
162
		}
163
		return $tagueur;
164
	}
165
 
166
	/**
167
	 * Retrouver les derniers votes image
168
	 * */
169
	private function getDerniersVotesImage() {
170
		$elements = array();
171
		$idsTags = $this->getIdsDerniersVotesImage();
172
		if (!empty($idsTags)) {
173
			$idsTagsConcat = implode(', ', $idsTags);
174
			$requete = 'SELECT DISTINCT id_tag, tag, date, '.
175
					'	do.id_observation, do.nom_sel, do.zone_geo, do.date_observation, di.id_image, '.
176
					'	duo.prenom AS auteur_prenom, duo.nom AS auteur_nom, '.
177
					'	du.prenom AS tagueur_prenom, du.nom AS tagueur_nom '.
178
					'FROM del_image_tag AS dit '.
179
					'	INNER JOIN del_image AS di '.
180
					'		ON ce_image = id_image '.
181
					'	INNER JOIN del_observation AS do '.
182
					'			ON di.ce_observation = do.id_observation '.
183
					'	LEFT JOIN del_utilisateur_infos AS duo '.
184
					'		ON do.ce_utilisateur = duo.id_utilisateur '.
185
					'	LEFT JOIN del_utilisateur_infos AS du '.
186
					'		ON if((CHAR_LENGTH(dit.ce_utilisateur) <> 32),CAST(dit.ce_utilisateur AS unsigned),0) '.
187
					'			= du.id_utilisateur '.
188
					'WHERE actif = 1 '.
189
					"	AND id_tag IN ($idsTagsConcat) ".
190
					'ORDER BY date DESC '.
191
					'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().' '.
192
					' -- '.__FILE__.' : '.__LINE__;
193
			$elements = $this->bdd->recupererTous($requete);
194
		}
195
		return $elements;
196
	}
197
 
198
	/**
199
	 * Retrouver les ids des derniers votes image
200
	 * */
201
	private function getIdsDerniersVotesImage() {
202
		$requete = 'SELECT DISTINCT id_tag '.
203
				'FROM del_image_tag '.
204
				'WHERE actif = 1 '.
205
				// Pas de filtre pour ce service. Utiliser le principe des autres ws de syndication si on devait en rajouter.
206
				'ORDER BY date DESC '.
207
				'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().' '.
208
				' -- '.__FILE__.' : '.__LINE__;
209
		$resultats = $this->bdd->recupererTous($requete);
210
 
211
		$idsTags = array();
212
		if ($resultats != false && is_array($resultats)) {
213
			foreach ($resultats as $infos) {
214
				$idsTags[] = $infos['id_tag'];
215
			}
216
		}
217
		return $idsTags;
218
	}
219
}