Subversion Repositories eFlore/Applications.del

Rev

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

Rev Author Line No. Line
1231 gduche 1
<?php
1820 jpm 2
// declare(encoding='UTF-8');
1231 gduche 3
/**
1284 jpm 4
 * Service fournissant des informations concernant les tags sur les images de DEL en fonction d'un protocole
1231 gduche 5
 * au format RSS1, RSS2 ou ATOM.
1284 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>
1231 gduche 17
 */
1821 jpm 18
class Tags {
1284 jpm 19
 
1821 jpm 20
	private $categorie = 'Tag';
21
	private $conteneur;
22
	private $bdd;
23
	private $navigation;
24
	private $syndication;
25
	private $mapping = array();
1284 jpm 26
 
1821 jpm 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');
1231 gduche 33
	}
1284 jpm 34
 
1821 jpm 35
	public function consulter() {
36
		if ($this->syndication->fluxAdminDemande()) {
37
			$this->syndication->demanderAutorisationAdmin();
1284 jpm 38
		}
39
 
1231 gduche 40
		$donnees_brutes = $this->getDerniersVotesImage();
1284 jpm 41
		$commentaires_formates = $this->formaterPourRss($donnees_brutes) ;
1231 gduche 42
		return $commentaires_formates;
43
	}
1284 jpm 44
 
1231 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('tag', $derniere_info_en_date['date']);
1231 gduche 51
		foreach ($elements as $element) {
52
			$donnees['items'][] = $this->construireDonneesCommunesAuxItems($element);
53
		}
54
		return $donnees;
55
	}
1284 jpm 56
 
1231 gduche 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);
1821 jpm 70
		$item['categorie'] = htmlentities($this->categorie);
1231 gduche 71
		$item['description'] = $this->creerDescription($info, $item);
72
		$item['description_encodee'] = htmlspecialchars($this->creerDescription($info, $item));
1294 jpm 73
		$item['modifier_par'] = $this->formaterNomTagueur($info);
1231 gduche 74
		return $item;
75
	}
1284 jpm 76
 
1231 gduche 77
	private function creerGuidItem($element) {
1821 jpm 78
		$guid = sprintf($this->conteneur->getParametre('syndication.tag_guid_tpl'), $element['id_tag']);
1231 gduche 79
		return $guid;
80
	}
81
 
82
	private function creerLienItem($element) {
1232 gduche 83
		// TODO : ajouter un lien vers la plateforme validation de picto lorsqu'elle sera dispo
1821 jpm 84
		$lien = sprintf($this->conteneur->getParametre('img_fiche_tpl'), $element['id_image']);
1231 gduche 85
		return $lien;
86
	}
1284 jpm 87
 
1231 gduche 88
	private function creerTitre($element) {
1284 jpm 89
		$tag = $element['tag'];
90
		$nomSel = htmlspecialchars($element['nom_sel']);
1296 jpm 91
		$tagueur = htmlspecialchars($this->formaterNomTagueur($element));
92
		$auteurImg = $this->formaterNomAuteurImg($element);
93
		$titre = "Tag «{$tag}» par $tagueur pour $nomSel de $auteurImg";
1231 gduche 94
		return $titre;
95
	}
1284 jpm 96
 
1231 gduche 97
	private function creerDescription($donnees, $item) {
1285 jpm 98
		$idTag = htmlspecialchars($donnees['id_tag']);
99
		$idObs = htmlspecialchars($donnees['id_observation']);
1284 jpm 100
		$idImg = htmlspecialchars($donnees['id_image']);
1821 jpm 101
		$urlImg = $this->syndication->getUrlImage($donnees['id_image']);
102
		$miniatureUrl = $this->syndication->getUrlImage($donnees['id_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']);
106
		$tag = htmlspecialchars($donnees['tag']);
107
		$dateTag = htmlspecialchars(strftime('%A %d %B %Y à %H:%M', strtotime($donnees['date'])));
108
		$auteurImg = htmlspecialchars($this->creerAuteurImg($donnees));
1284 jpm 109
		$tagueur = htmlspecialchars($this->creerTagueur($donnees));
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>'."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>';
1231 gduche 135
		return $description;
136
	}
1284 jpm 137
 
138
	private function creerAuteurImg($info) {
1296 jpm 139
		$auteur = $this->formaterNomAuteurImg($info).
1821 jpm 140
			($this->syndication->fluxAdminDemande() ? ' ('.$info['auteur_courriel'].')' : '');
1296 jpm 141
		return $auteur;
142
	}
143
 
144
	private function formaterNomAuteurImg($info) {
1284 jpm 145
		$auteur = 'Anonyme';
146
		if ($info['auteur_prenom'] != '' && $info['auteur_nom'] != '') {
1296 jpm 147
			$auteur = $info['auteur_prenom'].' '.$info['auteur_nom'];
1231 gduche 148
		}
1284 jpm 149
		return $auteur;
1231 gduche 150
	}
1284 jpm 151
 
152
	private function creerTagueur($info) {
153
		$tagueur = $this->formaterNomTagueur($info).
1821 jpm 154
			($this->syndication->fluxAdminDemande() ? ' ('.$info['tagueur_courriel'].')' : '');
1284 jpm 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
 
1231 gduche 166
	/**
1284 jpm 167
	 * Retrouver les derniers votes image
168
	 * */
169
	private function getDerniersVotesImage() {
1922 jpm 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, duo.courriel AS auteur_courriel, '.
177
					'	du.prenom AS tagueur_prenom, du.nom AS tagueur_nom, du.courriel AS tagueur_courriel '.
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 AS duo '.
184
					'		ON do.ce_utilisateur = duo.id_utilisateur '.
185
					'	LEFT JOIN del_utilisateur 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 '.
1284 jpm 204
				'WHERE actif = 1 '.
1922 jpm 205
				// Pas de filtre pour ce service. Utiliser le principe des autres ws de syndication si on devait en rajouter.
1284 jpm 206
				'ORDER BY date DESC '.
1821 jpm 207
				'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().' '.
208
				' -- '.__FILE__.' : '.__LINE__;
1922 jpm 209
		$resultats = $this->bdd->recupererTous($requete);
1284 jpm 210
 
1922 jpm 211
		$idsTags = array();
212
		if ($resultats != false && is_array($resultats)) {
213
			foreach ($resultats as $infos) {
214
				$idsTags[] = $infos['id_tag'];
1284 jpm 215
			}
1231 gduche 216
		}
1922 jpm 217
		return $idsTags;
1231 gduche 218
	}
1821 jpm 219
}