Subversion Repositories eFlore/Applications.del

Rev

Rev 2156 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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