Subversion Repositories eFlore/Applications.del

Rev

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

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