Subversion Repositories eFlore/Applications.del

Rev

Rev 826 | Rev 985 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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