1149 |
aurelien |
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 del
|
|
|
9 |
* @author Aurélien Peronnet <aurelien@tela-botanica.org>
|
|
|
10 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
11 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
12 |
* @version 0.1
|
|
|
13 |
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
|
|
|
14 |
*/
|
|
|
15 |
class MotsCles extends RestService {
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
/*
|
|
|
19 |
* url possibles :
|
|
|
20 |
* http://localhost/del/services/0.1/motscles/#id => les mots clés associés à une image donnée
|
|
|
21 |
* */
|
|
|
22 |
|
|
|
23 |
private $parametres = array();
|
|
|
24 |
private $ressources = array();
|
|
|
25 |
private $methode = null;
|
|
|
26 |
private $projetNom = array();
|
|
|
27 |
private $serviceNom = array();
|
|
|
28 |
private $cheminCourant = null;
|
|
|
29 |
|
|
|
30 |
private $conteneur;
|
|
|
31 |
|
|
|
32 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
|
|
|
33 |
protected $utilisationParametresBruts = true;
|
|
|
34 |
|
|
|
35 |
public function __construct() {
|
|
|
36 |
$this->cheminCourant = dirname(__FILE__).DS;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
public function consulter($ressources, $parametres) {
|
|
|
40 |
$this->methode = 'consulter';
|
|
|
41 |
$resultat = '';
|
|
|
42 |
$reponseHttp = new ReponseHttp();
|
|
|
43 |
try {
|
|
|
44 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
45 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
46 |
$resultat = $this->traiterRessources();
|
|
|
47 |
$reponseHttp->setResultatService($resultat);
|
|
|
48 |
} catch (Exception $e) {
|
|
|
49 |
$reponseHttp->ajouterErreur($e);
|
|
|
50 |
}
|
|
|
51 |
$reponseHttp->emettreLesEntetes();
|
|
|
52 |
$corps = $reponseHttp->getCorps();
|
|
|
53 |
return $corps;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public function ajouter($ressources, $parametres) {
|
|
|
57 |
$this->methode = 'ajouter';
|
|
|
58 |
$resultat = '';
|
|
|
59 |
$reponseHttp = new ReponseHttp();
|
|
|
60 |
|
|
|
61 |
try {
|
|
|
62 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
63 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
64 |
$resultat = $this->traiterRessources();
|
|
|
65 |
$reponseHttp->setResultatService($resultat);
|
|
|
66 |
} catch (Exception $e) {
|
|
|
67 |
$reponseHttp->ajouterErreur($e);
|
|
|
68 |
}
|
|
|
69 |
$corps = $reponseHttp->getCorps();
|
|
|
70 |
return $corps;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
public function modifier($ressources, $requeteDonnees) {
|
|
|
74 |
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
private function initialiserRessourcesEtParametres($ressources, $parametres) {
|
|
|
78 |
$this->ressources = $ressources;
|
|
|
79 |
$this->parametres = $parametres;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
private function traiterRessources() {
|
|
|
83 |
$retour = '';
|
|
|
84 |
$this->initialiserProjet();
|
|
|
85 |
$retour = $this->initialiserService();
|
|
|
86 |
return $retour;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
/*------------------------------------------------------------------------------------------------------------------
|
|
|
91 |
CONFIGURATION DU PROJET
|
|
|
92 |
------------------------------------------------------------------------------------------------------------------*/
|
|
|
93 |
private function initialiserProjet() {
|
|
|
94 |
$this->chargerNomDuProjet();
|
|
|
95 |
$this->chargerConfigProjet();
|
|
|
96 |
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
private function chargerNomDuProjet() {
|
|
|
100 |
$this->projetNom = 'motscles';
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
private function chargerConfigProjet() {
|
|
|
104 |
$projet = $this->projetNom;
|
|
|
105 |
$chemin = Config::get('chemin_configurations')."config_$projet.ini";
|
|
|
106 |
Config::charger($chemin);
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/*------------------------------------------------------------------------------------------------------------------
|
|
|
110 |
CONFIGURATION DU SERVICE
|
|
|
111 |
------------------------------------------------------------------------------------------------------------------*/
|
|
|
112 |
private function chargerNomService() {
|
|
|
113 |
// si la méthode est POST, on ajouter un commentaire
|
|
|
114 |
if ($this->methode == 'ajouter') {
|
|
|
115 |
$this->serviceNom = 'ajouter-mot-cle';
|
|
|
116 |
} else if ($this->methode == 'supprimer') {
|
|
|
117 |
$this->serviceNom = 'supprimer-mot-cle';
|
|
|
118 |
}
|
|
|
119 |
else {
|
|
|
120 |
//S'il n'y a pas de ressources => tous les commentaires
|
|
|
121 |
if (!isset($this->ressources) || empty($this->ressources)) {
|
|
|
122 |
$this->serviceNom = 'liste-mots-cles';
|
|
|
123 |
} else {
|
|
|
124 |
if (!is_numeric($this->ressources[0])) {
|
|
|
125 |
$message = "La première ressource doit être un identifiant";
|
|
|
126 |
$code = RestServeur::HTTP_CODE_ERREUR;
|
|
|
127 |
throw new Exception($message, $code);
|
|
|
128 |
} else {
|
|
|
129 |
$this->serviceNom = 'consulter-mots-cles';
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
private function editerMessageErreurRessource() {
|
|
|
136 |
$message = "Le service demandé '".$this->projetNom.'/'.implode('/', $this->ressources).
|
|
|
137 |
"' n'est pas disponible pour le projet ".$this->projetNom." !\n".
|
|
|
138 |
"Les services disponibles sont : images, images/#id/votes, images/#id/vote/#id_vote";
|
|
|
139 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
140 |
throw new Exception($message, $code);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
private function etreRessourceIdentifiant($num) {
|
|
|
144 |
$presenceId = false;
|
|
|
145 |
if (is_numeric($this->ressources[$num])) {
|
|
|
146 |
$presenceId = true;
|
|
|
147 |
} else {
|
|
|
148 |
$message = "Le service demandé '$service' nécessite d'avoir un identifiant d'image valide";
|
|
|
149 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
150 |
throw new Exception($message, $code);
|
|
|
151 |
}
|
|
|
152 |
return $presenceId;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
private function initialiserService() {
|
|
|
156 |
$this->chargerNomService();
|
|
|
157 |
|
|
|
158 |
$classe = $this->obtenirNomClasseService($this->serviceNom);
|
|
|
159 |
$chemins = array();
|
|
|
160 |
$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
|
|
|
161 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
162 |
|
|
|
163 |
$retour = '';
|
|
|
164 |
$service = null;
|
|
|
165 |
foreach ($chemins as $chemin) {
|
|
|
166 |
if (file_exists($chemin)) {
|
|
|
167 |
$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
|
|
|
168 |
require_once $chemin;
|
|
|
169 |
$service = new $classe($this->conteneur);
|
|
|
170 |
if ($this->methode == 'consulter') {
|
|
|
171 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
|
|
172 |
} elseif ($this->methode == 'ajouter') {
|
|
|
173 |
$retour = $service->ajouter($this->ressources, $this->parametres);
|
|
|
174 |
} elseif ($this->methode == 'modifier') {
|
|
|
175 |
$retour = $service->modifier($this->ressources, $this->parametres);
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
if (is_null($service)) {
|
|
|
181 |
$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
|
|
|
182 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
183 |
throw new Exception($message, $code);
|
|
|
184 |
}
|
|
|
185 |
return $retour;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
private function obtenirNomClasseService($mot) {
|
|
|
189 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
190 |
return $classeNom;
|
|
|
191 |
}
|
|
|
192 |
}
|
|
|
193 |
?>
|