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