| Line 1... |
Line 1... |
| 1 |
<?php
|
1 |
<?php
|
| 2 |
// declare(encoding='UTF-8');
|
2 |
// declare(encoding='UTF-8');
|
| 3 |
/**
|
3 |
/**
|
| 4 |
* Classe principale de chargement des services Observations.
|
4 |
* Classe principale de chargement des sous-services "images" de DEL.
|
| - |
|
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.
|
| 5 |
*
|
12 |
*
|
| 6 |
* URLs possibles :
|
13 |
* URLs possibles :
|
| 7 |
* GET :
|
14 |
* 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
|
15 |
* http://localhost/del/services/0.1/images/ => toutes les images : classe ListeImages
|
| 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 |
* http://localhost/del/services/0.1/images/#idImg/votes => tous les votes d'une image (#idImg) classés par protocole : classe VotesImage
|
| 16 |
*
|
17 |
*
|
| 17 |
* http://localhost/service:del:0.1/observations/#idObs/#idVote/vote
|
18 |
* Non Implémenté : http://localhost/del/services/0.1/images/#id => une image donnée => en test pour remplacer les appels à eflore/cel
|
| 18 |
* toutes les infos sur les votes d'une proposition
|
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é
|
| 19 |
*
|
20 |
*
|
| 20 |
* PUT :
|
21 |
* PUT :
|
| 21 |
* http://localhost/service:del:0.1/observations/#idObs/#idCommentaire/vote
|
22 |
* http://localhost/del/services/0.1/images/#idImg => ajouter un vote sur une image donnée (#idImg)
|
| 22 |
* ajoute un vote (+ ou -) pour une obs et une proposition donnée
|
- |
|
| 23 |
*
|
23 |
*
|
| 24 |
* POST :
|
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)
|
25 |
* http://localhost/del/services/0.1/images/#idImg => modifier un vote sur une image donnée (#idImg)
|
| 27 |
*
|
26 |
*
|
| - |
|
27 |
* DELETE :
|
| 28 |
* http://localhost/service:del:0.1/observations/#idObs/#idCommentaire/vote
|
28 |
* http://localhost/del/services/0.1/images/#idImg => supprimer une image donnée (#idImg)
|
| 29 |
* modifie un vote (+ ou -) pour une obs et une proposition donnée
|
29 |
* http://localhost/del/services/0.1/images/#idImg/votes/#idVote => supprimer un vote (#idVote) d'une image donnée (#idImg)
|
| 30 |
*
|
30 |
*
|
| 31 |
* @category DEL
|
31 |
* @category DEL
|
| 32 |
* @package Services
|
32 |
* @package Services
|
| 33 |
* @subpackage Observations
|
33 |
* @subpackage Images
|
| 34 |
* @version 0.1
|
34 |
* @version 0.1
|
| 35 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
35 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
| 36 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
36 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
| 37 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
37 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
| 38 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
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>
|
39 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
| 40 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
40 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
| 41 |
*/
|
41 |
*/
|
| - |
|
42 |
restore_error_handler();
|
| - |
|
43 |
restore_exception_handler();
|
| - |
|
44 |
error_reporting(E_ALL);
|
| Line 42... |
Line 45... |
| 42 |
|
45 |
|
| Line -... |
Line 46... |
| - |
|
46 |
class Tepik extends RestService {
|
| - |
|
47 |
|
| 43 |
class Tepik extends RestService {
|
48 |
private $conteneur;
|
| 44 |
|
49 |
private $cheminCourant;
|
| 45 |
private $parametres = array();
|
50 |
private $parametres = array();
|
| 46 |
private $ressources = array();
|
51 |
private $ressources = array();
|
| 47 |
private $methode = null;
|
52 |
private $methode;
|
| 48 |
private $serviceNom = 'tepik';
|
- |
|
| 49 |
private $sousServiceNom = null;
|
- |
|
| 50 |
private $cheminCourant = null;
|
- |
|
| Line 51... |
Line 53... |
| 51 |
|
53 |
private $serviceNom = 'tepik';
|
| 52 |
private $conteneur;
|
54 |
private $sousServiceNom;
|
| Line 53... |
Line 55... |
| 53 |
|
55 |
|
| - |
|
56 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
|
| 54 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
|
57 |
protected $utilisationParametresBruts = true;
|
| 55 |
protected $utilisationParametresBruts = true;
|
58 |
|
| Line 56... |
Line 59... |
| 56 |
|
59 |
public function __construct() {
|
| 57 |
public function __construct() {
|
60 |
$this->conteneur = new Conteneur();
|
| 58 |
$this->cheminCourant = dirname(__FILE__).DS;
|
61 |
$this->cheminCourant = dirname(__FILE__).DS;
|
| 59 |
}
|
62 |
}
|
| 60 |
|
63 |
|
| Line -... |
Line 64... |
| - |
|
64 |
public function consulter($ressources, $parametres) {
|
| - |
|
65 |
$this->methode = 'consulter';
|
| - |
|
66 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
| - |
|
67 |
return $this->executerService();
|
| Line 61... |
Line 68... |
| 61 |
public function consulter($ressources, $parametres) {
|
68 |
}
|
| 62 |
$this->methode = 'consulter';
|
- |
|
| 63 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
- |
|
| 64 |
return $this->executerService();
|
- |
|
| 65 |
}
|
69 |
|
| 66 |
|
70 |
|
| 67 |
|
- |
|
| 68 |
|
71 |
private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
|
| 69 |
private function executerService() {
|
72 |
$this->ressources = $ressources;
|
| 70 |
$reponseHttp = new ReponseHttp();
|
- |
|
| 71 |
try {
|
- |
|
| 72 |
$this->conteneur = new Conteneur($this->parametres);
|
- |
|
| 73 |
$resultat = $this->traiterRessources();
|
- |
|
| Line -... |
Line 73... |
| - |
|
73 |
$this->parametres = $parametres;
|
| 74 |
$reponseHttp->setResultatService($resultat);
|
74 |
}
|
| 75 |
} catch (Exception $e) {
|
75 |
|
| 76 |
$reponseHttp->ajouterErreur($e);
|
76 |
private function executerService() {
|
| 77 |
}
|
77 |
$resultat = $this->traiterRessources();
|
| Line 78... |
Line 78... |
| 78 |
$reponseHttp->emettreLesEntetes();
|
78 |
if ($resultat === true || $resultat === false) {
|
| 79 |
$corps = $reponseHttp->getCorps();
|
79 |
return $resultat;
|
| 80 |
return $corps;
|
80 |
}
|
| Line 92... |
Line 92... |
| 92 |
}
|
92 |
}
|
| Line 93... |
Line 93... |
| 93 |
|
93 |
|
| 94 |
private function analyserRessources() {
|
94 |
private function analyserRessources() {
|
| 95 |
if ($this->methode == 'consulter') {
|
95 |
if ($this->methode == 'consulter') {
|
| 96 |
$this->analyserRessoucesConsultation();
|
- |
|
| 97 |
}
|
- |
|
| 98 |
}
|
- |
|
| 99 |
|
96 |
$this->analyserRessoucesConsultation();
|
| 100 |
private function analyserRessoucesConsultation() {
|
- |
|
| 101 |
if (count($this->ressources) == 0) {
|
- |
|
| 102 |
// http://localhost/service:del:0.1/observations
|
97 |
} else if ($this->methode == 'modifier' || $this->methode == 'ajouter') {
|
| 103 |
$this->sousServiceNom = 'liste-observations';
|
98 |
$this->analyserRessoucesModification();
|
| 104 |
} else if (count($this->ressources) == 1) {
|
99 |
} else if ($this->methode == 'supprimer') {
|
| 105 |
if ($this->etreRessourceIdentifiant(0)) {
|
- |
|
| 106 |
// http://localhost/service:del:0.1/observations/#idObs
|
- |
|
| 107 |
$this->sousServiceNom = 'observation-details';
|
- |
|
| 108 |
}
|
100 |
$this->analyserRessoucesSuppression();
|
| 109 |
}
|
101 |
}
|
| 110 |
if ($this->sousServiceNom == null) {
|
102 |
if ($this->sousServiceNom == null) {
|
| 111 |
$this->lancerMessageErreurRessource();
|
103 |
$this->lancerMessageErreurRessource();
|
| 112 |
}
|
104 |
}
|
| Line 113... |
Line 105... |
| 113 |
}
|
105 |
}
|
| 114 |
|
106 |
|
| - |
|
107 |
private function analyserRessoucesConsultation() {
|
| 115 |
private function analyserRessoucesModification() {
|
108 |
if (count($this->ressources) == 0) {
|
| 116 |
if (count($this->ressources) == 1) {
|
109 |
$this->sousServiceNom = 'liste-images';
|
| 117 |
if ($this->methode == 'modifier' && $this->etreRessourceIdentifiant(0)) {
|
110 |
} else if (count($this->ressources) == 2) {
|
| 118 |
// http://localhost/service:del:0.1/observations/#idObs
|
111 |
if ($this->etreRessourceIdentifiant(0) && $this->verifierRessourceValeur(1, 'votes')) {
|
| 119 |
$this->sousServiceNom = 'observation-details';
|
112 |
$this->sousServiceNom = 'votes-image';
|
| 120 |
}
|
- |
|
| 121 |
}
|
- |
|
| 122 |
|
- |
|
| 123 |
if ($this->sousServiceNom == null) {
|
- |
|
| 124 |
$this->lancerMessageErreurRessource();
|
113 |
}
|
| Line -... |
Line 114... |
| - |
|
114 |
}
|
| 125 |
}
|
115 |
}
|
| 126 |
}
|
116 |
|
| 127 |
|
117 |
|
| 128 |
private function etreRessourceIdentifiant($num) {
|
118 |
private function etreRessourceIdentifiant($num) {
|
| 129 |
$presenceId = false;
|
119 |
$presenceId = false;
|
| Line 139... |
Line 129... |
| 139 |
$ok = true;
|
129 |
$ok = true;
|
| 140 |
}
|
130 |
}
|
| 141 |
return $ok;
|
131 |
return $ok;
|
| 142 |
}
|
132 |
}
|
| Line 143... |
Line -... |
| 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 |
|
133 |
|
| 152 |
private function lancerMessageErreurRessource() {
|
134 |
private function lancerMessageErreurRessource() {
|
| 153 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
135 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
| 154 |
$message = "La ressource demandée '$ressource' ".
|
136 |
$message = "La ressource demandée '$ressource' ".
|
| 155 |
"n'est pas disponible pour le service ".$this->serviceNom." !\n".
|
137 |
"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";
|
138 |
$this->getDoc();
|
| 160 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
139 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
| 161 |
throw new Exception($message, $code);
|
140 |
throw new Exception($message, $code);
|
| Line -... |
Line 141... |
| - |
|
141 |
}
|
| - |
|
142 |
|
| - |
|
143 |
public function getDoc() {
|
| - |
|
144 |
return "Les URLs disponibles sont : \n".
|
| - |
|
145 |
" * en GET : \n".
|
| - |
|
146 |
" - images => toutes les images\n".
|
| - |
|
147 |
" - images/#idImg/votes => tous les votes d'une image (#idImg) classés par protocole\n";
|
| 162 |
}
|
148 |
}
|
| 163 |
|
149 |
|
| 164 |
private function initialiserService() {
|
150 |
private function initialiserService() {
|
| 165 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
151 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
| 166 |
$chemins = array();
|
152 |
$chemins = array();
|
| Line 172... |
Line 158... |
| 172 |
if (file_exists($chemin)) {
|
158 |
if (file_exists($chemin)) {
|
| 173 |
require_once $chemin;
|
159 |
require_once $chemin;
|
| 174 |
$service = new $classe($this->conteneur);
|
160 |
$service = new $classe($this->conteneur);
|
| 175 |
if ($this->methode == 'consulter') {
|
161 |
if ($this->methode == 'consulter') {
|
| 176 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
162 |
$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 |
}
|
163 |
}
|
| 183 |
}
|
164 |
}
|
| 184 |
}
|
165 |
}
|
| Line 185... |
Line 166... |
| 185 |
|
166 |
|
| 186 |
if (is_null($service)) {
|
167 |
if (is_null($service)) {
|
| 187 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
168 |
$ressource = implode('/', $this->ressources);
|
| 188 |
$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
|
169 |
$msg = "Le classe '$classe' correspondant à la ressource '$ressource' ".
|
| 189 |
"n'existe pas dans le service '{$this->serviceNom}' !";
|
170 |
"n'existe pas dans le service '{$this->serviceNom}' !\n".$this->getDoc();
|
| 190 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
- |
|
| 191 |
throw new Exception($message, $code);
|
171 |
throw new Exception($msg, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
|
| 192 |
}
|
172 |
}
|
| 193 |
return $retour;
|
173 |
return $retour;
|
| Line 194... |
Line 174... |
| 194 |
}
|
174 |
}
|