702 |
gduche |
1 |
<?php
|
1795 |
jpm |
2 |
// declare(encoding='UTF-8');
|
702 |
gduche |
3 |
/**
|
1795 |
jpm |
4 |
* Classe principale de chargement des sous-services "images" de DEL.
|
1700 |
jpm |
5 |
*
|
|
|
6 |
* Cette classe se charge toujours de :
|
|
|
7 |
* - vérifier l'existance des ressources (services) demandés
|
|
|
8 |
* - vérifier la cohérence et le format des paramêtres passées dans l'url
|
|
|
9 |
* En fonction, de la compléxité du service, elle peut :
|
|
|
10 |
* - dans un premier temps, exécuter directement les actions : consulter, ajouter, modifier, supprimer.
|
|
|
11 |
* - dans un second temps, charger dynamiquement d'éventuelles sous-classes du service en fonction des ressources présentes dans l'URL.
|
|
|
12 |
*
|
1795 |
jpm |
13 |
* URLs possibles :
|
|
|
14 |
* GET :
|
|
|
15 |
* http://localhost/del/services/0.1/images/ => toutes les images : classe ListeImages
|
|
|
16 |
* http://localhost/del/services/0.1/images/#idImg/votes => tous les votes d'une image (#idImg) classés par protocole : classe VotesImage
|
1700 |
jpm |
17 |
*
|
1795 |
jpm |
18 |
* Non Implémenté : http://localhost/del/services/0.1/images/#id => une image donnée => en test pour remplacer les appels à eflore/cel
|
|
|
19 |
* Non Implémenté : http://localhost/del/services/0.1/images/#id/votes?protocole=#id => tous les votes d'une image et d'un protocole donné
|
|
|
20 |
*
|
|
|
21 |
* PUT :
|
|
|
22 |
* http://localhost/del/services/0.1/images/#idImg => ajouter un vote sur une image donnée (#idImg)
|
|
|
23 |
*
|
|
|
24 |
* POST :
|
|
|
25 |
* http://localhost/del/services/0.1/images/#idImg => modifier un vote sur une image donnée (#idImg)
|
|
|
26 |
*
|
|
|
27 |
* DELETE :
|
|
|
28 |
* http://localhost/del/services/0.1/images/#idImg => supprimer une image donnée (#idImg)
|
|
|
29 |
* http://localhost/del/services/0.1/images/#idImg/votes/#idVote => supprimer un vote (#idVote) d'une image donnée (#idImg)
|
|
|
30 |
*
|
|
|
31 |
* @category DEL
|
|
|
32 |
* @package Services
|
1700 |
jpm |
33 |
* @subpackage Images
|
1795 |
jpm |
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>
|
1700 |
jpm |
41 |
*/
|
1604 |
jpm |
42 |
restore_error_handler();
|
|
|
43 |
restore_exception_handler();
|
|
|
44 |
error_reporting(E_ALL);
|
1795 |
jpm |
45 |
|
746 |
gduche |
46 |
class Images extends RestService {
|
1795 |
jpm |
47 |
|
1827 |
jpm |
48 |
private $conteneur;
|
|
|
49 |
private $cheminCourant;
|
702 |
gduche |
50 |
private $parametres = array();
|
|
|
51 |
private $ressources = array();
|
1827 |
jpm |
52 |
private $methode;
|
1604 |
jpm |
53 |
private $serviceNom = 'images';
|
1827 |
jpm |
54 |
private $sousServiceNom;
|
718 |
gduche |
55 |
|
|
|
56 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
|
702 |
gduche |
57 |
protected $utilisationParametresBruts = true;
|
|
|
58 |
|
|
|
59 |
public function __construct() {
|
1827 |
jpm |
60 |
$this->conteneur = new Conteneur();
|
718 |
gduche |
61 |
$this->cheminCourant = dirname(__FILE__).DS;
|
702 |
gduche |
62 |
}
|
|
|
63 |
|
|
|
64 |
public function consulter($ressources, $parametres) {
|
782 |
delphine |
65 |
$this->methode = 'consulter';
|
1604 |
jpm |
66 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
67 |
return $this->executerService();
|
|
|
68 |
}
|
|
|
69 |
|
1622 |
jpm |
70 |
public function ajouter($ressources, $requeteDonnees) {
|
|
|
71 |
$this->methode = 'ajouter';
|
|
|
72 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
73 |
return $this->executerService();
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public function modifier($ressources, $requeteDonnees) {
|
1666 |
jpm |
77 |
$this->methode = 'modifier';
|
1622 |
jpm |
78 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
79 |
return $this->executerService();
|
|
|
80 |
}
|
|
|
81 |
|
1604 |
jpm |
82 |
public function supprimer($ressources) {
|
|
|
83 |
$this->methode = 'supprimer';
|
|
|
84 |
$this->initialiserRessourcesEtParametres($ressources);
|
|
|
85 |
return $this->executerService();
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
|
|
|
89 |
$this->ressources = $ressources;
|
|
|
90 |
$this->parametres = $parametres;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
private function executerService() {
|
1827 |
jpm |
94 |
$resultat = $this->traiterRessources();
|
|
|
95 |
if ($resultat === true || $resultat === false) {
|
|
|
96 |
return $resultat;
|
|
|
97 |
}
|
|
|
98 |
|
702 |
gduche |
99 |
$reponseHttp = new ReponseHttp();
|
1827 |
jpm |
100 |
$reponseHttp->setResultatService($resultat);
|
702 |
gduche |
101 |
$reponseHttp->emettreLesEntetes();
|
1827 |
jpm |
102 |
return $reponseHttp->getCorps();
|
702 |
gduche |
103 |
}
|
|
|
104 |
|
|
|
105 |
private function traiterRessources() {
|
1604 |
jpm |
106 |
$this->analyserRessources();
|
|
|
107 |
$retour = $this->initialiserService();
|
702 |
gduche |
108 |
return $retour;
|
|
|
109 |
}
|
758 |
delphine |
110 |
|
1604 |
jpm |
111 |
private function analyserRessources() {
|
1666 |
jpm |
112 |
if ($this->methode == 'consulter') {
|
|
|
113 |
$this->analyserRessoucesConsultation();
|
|
|
114 |
} else if ($this->methode == 'modifier' || $this->methode == 'ajouter') {
|
|
|
115 |
$this->analyserRessoucesModification();
|
1604 |
jpm |
116 |
} else if ($this->methode == 'supprimer') {
|
1666 |
jpm |
117 |
$this->analyserRessoucesSuppression();
|
1604 |
jpm |
118 |
}
|
1827 |
jpm |
119 |
if ($this->sousServiceNom == null) {
|
|
|
120 |
$this->lancerMessageErreurRessource();
|
|
|
121 |
}
|
702 |
gduche |
122 |
}
|
|
|
123 |
|
1666 |
jpm |
124 |
private function analyserRessoucesConsultation() {
|
1604 |
jpm |
125 |
if (count($this->ressources) == 0) {
|
|
|
126 |
$this->sousServiceNom = 'liste-images';
|
|
|
127 |
} else if (count($this->ressources) == 2) {
|
|
|
128 |
if ($this->etreRessourceIdentifiant(0) && $this->verifierRessourceValeur(1, 'votes')) {
|
|
|
129 |
$this->sousServiceNom = 'votes-image';
|
758 |
delphine |
130 |
}
|
702 |
gduche |
131 |
}
|
|
|
132 |
}
|
1604 |
jpm |
133 |
|
1666 |
jpm |
134 |
private function analyserRessoucesModification() {
|
|
|
135 |
if (count($this->ressources) == 2) {
|
|
|
136 |
if ($this->etreRessourceIdentifiant(0) && $this->verifierRessourceValeur(1, 'votes')) {
|
|
|
137 |
$this->sousServiceNom = 'votes-image';
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
private function analyserRessoucesSuppression() {
|
1604 |
jpm |
143 |
if (count($this->ressources) == 1 && $this->etreRessourceIdentifiant(0)) {
|
|
|
144 |
$this->sousServiceNom = 'liste-images';
|
1666 |
jpm |
145 |
} else if (count($this->ressources) == 3) {
|
|
|
146 |
if ($this->etreRessourceIdentifiant(0) && $this->verifierRessourceValeur(1, 'votes') && $this->etreRessourceIdentifiant(2) ) {
|
|
|
147 |
$this->sousServiceNom = 'votes-image';
|
|
|
148 |
}
|
758 |
delphine |
149 |
}
|
|
|
150 |
}
|
1604 |
jpm |
151 |
|
758 |
delphine |
152 |
private function etreRessourceIdentifiant($num) {
|
|
|
153 |
$presenceId = false;
|
1604 |
jpm |
154 |
if (isset($this->ressources[$num]) && is_numeric($this->ressources[$num])) {
|
758 |
delphine |
155 |
$presenceId = true;
|
754 |
delphine |
156 |
}
|
758 |
delphine |
157 |
return $presenceId;
|
754 |
delphine |
158 |
}
|
702 |
gduche |
159 |
|
1604 |
jpm |
160 |
private function verifierRessourceValeur($num, $valeur) {
|
|
|
161 |
$ok = false;
|
|
|
162 |
if (isset($this->ressources[$num]) && $this->ressources[$num] == $valeur) {
|
|
|
163 |
$ok = true;
|
|
|
164 |
}
|
|
|
165 |
return $ok;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
private function lancerMessageErreurRessource() {
|
|
|
169 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
170 |
$message = "La ressource demandée '$ressource' ".
|
|
|
171 |
"n'est pas disponible pour le service ".$this->serviceNom." !\n".
|
1827 |
jpm |
172 |
$this->getDoc();
|
1604 |
jpm |
173 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
174 |
throw new Exception($message, $code);
|
|
|
175 |
}
|
|
|
176 |
|
1827 |
jpm |
177 |
public function getDoc() {
|
|
|
178 |
return "Les URLs disponibles sont : \n".
|
|
|
179 |
" * en GET : \n".
|
|
|
180 |
" - images => toutes les images\n".
|
|
|
181 |
" - images/#idImg/votes => tous les votes d'une image (#idImg) classés par protocole\n".
|
|
|
182 |
" * en PUT : \n".
|
|
|
183 |
" - /images/#idImg/votes => ajouter un vote sur une image donnée (#idImg)\n".
|
|
|
184 |
" * en POST : \n".
|
|
|
185 |
" - /images/#idImg/votes => modifier un vote sur une image donnée (#idImg)\n".
|
|
|
186 |
" * en DELETE : \n".
|
|
|
187 |
" - /images/#idImg => supprimer une image donnée (#idImg)\n".
|
|
|
188 |
" - /images/#idImg/votes/#idVote => supprimer un vote (#idVote) d'une image donnée (#idImg)";
|
|
|
189 |
}
|
|
|
190 |
|
702 |
gduche |
191 |
private function initialiserService() {
|
1604 |
jpm |
192 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
702 |
gduche |
193 |
$chemins = array();
|
1604 |
jpm |
194 |
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
|
718 |
gduche |
195 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
702 |
gduche |
196 |
$retour = '';
|
|
|
197 |
$service = null;
|
|
|
198 |
foreach ($chemins as $chemin) {
|
|
|
199 |
if (file_exists($chemin)) {
|
751 |
delphine |
200 |
require_once $chemin;
|
724 |
gduche |
201 |
$service = new $classe($this->conteneur);
|
782 |
delphine |
202 |
if ($this->methode == 'consulter') {
|
|
|
203 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
1666 |
jpm |
204 |
} elseif ($this->methode == 'ajouter') {
|
|
|
205 |
$retour = $service->ajouter($this->ressources, $this->parametres);
|
|
|
206 |
} elseif ($this->methode == 'modifier') {
|
|
|
207 |
$retour = $service->modifier($this->ressources, $this->parametres);
|
1472 |
aurelien |
208 |
} elseif ($this->methode == 'supprimer') {
|
|
|
209 |
$retour = $service->supprimer($this->ressources);
|
782 |
delphine |
210 |
}
|
702 |
gduche |
211 |
}
|
|
|
212 |
}
|
1604 |
jpm |
213 |
|
702 |
gduche |
214 |
if (is_null($service)) {
|
1827 |
jpm |
215 |
$ressource = implode('/', $this->ressources);
|
|
|
216 |
$msg = "Le classe '$classe' correspondant à la ressource '$ressource' ".
|
|
|
217 |
"n'existe pas dans le service '{$this->serviceNom}' !\n".$this->getDoc();
|
|
|
218 |
throw new Exception($msg, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
|
702 |
gduche |
219 |
}
|
|
|
220 |
return $retour;
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
private function obtenirNomClasseService($mot) {
|
|
|
224 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
225 |
return $classeNom;
|
|
|
226 |
}
|
1793 |
jpm |
227 |
}
|