Subversion Repositories eFlore/Applications.del

Rev

Rev 1180 | Rev 1358 | 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
 
1025 aurelien 139
		$requeteLiaisons = 'SELECT *, dob.commentaire as dob_commentaire '.
761 gduche 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 '.
1180 aurelien 143
						   'LEFT 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) {
1280 aurelien 158
 
159
		if($liaison['ce_utilisateur'] == 0) {
160
			$liaison['nom'] = $liaison['nom_utilisateur'];
161
			$liaison['prenom'] =$liaison['prenom_utilisateur'];
162
		}
163
 
761 gduche 164
		$observation = $this->formaterObservation($liaison);
165
		return $observation;
166
	}
167
 
168
	/**
169
	 * Sélectionner toutes les images de chaque observation
170
	 * @param array $observations la liste des observations
171
	 * */
172
	private function chargerImages($observation) {
772 aurelien 173
		$requeteImages = 'SELECT * FROM '. $this->gestionBdd->formaterTable('del_obs_image', 'doi').
761 gduche 174
						 'INNER JOIN '.$this->gestionBdd->formaterTable('del_image', 'di').
175
						 'ON doi.id_image = di.id_image '.
176
						 'WHERE doi.id_observation = '.$observation['id_observation'];
177
 
178
		$images = $this->bdd->recupererTous($requeteImages);
179
		$images = $this->formaterImages($images);
841 aurelien 180
		$images = $this->chargerVotesImage($images);
761 gduche 181
		$observation['images'] = $images;
182
 
183
		return $observation;
184
	}
185
 
186
	/**
841 aurelien 187
	* Charger les votes pour chaque image
188
	* */
189
	private function chargerVotesImage($images) {
190
		$requeteVotes = 'SELECT v.*, p.* FROM '.
191
		$this->gestionBdd->formaterTable('del_image_vote', 'v').
192
									' INNER JOIN del_image_protocole p '.
193
										'ON v.ce_protocole = p.id_protocole '.
194
		$this->chargerClauseWhereVotesImage();
195
		$resultatsVotes = $this->bdd->recupererTous($requeteVotes);
196
 
197
		$votes = $this->formaterVotesImages($resultatsVotes);
198
 
199
		foreach ($images as $id => $image) {
200
			if (isset($votes[$image['id_image']])) {
201
				foreach($votes[$image['id_image']] as $id_vote => $vote_image) {
202
					$images[$id]['protocoles_votes'][$id_vote] = $vote_image;
203
				}
204
			}
205
		}
206
 
207
		return $images;
208
	}
209
 
210
	private function chargerClauseWhereVotesImage() {
211
		if (sizeof($this->imageIds) > 0) {
212
			$chaineImageIds = implode(',', $this->imageIds);
213
			$where[] = 'v.ce_image  IN ('.$chaineImageIds.')';
214
		}
215
		if (isset($this->parametres['protocole'])) {
216
			$where[] = 'v.ce_protocole = '.$this->proteger($this->parametres['protocole']);
217
		}
218
 
219
		$where = (!empty($where)) ? 'WHERE '.implode(' AND ', $where) : '';
220
		return $where;
221
	}
222
 
223
	/**
761 gduche 224
	* Récupérer tous les commentaires au total
225
	* @param array $observations la liste des observations à mettre à jour
226
	* */
227
	private function chargerCommentaires($observation) {
228
 
229
		$requeteCommentaires = 'SELECT * FROM '.$this->gestionBdd->formaterTable('del_commentaire', 'dc').
230
							   'WHERE ce_observation = '.$observation['id_observation'];
231
		$commentaires = $this->bdd->recupererTous($requeteCommentaires);
841 aurelien 232
 
233
		$commentaires_formates = array();
234
		foreach ($commentaires as $commentaire) {
950 aurelien 235
			$commentaire = $this->formaterCommentaire($commentaire);
1000 delphine 236
			if (isset($commentaire['nom_sel']) && $commentaire['nom_sel'] != null) {
787 delphine 237
				$commentaire['votes'] = $this->chargerVotes($commentaire['id_commentaire']);
238
			}
841 aurelien 239
			$commentaires_formates[$commentaire['id_commentaire']] = $commentaire;
787 delphine 240
		}
841 aurelien 241
		$observation['commentaires'] = $commentaires_formates;
761 gduche 242
 
243
		return $observation;
244
	}
245
 
787 delphine 246
	private function chargerVotes($id_commentaire) {
247
		$requeteVotes = 'SELECT * FROM '.
248
			$this->gestionBdd->formaterTable('del_commentaire_vote').
249
			'WHERE ce_proposition = '.$this->proteger($id_commentaire);
250
		$resultatsVotes = $this->bdd->recupererTous($requeteVotes);
841 aurelien 251
		$votes = array();
252
		foreach ($resultatsVotes as $vote) {
950 aurelien 253
			$id_vote = $vote['id_vote'];
254
			$votes[$id_vote] = $this->formaterVote($vote);
841 aurelien 255
		}
787 delphine 256
		return $votes;
257
	}
761 gduche 258
 
259
 
260
	/*-------------------------------------------------------------------------------
261
								FORMATER ET METTRE EN FORME
262
	--------------------------------------------------------------------------------*/
263
 
264
	/**
265
	 * Formater les images d'une observation
266
	 * @param array $images les images de l'observation
267
	 * */
268
	private function formaterImages($images) {
269
		$imagesRetour = array();
270
		foreach ($images as $image) {
841 aurelien 271
 
272
			$this->imageIds[] = $image['id_image'];
761 gduche 273
			$imageCourante = array();
274
			$imageCourante['id_image'] = $image['id_image'];
275
			$imageCourante['date'] = $image['date_prise_de_vue'];
276
			$imageCourante['binaire.href'] = $this->formaterLienImage($image['id_image']);
277
			$imageCourante['hauteur'] = $image['hauteur'];
278
			$imageRetour['largeur'] = $image['largeur'];
279
 
280
			$imagesRetour[] = $imageCourante;
281
		}
282
 
283
		return $imagesRetour;
284
	}
285
 
286
	/**
287
	*  Formater une observation depuis une ligne liaison
288
	*  @param $liaison liaison issue de la recherche
289
	*  @return $observation l'observation mise en forme
290
	* */
291
	private function formaterObservation($liaison) {
292
		$observation = array();
293
 
294
		foreach ($this->mappingObservation as $nomOriginal => $nomFinal) {
295
			$observation[$nomFinal] = $liaison[$nomOriginal];
296
		}
297
 
298
		$observation['images'] = array();
299
 
300
		return $observation;
301
	}
302
 
303
	/**
304
	 * Formater le lien de l'image en fonction du fichier de config et de l'identifiant de l'image
305
	 * */
306
	private function formaterLienImage($idImage) {
307
		$idImage = sprintf('%09s', $idImage);
308
		$url = $this->conteneur->getParametre('url_images');
309
		$urlImage = str_replace('%s', $idImage, $url);
310
		return $urlImage;
311
	}
312
 
313
	private function proteger($valeur) {
314
		if (is_array($valeur)) {
315
			return $this->bdd->protegerTableau($valeur);
316
		} else {
317
			return $this->bdd->proteger($valeur);
318
		}
319
	}
787 delphine 320
 
321
	/**
950 aurelien 322
	*
323
	* Formate un commentaire en fonction du fichier de configuration
324
	*/
325
	private function formaterCommentaire($commentaire) {
326
		$commentaire_formate = array();
327
		foreach ($this->mappingCommentaire as $nomOriginal => $nomFinal) {
1000 delphine 328
			if (isset($commentaire[$nomOriginal])) {
950 aurelien 329
			$commentaire_formate[$nomFinal] = $commentaire[$nomOriginal];
1000 delphine 330
			}
950 aurelien 331
		}
332
		return $commentaire_formate;
333
	}
334
 
335
	/**
787 delphine 336
	*  Formater un vote en fonction du fichier de configuration config_votes.ini
337
	*  @param $votes array()
338
	* */
950 aurelien 339
	private function formaterVote($vote) {
787 delphine 340
		$retour = array();
841 aurelien 341
		foreach ($vote as $param => $valeur) {
342
			$retour[$this->mappingVotes[$param]] = $valeur;
343
		}
344
		return $retour;
345
	}
346
 
347
	/**
348
	*  Formater une observation depuis une ligne liaison
349
	*  @param $liaison liaison issue de la recherche
350
	*  @return $observation l'observation mise en forme
351
	* */
352
	private function formaterVotesImages($votes) {
353
		$retour = array();
787 delphine 354
		foreach ($votes as $vote) {
841 aurelien 355
			$retour_vote = array();
356
			foreach ($vote as $param=>$valeur) {
357
				if (strpos($this->mappingVotes[$param], 'protocole.') === 0) {
358
					$retour_protocole[$this->mappingVotes[$param]] = $valeur;
359
				} else {
360
					$retour_vote[$this->mappingVotes[$param]] = $valeur;
361
				}
787 delphine 362
			}
841 aurelien 363
			if (!isset($retour[$vote['ce_image']][$vote['ce_protocole']])) {
364
				$retour[$vote['ce_image']][$vote['ce_protocole']] = $retour_protocole;
365
			}
366
			$retour[$vote['ce_image']][$vote['ce_protocole']]['votes'][$vote['id_vote']] = $retour_vote;
787 delphine 367
		}
841 aurelien 368
 
787 delphine 369
		return $retour;
370
	}
761 gduche 371
}
372
?>