Subversion Repositories eFlore/Applications.del

Rev

Rev 1358 | Rev 1379 | 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
		// Lancement du service
50
		$liaisons = $this->chargerLiaisons();
1361 raphael 51
		$observation = array();
52
 
53
		if($liaisons) {
54
			$observation = $this->chargerObservation($liaisons);
55
			// modifie $observation
56
			$this->chargerImages($observation);
57
			// modifie $observation
58
			$this->chargerCommentaires($observation);
59
		}
60
 
761 gduche 61
		// Mettre en forme le résultat et l'envoyer pour affichage
62
		$resultat = new ResultatService();
63
		$resultat->corps = $observation;
64
		return $resultat;
65
	}
66
 
67
	/*-------------------------------------------------------------------------------
68
	 							CONFIGURATION DU SERVICE
69
	 --------------------------------------------------------------------------------*/
70
	/**
71
	 * Configuration du service en fonction du fichier de config config_del.ini
72
	 * */
73
	private function configurer($ressources) {
74
		$this->mappingObservation = $this->conteneur->getParametre('mapping_observation');
787 delphine 75
		$this->mappingVotes = $this->conteneur->getParametre('mapping_votes');
950 aurelien 76
		$this->mappingCommentaire = $this->conteneur->getParametre('mapping_commentaire');
761 gduche 77
		if (empty($ressources) || sizeof($ressources) > 1 ) {
78
			$e = 'Le service observation accepete 1 et 1 seule ressource';
79
			throw new Exception($e, RestServeur::HTTP_CODE_ERREUR);
80
		} else {
81
			$this->id_observation = $ressources[0];
82
		}
83
	}
84
 
85
	/**
86
	 * Vérifier que le service est bien configuré
87
	 * */
88
	private function verifierConfiguration() {
89
 
90
		$erreurs = array();
91
		$tableauImages = $this->conteneur->getParametre('observations');
92
		if (empty($tableauImages)) {
93
			$erreurs[] = '- le fichier de configuration ne contient pas le tableau [images] ou celui-ci est vide ;';
94
		} else {
95
			if ($this->conteneur->getParametre('url_service') == null) {
96
				$erreurs[] = '- paramètre "url_service" manquant ;';
97
			}
98
 
99
			if ($this->conteneur->getParametre('url_images') == null) {
100
				$erreurs[] = '- paramètre "url_images" manquant ;';
101
			}
102
 
103
		}
104
 
105
		if (empty($this->mappingObservation)) {
106
			$erreurs[] = '- le fichier de configuration ne contient pas le tableau [mapping_observation] ou celui-ci est vide ;';
107
		} else {
108
			$champsMappingObs = array('id_observation', 'date_observation', 'date_transmission', 'famille', 'nom_sel', 'nom_sel_nn', 'nt',
109
								'ce_zone_geo', 'zone_geo', 'lieudit', 'station', 'courriel', 'ce_utilisateur', 'nom', 'prenom');
110
 
111
			foreach ($champsMappingObs as $champ) {
112
				if (!isset($this->mappingObservation[$champ])) {
113
					$erreurs[] = '- le mapping du champ "'.$champ.'" pour l\'observation est manquant ;';
114
				}
115
			}
116
		}
117
 
950 aurelien 118
		if (empty($this->mappingCommentaire)) {
119
			$erreurs[] = '- le fichier de configuration ne contient pas le tableau [mapping_commentaire] ou celui-ci est vide ;';
120
		} else {
121
			$champsMappingCom = array('id_commentaire', 'texte', 'ce_utilisateur', 'utilisateur_nom', 'utilisateur_prenom', 'utilisateur_courriel', 'date');
122
			foreach ($champsMappingCom as $champ) {
123
				if (!isset($this->mappingCommentaire[$champ])) {
124
					$erreurs[] = '- le mapping du champ "'.$champ.'" pour le commentaire est manquant ;';
125
				}
126
			}
127
		}
128
 
761 gduche 129
		if (!empty($erreurs)) {
130
			$e = 'Erreur lors de la configuration : '."\n";
131
			$e .= implode("\n", $erreurs);
132
			throw new Exception($e, RestServeur::HTTP_CODE_ERREUR);
133
		}
134
	}
135
 
136
	/*-------------------------------------------------------------------------------
137
								CHARGEMENT DES OBSERVATIONS
138
	--------------------------------------------------------------------------------*/
139
	/**
140
	* Chargement depuis la bdd de toutes les liaisons entre images et observations
141
	* */
142
	private function chargerLiaisons() {
143
 
1025 aurelien 144
		$requeteLiaisons = 'SELECT *, dob.commentaire as dob_commentaire '.
761 gduche 145
						   'FROM '.$this->gestionBdd->formaterTable('del_observation', 'dob').
146
						   'INNER JOIN '.$this->gestionBdd->formaterTable('del_obs_image', 'doi').
147
						   'ON doi.id_observation = dob.id_observation '.
1180 aurelien 148
						   'LEFT JOIN del_utilisateur du '.
820 gduche 149
						   'ON du.id_utilisateur = dob.ce_utilisateur '.
761 gduche 150
						   'WHERE doi.id_observation = '.$this->id_observation;
151
		$requeteLiaisons .=  ' GROUP BY doi.id_observation';
152
		$requeteLiaisons .= ' ORDER BY date_transmission DESC ';
153
		$requeteLiaisons .= $this->gestionBdd->getLimitSql();
154
 
155
		return $this->bdd->recuperer($requeteLiaisons);
156
	}
157
 
158
	/**
159
	* Retourner un tableau d'images formaté en fonction des liaisons trouvées
160
	* @param $liaisons les liaisons de la table del_obs_images
161
	* */
162
	private function chargerObservation($liaison) {
1361 raphael 163
 
1280 aurelien 164
		if($liaison['ce_utilisateur'] == 0) {
165
			$liaison['nom'] = $liaison['nom_utilisateur'];
166
			$liaison['prenom'] =$liaison['prenom_utilisateur'];
167
		}
168
 
761 gduche 169
		$observation = $this->formaterObservation($liaison);
170
		return $observation;
171
	}
172
 
173
	/**
174
	 * Sélectionner toutes les images de chaque observation
175
	 * @param array $observations la liste des observations
176
	 * */
1358 raphael 177
	private function chargerImages(&$observation) {
178
		$images = $this->bdd->recupererTous(sprintf(
179
			'SELECT * FROM %s INNER JOIN %s ON doi.id_image = di.id_image WHERE doi.id_observation = %d',
180
			$this->gestionBdd->formaterTable('del_obs_image', 'doi'),
181
			$this->gestionBdd->formaterTable('del_image', 'di'),
182
			$observation['id_observation']));
183
		if(!$images) return;
761 gduche 184
		$images = $this->formaterImages($images);
1358 raphael 185
		// modifie $images
186
		$this->chargerVotesImage($images);
761 gduche 187
		$observation['images'] = $images;
188
	}
189
 
190
	/**
841 aurelien 191
	* Charger les votes pour chaque image
192
	* */
1358 raphael 193
	private function chargerVotesImage(&$images) {
194
		$resultatsVotes = $this->bdd->recupererTous(sprintf(
195
			'SELECT v.*, p.* FROM %s INNER JOIN del_image_protocole p ON v.ce_protocole = p.id_protocole %s -- %s',
196
			$this->gestionBdd->formaterTable('del_image_vote', 'v'),
197
			$this->chargerClauseWhereVotesImage(),
198
			__FILE__ . ':' . __LINE__));
199
		if(!$resultatsVotes) return;
841 aurelien 200
 
201
		$votes = $this->formaterVotesImages($resultatsVotes);
202
		foreach ($images as $id => $image) {
203
			if (isset($votes[$image['id_image']])) {
204
				foreach($votes[$image['id_image']] as $id_vote => $vote_image) {
205
					$images[$id]['protocoles_votes'][$id_vote] = $vote_image;
206
				}
207
			}
208
		}
209
	}
210
 
211
	private function chargerClauseWhereVotesImage() {
212
		if (sizeof($this->imageIds) > 0) {
213
			$chaineImageIds = implode(',', $this->imageIds);
214
			$where[] = 'v.ce_image  IN ('.$chaineImageIds.')';
215
		}
216
		if (isset($this->parametres['protocole'])) {
217
			$where[] = 'v.ce_protocole = '.$this->proteger($this->parametres['protocole']);
218
		}
219
 
220
		$where = (!empty($where)) ? 'WHERE '.implode(' AND ', $where) : '';
221
		return $where;
222
	}
223
 
224
	/**
761 gduche 225
	* Récupérer tous les commentaires au total
226
	* @param array $observations la liste des observations à mettre à jour
227
	* */
1358 raphael 228
	private function chargerCommentaires(&$observation) {
761 gduche 229
		$requeteCommentaires = 'SELECT * FROM '.$this->gestionBdd->formaterTable('del_commentaire', 'dc').
230
							   'WHERE ce_observation = '.$observation['id_observation'];
231
		$commentaires = $this->bdd->recupererTous($requeteCommentaires);
1358 raphael 232
		if(!$commentaires) return;
233
 
841 aurelien 234
		$commentaires_formates = array();
235
		foreach ($commentaires as $commentaire) {
950 aurelien 236
			$commentaire = $this->formaterCommentaire($commentaire);
1000 delphine 237
			if (isset($commentaire['nom_sel']) && $commentaire['nom_sel'] != null) {
787 delphine 238
				$commentaire['votes'] = $this->chargerVotes($commentaire['id_commentaire']);
239
			}
841 aurelien 240
			$commentaires_formates[$commentaire['id_commentaire']] = $commentaire;
787 delphine 241
		}
841 aurelien 242
		$observation['commentaires'] = $commentaires_formates;
761 gduche 243
	}
244
 
787 delphine 245
	private function chargerVotes($id_commentaire) {
246
		$requeteVotes = 'SELECT * FROM '.
247
			$this->gestionBdd->formaterTable('del_commentaire_vote').
248
			'WHERE ce_proposition = '.$this->proteger($id_commentaire);
249
		$resultatsVotes = $this->bdd->recupererTous($requeteVotes);
841 aurelien 250
		$votes = array();
251
		foreach ($resultatsVotes as $vote) {
950 aurelien 252
			$id_vote = $vote['id_vote'];
253
			$votes[$id_vote] = $this->formaterVote($vote);
841 aurelien 254
		}
787 delphine 255
		return $votes;
256
	}
761 gduche 257
 
258
 
259
	/*-------------------------------------------------------------------------------
260
								FORMATER ET METTRE EN FORME
261
	--------------------------------------------------------------------------------*/
262
 
263
	/**
264
	 * Formater les images d'une observation
265
	 * @param array $images les images de l'observation
1358 raphael 266
	 * // TODO: en faire le maximum dans le SELECT
761 gduche 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
?>