Subversion Repositories eFlore/Applications.del

Rev

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

Rev Author Line No. Line
2215 delphine 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
 * Classe principale de chargement des services Observations.
5
 *
6
 * URLs possibles :
7
 * GET :
8
 * http://localhost/service:del:0.1/observations
9
 * toutes les observations (infos obs, infos images, infos propositions, infos nb commentaires)
10
 *
11
 * http://localhost/service:del:0.1/observations?retour.format=widget
12
 * toutes les infos des observations pour le Widget DEL
13
 *
14
 * http://localhost/service:del:0.1/observations/#idObs
15
 * une observation donnée et ses images, SANS LES propositions & nombre de commentaire*
16
 *
17
 * http://localhost/service:del:0.1/observations/#idObs/#idVote/vote
18
 * toutes les infos sur les votes d'une proposition
19
 *
20
 * PUT :
21
 * http://localhost/service:del:0.1/observations/#idObs/#idCommentaire/vote
22
 * ajoute un vote (+ ou -) pour une obs et une proposition donnée
23
 *
24
 * POST :
25
 * http://localhost/service:del:0.1/observations/#idObs
26
 * utilisé seulement par les admins pour modifier une obs depuis DEL (dépublication des obs)
27
 *
28
 * http://localhost/service:del:0.1/observations/#idObs/#idCommentaire/vote
29
 * modifie un vote (+ ou -) pour une obs et une proposition donnée
30
 *
31
 * @category   DEL
32
 * @package    Services
33
 * @subpackage Observations
34
 * @version    0.1
35
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
36
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
37
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
38
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
39
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
40
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
41
 */
42
 
43
class Observations extends RestService {
44
 
45
	private $parametres = array();
46
	private $ressources = array();
47
	private $methode = null;
2216 delphine 48
	private $serviceNom = 'tepik';
2215 delphine 49
	private $sousServiceNom = null;
50
	private $cheminCourant = null;
51
 
52
	private $conteneur;
53
 
54
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
55
	protected $utilisationParametresBruts = true;
56
 
57
	public function __construct() {
58
		$this->cheminCourant = dirname(__FILE__).DS;
59
	}
60
 
61
	public function consulter($ressources, $parametres) {
62
		$this->methode = 'consulter';
63
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
64
		return $this->executerService();
65
	}
66
 
67
	public function ajouter($ressources, $requeteDonnees) {
68
		$this->methode = 'ajouter';
69
		$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
70
		return $this->executerService();
71
	}
72
 
73
	public function modifier($ressources, $requeteDonnees) {
74
		$this->methode = 'modifier';
75
		$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
76
		return $this->executerService();
77
	}
78
 
79
	private function executerService() {
80
		$reponseHttp = new ReponseHttp();
81
		try {
82
			$this->conteneur = new Conteneur($this->parametres);
83
			$resultat = $this->traiterRessources();
84
			$reponseHttp->setResultatService($resultat);
85
		} catch (Exception $e) {
86
			$reponseHttp->ajouterErreur($e);
87
		}
88
		$reponseHttp->emettreLesEntetes();
89
		$corps = $reponseHttp->getCorps();
90
		return $corps;
91
	}
92
 
93
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
94
		$this->ressources = $ressources;
95
		$this->parametres = $parametres;
96
	}
97
 
98
	private function traiterRessources() {
99
		$this->analyserRessources();
100
		$retour = $this->initialiserService();
101
		return $retour;
102
	}
103
 
104
	private function analyserRessources() {
105
		if ($this->methode == 'consulter') {
106
			$this->analyserRessoucesConsultation();
107
		} else if ($this->methode == 'modifier' || $this->methode == 'ajouter') {
108
			$this->analyserRessoucesModification();
109
		}
110
	}
111
 
112
	private function analyserRessoucesConsultation() {
113
		if (count($this->ressources) == 0) {
114
			// http://localhost/service:del:0.1/observations
115
			$this->sousServiceNom = 'liste-observations';
116
		} else if (count($this->ressources) == 1) {
117
			if ($this->etreRessourceIdentifiant(0)) {
118
				// http://localhost/service:del:0.1/observations/#idObs
119
				$this->sousServiceNom = 'observation-details';
120
			}
2216 delphine 121
		}
2215 delphine 122
		if ($this->sousServiceNom == null) {
123
			$this->lancerMessageErreurRessource();
124
		}
125
	}
126
 
127
	private function analyserRessoucesModification() {
128
		if (count($this->ressources) == 1) {
129
			if ($this->methode == 'modifier' && $this->etreRessourceIdentifiant(0)) {
130
				// http://localhost/service:del:0.1/observations/#idObs
131
				$this->sousServiceNom = 'observation-details';
132
			}
133
		}
2216 delphine 134
 
2215 delphine 135
		if ($this->sousServiceNom == null) {
136
			$this->lancerMessageErreurRessource();
137
		}
138
	}
139
 
140
	private function etreRessourceIdentifiant($num) {
141
		$presenceId = false;
142
		if (isset($this->ressources[$num]) && is_numeric($this->ressources[$num])) {
143
			$presenceId = true;
144
		}
145
		return $presenceId;
146
	}
147
 
148
	private function verifierRessourceValeur($num, $valeur) {
149
		$ok = false;
150
		if (isset($this->ressources[$num]) && $this->ressources[$num] == $valeur) {
151
			$ok = true;
152
		}
153
		return $ok;
154
	}
155
 
156
	private function verifierParametreValeur($cle, $valeur) {
157
		$ok = false;
158
		if (isset($this->parametres[$cle]) && $this->ressources[$cle] == $valeur) {
159
			$ok = true;
160
		}
161
		return $ok;
162
	}
163
 
164
	private function lancerMessageErreurRessource() {
165
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
166
		$message = "La ressource demandée '$ressource' ".
167
			"n'est pas disponible pour le service ".$this->serviceNom." !\n".
168
			"Les URLs disponibles sont : \n".
169
			" - en GET : observations, observations/#idObs/#idProposition/vote \n".
170
			" - en POST : observations/#id, observations/#idObs/#idProposition/vote \n".
171
			" - en PUT : observations/#idObs/#idProposition/vote \n";
172
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
173
		throw new Exception($message, $code);
174
	}
175
 
176
	private function initialiserService() {
177
		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
178
		$chemins = array();
179
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
180
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
181
		$retour = '';
182
		$service = null;
183
		foreach ($chemins as $chemin) {
184
			if (file_exists($chemin)) {
185
				require_once $chemin;
186
				$service = new $classe($this->conteneur);
187
				if ($this->methode == 'consulter') {
188
					$retour = $service->consulter($this->ressources, $this->parametres);
189
				} elseif ($this->methode == 'ajouter') {
190
					$retour = $service->ajouter($this->ressources, $this->parametres);
191
				} elseif ($this->methode == 'modifier') {
192
					$retour = $service->modifier($this->ressources, $this->parametres);
193
				} else {
194
					$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
195
						"ne possède pas de méthode '{$this->methode}' !";
196
					$code = RestServeur::HTTP_NON_IMPLEMENTE;
197
					throw new Exception($message, $code);
198
				}
199
			}
200
		}
201
 
202
		if (is_null($service)) {
203
			$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
204
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
205
				"n'existe pas dans le service '{$this->serviceNom}' !";
206
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
207
			throw new Exception($message, $code);
208
		}
209
		return $retour;
210
	}
211
 
212
	private function obtenirNomClasseService($mot) {
213
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
214
		return $classeNom;
215
	}
216
}