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 |
|
2217 |
delphine |
43 |
class Tepik extends RestService {
|
2215 |
delphine |
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 |
|
|
|
68 |
|
|
|
69 |
private function executerService() {
|
|
|
70 |
$reponseHttp = new ReponseHttp();
|
|
|
71 |
try {
|
|
|
72 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
73 |
$resultat = $this->traiterRessources();
|
|
|
74 |
$reponseHttp->setResultatService($resultat);
|
|
|
75 |
} catch (Exception $e) {
|
|
|
76 |
$reponseHttp->ajouterErreur($e);
|
|
|
77 |
}
|
|
|
78 |
$reponseHttp->emettreLesEntetes();
|
|
|
79 |
$corps = $reponseHttp->getCorps();
|
|
|
80 |
return $corps;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
|
|
|
84 |
$this->ressources = $ressources;
|
|
|
85 |
$this->parametres = $parametres;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
private function traiterRessources() {
|
|
|
89 |
$this->analyserRessources();
|
|
|
90 |
$retour = $this->initialiserService();
|
|
|
91 |
return $retour;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
private function analyserRessources() {
|
|
|
95 |
if ($this->methode == 'consulter') {
|
|
|
96 |
$this->analyserRessoucesConsultation();
|
2217 |
delphine |
97 |
}
|
2215 |
delphine |
98 |
}
|
|
|
99 |
|
|
|
100 |
private function analyserRessoucesConsultation() {
|
|
|
101 |
if (count($this->ressources) == 0) {
|
|
|
102 |
// http://localhost/service:del:0.1/observations
|
|
|
103 |
$this->sousServiceNom = 'liste-observations';
|
|
|
104 |
} else if (count($this->ressources) == 1) {
|
|
|
105 |
if ($this->etreRessourceIdentifiant(0)) {
|
|
|
106 |
// http://localhost/service:del:0.1/observations/#idObs
|
|
|
107 |
$this->sousServiceNom = 'observation-details';
|
|
|
108 |
}
|
2216 |
delphine |
109 |
}
|
2215 |
delphine |
110 |
if ($this->sousServiceNom == null) {
|
|
|
111 |
$this->lancerMessageErreurRessource();
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
private function analyserRessoucesModification() {
|
|
|
116 |
if (count($this->ressources) == 1) {
|
|
|
117 |
if ($this->methode == 'modifier' && $this->etreRessourceIdentifiant(0)) {
|
|
|
118 |
// http://localhost/service:del:0.1/observations/#idObs
|
|
|
119 |
$this->sousServiceNom = 'observation-details';
|
|
|
120 |
}
|
|
|
121 |
}
|
2216 |
delphine |
122 |
|
2215 |
delphine |
123 |
if ($this->sousServiceNom == null) {
|
|
|
124 |
$this->lancerMessageErreurRessource();
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
private function etreRessourceIdentifiant($num) {
|
|
|
129 |
$presenceId = false;
|
|
|
130 |
if (isset($this->ressources[$num]) && is_numeric($this->ressources[$num])) {
|
|
|
131 |
$presenceId = true;
|
|
|
132 |
}
|
|
|
133 |
return $presenceId;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
private function verifierRessourceValeur($num, $valeur) {
|
|
|
137 |
$ok = false;
|
|
|
138 |
if (isset($this->ressources[$num]) && $this->ressources[$num] == $valeur) {
|
|
|
139 |
$ok = true;
|
|
|
140 |
}
|
|
|
141 |
return $ok;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
private function verifierParametreValeur($cle, $valeur) {
|
|
|
145 |
$ok = false;
|
|
|
146 |
if (isset($this->parametres[$cle]) && $this->ressources[$cle] == $valeur) {
|
|
|
147 |
$ok = true;
|
|
|
148 |
}
|
|
|
149 |
return $ok;
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
private function lancerMessageErreurRessource() {
|
|
|
153 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
154 |
$message = "La ressource demandée '$ressource' ".
|
|
|
155 |
"n'est pas disponible pour le service ".$this->serviceNom." !\n".
|
|
|
156 |
"Les URLs disponibles sont : \n".
|
|
|
157 |
" - en GET : observations, observations/#idObs/#idProposition/vote \n".
|
|
|
158 |
" - en POST : observations/#id, observations/#idObs/#idProposition/vote \n".
|
|
|
159 |
" - en PUT : observations/#idObs/#idProposition/vote \n";
|
|
|
160 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
161 |
throw new Exception($message, $code);
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
private function initialiserService() {
|
|
|
165 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
|
|
166 |
$chemins = array();
|
|
|
167 |
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
|
|
|
168 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
169 |
$retour = '';
|
|
|
170 |
$service = null;
|
|
|
171 |
foreach ($chemins as $chemin) {
|
|
|
172 |
if (file_exists($chemin)) {
|
|
|
173 |
require_once $chemin;
|
|
|
174 |
$service = new $classe($this->conteneur);
|
|
|
175 |
if ($this->methode == 'consulter') {
|
|
|
176 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
|
|
177 |
} else {
|
|
|
178 |
$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
|
|
|
179 |
"ne possède pas de méthode '{$this->methode}' !";
|
|
|
180 |
$code = RestServeur::HTTP_NON_IMPLEMENTE;
|
|
|
181 |
throw new Exception($message, $code);
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
if (is_null($service)) {
|
|
|
187 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
188 |
$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
|
|
|
189 |
"n'existe pas dans le service '{$this->serviceNom}' !";
|
|
|
190 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
191 |
throw new Exception($message, $code);
|
|
|
192 |
}
|
|
|
193 |
return $retour;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
private function obtenirNomClasseService($mot) {
|
|
|
197 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
198 |
return $classeNom;
|
|
|
199 |
}
|
|
|
200 |
}
|