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
|
|
|
23 |
* http://localhost/del/services/0.1/images/#id => une image donnée => non utilisé sur DEL pour l'instant
|
|
|
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();
|
782 |
delphine |
66 |
try {
|
|
|
67 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
68 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
69 |
$resultat = $this->traiterRessources();
|
|
|
70 |
} catch (Exception $e) {
|
|
|
71 |
$reponseHttp->ajouterErreur($e);
|
825 |
aurelien |
72 |
$reponseHttp->emettreLesEntetes();
|
828 |
aurelien |
73 |
echo $reponseHttp->getCorps();
|
782 |
delphine |
74 |
}
|
|
|
75 |
}
|
824 |
aurelien |
76 |
|
|
|
77 |
public function modifier($ressources, $requeteDonnees) {
|
|
|
78 |
$this->methode = 'modifier';
|
|
|
79 |
$resultat = '';
|
|
|
80 |
$reponseHttp = new ReponseHttp();
|
|
|
81 |
try {
|
|
|
82 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
83 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
84 |
$resultat = $this->traiterRessources();
|
|
|
85 |
} catch (Exception $e) {
|
|
|
86 |
$reponseHttp->ajouterErreur($e);
|
825 |
aurelien |
87 |
$reponseHttp->emettreLesEntetes();
|
828 |
aurelien |
88 |
echo $reponseHttp->getCorps();
|
824 |
aurelien |
89 |
}
|
|
|
90 |
}
|
702 |
gduche |
91 |
|
|
|
92 |
private function initialiserRessourcesEtParametres($ressources, $parametres) {
|
|
|
93 |
$this->ressources = $ressources;
|
|
|
94 |
$this->parametres = $parametres;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
private function traiterRessources() {
|
|
|
98 |
$retour = '';
|
751 |
delphine |
99 |
$this->initialiserProjet();
|
|
|
100 |
if ($this->avoirRessourceService()) {
|
|
|
101 |
$retour = $this->initialiserService();
|
702 |
gduche |
102 |
}
|
|
|
103 |
return $retour;
|
|
|
104 |
}
|
751 |
delphine |
105 |
|
758 |
delphine |
106 |
|
751 |
delphine |
107 |
/*------------------------------------------------------------------------------------------------------------------
|
|
|
108 |
CONFIGURATION DU PROJET
|
|
|
109 |
------------------------------------------------------------------------------------------------------------------*/
|
702 |
gduche |
110 |
private function initialiserProjet() {
|
|
|
111 |
$this->chargerNomDuProjet();
|
|
|
112 |
$this->chargerConfigProjet();
|
|
|
113 |
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
private function chargerNomDuProjet() {
|
746 |
gduche |
117 |
$this->projetNom = 'images';
|
702 |
gduche |
118 |
}
|
|
|
119 |
|
|
|
120 |
private function chargerConfigProjet() {
|
|
|
121 |
$projet = $this->projetNom;
|
|
|
122 |
$chemin = Config::get('chemin_configurations')."config_$projet.ini";
|
|
|
123 |
Config::charger($chemin);
|
|
|
124 |
}
|
|
|
125 |
|
751 |
delphine |
126 |
/*------------------------------------------------------------------------------------------------------------------
|
|
|
127 |
CONFIGURATION DU SERVICE
|
|
|
128 |
------------------------------------------------------------------------------------------------------------------*/
|
702 |
gduche |
129 |
private function avoirRessourceService() {
|
751 |
delphine |
130 |
/*
|
|
|
131 |
* url possibles :
|
|
|
132 |
* http://localhost/del/services/0.1/images/ => toutes les images, appel à la classe ListeImages
|
|
|
133 |
* http://localhost/del/services/0.1/images/#id => une image donnée => non utilisé sur DEL pour l'instant
|
|
|
134 |
* http://localhost/del/services/0.1/images/#id/votes/ => tous les votes d'une image classés par protocole
|
|
|
135 |
* http://localhost/del/services/0.1/images/#id/votes/protocole/#id => tous les votes d'une image et d'un protocole donné
|
|
|
136 |
* http://localhost/del/services/0.1/images/#id/votes/#id => un vote donné pour une image donnée.
|
|
|
137 |
* */
|
702 |
gduche |
138 |
$presenceRessourceService = false;
|
754 |
delphine |
139 |
if (isset($this->ressources[0])) {
|
1142 |
aurelien |
140 |
if($this->ressources[0] == 'images-determinations-probables') {
|
|
|
141 |
$presenceRessourceService = true;
|
|
|
142 |
$this->serviceNom = 'liste-images-determinations-probables';
|
|
|
143 |
} else if ($this->etreRessourceIdentifiant(0)) {
|
754 |
delphine |
144 |
if (isset($this->ressources[1])) {
|
758 |
delphine |
145 |
$presenceRessourceService = $this->avoirRessourceSousService();
|
751 |
delphine |
146 |
} else {
|
758 |
delphine |
147 |
$this->editerMessageErreurRessource();
|
751 |
delphine |
148 |
}
|
758 |
delphine |
149 |
}
|
702 |
gduche |
150 |
} else {
|
751 |
delphine |
151 |
$presenceRessourceService = true;
|
|
|
152 |
$this->serviceNom = 'liste-images';
|
702 |
gduche |
153 |
}
|
|
|
154 |
return $presenceRessourceService;
|
|
|
155 |
}
|
754 |
delphine |
156 |
|
|
|
157 |
private function avoirRessourceSousService() {
|
|
|
158 |
$presenceRessourceService = false;
|
758 |
delphine |
159 |
if (sizeof($this->ressources) == 2) {
|
|
|
160 |
if ($this->ressources[1] == 'votes') {
|
|
|
161 |
$presenceRessourceService = true;
|
|
|
162 |
$this->serviceNom = 'votes-image';
|
|
|
163 |
} else {
|
|
|
164 |
$this->editerMessageErreurRessource();
|
|
|
165 |
}
|
754 |
delphine |
166 |
} else {
|
758 |
delphine |
167 |
if (isset($this->ressources[2]) && $this->etreRessourceIdentifiant(2)) {
|
|
|
168 |
$presenceRessourceService = true;
|
|
|
169 |
$this->serviceNom = 'vote-image';
|
|
|
170 |
} else {
|
|
|
171 |
$this->editerMessageErreurRessource();
|
|
|
172 |
}
|
|
|
173 |
}
|
|
|
174 |
return $presenceRessourceService;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
private function editerMessageErreurRessource() {
|
|
|
178 |
$message = "Le service demandé '".$this->projetNom.'/'.implode('/', $this->ressources).
|
|
|
179 |
"' n'est pas disponible pour le projet ".$this->projetNom." !\n".
|
|
|
180 |
"Les services disponibles sont : images, images/#id/votes, images/#id/vote/#id_vote";
|
|
|
181 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
182 |
throw new Exception($message, $code);
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
private function etreRessourceIdentifiant($num) {
|
|
|
186 |
$presenceId = false;
|
|
|
187 |
if (is_numeric($this->ressources[$num])) {
|
|
|
188 |
$presenceId = true;
|
|
|
189 |
} else {
|
|
|
190 |
$message = "Le service demandé '$service' nécessite d'avoir un identifiant d'image valide";
|
754 |
delphine |
191 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
192 |
throw new Exception($message, $code);
|
|
|
193 |
}
|
758 |
delphine |
194 |
return $presenceId;
|
754 |
delphine |
195 |
}
|
702 |
gduche |
196 |
|
|
|
197 |
private function initialiserService() {
|
751 |
delphine |
198 |
//$this->chargerNomDuService();
|
746 |
gduche |
199 |
|
702 |
gduche |
200 |
$classe = $this->obtenirNomClasseService($this->serviceNom);
|
|
|
201 |
$chemins = array();
|
718 |
gduche |
202 |
$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
|
|
|
203 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
702 |
gduche |
204 |
$retour = '';
|
|
|
205 |
$service = null;
|
|
|
206 |
foreach ($chemins as $chemin) {
|
|
|
207 |
if (file_exists($chemin)) {
|
718 |
gduche |
208 |
$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
|
751 |
delphine |
209 |
require_once $chemin;
|
724 |
gduche |
210 |
$service = new $classe($this->conteneur);
|
782 |
delphine |
211 |
if ($this->methode == 'consulter') {
|
|
|
212 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
|
|
213 |
} elseif ($this->methode == 'ajouter') {
|
|
|
214 |
$retour = $service->ajouter($this->ressources, $this->parametres);
|
824 |
aurelien |
215 |
} elseif ($this->methode == 'modifier') {
|
|
|
216 |
$retour = $service->modifier($this->ressources, $this->parametres);
|
782 |
delphine |
217 |
}
|
702 |
gduche |
218 |
}
|
|
|
219 |
}
|
746 |
gduche |
220 |
|
702 |
gduche |
221 |
if (is_null($service)) {
|
751 |
delphine |
222 |
$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
|
702 |
gduche |
223 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
224 |
throw new Exception($message, $code);
|
|
|
225 |
}
|
|
|
226 |
return $retour;
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
private function obtenirNomClasseService($mot) {
|
|
|
230 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
231 |
return $classeNom;
|
|
|
232 |
}
|
|
|
233 |
|
751 |
delphine |
234 |
|
702 |
gduche |
235 |
}
|
|
|
236 |
?>
|