Subversion Repositories eFlore/Applications.del

Rev

Rev 826 | Rev 985 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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;
787 delphine 103
			if (is_numeric($this->ressources[$num])) {
755 gduche 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
			}
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
				}
151
			}
152
		} else {
153
			$presenceRessourceService = true;
154
			$this->serviceNom = 'liste-observations';
155
		}
156
		return $presenceRessourceService;
157
	}
158
 
159
	private function avoirRessourceSousService() {
160
		$presenceRessourceService = false;
161
		$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
787 delphine 162
		if ($this->avoirRessourceIdentifiant(1)) {
163
			$service = $this->ressources[2];
164
			if (in_array($service, $servicesDispo)) {
165
				$presenceRessourceService = true;
166
				$this->serviceNom = 'vote-observation';
167
			} else {
168
				$message = "Le service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
169
							"Les services disponibles sont : ".implode(', ', $servicesDispo);
170
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
171
				throw new Exception($message, $code);
172
			}
755 gduche 173
		}
174
		return $presenceRessourceService;
175
	}
176
 
177
	private function initialiserService() {
178
		//$this->chargerNomDuService();
179
 
180
		$classe = $this->obtenirNomClasseService($this->serviceNom);
181
		$chemins = array();
182
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
183
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
184
		$retour = '';
185
		$service = null;
186
		foreach ($chemins as $chemin) {
187
			if (file_exists($chemin)) {
188
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
189
				require_once $chemin;
190
				$service = new $classe($this->conteneur);
787 delphine 191
				if ($this->methode == 'consulter') {
192
					$retour = $service->consulter($this->ressources, $this->parametres);
193
				} elseif ($this->methode == 'ajouter') {
194
					$retour = $service->ajouter($this->ressources, $this->parametres);
841 aurelien 195
				} elseif ($this->methode == 'modifier') {
196
					$retour = $service->modifier($this->ressources, $this->parametres);
787 delphine 197
				}
755 gduche 198
			}
199
		}
200
 
201
		if (is_null($service)) {
202
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
203
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
204
			throw new Exception($message, $code);
205
		}
206
		return $retour;
207
	}
208
 
209
	private function obtenirNomClasseService($mot) {
210
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
211
		return $classeNom;
212
	}
213
 
214
 
215
}
216
?>