755 |
gduche |
1 |
<?php
|
1795 |
jpm |
2 |
// declare(encoding='UTF-8');
|
755 |
gduche |
3 |
/**
|
1684 |
jpm |
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 |
*
|
1795 |
jpm |
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>
|
1684 |
jpm |
41 |
*/
|
755 |
gduche |
42 |
class Observations extends RestService {
|
|
|
43 |
|
|
|
44 |
private $parametres = array();
|
|
|
45 |
private $ressources = array();
|
787 |
delphine |
46 |
private $methode = null;
|
1684 |
jpm |
47 |
private $serviceNom = 'observations';
|
|
|
48 |
private $sousServiceNom = null;
|
755 |
gduche |
49 |
private $cheminCourant = null;
|
|
|
50 |
|
|
|
51 |
private $conteneur;
|
1684 |
jpm |
52 |
|
755 |
gduche |
53 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
|
|
|
54 |
protected $utilisationParametresBruts = true;
|
|
|
55 |
|
|
|
56 |
public function __construct() {
|
|
|
57 |
$this->cheminCourant = dirname(__FILE__).DS;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
public function consulter($ressources, $parametres) {
|
787 |
delphine |
61 |
$this->methode = 'consulter';
|
1684 |
jpm |
62 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
63 |
return $this->executerService();
|
755 |
gduche |
64 |
}
|
1684 |
jpm |
65 |
|
787 |
delphine |
66 |
public function ajouter($ressources, $requeteDonnees) {
|
|
|
67 |
$this->methode = 'ajouter';
|
1684 |
jpm |
68 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
69 |
return $this->executerService();
|
787 |
delphine |
70 |
}
|
1684 |
jpm |
71 |
|
841 |
aurelien |
72 |
public function modifier($ressources, $requeteDonnees) {
|
|
|
73 |
$this->methode = 'modifier';
|
1684 |
jpm |
74 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
75 |
return $this->executerService();
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
private function executerService() {
|
841 |
aurelien |
79 |
$reponseHttp = new ReponseHttp();
|
|
|
80 |
try {
|
|
|
81 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
82 |
$resultat = $this->traiterRessources();
|
1684 |
jpm |
83 |
$reponseHttp->setResultatService($resultat);
|
841 |
aurelien |
84 |
} catch (Exception $e) {
|
|
|
85 |
$reponseHttp->ajouterErreur($e);
|
|
|
86 |
}
|
1684 |
jpm |
87 |
$reponseHttp->emettreLesEntetes();
|
|
|
88 |
$corps = $reponseHttp->getCorps();
|
|
|
89 |
return $corps;
|
841 |
aurelien |
90 |
}
|
755 |
gduche |
91 |
|
1684 |
jpm |
92 |
private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
|
755 |
gduche |
93 |
$this->ressources = $ressources;
|
|
|
94 |
$this->parametres = $parametres;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
private function traiterRessources() {
|
1684 |
jpm |
98 |
$this->chargerConfigService();
|
|
|
99 |
$this->analyserRessources();
|
|
|
100 |
$retour = $this->initialiserService();
|
755 |
gduche |
101 |
return $retour;
|
|
|
102 |
}
|
1684 |
jpm |
103 |
|
|
|
104 |
private function chargerConfigService() {
|
|
|
105 |
$chemin = Config::get('chemin_configurations')."config_{$this->serviceNom}.ini";
|
|
|
106 |
Config::charger($chemin);
|
755 |
gduche |
107 |
}
|
1684 |
jpm |
108 |
|
755 |
gduche |
109 |
/*------------------------------------------------------------------------------------------------------------------
|
1684 |
jpm |
110 |
CONFIGURATION DU SERVICE
|
755 |
gduche |
111 |
------------------------------------------------------------------------------------------------------------------*/
|
|
|
112 |
|
1684 |
jpm |
113 |
private function analyserRessources() {
|
|
|
114 |
if ($this->methode == 'consulter') {
|
|
|
115 |
$this->analyserRessoucesConsultation();
|
|
|
116 |
} else if ($this->methode == 'modifier' || $this->methode == 'ajouter') {
|
|
|
117 |
$this->analyserRessoucesModification();
|
|
|
118 |
}
|
755 |
gduche |
119 |
}
|
|
|
120 |
|
1684 |
jpm |
121 |
private function analyserRessoucesConsultation() {
|
|
|
122 |
if (count($this->ressources) == 0) {
|
|
|
123 |
if ($this->verifierParametreValeur('retour.format', 'widget')) {
|
|
|
124 |
// http://localhost/service:del:0.1/observations?retour.format=widget
|
|
|
125 |
$this->sousServiceNom = 'liste-observations-widget';
|
|
|
126 |
} else {
|
|
|
127 |
// http://localhost/service:del:0.1/observations
|
|
|
128 |
$this->sousServiceNom = 'liste-observations';
|
|
|
129 |
}
|
|
|
130 |
} else if (count($this->ressources) == 1) {
|
|
|
131 |
if ($this->etreRessourceIdentifiant(0)) {
|
|
|
132 |
// http://localhost/service:del:0.1/observations/#idObs
|
|
|
133 |
$this->sousServiceNom = 'observation';
|
|
|
134 |
}
|
|
|
135 |
} else if (count($this->ressources) == 3) {
|
|
|
136 |
if ($this->etreRessourceIdentifiant(0) && $this->etreRessourceIdentifiant(1) && $this->verifierRessourceValeur(2, 'vote')) {
|
|
|
137 |
// http://localhost/service:del:0.1/observations/#idObs/#idProposition/vote/
|
|
|
138 |
$this->sousServiceNom = 'vote-observation';
|
|
|
139 |
}
|
|
|
140 |
}
|
755 |
gduche |
141 |
|
1684 |
jpm |
142 |
if ($this->sousServiceNom == null) {
|
|
|
143 |
$this->lancerMessageErreurRessource();
|
|
|
144 |
}
|
755 |
gduche |
145 |
}
|
|
|
146 |
|
1684 |
jpm |
147 |
private function analyserRessoucesModification() {
|
|
|
148 |
if (count($this->ressources) == 1) {
|
|
|
149 |
if ($this->methode == 'modifier' && $this->etreRessourceIdentifiant(0)) {
|
|
|
150 |
// http://localhost/service:del:0.1/observations/#idObs
|
|
|
151 |
$this->sousServiceNom = 'observation';
|
985 |
aurelien |
152 |
}
|
1684 |
jpm |
153 |
} else if (count($this->ressources) == 3) {
|
|
|
154 |
if ($this->etreRessourceIdentifiant(0) && $this->etreRessourceIdentifiant(1) && $this->verifierRessourceValeur(2, 'vote')) {
|
|
|
155 |
// http://localhost/service:del:0.1/observations/#idObs/#idProposition/vote/
|
|
|
156 |
$this->sousServiceNom = 'vote-observation';
|
985 |
aurelien |
157 |
}
|
755 |
gduche |
158 |
}
|
1684 |
jpm |
159 |
|
|
|
160 |
if ($this->sousServiceNom == null) {
|
|
|
161 |
$this->lancerMessageErreurRessource();
|
|
|
162 |
}
|
755 |
gduche |
163 |
}
|
1684 |
jpm |
164 |
|
|
|
165 |
private function etreRessourceIdentifiant($num) {
|
|
|
166 |
$presenceId = false;
|
|
|
167 |
if (isset($this->ressources[$num]) && is_numeric($this->ressources[$num])) {
|
|
|
168 |
$presenceId = true;
|
755 |
gduche |
169 |
}
|
1684 |
jpm |
170 |
return $presenceId;
|
755 |
gduche |
171 |
}
|
|
|
172 |
|
1684 |
jpm |
173 |
private function verifierRessourceValeur($num, $valeur) {
|
|
|
174 |
$ok = false;
|
|
|
175 |
if (isset($this->ressources[$num]) && $this->ressources[$num] == $valeur) {
|
|
|
176 |
$ok = true;
|
|
|
177 |
}
|
|
|
178 |
return $ok;
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
private function verifierParametreValeur($cle, $valeur) {
|
|
|
182 |
$ok = false;
|
|
|
183 |
if (isset($this->parametres[$cle]) && $this->ressources[$cle] == $valeur) {
|
|
|
184 |
$ok = true;
|
|
|
185 |
}
|
|
|
186 |
return $ok;
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
private function lancerMessageErreurRessource() {
|
|
|
190 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
191 |
$message = "La ressource demandée '$ressource' ".
|
|
|
192 |
"n'est pas disponible pour le service ".$this->serviceNom." !\n".
|
|
|
193 |
"Les URLs disponibles sont : \n".
|
|
|
194 |
" - en GET : observations, observations/#idObs/#idProposition/vote \n".
|
|
|
195 |
" - en POST : observations/#id, observations/#idObs/#idProposition/vote \n".
|
|
|
196 |
" - en PUT : observations/#idObs/#idProposition/vote \n";
|
|
|
197 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
198 |
throw new Exception($message, $code);
|
|
|
199 |
}
|
|
|
200 |
|
755 |
gduche |
201 |
private function initialiserService() {
|
1684 |
jpm |
202 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
755 |
gduche |
203 |
$chemins = array();
|
1684 |
jpm |
204 |
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
|
755 |
gduche |
205 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
206 |
$retour = '';
|
|
|
207 |
$service = null;
|
|
|
208 |
foreach ($chemins as $chemin) {
|
|
|
209 |
if (file_exists($chemin)) {
|
1684 |
jpm |
210 |
$this->conteneur->chargerConfiguration('config_'.$this->serviceNom.'.ini');
|
755 |
gduche |
211 |
require_once $chemin;
|
|
|
212 |
$service = new $classe($this->conteneur);
|
787 |
delphine |
213 |
if ($this->methode == 'consulter') {
|
|
|
214 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
|
|
215 |
} elseif ($this->methode == 'ajouter') {
|
|
|
216 |
$retour = $service->ajouter($this->ressources, $this->parametres);
|
841 |
aurelien |
217 |
} elseif ($this->methode == 'modifier') {
|
|
|
218 |
$retour = $service->modifier($this->ressources, $this->parametres);
|
1706 |
jpm |
219 |
} else {
|
|
|
220 |
$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
|
|
|
221 |
"ne possède pas de méthode '{$this->methode}' !";
|
|
|
222 |
$code = RestServeur::HTTP_NON_IMPLEMENTE;
|
|
|
223 |
throw new Exception($message, $code);
|
787 |
delphine |
224 |
}
|
755 |
gduche |
225 |
}
|
|
|
226 |
}
|
1684 |
jpm |
227 |
|
755 |
gduche |
228 |
if (is_null($service)) {
|
1684 |
jpm |
229 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
230 |
$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
|
|
|
231 |
"n'existe pas dans le service '{$this->serviceNom}' !";
|
755 |
gduche |
232 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
233 |
throw new Exception($message, $code);
|
|
|
234 |
}
|
|
|
235 |
return $retour;
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
private function obtenirNomClasseService($mot) {
|
|
|
239 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
240 |
return $classeNom;
|
|
|
241 |
}
|
1684 |
jpm |
242 |
}
|