Subversion Repositories eFlore/Applications.del

Rev

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

Rev 841 Rev 985
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
	
72
	
73
	public function modifier($ressources, $requeteDonnees) {
73
	public function modifier($ressources, $requeteDonnees) {
74
		$this->methode = 'modifier';
74
		$this->methode = 'modifier';
75
		$resultat = '';
75
		$resultat = '';
76
		$reponseHttp = new ReponseHttp();
76
		$reponseHttp = new ReponseHttp();
77
		try {
77
		try {
78
			$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
78
			$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
79
			$this->conteneur = new Conteneur($this->parametres);
79
			$this->conteneur = new Conteneur($this->parametres);
80
			$resultat = $this->traiterRessources();
80
			$resultat = $this->traiterRessources();
81
		} catch (Exception $e) {
81
		} catch (Exception $e) {
82
			$reponseHttp->ajouterErreur($e);
82
			$reponseHttp->ajouterErreur($e);
83
			$reponseHttp->emettreLesEntetes();
83
			$reponseHttp->emettreLesEntetes();
84
		}
84
		}
85
	}
85
	}
86
 
86
 
87
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
87
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
88
		$this->ressources = $ressources;
88
		$this->ressources = $ressources;
89
		$this->parametres = $parametres;
89
		$this->parametres = $parametres;
90
	}
90
	}
91
 
91
 
92
	private function traiterRessources() {
92
	private function traiterRessources() {
93
		$retour = '';
93
		$retour = '';
94
		$this->initialiserProjet();
94
		$this->initialiserProjet();
95
		if ($this->avoirRessourceService()) {
95
		if ($this->avoirRessourceService()) {
96
			$retour = $this->initialiserService();
96
			$retour = $this->initialiserService();
97
		}
97
		}
98
		return $retour;
98
		return $retour;
99
	}
99
	}
100
	
100
	
101
	private function avoirRessourceIdentifiant($num) {
101
	private function avoirRessourceIdentifiant($num) {
102
		$presenceId = false;
102
		$presenceId = false;
103
			if (is_numeric($this->ressources[$num])) {
103
		if (is_numeric($this->ressources[$num])) {
104
				$presenceId = true;
104
			$presenceId = true;
105
			} else {
105
		} else {
106
				$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";
107
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
107
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
108
				throw new Exception($message, $code);
108
			throw new Exception($message, $code);
109
			}
109
		}
110
		return $presenceId;
110
		return $presenceId;
111
	}
111
	}
112
	/*------------------------------------------------------------------------------------------------------------------
112
	/*------------------------------------------------------------------------------------------------------------------
113
										CONFIGURATION DU PROJET
113
										CONFIGURATION DU PROJET
114
	------------------------------------------------------------------------------------------------------------------*/
114
	------------------------------------------------------------------------------------------------------------------*/
115
	private function initialiserProjet() {
115
	private function initialiserProjet() {
116
		$this->chargerNomDuProjet();
116
		$this->chargerNomDuProjet();
117
		$this->chargerConfigProjet();
117
		$this->chargerConfigProjet();
118
 
118
 
119
	}
119
	}
120
 
120
 
121
	private function chargerNomDuProjet() {
121
	private function chargerNomDuProjet() {
122
		$this->projetNom = 'observations';
122
		$this->projetNom = 'observations';
123
	}
123
	}
124
 
124
 
125
	private function chargerConfigProjet() {
125
	private function chargerConfigProjet() {
126
		$projet = $this->projetNom;
126
		$projet = $this->projetNom;
127
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
127
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
128
		Config::charger($chemin);
128
		Config::charger($chemin);
129
	}
129
	}
130
 
130
 
131
	/*------------------------------------------------------------------------------------------------------------------
131
	/*------------------------------------------------------------------------------------------------------------------
132
								CONFIGURATION DU SERVICE
132
								CONFIGURATION DU SERVICE
133
	------------------------------------------------------------------------------------------------------------------*/
133
	------------------------------------------------------------------------------------------------------------------*/
134
	private function avoirRessourceService() {
134
	private function avoirRessourceService() {
135
		/*
135
		/*
136
		* url possibles :
136
		* url possibles :
137
		* 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)
138
		* 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
139
		* */
139
		* */
140
		$presenceRessourceService = false;
140
		$presenceRessourceService = false;
141
		if (isset($this->ressources[0])) {
141
		if (isset($this->ressources[0])) {
142
			if ($this->avoirRessourceIdentifiant(0)) {
142
			if ($this->avoirRessourceIdentifiant(0)) {
143
				if (sizeof($this->ressources) == 1) {
143
				if (sizeof($this->ressources) == 1) {
144
					$presenceRessourceService = true;
144
					$presenceRessourceService = true;
145
					$this->serviceNom = 'observation';
145
					$this->serviceNom = 'observation';
146
				} else {
146
				} else {
147
					if (isset($this->ressources[1])) {
147
					if (isset($this->ressources[1])) {
148
						$presenceRessourceService = $this->avoirRessourceSousService();
148
						$presenceRessourceService = $this->avoirRessourceSousService();
149
					}
149
					}
150
				} 
150
				} 
151
			} 
151
			}
152
		} else {
152
		} else {
-
 
153
			if (isset($this->parametres['retour.format']) && $this->parametres['retour.format'] == 'widget') {
-
 
154
				$this->methode = 'consulter';
153
			$presenceRessourceService = true;
155
				$presenceRessourceService = true;
-
 
156
				$this->serviceNom = 'liste-observations-widget';
-
 
157
			} else {
-
 
158
				$presenceRessourceService = true;
154
			$this->serviceNom = 'liste-observations';
159
				$this->serviceNom = 'liste-observations';
-
 
160
			}
155
		}
161
		}
156
		return $presenceRessourceService;
162
		return $presenceRessourceService;
157
	}
163
	}
158
	
164
	
159
	private function avoirRessourceSousService() {
165
	private function avoirRessourceSousService() {
160
		$presenceRessourceService = false;
166
		$presenceRessourceService = false;
161
		$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
167
		$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
162
		if ($this->avoirRessourceIdentifiant(1)) {
168
		if ($this->avoirRessourceIdentifiant(1)) {
163
			$service = $this->ressources[2];
169
			$service = $this->ressources[2];
164
			if (in_array($service, $servicesDispo)) {
170
			if (in_array($service, $servicesDispo)) {
165
				$presenceRessourceService = true;
171
				$presenceRessourceService = true;
166
				$this->serviceNom = 'vote-observation';
172
				$this->serviceNom = 'vote-observation';
167
			} else {
173
			} else {
168
				$message = "Le service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
174
				$message = "Le service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
169
							"Les services disponibles sont : ".implode(', ', $servicesDispo);
175
							"Les services disponibles sont : ".implode(', ', $servicesDispo);
170
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
176
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
171
				throw new Exception($message, $code);
177
				throw new Exception($message, $code);
172
			}
178
			}
173
		}
179
		}
174
		return $presenceRessourceService;
180
		return $presenceRessourceService;
175
	}
181
	}
176
 
182
 
177
	private function initialiserService() {
183
	private function initialiserService() {
178
		//$this->chargerNomDuService();
184
		//$this->chargerNomDuService();
179
		
185
		
180
		$classe = $this->obtenirNomClasseService($this->serviceNom);
186
		$classe = $this->obtenirNomClasseService($this->serviceNom);
181
		$chemins = array();
187
		$chemins = array();
182
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
188
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
183
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
189
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
184
		$retour = '';
190
		$retour = '';
185
		$service = null;
191
		$service = null;
186
		foreach ($chemins as $chemin) {
192
		foreach ($chemins as $chemin) {
187
			if (file_exists($chemin)) {
193
			if (file_exists($chemin)) {
188
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
194
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
189
				require_once $chemin;
195
				require_once $chemin;
190
				$service = new $classe($this->conteneur);
196
				$service = new $classe($this->conteneur);
191
				if ($this->methode == 'consulter') {
197
				if ($this->methode == 'consulter') {
192
					$retour = $service->consulter($this->ressources, $this->parametres);
198
					$retour = $service->consulter($this->ressources, $this->parametres);
193
				} elseif ($this->methode == 'ajouter') {
199
				} elseif ($this->methode == 'ajouter') {
194
					$retour = $service->ajouter($this->ressources, $this->parametres);
200
					$retour = $service->ajouter($this->ressources, $this->parametres);
195
				} elseif ($this->methode == 'modifier') {
201
				} elseif ($this->methode == 'modifier') {
196
					$retour = $service->modifier($this->ressources, $this->parametres);
202
					$retour = $service->modifier($this->ressources, $this->parametres);
197
				}
203
				}
198
			}
204
			}
199
		}
205
		}
200
		
206
		
201
		if (is_null($service)) {
207
		if (is_null($service)) {
202
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
208
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
203
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
209
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
204
			throw new Exception($message, $code);
210
			throw new Exception($message, $code);
205
		}
211
		}
206
		return $retour;
212
		return $retour;
207
	}
213
	}
208
 
214
 
209
	private function obtenirNomClasseService($mot) {
215
	private function obtenirNomClasseService($mot) {
210
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
216
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
211
		return $classeNom;
217
		return $classeNom;
212
	}
218
	}
213
 
219
 
214
 
220
 
215
}
221
}
216
?>
222
?>