2212 |
arthur |
1 |
<?php
|
|
|
2 |
// declare(encoding='UTF-8');
|
|
|
3 |
/**
|
|
|
4 |
* Classe principale de chargement des sous-services concernant les commentaires.
|
|
|
5 |
*
|
|
|
6 |
* URLs possibles :
|
|
|
7 |
*
|
|
|
8 |
* GET :
|
|
|
9 |
* http://localhost/del/services/0.1/commentaires => liste tous les commentaires
|
|
|
10 |
* http://localhost/del/services/0.1/commentaires/#id => retourne le contenu d'un commentaire d'id #id
|
|
|
11 |
*
|
|
|
12 |
* PUT :
|
|
|
13 |
* http://localhost/del/services/0.1/commentaires => Ajoute un nouveau commentaire
|
|
|
14 |
*
|
|
|
15 |
* DELETE :
|
|
|
16 |
* http://localhost/del/services/0.1/commentaires/#id => supprime le commentaire d'id #id
|
|
|
17 |
*
|
|
|
18 |
* @category DEL
|
|
|
19 |
* @package Services
|
|
|
20 |
* @subpackage Commentaires
|
|
|
21 |
* @version 0.1
|
|
|
22 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
|
|
23 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
24 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
|
|
25 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
26 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
27 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
|
|
28 |
*/
|
|
|
29 |
class Commentaires extends RestService {
|
|
|
30 |
|
|
|
31 |
private $parametres = array();
|
|
|
32 |
private $ressources = array();
|
|
|
33 |
private $methode = null;
|
|
|
34 |
private $serviceNom = 'commentaires';
|
|
|
35 |
private $sousServiceNom = null;
|
|
|
36 |
private $cheminCourant = null;
|
|
|
37 |
private $erreur = null;
|
|
|
38 |
|
|
|
39 |
private $conteneur;
|
|
|
40 |
|
|
|
41 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
|
|
|
42 |
protected $utilisationParametresBruts = true;
|
|
|
43 |
|
|
|
44 |
public function __construct() {
|
|
|
45 |
$this->cheminCourant = dirname(__FILE__).DS;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
public function consulter($ressources, $parametres) {
|
|
|
49 |
$this->methode = 'consulter';
|
|
|
50 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
51 |
return $this->executerService();
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
public function ajouter($ressources, $requeteDonnees) {
|
|
|
55 |
$this->methode = 'ajouter';
|
|
|
56 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
57 |
return $this->executerService();
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
public function supprimer($ressources) {
|
|
|
61 |
$this->methode = 'supprimer';
|
|
|
62 |
$this->initialiserRessourcesEtParametres($ressources);
|
|
|
63 |
return $this->executerService();
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
|
|
|
67 |
$this->ressources = $ressources;
|
|
|
68 |
$this->parametres = $parametres;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
private function executerService() {
|
|
|
72 |
$reponseHttp = new ReponseHttp();
|
|
|
73 |
try {
|
|
|
74 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
75 |
$resultat = $this->traiterRessources();
|
|
|
76 |
$reponseHttp->setResultatService($resultat);
|
|
|
77 |
} catch (Exception $e) {
|
|
|
78 |
$reponseHttp->ajouterErreur($e);
|
|
|
79 |
}
|
|
|
80 |
$reponseHttp->emettreLesEntetes();
|
|
|
81 |
$corps = $reponseHttp->getCorps();
|
|
|
82 |
return $corps;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
private function traiterRessources() {
|
|
|
86 |
$this->analyserRessources();
|
|
|
87 |
$retour = $this->initialiserService();
|
|
|
88 |
return $retour;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
private function analyserRessources() {
|
|
|
92 |
if ($this->methode == 'consulter') {
|
|
|
93 |
if (!isset($this->ressources) || empty($this->ressources)) {
|
|
|
94 |
$this->sousServiceNom = 'liste-commentaires';
|
|
|
95 |
} else if (isset($this->ressources[0]) && count($this->ressources) == 1 && is_numeric($this->ressources[0])) {
|
|
|
96 |
$this->sousServiceNom = 'commentaire-details';
|
|
|
97 |
}
|
|
|
98 |
} else if ($this->methode == 'ajouter') {
|
|
|
99 |
$this->sousServiceNom = 'ajouter-commentaire';
|
|
|
100 |
} else if ($this->methode == 'supprimer') {
|
|
|
101 |
if (isset($this->ressources[0]) && count($this->ressources) == 1 && is_numeric($this->ressources[0])) {
|
|
|
102 |
$this->sousServiceNom = 'supprimer-commentaire';
|
|
|
103 |
} else {
|
|
|
104 |
$this->erreur = "L'identifiant du commentaire est obligatoire et doit être un entier.";
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
if ($this->sousServiceNom == null) {
|
|
|
109 |
$this->lancerMessageErreurRessource();
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
private function lancerMessageErreurRessource() {
|
|
|
114 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
115 |
if ($this->erreur != null) {
|
|
|
116 |
$message = $this->erreur;
|
|
|
117 |
} else {
|
|
|
118 |
$message = "La ressource demandée '$ressource' ".
|
|
|
119 |
"n'est pas disponible pour le service ".$this->serviceNom." !\n".
|
|
|
120 |
"Les URLs disponibles sont : \n".
|
|
|
121 |
" - en GET : commentaires, commentaires/#id \n".
|
|
|
122 |
" - en PUT : commentaires".
|
|
|
123 |
" - en DELETE : commentaires/#id\n".
|
|
|
124 |
"#id représente un nombre entier identifiant un commentaire.";
|
|
|
125 |
}
|
|
|
126 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
127 |
throw new Exception($message, $code);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
private function initialiserService() {
|
|
|
131 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
|
|
132 |
//echo $this->sousServiceNom.':'.$classe."\n";
|
|
|
133 |
//echo 'Ressources :'.print_r($this->ressources, true);
|
|
|
134 |
//echo 'Parametres :'.print_r($this->parametres, true);
|
|
|
135 |
$chemins = array();
|
|
|
136 |
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
|
|
|
137 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
138 |
$retour = '';
|
|
|
139 |
$service = null;
|
|
|
140 |
foreach ($chemins as $chemin) {
|
|
|
141 |
if (file_exists($chemin)) {
|
|
|
142 |
require_once $chemin;
|
|
|
143 |
$service = new $classe($this->conteneur);
|
|
|
144 |
if ($this->methode == 'consulter') {
|
|
|
145 |
$retour = $service->consulter($this->ressources);
|
|
|
146 |
} elseif ($this->methode == 'ajouter') {
|
|
|
147 |
$retour = $service->ajouter($this->parametres);
|
|
|
148 |
} elseif ($this->methode == 'supprimer') {
|
|
|
149 |
$retour = $service->supprimer($this->ressources);
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
if (is_null($service)) {
|
|
|
155 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
156 |
$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
|
|
|
157 |
"n'existe pas dans le service '{$this->serviceNom}' !";
|
|
|
158 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
159 |
throw new Exception($message, $code);
|
|
|
160 |
}
|
|
|
161 |
return $retour;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
private function obtenirNomClasseService($mot) {
|
|
|
165 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
166 |
return $classeNom;
|
|
|
167 |
}
|
|
|
168 |
}
|