Subversion Repositories eFlore/Applications.del

Rev

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