755 |
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 |
*/
|
|
|
17 |
class Observations extends RestService {
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
/*
|
|
|
21 |
* url possibles :
|
|
|
22 |
* http://localhost/del/services/0.1/observations/ => toutes les observations (infos obs, infos images, infos propositions, infos nb commentaires)
|
|
|
23 |
* http://localhost/del/services/0.1/observations/#id => une observation donnée et ses images, SANS LES propositions & nombre de commentaire
|
|
|
24 |
* */
|
|
|
25 |
|
|
|
26 |
private $parametres = array();
|
|
|
27 |
private $ressources = array();
|
787 |
delphine |
28 |
private $methode = null;
|
755 |
gduche |
29 |
private $projetNom = array();
|
|
|
30 |
private $serviceNom = array();
|
|
|
31 |
private $cheminCourant = null;
|
|
|
32 |
|
|
|
33 |
private $conteneur;
|
|
|
34 |
|
|
|
35 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
|
|
|
36 |
protected $utilisationParametresBruts = true;
|
|
|
37 |
|
|
|
38 |
public function __construct() {
|
|
|
39 |
$this->cheminCourant = dirname(__FILE__).DS;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
public function consulter($ressources, $parametres) {
|
787 |
delphine |
43 |
$this->methode = 'consulter';
|
755 |
gduche |
44 |
$resultat = '';
|
|
|
45 |
$reponseHttp = new ReponseHttp();
|
|
|
46 |
try {
|
|
|
47 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
48 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
49 |
$resultat = $this->traiterRessources();
|
|
|
50 |
$reponseHttp->setResultatService($resultat);
|
|
|
51 |
} catch (Exception $e) {
|
|
|
52 |
$reponseHttp->ajouterErreur($e);
|
|
|
53 |
}
|
|
|
54 |
$reponseHttp->emettreLesEntetes();
|
|
|
55 |
$corps = $reponseHttp->getCorps();
|
|
|
56 |
return $corps;
|
|
|
57 |
}
|
787 |
delphine |
58 |
|
|
|
59 |
public function ajouter($ressources, $requeteDonnees) {
|
|
|
60 |
$this->methode = 'ajouter';
|
|
|
61 |
$resultat = '';
|
826 |
aurelien |
62 |
$reponseHttp = new ReponseHttp();
|
787 |
delphine |
63 |
try {
|
|
|
64 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
65 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
66 |
$resultat = $this->traiterRessources();
|
|
|
67 |
} catch (Exception $e) {
|
|
|
68 |
$reponseHttp->ajouterErreur($e);
|
826 |
aurelien |
69 |
$reponseHttp->emettreLesEntetes();
|
787 |
delphine |
70 |
}
|
|
|
71 |
}
|
841 |
aurelien |
72 |
|
|
|
73 |
public function modifier($ressources, $requeteDonnees) {
|
|
|
74 |
$this->methode = 'modifier';
|
|
|
75 |
$resultat = '';
|
|
|
76 |
$reponseHttp = new ReponseHttp();
|
|
|
77 |
try {
|
|
|
78 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
79 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
80 |
$resultat = $this->traiterRessources();
|
|
|
81 |
} catch (Exception $e) {
|
|
|
82 |
$reponseHttp->ajouterErreur($e);
|
|
|
83 |
$reponseHttp->emettreLesEntetes();
|
|
|
84 |
}
|
|
|
85 |
}
|
755 |
gduche |
86 |
|
|
|
87 |
private function initialiserRessourcesEtParametres($ressources, $parametres) {
|
|
|
88 |
$this->ressources = $ressources;
|
|
|
89 |
$this->parametres = $parametres;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
private function traiterRessources() {
|
|
|
93 |
$retour = '';
|
|
|
94 |
$this->initialiserProjet();
|
|
|
95 |
if ($this->avoirRessourceService()) {
|
|
|
96 |
$retour = $this->initialiserService();
|
|
|
97 |
}
|
|
|
98 |
return $retour;
|
|
|
99 |
}
|
|
|
100 |
|
787 |
delphine |
101 |
private function avoirRessourceIdentifiant($num) {
|
755 |
gduche |
102 |
$presenceId = false;
|
985 |
aurelien |
103 |
if (is_numeric($this->ressources[$num])) {
|
|
|
104 |
$presenceId = true;
|
|
|
105 |
} else {
|
|
|
106 |
$message = "Le service demandé '$service' nécessite d'avoir un identifiant d'image valide";
|
|
|
107 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
108 |
throw new Exception($message, $code);
|
|
|
109 |
}
|
755 |
gduche |
110 |
return $presenceId;
|
|
|
111 |
}
|
|
|
112 |
/*------------------------------------------------------------------------------------------------------------------
|
|
|
113 |
CONFIGURATION DU PROJET
|
|
|
114 |
------------------------------------------------------------------------------------------------------------------*/
|
|
|
115 |
private function initialiserProjet() {
|
|
|
116 |
$this->chargerNomDuProjet();
|
|
|
117 |
$this->chargerConfigProjet();
|
|
|
118 |
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
private function chargerNomDuProjet() {
|
|
|
122 |
$this->projetNom = 'observations';
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
private function chargerConfigProjet() {
|
|
|
126 |
$projet = $this->projetNom;
|
|
|
127 |
$chemin = Config::get('chemin_configurations')."config_$projet.ini";
|
|
|
128 |
Config::charger($chemin);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/*------------------------------------------------------------------------------------------------------------------
|
|
|
132 |
CONFIGURATION DU SERVICE
|
|
|
133 |
------------------------------------------------------------------------------------------------------------------*/
|
|
|
134 |
private function avoirRessourceService() {
|
|
|
135 |
/*
|
|
|
136 |
* url possibles :
|
|
|
137 |
* http://localhost/del/services/0.1/observations/ => toutes les observations (infos obs, infos images, infos propositions, infos nb commentaires)
|
|
|
138 |
* http://localhost/del/services/0.1/observations/#id => une observation donnée et ses images, SANS LES propositions & nombre de commentaire
|
|
|
139 |
* */
|
|
|
140 |
$presenceRessourceService = false;
|
|
|
141 |
if (isset($this->ressources[0])) {
|
787 |
delphine |
142 |
if ($this->avoirRessourceIdentifiant(0)) {
|
755 |
gduche |
143 |
if (sizeof($this->ressources) == 1) {
|
|
|
144 |
$presenceRessourceService = true;
|
|
|
145 |
$this->serviceNom = 'observation';
|
|
|
146 |
} else {
|
|
|
147 |
if (isset($this->ressources[1])) {
|
|
|
148 |
$presenceRessourceService = $this->avoirRessourceSousService();
|
|
|
149 |
}
|
|
|
150 |
}
|
985 |
aurelien |
151 |
}
|
755 |
gduche |
152 |
} else {
|
985 |
aurelien |
153 |
if (isset($this->parametres['retour.format']) && $this->parametres['retour.format'] == 'widget') {
|
|
|
154 |
$this->methode = 'consulter';
|
|
|
155 |
$presenceRessourceService = true;
|
|
|
156 |
$this->serviceNom = 'liste-observations-widget';
|
|
|
157 |
} else {
|
|
|
158 |
$presenceRessourceService = true;
|
1454 |
raphael |
159 |
$this->serviceNom = 'liste-observations';
|
985 |
aurelien |
160 |
}
|
755 |
gduche |
161 |
}
|
|
|
162 |
return $presenceRessourceService;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
private function avoirRessourceSousService() {
|
|
|
166 |
$presenceRessourceService = false;
|
|
|
167 |
$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
|
787 |
delphine |
168 |
if ($this->avoirRessourceIdentifiant(1)) {
|
|
|
169 |
$service = $this->ressources[2];
|
|
|
170 |
if (in_array($service, $servicesDispo)) {
|
|
|
171 |
$presenceRessourceService = true;
|
|
|
172 |
$this->serviceNom = 'vote-observation';
|
|
|
173 |
} else {
|
|
|
174 |
$message = "Le service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
|
|
|
175 |
"Les services disponibles sont : ".implode(', ', $servicesDispo);
|
|
|
176 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
177 |
throw new Exception($message, $code);
|
|
|
178 |
}
|
755 |
gduche |
179 |
}
|
|
|
180 |
return $presenceRessourceService;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
private function initialiserService() {
|
|
|
184 |
//$this->chargerNomDuService();
|
|
|
185 |
|
|
|
186 |
$classe = $this->obtenirNomClasseService($this->serviceNom);
|
|
|
187 |
$chemins = array();
|
|
|
188 |
$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
|
|
|
189 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
190 |
$retour = '';
|
|
|
191 |
$service = null;
|
|
|
192 |
foreach ($chemins as $chemin) {
|
|
|
193 |
if (file_exists($chemin)) {
|
|
|
194 |
$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
|
|
|
195 |
require_once $chemin;
|
|
|
196 |
$service = new $classe($this->conteneur);
|
787 |
delphine |
197 |
if ($this->methode == 'consulter') {
|
|
|
198 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
|
|
199 |
} elseif ($this->methode == 'ajouter') {
|
|
|
200 |
$retour = $service->ajouter($this->ressources, $this->parametres);
|
841 |
aurelien |
201 |
} elseif ($this->methode == 'modifier') {
|
|
|
202 |
$retour = $service->modifier($this->ressources, $this->parametres);
|
787 |
delphine |
203 |
}
|
755 |
gduche |
204 |
}
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
if (is_null($service)) {
|
|
|
208 |
$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
|
|
|
209 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
210 |
throw new Exception($message, $code);
|
|
|
211 |
}
|
|
|
212 |
return $retour;
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
private function obtenirNomClasseService($mot) {
|
|
|
216 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
217 |
return $classeNom;
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
|
|
|
221 |
}
|
|
|
222 |
?>
|