Subversion Repositories eFlore/Applications.del

Rev

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