Subversion Repositories eFlore/Applications.del

Rev

Rev 1799 | Rev 1845 | 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
/**
1684 jpm 3
 * Web service retournant toutes les infos d'une observation donnée :
1385 raphael 4
 * images, votes sur image et protocole, commentaires, votes sur commentaires, ...
761 gduche 5
 *
1684 jpm 6
 * @category	DEL
7
 * @package		Observations
8
 * @version		0.1
1385 raphael 9
 * @author		Raphaël Droz <raphael@tela-botanica.org>
1684 jpm 10
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
1385 raphael 11
 * @copyright	Copyright (c) 2013, Tela Botanica (accueil@tela-botanica.org)
1684 jpm 12
 * @license		GPL v3 <http://www.gnu.org/licenses/gpl.txt>
13
 * @license		CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
14
 * @see			http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=ApiIdentiplante01Observations
1385 raphael 15
 *
1684 jpm 16
 * @config-depend: "url_image" (dans configurations/config_observations.ini) ex: http://www.tela-botanica.org/appli:cel-img:%09dXL.jpg
761 gduche 17
 */
18
 
19
// http://localhost/del/services/0.1/observations/#id => une observation donnée et ses images, SANS LES propositions & nombre de commentaire
1385 raphael 20
 
1490 raphael 21
require_once(dirname(__FILE__) . '/../DelTk.php');
1385 raphael 22
 
1840 jpm 23
class ObservationDetails {
1385 raphael 24
 
1684 jpm 25
	private $conteneur;
26
	private $bdd;
1840 jpm 27
	private $sql;
28
	private $idObs;
29
	private $protocole;
30
	private $observation;
31
	private $mappings = array();
32
 
33
	public function __construct(Conteneur $conteneur) {
34
		$this->conteneur = $conteneur;
35
		$this->bdd = $this->conteneur->getBdd();
36
		$this->sql = $this->conteneur->getSql();
1385 raphael 37
 
1840 jpm 38
		$this->mappings['observations'] = $this->conteneur->getParametreTableau('observations.mapping');
39
		$this->mappings['images'] = $this->conteneur->getParametreTableau('images.mapping');
40
		$this->mappings['votes'] = $this->conteneur->getParametreTableau('votes.mapping');
41
		$this->mappings['commentaires'] = $this->conteneur->getParametreTableau('commentaires.mapping');
42
		// les deux alias suivants sont particuliers afin d'éviter un conflit d'alias lors des jointures avec del_commentaire_vote
43
		$this->mappings['commentaires']['ce_utilisateur'] = '__auteur_com';
44
		$this->mappings['commentaires']['date'] = '__date_com';
761 gduche 45
	}
1666 jpm 46
 
761 gduche 47
	public function consulter($ressources, $parametres) {
1840 jpm 48
		$this->idObs = $ressources[0];
49
		$this->protocole = isset($parametres['protocole']) && is_numeric($parametres['protocole']) ? intval($parametres['protocole']) : null;
1385 raphael 50
 
51
		// 1) récupération de l'observation (et de ses images (v_del_image est une vue utilisant des INNER JOIN))
1840 jpm 52
		$infos = $this->getInfosObservationEtImages();
53
		if (! $infos) {
54
			$message = "Aucune observation ne possède d'identifiant '{$this->idObs}'.";
55
			throw new Exception($message, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
1379 raphael 56
		}
1840 jpm 57
		$this->formaterObservation($infos);
58
		//var_dump($this->observation);
1385 raphael 59
		// 3) charge les données de votes et protocoles associés aux images
1840 jpm 60
		if ($this->observation['images']) {
61
			$idsImages = array_keys($this->observation['images']);
62
			$votes = $this->sql->getVotesDesImages($idsImages, $this->protocole);
63
			$this->sql->ajouterInfosVotesProtocoles($votes, $this->observation['images']);
1385 raphael 64
		}
65
 
1840 jpm 66
		// 4) charge les commentaires et les votes associés -> modifie/créé $observation['commentaires']
67
		$commentaires = $this->getCommentaires();
68
		$this->ajouterCommentaires($commentaires);
1385 raphael 69
 
70
		// désindexe le tableau (tel qu'apparement attendu par les applis), c'est une exception
1840 jpm 71
		// TODO : corriger l'appli cliente pour utiliser les index puis supprimer cette ligne
72
		$this->observation['images'] = array_values($this->observation['images']);
73
 
1385 raphael 74
		// autre élément de post-processing: le ce_utilisateur de l'observation non-numeric...
1840 jpm 75
		$this->nettoyerAuteur();
1389 raphael 76
 
761 gduche 77
		// Mettre en forme le résultat et l'envoyer pour affichage
78
		$resultat = new ResultatService();
1840 jpm 79
		$resultat->corps = $this->observation;
761 gduche 80
		return $resultat;
81
	}
1385 raphael 82
 
1840 jpm 83
	private function getInfosObservationEtImages() {
84
		$obsChamps = $this->sql->getAliasDesChamps($this->mappings['observations'], null, 'dob');
85
		$imgChamps = $this->sql->getAliasDesChamps($this->mappings['images'], null, 'dob');
1385 raphael 86
 
1840 jpm 87
		// champs de l'annuaire (del_utilisateur): id_utilisateur prenom, nom, courriel
88
		$annuaireChamps = implode(', ', array(
89
			"IFNULL(du.prenom, prenom_utilisateur) AS `auteur.prenom`",
90
			"IFNULL(du.nom, nom_utilisateur) AS `auteur.nom`",
91
			"IFNULL(du.courriel, courriel_utilisateur) AS `auteur.courriel`"));
1385 raphael 92
 
1840 jpm 93
		$requete = "SELECT $obsChamps, $imgChamps, $annuaireChamps ".
94
			"FROM v_del_image as dob ".
95
			"LEFT JOIN del_utilisateur AS du ".
96
			"	ON CAST(du.id_utilisateur AS CHAR) = CAST(dob.ce_utilisateur AS CHAR) ".
97
			"WHERE dob.id_observation = {$this->idObs} ".
98
			'-- '.__FILE__.':'.__LINE__;
99
		//var_dump($requete);
100
		return $this->bdd->recupererTous($requete);
1385 raphael 101
	}
102
 
1840 jpm 103
	private function formaterObservation($infos) {
104
		$urlImgTpl = $this->conteneur->getParametre('cel_img_url_tpl');
105
		$imageFormat = 'XL';
1666 jpm 106
 
1840 jpm 107
		$infos = array_map('array_filter', $infos);
108
		foreach ($infos as $info) {
109
			$image = array_intersect_key($info, array_flip(array('id_image', 'date', 'hauteur' , 'largeur', 'nom_original')));
110
			$image['binaire.href'] = sprintf($urlImgTpl, $image['id_image'], $imageFormat);
111
			unset($info['id_image'], $info['date'], $info['hauteur'], $info['largeur'], $info['nom_original']);
1385 raphael 112
 
1840 jpm 113
			if (!isset($this->observation)) $this->observation = $info;
114
			$this->observation['images'][$image['id_image']] = $image;
761 gduche 115
		}
116
	}
1666 jpm 117
 
1840 jpm 118
	private function getCommentaires() {
119
		$selectVotes = array('id_vote', 'ce_proposition', 'ce_utilisateur', 'valeur', 'date');
120
		$selectCommentaires = array('id_commentaire', 'ce_observation', 'ce_proposition', 'ce_commentaire_parent', 'texte',
121
			'ce_utilisateur', 'utilisateur_prenom', 'utilisateur_nom', 'utilisateur_courriel',
122
			'nom_sel', 'nom_sel_nn', 'nom_ret', 'nom_ret_nn', 'nt', 'famille', 'nom_referentiel', 'date',
123
			'proposition_initiale');
1385 raphael 124
 
1840 jpm 125
		$voteChamps = $this->sql->getAliasDesChamps($this->mappings['votes'], $selectVotes, 'cv');
126
		$commentaireChamps = $this->sql->getAliasDesChamps($this->mappings['commentaires'], $selectCommentaires, 'dc');
1666 jpm 127
 
1840 jpm 128
		// LEFT JOIN optionnel, mais explicatif : récupèration des infos de vote que pour les commentaires comportant un nom_sel "valide"
129
		$requete = "SELECT $commentaireChamps, $voteChamps ".
130
			"FROM del_commentaire AS dc ".
131
			"	LEFT JOIN del_commentaire_vote AS cv ".
132
			"	ON (cv.ce_proposition = dc.id_commentaire AND dc.nom_sel != '' AND dc.nom_sel IS NOT NULL) ".
133
			"WHERE ce_observation = {$this->idObs} ".
134
			'-- '.__FILE__.':'.__LINE__;
1666 jpm 135
 
1840 jpm 136
		$commentaires = $this->bdd->recupererTous($requete);
137
		return $commentaires;
761 gduche 138
 
139
	}
1361 raphael 140
 
1840 jpm 141
	private function ajouterCommentaires($commentaires) {
1684 jpm 142
		if (!$commentaires) return;
1358 raphael 143
 
1385 raphael 144
		$ret = array();
145
		foreach ($commentaires as $comment) {
1840 jpm 146
			$commentId = $comment['id_commentaire'];
147
			$voteId = $comment['vote.id'];
1385 raphael 148
 
1840 jpm 149
			if (!array_key_exists($commentId, $ret)) {
150
				$comment_extract = array_intersect_key($comment, array_flip($this->mappings['commentaires']));
151
 
1385 raphael 152
				// cas particulier: conflit d'aliases avec del_commentaire_vote
153
				$comment_extract['auteur.id'] = $comment_extract['__auteur_com'];
154
				$comment_extract['date'] = $comment_extract['__date_com'];
155
				unset($comment_extract['__auteur_com'], $comment_extract['__date_com']);
156
 
157
				// toujours un éléments "votes", quand bien même il n'y en aurait pas
158
				$comment_extract['votes'] = array();
1840 jpm 159
				$ret[$commentId] = $comment_extract;
787 delphine 160
			}
1385 raphael 161
 
1840 jpm 162
			if (!$comment['nom_sel'] || ! $voteId) continue;
163
			$vote = array_intersect_key($comment, array_flip($this->mappings['votes']));
164
			$ret[$commentId]['votes'][$voteId] = $vote;
787 delphine 165
		}
1840 jpm 166
		$this->observation['commentaires'] = $ret;
761 gduche 167
	}
168
 
1840 jpm 169
	private function nettoyerAuteur() {
170
		if (!is_numeric($this->observation['auteur.id'])) {
171
			$this->observation['auteur.id'] = '0';
1490 raphael 172
		}
1840 jpm 173
		if (!isset($this->observation['auteur.nom'])) {
174
			$this->observation['auteur.nom'] = '[inconnu]';
175
		}
1490 raphael 176
	}
1385 raphael 177
 
1684 jpm 178
	/**
179
	 * Modifie une observation directement dans le CEL en faisant un appel à un web service du CEL.
180
	 * Utilisé uniquement par les admins.
181
	 * Permet de dépublier une observation.
182
	 *
183
	 * @param array		$ressources tableau des informations contenues dans l'url après le nom du service
184
	 * @param array		$parametres contenu du post
1689 jpm 185
	 * @return mixed	Chaine "OK" (en majuscule) en cas de succès, booléen "false" en cas d'échec
1684 jpm 186
	 */
187
	public function modifier($ressources, $parametres) {
1690 jpm 188
		$controlAcces = $this->conteneur->getControleAcces();
189
		$controlAcces->etreUtilisateurAvecDroitAdmin();
190
 
1684 jpm 191
		$retour = false;
192
		if (isset($parametres['transmission'])) {
193
			$idObs = $ressources[0];
194
			$clientRest = $this->conteneur->getRestClient();
195
			$urlTpl = $this->conteneur->getParametre('urlServiceCelObs');
196
			$url = $urlTpl.$idObs;
1689 jpm 197
			$retourCel = $clientRest->modifier($url, $parametres);
198
			$retour = preg_match('/^OK$/i', $retourCel) ? 'OK' : false;
1697 jpm 199
			if ($retour === false) {
200
				$message = "Erreur du web service CEL : ".$retourCel;
201
				$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
202
				throw new Exception($message, $code);
203
			}
204
		} else {
205
			$message = "Ce web service doit contenir un paramètre 'transmission'.";
206
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
207
			throw new Exception($message, $code);
1684 jpm 208
		}
209
		return $retour;
210
	}
1385 raphael 211
}