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