702 |
gduche |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Description :
|
|
|
4 |
* Classe principale de chargement des services d'eFlore.
|
|
|
5 |
*
|
|
|
6 |
* Encodage en entrée : utf8
|
|
|
7 |
* Encodage en sortie : utf8
|
|
|
8 |
* @package eflore-projets
|
|
|
9 |
* @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
|
|
|
10 |
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
|
|
|
11 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
12 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
13 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
14 |
* @version 0.1
|
|
|
15 |
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
|
|
|
16 |
*/
|
746 |
gduche |
17 |
class Images extends RestService {
|
702 |
gduche |
18 |
|
746 |
gduche |
19 |
|
|
|
20 |
/*
|
|
|
21 |
* url possibles :
|
|
|
22 |
* http://localhost/del/services/0.1/images/ => toutes les images, appel à la classe ListeImages
|
1443 |
mathias |
23 |
* http://localhost/del/services/0.1/images/#id => une image donnée => en test pour remplacer les appels à eflore/cel
|
746 |
gduche |
24 |
* http://localhost/del/services/0.1/images/#id/votes/ => tous les votes d'une image classés par protocole
|
|
|
25 |
* http://localhost/del/services/0.1/images/#id/votes/protocole/#id => tous les votes d'une image et d'un protocole donné
|
|
|
26 |
* http://localhost/del/services/0.1/images/#id/votes/#id => un vote donné pour une image donnée.
|
|
|
27 |
* */
|
|
|
28 |
|
702 |
gduche |
29 |
private $parametres = array();
|
|
|
30 |
private $ressources = array();
|
782 |
delphine |
31 |
private $methode = null;
|
702 |
gduche |
32 |
private $projetNom = array();
|
|
|
33 |
private $serviceNom = array();
|
718 |
gduche |
34 |
private $cheminCourant = null;
|
|
|
35 |
|
|
|
36 |
private $conteneur;
|
|
|
37 |
|
|
|
38 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
|
702 |
gduche |
39 |
protected $utilisationParametresBruts = true;
|
|
|
40 |
|
|
|
41 |
public function __construct() {
|
718 |
gduche |
42 |
$this->cheminCourant = dirname(__FILE__).DS;
|
702 |
gduche |
43 |
}
|
|
|
44 |
|
|
|
45 |
public function consulter($ressources, $parametres) {
|
782 |
delphine |
46 |
$this->methode = 'consulter';
|
702 |
gduche |
47 |
$resultat = '';
|
|
|
48 |
$reponseHttp = new ReponseHttp();
|
|
|
49 |
try {
|
|
|
50 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
718 |
gduche |
51 |
$this->conteneur = new Conteneur($this->parametres);
|
702 |
gduche |
52 |
$resultat = $this->traiterRessources();
|
|
|
53 |
$reponseHttp->setResultatService($resultat);
|
|
|
54 |
} catch (Exception $e) {
|
|
|
55 |
$reponseHttp->ajouterErreur($e);
|
|
|
56 |
}
|
|
|
57 |
$reponseHttp->emettreLesEntetes();
|
|
|
58 |
$corps = $reponseHttp->getCorps();
|
|
|
59 |
return $corps;
|
|
|
60 |
}
|
782 |
delphine |
61 |
|
|
|
62 |
public function ajouter($ressources, $requeteDonnees) {
|
|
|
63 |
$this->methode = 'ajouter';
|
|
|
64 |
$resultat = '';
|
824 |
aurelien |
65 |
$reponseHttp = new ReponseHttp();
|
1472 |
aurelien |
66 |
|
782 |
delphine |
67 |
try {
|
|
|
68 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
69 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
70 |
$resultat = $this->traiterRessources();
|
1472 |
aurelien |
71 |
$reponseHttp->setResultatService($resultat);
|
782 |
delphine |
72 |
} catch (Exception $e) {
|
|
|
73 |
$reponseHttp->ajouterErreur($e);
|
|
|
74 |
}
|
1472 |
aurelien |
75 |
$corps = $reponseHttp->getCorps();
|
|
|
76 |
return $corps;
|
782 |
delphine |
77 |
}
|
824 |
aurelien |
78 |
|
|
|
79 |
public function modifier($ressources, $requeteDonnees) {
|
|
|
80 |
$this->methode = 'modifier';
|
|
|
81 |
$resultat = '';
|
|
|
82 |
$reponseHttp = new ReponseHttp();
|
|
|
83 |
try {
|
|
|
84 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
85 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
86 |
$resultat = $this->traiterRessources();
|
|
|
87 |
} catch (Exception $e) {
|
|
|
88 |
$reponseHttp->ajouterErreur($e);
|
825 |
aurelien |
89 |
$reponseHttp->emettreLesEntetes();
|
828 |
aurelien |
90 |
echo $reponseHttp->getCorps();
|
824 |
aurelien |
91 |
}
|
|
|
92 |
}
|
1472 |
aurelien |
93 |
|
1478 |
aurelien |
94 |
public function supprimer($ressources) {
|
|
|
95 |
$requeteDonnees = array();
|
1472 |
aurelien |
96 |
$this->methode = 'supprimer';
|
|
|
97 |
$resultat = '';
|
|
|
98 |
$reponseHttp = new ReponseHttp();
|
|
|
99 |
try {
|
|
|
100 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
101 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
102 |
$resultat = $this->traiterRessources();
|
|
|
103 |
} catch (Exception $e) {
|
|
|
104 |
$reponseHttp->ajouterErreur($e);
|
|
|
105 |
$reponseHttp->emettreLesEntetes();
|
|
|
106 |
echo $reponseHttp->getCorps();
|
|
|
107 |
}
|
|
|
108 |
}
|
702 |
gduche |
109 |
|
|
|
110 |
private function initialiserRessourcesEtParametres($ressources, $parametres) {
|
|
|
111 |
$this->ressources = $ressources;
|
|
|
112 |
$this->parametres = $parametres;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
private function traiterRessources() {
|
|
|
116 |
$retour = '';
|
751 |
delphine |
117 |
$this->initialiserProjet();
|
|
|
118 |
if ($this->avoirRessourceService()) {
|
|
|
119 |
$retour = $this->initialiserService();
|
702 |
gduche |
120 |
}
|
|
|
121 |
return $retour;
|
|
|
122 |
}
|
751 |
delphine |
123 |
|
758 |
delphine |
124 |
|
751 |
delphine |
125 |
/*------------------------------------------------------------------------------------------------------------------
|
|
|
126 |
CONFIGURATION DU PROJET
|
|
|
127 |
------------------------------------------------------------------------------------------------------------------*/
|
702 |
gduche |
128 |
private function initialiserProjet() {
|
|
|
129 |
$this->chargerNomDuProjet();
|
|
|
130 |
$this->chargerConfigProjet();
|
|
|
131 |
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
private function chargerNomDuProjet() {
|
746 |
gduche |
135 |
$this->projetNom = 'images';
|
702 |
gduche |
136 |
}
|
|
|
137 |
|
|
|
138 |
private function chargerConfigProjet() {
|
|
|
139 |
$projet = $this->projetNom;
|
|
|
140 |
$chemin = Config::get('chemin_configurations')."config_$projet.ini";
|
|
|
141 |
Config::charger($chemin);
|
|
|
142 |
}
|
|
|
143 |
|
751 |
delphine |
144 |
/*------------------------------------------------------------------------------------------------------------------
|
|
|
145 |
CONFIGURATION DU SERVICE
|
|
|
146 |
------------------------------------------------------------------------------------------------------------------*/
|
702 |
gduche |
147 |
private function avoirRessourceService() {
|
751 |
delphine |
148 |
/*
|
|
|
149 |
* url possibles :
|
|
|
150 |
* http://localhost/del/services/0.1/images/ => toutes les images, appel à la classe ListeImages
|
1443 |
mathias |
151 |
* http://localhost/del/services/0.1/images/#id => une image donnée => en test pour remplacer les appels à eflore/cel
|
751 |
delphine |
152 |
* http://localhost/del/services/0.1/images/#id/votes/ => tous les votes d'une image classés par protocole
|
|
|
153 |
* http://localhost/del/services/0.1/images/#id/votes/protocole/#id => tous les votes d'une image et d'un protocole donné
|
|
|
154 |
* http://localhost/del/services/0.1/images/#id/votes/#id => un vote donné pour une image donnée.
|
|
|
155 |
* */
|
702 |
gduche |
156 |
$presenceRessourceService = false;
|
754 |
delphine |
157 |
if (isset($this->ressources[0])) {
|
1153 |
aurelien |
158 |
if ($this->etreRessourceIdentifiant(0)) {
|
754 |
delphine |
159 |
if (isset($this->ressources[1])) {
|
758 |
delphine |
160 |
$presenceRessourceService = $this->avoirRessourceSousService();
|
751 |
delphine |
161 |
} else {
|
1443 |
mathias |
162 |
$presenceRessourceService = true;
|
|
|
163 |
$this->serviceNom = 'liste-images';
|
751 |
delphine |
164 |
}
|
758 |
delphine |
165 |
}
|
702 |
gduche |
166 |
} else {
|
751 |
delphine |
167 |
$presenceRessourceService = true;
|
|
|
168 |
$this->serviceNom = 'liste-images';
|
702 |
gduche |
169 |
}
|
|
|
170 |
return $presenceRessourceService;
|
|
|
171 |
}
|
754 |
delphine |
172 |
|
|
|
173 |
private function avoirRessourceSousService() {
|
1472 |
aurelien |
174 |
// TODO: utilité de votes-image et vote-image ?
|
|
|
175 |
// TODO: cette fonction est ignoble, la simplifier
|
754 |
delphine |
176 |
$presenceRessourceService = false;
|
1472 |
aurelien |
177 |
if (sizeof($this->ressources) >= 2) {
|
758 |
delphine |
178 |
if ($this->ressources[1] == 'votes') {
|
|
|
179 |
$presenceRessourceService = true;
|
|
|
180 |
$this->serviceNom = 'votes-image';
|
|
|
181 |
} else {
|
|
|
182 |
$this->editerMessageErreurRessource();
|
|
|
183 |
}
|
754 |
delphine |
184 |
} else {
|
758 |
delphine |
185 |
if (isset($this->ressources[2]) && $this->etreRessourceIdentifiant(2)) {
|
|
|
186 |
$presenceRessourceService = true;
|
|
|
187 |
$this->serviceNom = 'vote-image';
|
|
|
188 |
} else {
|
|
|
189 |
$this->editerMessageErreurRessource();
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
return $presenceRessourceService;
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
private function editerMessageErreurRessource() {
|
|
|
196 |
$message = "Le service demandé '".$this->projetNom.'/'.implode('/', $this->ressources).
|
|
|
197 |
"' n'est pas disponible pour le projet ".$this->projetNom." !\n".
|
|
|
198 |
"Les services disponibles sont : images, images/#id/votes, images/#id/vote/#id_vote";
|
|
|
199 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
200 |
throw new Exception($message, $code);
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
private function etreRessourceIdentifiant($num) {
|
|
|
204 |
$presenceId = false;
|
|
|
205 |
if (is_numeric($this->ressources[$num])) {
|
|
|
206 |
$presenceId = true;
|
|
|
207 |
} else {
|
|
|
208 |
$message = "Le service demandé '$service' nécessite d'avoir un identifiant d'image valide";
|
754 |
delphine |
209 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
210 |
throw new Exception($message, $code);
|
|
|
211 |
}
|
758 |
delphine |
212 |
return $presenceId;
|
754 |
delphine |
213 |
}
|
702 |
gduche |
214 |
|
|
|
215 |
private function initialiserService() {
|
751 |
delphine |
216 |
//$this->chargerNomDuService();
|
746 |
gduche |
217 |
|
702 |
gduche |
218 |
$classe = $this->obtenirNomClasseService($this->serviceNom);
|
|
|
219 |
$chemins = array();
|
718 |
gduche |
220 |
$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
|
|
|
221 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
702 |
gduche |
222 |
$retour = '';
|
|
|
223 |
$service = null;
|
|
|
224 |
foreach ($chemins as $chemin) {
|
|
|
225 |
if (file_exists($chemin)) {
|
718 |
gduche |
226 |
$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
|
751 |
delphine |
227 |
require_once $chemin;
|
724 |
gduche |
228 |
$service = new $classe($this->conteneur);
|
782 |
delphine |
229 |
if ($this->methode == 'consulter') {
|
|
|
230 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
|
|
231 |
} elseif ($this->methode == 'ajouter') {
|
|
|
232 |
$retour = $service->ajouter($this->ressources, $this->parametres);
|
824 |
aurelien |
233 |
} elseif ($this->methode == 'modifier') {
|
|
|
234 |
$retour = $service->modifier($this->ressources, $this->parametres);
|
1472 |
aurelien |
235 |
} elseif ($this->methode == 'supprimer') {
|
|
|
236 |
$retour = $service->supprimer($this->ressources);
|
782 |
delphine |
237 |
}
|
702 |
gduche |
238 |
}
|
|
|
239 |
}
|
746 |
gduche |
240 |
|
702 |
gduche |
241 |
if (is_null($service)) {
|
751 |
delphine |
242 |
$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
|
702 |
gduche |
243 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
244 |
throw new Exception($message, $code);
|
|
|
245 |
}
|
|
|
246 |
return $retour;
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
private function obtenirNomClasseService($mot) {
|
|
|
250 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
251 |
return $classeNom;
|
|
|
252 |
}
|
|
|
253 |
}
|
|
|
254 |
?>
|