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 |
}
|
755 |
gduche |
72 |
|
|
|
73 |
private function initialiserRessourcesEtParametres($ressources, $parametres) {
|
|
|
74 |
$this->ressources = $ressources;
|
|
|
75 |
$this->parametres = $parametres;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
private function traiterRessources() {
|
|
|
79 |
$retour = '';
|
|
|
80 |
$this->initialiserProjet();
|
|
|
81 |
if ($this->avoirRessourceService()) {
|
|
|
82 |
$retour = $this->initialiserService();
|
|
|
83 |
}
|
|
|
84 |
return $retour;
|
|
|
85 |
}
|
|
|
86 |
|
787 |
delphine |
87 |
private function avoirRessourceIdentifiant($num) {
|
755 |
gduche |
88 |
$presenceId = false;
|
787 |
delphine |
89 |
if (is_numeric($this->ressources[$num])) {
|
755 |
gduche |
90 |
$presenceId = true;
|
|
|
91 |
} else {
|
|
|
92 |
$message = "Le service demandé '$service' nécessite d'avoir un identifiant d'image valide";
|
|
|
93 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
94 |
throw new Exception($message, $code);
|
|
|
95 |
}
|
|
|
96 |
return $presenceId;
|
|
|
97 |
}
|
|
|
98 |
/*------------------------------------------------------------------------------------------------------------------
|
|
|
99 |
CONFIGURATION DU PROJET
|
|
|
100 |
------------------------------------------------------------------------------------------------------------------*/
|
|
|
101 |
private function initialiserProjet() {
|
|
|
102 |
$this->chargerNomDuProjet();
|
|
|
103 |
$this->chargerConfigProjet();
|
|
|
104 |
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
private function chargerNomDuProjet() {
|
|
|
108 |
$this->projetNom = 'observations';
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
private function chargerConfigProjet() {
|
|
|
112 |
$projet = $this->projetNom;
|
|
|
113 |
$chemin = Config::get('chemin_configurations')."config_$projet.ini";
|
|
|
114 |
Config::charger($chemin);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/*------------------------------------------------------------------------------------------------------------------
|
|
|
118 |
CONFIGURATION DU SERVICE
|
|
|
119 |
------------------------------------------------------------------------------------------------------------------*/
|
|
|
120 |
private function avoirRessourceService() {
|
|
|
121 |
/*
|
|
|
122 |
* url possibles :
|
|
|
123 |
* http://localhost/del/services/0.1/observations/ => toutes les observations (infos obs, infos images, infos propositions, infos nb commentaires)
|
|
|
124 |
* http://localhost/del/services/0.1/observations/#id => une observation donnée et ses images, SANS LES propositions & nombre de commentaire
|
|
|
125 |
* */
|
|
|
126 |
$presenceRessourceService = false;
|
|
|
127 |
if (isset($this->ressources[0])) {
|
787 |
delphine |
128 |
if ($this->avoirRessourceIdentifiant(0)) {
|
755 |
gduche |
129 |
if (sizeof($this->ressources) == 1) {
|
|
|
130 |
$presenceRessourceService = true;
|
|
|
131 |
$this->serviceNom = 'observation';
|
|
|
132 |
} else {
|
|
|
133 |
if (isset($this->ressources[1])) {
|
|
|
134 |
$presenceRessourceService = $this->avoirRessourceSousService();
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
} else {
|
|
|
139 |
$presenceRessourceService = true;
|
|
|
140 |
$this->serviceNom = 'liste-observations';
|
|
|
141 |
}
|
|
|
142 |
return $presenceRessourceService;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
private function avoirRessourceSousService() {
|
|
|
146 |
$presenceRessourceService = false;
|
|
|
147 |
$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
|
787 |
delphine |
148 |
if ($this->avoirRessourceIdentifiant(1)) {
|
|
|
149 |
$service = $this->ressources[2];
|
|
|
150 |
if (in_array($service, $servicesDispo)) {
|
|
|
151 |
$presenceRessourceService = true;
|
|
|
152 |
$this->serviceNom = 'vote-observation';
|
|
|
153 |
} else {
|
|
|
154 |
$message = "Le service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
|
|
|
155 |
"Les services disponibles sont : ".implode(', ', $servicesDispo);
|
|
|
156 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
157 |
throw new Exception($message, $code);
|
|
|
158 |
}
|
755 |
gduche |
159 |
}
|
|
|
160 |
return $presenceRessourceService;
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
private function initialiserService() {
|
|
|
164 |
//$this->chargerNomDuService();
|
|
|
165 |
|
|
|
166 |
$classe = $this->obtenirNomClasseService($this->serviceNom);
|
|
|
167 |
$chemins = array();
|
|
|
168 |
$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
|
|
|
169 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
170 |
$retour = '';
|
|
|
171 |
$service = null;
|
|
|
172 |
foreach ($chemins as $chemin) {
|
|
|
173 |
if (file_exists($chemin)) {
|
|
|
174 |
$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
|
|
|
175 |
require_once $chemin;
|
|
|
176 |
$service = new $classe($this->conteneur);
|
787 |
delphine |
177 |
if ($this->methode == 'consulter') {
|
|
|
178 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
|
|
179 |
} elseif ($this->methode == 'ajouter') {
|
|
|
180 |
$retour = $service->ajouter($this->ressources, $this->parametres);
|
|
|
181 |
}
|
755 |
gduche |
182 |
}
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
if (is_null($service)) {
|
|
|
186 |
$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
|
|
|
187 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
188 |
throw new Exception($message, $code);
|
|
|
189 |
}
|
|
|
190 |
return $retour;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
private function obtenirNomClasseService($mot) {
|
|
|
194 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
195 |
return $classeNom;
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
|
|
|
199 |
}
|
|
|
200 |
?>
|