Subversion Repositories eFlore/Applications.del

Rev

Rev 950 | Rev 1025 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
761 gduche 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
	* Le web service observations récupère toutes les observations et, pour chacune d'elle, les
5
	* images qui lui sont associées.
6
 *
7
 * @category	php 5.2
8
 * @package	del
9
 * @subpackage images
10
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
 * @copyright	Copyright (c) 2012, Tela Botanica (accueil@tela-botanica.org)
12
 * @license	http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
13
 * @license	http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
14
 * @version	$Id: Bdd.php 403 2012-02-22 14:35:20Z gduche $
15
 * @see http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=ApiIdentiplante01Observations
16
 */
17
 
18
// http://localhost/del/services/0.1/observations/#id => une observation donnée et ses images, SANS LES propositions & nombre de commentaire
19
class Observation {
20
 
21
	private $conteneur;
22
	private $gestionBdd;
23
	private $bdd;
24
	private $id_observation;
841 aurelien 25
	private $imageIds;
761 gduche 26
 
27
	public function __construct(Conteneur $conteneur = null) {
28
		$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
787 delphine 29
		$this->conteneur->chargerConfiguration('config_votes.ini');
841 aurelien 30
		$this->conteneur->chargerConfiguration('config_mapping_votes.ini');
950 aurelien 31
		$this->conteneur->chargerConfiguration('config_mapping_commentaires.ini');
761 gduche 32
		$this->gestionBdd = $conteneur->getGestionBdd();
33
		$this->bdd = $this->gestionBdd->getBdd();
34
	}
35
 
36
	/**
37
	 * Méthode principale de la classe.
38
	 * Lance la récupération des images dans la base et les place dans un objet ResultatService
39
	 * pour l'afficher.
40
	 * @param array $ressources les ressources situées après l'url de base (ex : http://url/ressource1/ressource2)
41
	 * @param array $parametres les paramètres situés après le ? dans l'url
42
	 * */
43
	public function consulter($ressources, $parametres) {
44
 
45
		// Gestion des configuration du script
46
		$this->configurer($ressources);
47
		$this->verifierConfiguration();
48
 
49
 
50
		// Lancement du service
51
		$liaisons = $this->chargerLiaisons();
52
		$observation = $this->chargerObservation($liaisons);
53
		$observation = $this->chargerImages($observation);
54
		$observation = $this->chargerCommentaires($observation);
55
 
56
		// Mettre en forme le résultat et l'envoyer pour affichage
57
		$resultat = new ResultatService();
58
		$resultat->corps = $observation;
59
		return $resultat;
60
	}
61
 
62
	/*-------------------------------------------------------------------------------
63
	 							CONFIGURATION DU SERVICE
64
	 --------------------------------------------------------------------------------*/
65
	/**
66
	 * Configuration du service en fonction du fichier de config config_del.ini
67
	 * */
68
	private function configurer($ressources) {
69
		$this->mappingObservation = $this->conteneur->getParametre('mapping_observation');
787 delphine 70
		$this->mappingVotes = $this->conteneur->getParametre('mapping_votes');
950 aurelien 71
		$this->mappingCommentaire = $this->conteneur->getParametre('mapping_commentaire');
761 gduche 72
		if (empty($ressources) || sizeof($ressources) > 1 ) {
73
			$e = 'Le service observation accepete 1 et 1 seule ressource';
74
			throw new Exception($e, RestServeur::HTTP_CODE_ERREUR);
75
		} else {
76
			$this->id_observation = $ressources[0];
77
		}
78
	}
79
 
80
	/**
81
	 * Vérifier que le service est bien configuré
82
	 * */
83
	private function verifierConfiguration() {
84
 
85
		$erreurs = array();
86
		$tableauImages = $this->conteneur->getParametre('observations');
87
		if (empty($tableauImages)) {
88
			$erreurs[] = '- le fichier de configuration ne contient pas le tableau [images] ou celui-ci est vide ;';
89
		} else {
90
			if ($this->conteneur->getParametre('url_service') == null) {
91
				$erreurs[] = '- paramètre "url_service" manquant ;';
92
			}
93
 
94
			if ($this->conteneur->getParametre('url_images') == null) {
95
				$erreurs[] = '- paramètre "url_images" manquant ;';
96
			}
97
 
98
		}
99
 
100
		if (empty($this->mappingObservation)) {
101
			$erreurs[] = '- le fichier de configuration ne contient pas le tableau [mapping_observation] ou celui-ci est vide ;';
102
		} else {
103
			$champsMappingObs = array('id_observation', 'date_observation', 'date_transmission', 'famille', 'nom_sel', 'nom_sel_nn', 'nt',
104
								'ce_zone_geo', 'zone_geo', 'lieudit', 'station', 'courriel', 'ce_utilisateur', 'nom', 'prenom');
105
 
106
			foreach ($champsMappingObs as $champ) {
107
				if (!isset($this->mappingObservation[$champ])) {
108
					$erreurs[] = '- le mapping du champ "'.$champ.'" pour l\'observation est manquant ;';
109
				}
110
			}
111
		}
112
 
950 aurelien 113
		if (empty($this->mappingCommentaire)) {
114
			$erreurs[] = '- le fichier de configuration ne contient pas le tableau [mapping_commentaire] ou celui-ci est vide ;';
115
		} else {
116
			$champsMappingCom = array('id_commentaire', 'texte', 'ce_utilisateur', 'utilisateur_nom', 'utilisateur_prenom', 'utilisateur_courriel', 'date');
117
			foreach ($champsMappingCom as $champ) {
118
				if (!isset($this->mappingCommentaire[$champ])) {
119
					$erreurs[] = '- le mapping du champ "'.$champ.'" pour le commentaire est manquant ;';
120
				}
121
			}
122
		}
123
 
761 gduche 124
		if (!empty($erreurs)) {
125
			$e = 'Erreur lors de la configuration : '."\n";
126
			$e .= implode("\n", $erreurs);
127
			throw new Exception($e, RestServeur::HTTP_CODE_ERREUR);
128
		}
129
	}
130
 
131
	/*-------------------------------------------------------------------------------
132
								CHARGEMENT DES OBSERVATIONS
133
	--------------------------------------------------------------------------------*/
134
	/**
135
	* Chargement depuis la bdd de toutes les liaisons entre images et observations
136
	* */
137
	private function chargerLiaisons() {
138
 
139
		$requeteLiaisons = 'SELECT * '.
140
						   'FROM '.$this->gestionBdd->formaterTable('del_observation', 'dob').
141
						   'INNER JOIN '.$this->gestionBdd->formaterTable('del_obs_image', 'doi').
142
						   'ON doi.id_observation = dob.id_observation '.
143
						   'INNER JOIN del_utilisateur du '.
820 gduche 144
						   'ON du.id_utilisateur = dob.ce_utilisateur '.
761 gduche 145
						   'WHERE doi.id_observation = '.$this->id_observation;
146
		$requeteLiaisons .=  ' GROUP BY doi.id_observation';
147
		$requeteLiaisons .= ' ORDER BY date_transmission DESC ';
148
		$requeteLiaisons .= $this->gestionBdd->getLimitSql();
149
 
150
		return $this->bdd->recuperer($requeteLiaisons);
151
	}
152
 
153
	/**
154
	* Retourner un tableau d'images formaté en fonction des liaisons trouvées
155
	* @param $liaisons les liaisons de la table del_obs_images
156
	* */
157
	private function chargerObservation($liaison) {
158
		$observation = $this->formaterObservation($liaison);
159
		return $observation;
160
	}
161
 
162
	/**
163
	 * Sélectionner toutes les images de chaque observation
164
	 * @param array $observations la liste des observations
165
	 * */
166
	private function chargerImages($observation) {
772 aurelien 167
		$requeteImages = 'SELECT * FROM '. $this->gestionBdd->formaterTable('del_obs_image', 'doi').
761 gduche 168
						 'INNER JOIN '.$this->gestionBdd->formaterTable('del_image', 'di').
169
						 'ON doi.id_image = di.id_image '.
170
						 'WHERE doi.id_observation = '.$observation['id_observation'];
171
 
172
		$images = $this->bdd->recupererTous($requeteImages);
173
		$images = $this->formaterImages($images);
841 aurelien 174
		$images = $this->chargerVotesImage($images);
761 gduche 175
		$observation['images'] = $images;
176
 
177
		return $observation;
178
	}
179
 
180
	/**
841 aurelien 181
	* Charger les votes pour chaque image
182
	* */
183
	private function chargerVotesImage($images) {
184
		$requeteVotes = 'SELECT v.*, p.* FROM '.
185
		$this->gestionBdd->formaterTable('del_image_vote', 'v').
186
									' INNER JOIN del_image_protocole p '.
187
										'ON v.ce_protocole = p.id_protocole '.
188
		$this->chargerClauseWhereVotesImage();
189
		$resultatsVotes = $this->bdd->recupererTous($requeteVotes);
190
 
191
		$votes = $this->formaterVotesImages($resultatsVotes);
192
 
193
		foreach ($images as $id => $image) {
194
			if (isset($votes[$image['id_image']])) {
195
				foreach($votes[$image['id_image']] as $id_vote => $vote_image) {
196
					$images[$id]['protocoles_votes'][$id_vote] = $vote_image;
197
				}
198
			}
199
		}
200
 
201
		return $images;
202
	}
203
 
204
	private function chargerClauseWhereVotesImage() {
205
		if (sizeof($this->imageIds) > 0) {
206
			$chaineImageIds = implode(',', $this->imageIds);
207
			$where[] = 'v.ce_image  IN ('.$chaineImageIds.')';
208
		}
209
		if (isset($this->parametres['protocole'])) {
210
			$where[] = 'v.ce_protocole = '.$this->proteger($this->parametres['protocole']);
211
		}
212
 
213
		$where = (!empty($where)) ? 'WHERE '.implode(' AND ', $where) : '';
214
		return $where;
215
	}
216
 
217
	/**
761 gduche 218
	* Récupérer tous les commentaires au total
219
	* @param array $observations la liste des observations à mettre à jour
220
	* */
221
	private function chargerCommentaires($observation) {
222
 
223
		$requeteCommentaires = 'SELECT * FROM '.$this->gestionBdd->formaterTable('del_commentaire', 'dc').
224
							   'WHERE ce_observation = '.$observation['id_observation'];
225
		$commentaires = $this->bdd->recupererTous($requeteCommentaires);
841 aurelien 226
 
227
		$commentaires_formates = array();
228
		foreach ($commentaires as $commentaire) {
950 aurelien 229
			$commentaire = $this->formaterCommentaire($commentaire);
1000 delphine 230
			if (isset($commentaire['nom_sel']) && $commentaire['nom_sel'] != null) {
787 delphine 231
				$commentaire['votes'] = $this->chargerVotes($commentaire['id_commentaire']);
232
			}
841 aurelien 233
			$commentaires_formates[$commentaire['id_commentaire']] = $commentaire;
787 delphine 234
		}
841 aurelien 235
		$observation['commentaires'] = $commentaires_formates;
761 gduche 236
 
237
		return $observation;
238
	}
239
 
787 delphine 240
	private function chargerVotes($id_commentaire) {
241
		$requeteVotes = 'SELECT * FROM '.
242
			$this->gestionBdd->formaterTable('del_commentaire_vote').
243
			'WHERE ce_proposition = '.$this->proteger($id_commentaire);
244
		$resultatsVotes = $this->bdd->recupererTous($requeteVotes);
841 aurelien 245
		$votes = array();
246
		foreach ($resultatsVotes as $vote) {
950 aurelien 247
			$id_vote = $vote['id_vote'];
248
			$votes[$id_vote] = $this->formaterVote($vote);
841 aurelien 249
		}
787 delphine 250
		return $votes;
251
	}
761 gduche 252
 
253
 
254
	/*-------------------------------------------------------------------------------
255
								FORMATER ET METTRE EN FORME
256
	--------------------------------------------------------------------------------*/
257
 
258
	/**
259
	 * Formater les images d'une observation
260
	 * @param array $images les images de l'observation
261
	 * */
262
	private function formaterImages($images) {
263
		$imagesRetour = array();
264
		foreach ($images as $image) {
841 aurelien 265
 
266
			$this->imageIds[] = $image['id_image'];
761 gduche 267
			$imageCourante = array();
268
			$imageCourante['id_image'] = $image['id_image'];
269
			$imageCourante['date'] = $image['date_prise_de_vue'];
270
			$imageCourante['binaire.href'] = $this->formaterLienImage($image['id_image']);
271
			$imageCourante['hauteur'] = $image['hauteur'];
272
			$imageRetour['largeur'] = $image['largeur'];
273
 
274
			$imagesRetour[] = $imageCourante;
275
		}
276
 
277
		return $imagesRetour;
278
	}
279
 
280
	/**
281
	*  Formater une observation depuis une ligne liaison
282
	*  @param $liaison liaison issue de la recherche
283
	*  @return $observation l'observation mise en forme
284
	* */
285
	private function formaterObservation($liaison) {
286
		$observation = array();
287
 
288
		foreach ($this->mappingObservation as $nomOriginal => $nomFinal) {
289
			$observation[$nomFinal] = $liaison[$nomOriginal];
290
		}
291
 
292
		$observation['images'] = array();
293
 
294
		return $observation;
295
	}
296
 
297
	/**
298
	 * Formater le lien de l'image en fonction du fichier de config et de l'identifiant de l'image
299
	 * */
300
	private function formaterLienImage($idImage) {
301
		$idImage = sprintf('%09s', $idImage);
302
		$url = $this->conteneur->getParametre('url_images');
303
		$urlImage = str_replace('%s', $idImage, $url);
304
		return $urlImage;
305
	}
306
 
307
	private function proteger($valeur) {
308
		if (is_array($valeur)) {
309
			return $this->bdd->protegerTableau($valeur);
310
		} else {
311
			return $this->bdd->proteger($valeur);
312
		}
313
	}
787 delphine 314
 
315
	/**
950 aurelien 316
	*
317
	* Formate un commentaire en fonction du fichier de configuration
318
	*/
319
	private function formaterCommentaire($commentaire) {
320
		$commentaire_formate = array();
321
		foreach ($this->mappingCommentaire as $nomOriginal => $nomFinal) {
1000 delphine 322
			if (isset($commentaire[$nomOriginal])) {
950 aurelien 323
			$commentaire_formate[$nomFinal] = $commentaire[$nomOriginal];
1000 delphine 324
			}
950 aurelien 325
		}
326
		return $commentaire_formate;
327
	}
328
 
329
	/**
787 delphine 330
	*  Formater un vote en fonction du fichier de configuration config_votes.ini
331
	*  @param $votes array()
332
	* */
950 aurelien 333
	private function formaterVote($vote) {
787 delphine 334
		$retour = array();
841 aurelien 335
		foreach ($vote as $param => $valeur) {
336
			$retour[$this->mappingVotes[$param]] = $valeur;
337
		}
338
		return $retour;
339
	}
340
 
341
	/**
342
	*  Formater une observation depuis une ligne liaison
343
	*  @param $liaison liaison issue de la recherche
344
	*  @return $observation l'observation mise en forme
345
	* */
346
	private function formaterVotesImages($votes) {
347
		$retour = array();
787 delphine 348
		foreach ($votes as $vote) {
841 aurelien 349
			$retour_vote = array();
350
			foreach ($vote as $param=>$valeur) {
351
				if (strpos($this->mappingVotes[$param], 'protocole.') === 0) {
352
					$retour_protocole[$this->mappingVotes[$param]] = $valeur;
353
				} else {
354
					$retour_vote[$this->mappingVotes[$param]] = $valeur;
355
				}
787 delphine 356
			}
841 aurelien 357
			if (!isset($retour[$vote['ce_image']][$vote['ce_protocole']])) {
358
				$retour[$vote['ce_image']][$vote['ce_protocole']] = $retour_protocole;
359
			}
360
			$retour[$vote['ce_image']][$vote['ce_protocole']]['votes'][$vote['id_vote']] = $retour_vote;
787 delphine 361
		}
841 aurelien 362
 
787 delphine 363
		return $retour;
364
	}
761 gduche 365
}
366
?>