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