Subversion Repositories eFlore/Applications.del

Rev

Rev 1978 | 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
 
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
 
1978 aurelien 88
		$requete = "SELECT $obsChamps, $imgChamps, $annuaireChamps ".
1881 jpm 89
			"FROM del_observation AS do ".
90
			"	LEFT JOIN del_image AS di ON (do.id_observation = di.ce_observation) ".
91
			"	LEFT JOIN del_utilisateur AS du ON (do.ce_utilisateur = du.id_utilisateur) ".
92
			"WHERE do.id_observation = {$this->idObs} ".
1840 jpm 93
			'-- '.__FILE__.':'.__LINE__;
1881 jpm 94
		//Debug::printr($requete);
1922 jpm 95
		return $this->bdd->recupererTous($requete);
1385 raphael 96
	}
97
 
1840 jpm 98
	private function formaterObservation($infos) {
1881 jpm 99
		$infos = array_filter($infos);
1922 jpm 100
		foreach ($infos as $info) {
101
			$image = array_intersect_key($info, array_flip(array('id_image', 'date', 'hauteur' , 'largeur', 'nom_original')));
102
			$urlImgTpl = $this->conteneur->getParametre('cel_img_url_tpl');
103
			$imageFormat = 'XL';
104
			$image['binaire.href'] = sprintf($urlImgTpl, $image['id_image'], $imageFormat);
105
			unset($info['id_image'], $info['date'], $info['hauteur'], $info['largeur'], $info['nom_original']);
1881 jpm 106
 
1922 jpm 107
			// ATTENTION : la requête retourne de nombreuses lignes avec les mêmes données (test de l'existence nécessaire)
108
			if (!isset($this->observation)) {
109
				$this->observation = $info;
110
				$this->observation['images'] = array();
111
			}
112
			if (!isset($this->observation['images'][$image['id_image']])) {
113
				$this->observation['images'][$image['id_image']] = $image;
114
			}
115
		}
761 gduche 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',
1938 aurelien 123
			'proposition_initiale','proposition_retenue');
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() {
1881 jpm 170
		if (!isset($this->observation['auteur.id']) || !is_numeric($this->observation['auteur.id'])) {
1840 jpm 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) {
2077 mathias 188
		$gestionUtilisateurs = $this->conteneur->getUtilisateur();
189
		$gestionUtilisateurs->etreUtilisateurAvecDroitAdmin();
1690 jpm 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
}