Subversion Repositories eFlore/Applications.del

Rev

Rev 1922 | Rev 1978 | 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
 
1840 jpm 82
		// champs de l'annuaire (del_utilisateur): id_utilisateur prenom, nom, courriel
83
		$annuaireChamps = implode(', ', array(
1881 jpm 84
			"IFNULL(du.prenom, do.prenom_utilisateur) AS `auteur.prenom`",
85
			"IFNULL(du.nom, do.nom_utilisateur) AS `auteur.nom`",
86
			"IFNULL(du.courriel, do.courriel_utilisateur) AS `auteur.courriel`"));
1385 raphael 87
 
1922 jpm 88
		// TODO : bizarement MYSQL 5.6 retourne plusieurs fois les mêmes enregistrements d'où le DISTINCT (normalement inutile)
89
		$requete = "SELECT DISTINCT $obsChamps, $imgChamps, $annuaireChamps ".
1881 jpm 90
			"FROM del_observation AS do ".
91
			"	LEFT JOIN del_image AS di ON (do.id_observation = di.ce_observation) ".
92
			"	LEFT JOIN del_utilisateur AS du ON (do.ce_utilisateur = du.id_utilisateur) ".
93
			"WHERE do.id_observation = {$this->idObs} ".
1840 jpm 94
			'-- '.__FILE__.':'.__LINE__;
1881 jpm 95
		//Debug::printr($requete);
1922 jpm 96
		return $this->bdd->recupererTous($requete);
1385 raphael 97
	}
98
 
1840 jpm 99
	private function formaterObservation($infos) {
1881 jpm 100
		$infos = array_filter($infos);
1922 jpm 101
		foreach ($infos as $info) {
102
			$image = array_intersect_key($info, array_flip(array('id_image', 'date', 'hauteur' , 'largeur', 'nom_original')));
103
			$urlImgTpl = $this->conteneur->getParametre('cel_img_url_tpl');
104
			$imageFormat = 'XL';
105
			$image['binaire.href'] = sprintf($urlImgTpl, $image['id_image'], $imageFormat);
106
			unset($info['id_image'], $info['date'], $info['hauteur'], $info['largeur'], $info['nom_original']);
1881 jpm 107
 
1922 jpm 108
			// ATTENTION : la requête retourne de nombreuses lignes avec les mêmes données (test de l'existence nécessaire)
109
			if (!isset($this->observation)) {
110
				$this->observation = $info;
111
				$this->observation['images'] = array();
112
			}
113
			if (!isset($this->observation['images'][$image['id_image']])) {
114
				$this->observation['images'][$image['id_image']] = $image;
115
			}
116
		}
761 gduche 117
	}
1666 jpm 118
 
1840 jpm 119
	private function getCommentaires() {
120
		$selectVotes = array('id_vote', 'ce_proposition', 'ce_utilisateur', 'valeur', 'date');
121
		$selectCommentaires = array('id_commentaire', 'ce_observation', 'ce_proposition', 'ce_commentaire_parent', 'texte',
122
			'ce_utilisateur', 'utilisateur_prenom', 'utilisateur_nom', 'utilisateur_courriel',
123
			'nom_sel', 'nom_sel_nn', 'nom_ret', 'nom_ret_nn', 'nt', 'famille', 'nom_referentiel', 'date',
1938 aurelien 124
			'proposition_initiale','proposition_retenue');
1385 raphael 125
 
1840 jpm 126
		$voteChamps = $this->sql->getAliasDesChamps($this->mappings['votes'], $selectVotes, 'cv');
127
		$commentaireChamps = $this->sql->getAliasDesChamps($this->mappings['commentaires'], $selectCommentaires, 'dc');
1666 jpm 128
 
1840 jpm 129
		// LEFT JOIN optionnel, mais explicatif : récupèration des infos de vote que pour les commentaires comportant un nom_sel "valide"
130
		$requete = "SELECT $commentaireChamps, $voteChamps ".
131
			"FROM del_commentaire AS dc ".
132
			"	LEFT JOIN del_commentaire_vote AS cv ".
133
			"	ON (cv.ce_proposition = dc.id_commentaire AND dc.nom_sel != '' AND dc.nom_sel IS NOT NULL) ".
134
			"WHERE ce_observation = {$this->idObs} ".
135
			'-- '.__FILE__.':'.__LINE__;
1666 jpm 136
 
1840 jpm 137
		$commentaires = $this->bdd->recupererTous($requete);
138
		return $commentaires;
761 gduche 139
 
140
	}
1361 raphael 141
 
1840 jpm 142
	private function ajouterCommentaires($commentaires) {
1684 jpm 143
		if (!$commentaires) return;
1358 raphael 144
 
1385 raphael 145
		$ret = array();
146
		foreach ($commentaires as $comment) {
1840 jpm 147
			$commentId = $comment['id_commentaire'];
148
			$voteId = $comment['vote.id'];
1385 raphael 149
 
1840 jpm 150
			if (!array_key_exists($commentId, $ret)) {
151
				$comment_extract = array_intersect_key($comment, array_flip($this->mappings['commentaires']));
152
 
1385 raphael 153
				// cas particulier: conflit d'aliases avec del_commentaire_vote
154
				$comment_extract['auteur.id'] = $comment_extract['__auteur_com'];
155
				$comment_extract['date'] = $comment_extract['__date_com'];
156
				unset($comment_extract['__auteur_com'], $comment_extract['__date_com']);
157
 
158
				// toujours un éléments "votes", quand bien même il n'y en aurait pas
159
				$comment_extract['votes'] = array();
1840 jpm 160
				$ret[$commentId] = $comment_extract;
787 delphine 161
			}
1385 raphael 162
 
1840 jpm 163
			if (!$comment['nom_sel'] || ! $voteId) continue;
164
			$vote = array_intersect_key($comment, array_flip($this->mappings['votes']));
165
			$ret[$commentId]['votes'][$voteId] = $vote;
787 delphine 166
		}
1840 jpm 167
		$this->observation['commentaires'] = $ret;
761 gduche 168
	}
169
 
1840 jpm 170
	private function nettoyerAuteur() {
1881 jpm 171
		if (!isset($this->observation['auteur.id']) || !is_numeric($this->observation['auteur.id'])) {
1840 jpm 172
			$this->observation['auteur.id'] = '0';
1490 raphael 173
		}
1840 jpm 174
		if (!isset($this->observation['auteur.nom'])) {
175
			$this->observation['auteur.nom'] = '[inconnu]';
176
		}
1490 raphael 177
	}
1385 raphael 178
 
1684 jpm 179
	/**
180
	 * Modifie une observation directement dans le CEL en faisant un appel à un web service du CEL.
181
	 * Utilisé uniquement par les admins.
182
	 * Permet de dépublier une observation.
183
	 *
184
	 * @param array		$ressources tableau des informations contenues dans l'url après le nom du service
185
	 * @param array		$parametres contenu du post
1689 jpm 186
	 * @return mixed	Chaine "OK" (en majuscule) en cas de succès, booléen "false" en cas d'échec
1684 jpm 187
	 */
188
	public function modifier($ressources, $parametres) {
1690 jpm 189
		$controlAcces = $this->conteneur->getControleAcces();
190
		$controlAcces->etreUtilisateurAvecDroitAdmin();
191
 
1684 jpm 192
		$retour = false;
193
		if (isset($parametres['transmission'])) {
194
			$idObs = $ressources[0];
195
			$clientRest = $this->conteneur->getRestClient();
196
			$urlTpl = $this->conteneur->getParametre('urlServiceCelObs');
197
			$url = $urlTpl.$idObs;
1689 jpm 198
			$retourCel = $clientRest->modifier($url, $parametres);
199
			$retour = preg_match('/^OK$/i', $retourCel) ? 'OK' : false;
1697 jpm 200
			if ($retour === false) {
201
				$message = "Erreur du web service CEL : ".$retourCel;
202
				$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
203
				throw new Exception($message, $code);
204
			}
205
		} else {
206
			$message = "Ce web service doit contenir un paramètre 'transmission'.";
207
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
208
			throw new Exception($message, $code);
1684 jpm 209
		}
210
		return $retour;
211
	}
1385 raphael 212
}