Subversion Repositories eFlore/Applications.del

Rev

Rev 1454 | Rev 1706 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1454 Rev 1684
Line 1... Line 1...
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 {
Line 18... Line -...
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;
Line 31... Line 51...
31
	private $cheminCourant = null;
51
	private $cheminCourant = null;
Line 32... Line 52...
32
 
52
 
Line 39... Line 59...
39
		$this->cheminCourant = dirname(__FILE__).DS;
59
		$this->cheminCourant = dirname(__FILE__).DS;
40
	}
60
	}
Line 41... Line 61...
41
 
61
 
42
	public function consulter($ressources, $parametres) {
62
	public function consulter($ressources, $parametres) {
43
		$this->methode = 'consulter';
-
 
44
		$resultat = '';
-
 
45
		$reponseHttp = new ReponseHttp();
-
 
46
		try {
63
		$this->methode = 'consulter';
47
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
-
 
48
			$this->conteneur = new Conteneur($this->parametres);
64
		$this->initialiserRessourcesEtParametres($ressources, $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;
65
		return $this->executerService();
Line 57... Line 66...
57
	}
66
	}
58
	
67
 
59
	public function ajouter($ressources, $requeteDonnees) {
-
 
60
		$this->methode = 'ajouter';
-
 
61
		$resultat = '';
-
 
62
		$reponseHttp = new ReponseHttp();
68
	public function ajouter($ressources, $requeteDonnees) {
63
		try {
-
 
64
			$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
69
		$this->methode = 'ajouter';
65
			$this->conteneur = new Conteneur($this->parametres);
-
 
66
			$resultat = $this->traiterRessources();
-
 
67
		} catch (Exception $e) {
-
 
68
			$reponseHttp->ajouterErreur($e);
-
 
69
			$reponseHttp->emettreLesEntetes();
70
		$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
Line 70... Line 71...
70
		}
71
		return $this->executerService();
71
	}
72
	}
-
 
73
 
72
	
74
	public function modifier($ressources, $requeteDonnees) {
-
 
75
		$this->methode = 'modifier';
-
 
76
		$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
-
 
77
		return $this->executerService();
73
	public function modifier($ressources, $requeteDonnees) {
78
	}
74
		$this->methode = 'modifier';
79
 
75
		$resultat = '';
-
 
76
		$reponseHttp = new ReponseHttp();
80
	private function executerService() {
77
		try {
81
		$reponseHttp = new ReponseHttp();
-
 
82
		try {
78
			$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
83
			$this->conteneur = new Conteneur($this->parametres);
79
			$this->conteneur = new Conteneur($this->parametres);
84
			$resultat = $this->traiterRessources();
80
			$resultat = $this->traiterRessources();
-
 
81
		} catch (Exception $e) {
85
			$reponseHttp->setResultatService($resultat);
-
 
86
		} catch (Exception $e) {
-
 
87
			$reponseHttp->ajouterErreur($e);
-
 
88
		}
82
			$reponseHttp->ajouterErreur($e);
89
		$reponseHttp->emettreLesEntetes();
Line 83... Line 90...
83
			$reponseHttp->emettreLesEntetes();
90
		$corps = $reponseHttp->getCorps();
84
		}
91
		return $corps;
85
	}
92
	}
86
 
93
 
Line 87... Line 94...
87
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
94
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
88
		$this->ressources = $ressources;
-
 
89
		$this->parametres = $parametres;
95
		$this->ressources = $ressources;
90
	}
96
		$this->parametres = $parametres;
91
 
97
	}
92
	private function traiterRessources() {
-
 
93
		$retour = '';
98
 
94
		$this->initialiserProjet();
99
	private function traiterRessources() {
Line 95... Line 100...
95
		if ($this->avoirRessourceService()) {
100
		$this->chargerConfigService();
96
			$retour = $this->initialiserService();
-
 
97
		}
-
 
98
		return $retour;
-
 
99
	}
-
 
100
	
101
		$this->analyserRessources();
101
	private function avoirRessourceIdentifiant($num) {
-
 
102
		$presenceId = false;
-
 
103
		if (is_numeric($this->ressources[$num])) {
-
 
104
			$presenceId = true;
102
		$retour = $this->initialiserService();
105
		} else {
103
		return $retour;
-
 
104
	}
106
			$message = "Le service demandé '$service' nécessite d'avoir un identifiant d'image valide";
105
 
107
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
106
	private function chargerConfigService() {
108
			throw new Exception($message, $code);
107
		$chemin = Config::get('chemin_configurations')."config_{$this->serviceNom}.ini";
109
		}
-
 
110
		return $presenceId;
-
 
111
	}
-
 
Line -... Line 108...
-
 
108
		Config::charger($chemin);
-
 
109
	}
-
 
110
 
-
 
111
	/*------------------------------------------------------------------------------------------------------------------
-
 
112
								CONFIGURATION DU SERVICE
-
 
113
	------------------------------------------------------------------------------------------------------------------*/
112
	/*------------------------------------------------------------------------------------------------------------------
114
 
Line 113... Line 115...
113
										CONFIGURATION DU PROJET
115
	private function analyserRessources() {
-
 
116
		if ($this->methode == 'consulter') {
-
 
117
			$this->analyserRessoucesConsultation();
-
 
118
		} else if ($this->methode == 'modifier' || $this->methode == 'ajouter') {
-
 
119
			$this->analyserRessoucesModification();
-
 
120
		}
-
 
121
	}
-
 
122
 
-
 
123
	private function analyserRessoucesConsultation() {
-
 
124
		if (count($this->ressources) == 0) {
-
 
125
			if ($this->verifierParametreValeur('retour.format', 'widget')) {
-
 
126
				// http://localhost/service:del:0.1/observations?retour.format=widget
114
	------------------------------------------------------------------------------------------------------------------*/
127
				$this->sousServiceNom = 'liste-observations-widget';
-
 
128
			} else {
-
 
129
				// http://localhost/service:del:0.1/observations
-
 
130
				$this->sousServiceNom = 'liste-observations';
-
 
131
			}
-
 
132
		} else if (count($this->ressources) == 1) {
-
 
133
			if ($this->etreRessourceIdentifiant(0)) {
115
	private function initialiserProjet() {
134
				// http://localhost/service:del:0.1/observations/#idObs
Line 116... Line 135...
116
		$this->chargerNomDuProjet();
135
				$this->sousServiceNom = 'observation';
117
		$this->chargerConfigProjet();
136
			}
118
 
-
 
119
	}
137
		} else if (count($this->ressources) == 3) {
120
 
138
			if ($this->etreRessourceIdentifiant(0) && $this->etreRessourceIdentifiant(1) && $this->verifierRessourceValeur(2, 'vote')) {
Line 121... Line -...
121
	private function chargerNomDuProjet() {
-
 
122
		$this->projetNom = 'observations';
-
 
123
	}
-
 
124
 
139
				// http://localhost/service:del:0.1/observations/#idObs/#idProposition/vote/
125
	private function chargerConfigProjet() {
-
 
126
		$projet = $this->projetNom;
-
 
127
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
-
 
128
		Config::charger($chemin);
-
 
129
	}
-
 
130
 
-
 
131
	/*------------------------------------------------------------------------------------------------------------------
140
				$this->sousServiceNom = 'vote-observation';
132
								CONFIGURATION DU SERVICE
141
			}
133
	------------------------------------------------------------------------------------------------------------------*/
-
 
134
	private function avoirRessourceService() {
142
		}
135
		/*
143
 
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
		* */
144
		if ($this->sousServiceNom == null) {
-
 
145
			$this->lancerMessageErreurRessource();
-
 
146
		}
-
 
147
	}
-
 
148
 
140
		$presenceRessourceService = false;
149
	private function analyserRessoucesModification() {
141
		if (isset($this->ressources[0])) {
150
		if (count($this->ressources) == 1) {
142
			if ($this->avoirRessourceIdentifiant(0)) {
151
			if ($this->methode == 'modifier' && $this->etreRessourceIdentifiant(0)) {
143
				if (sizeof($this->ressources) == 1) {
-
 
144
					$presenceRessourceService = true;
152
				// http://localhost/service:del:0.1/observations/#idObs
145
					$this->serviceNom = 'observation';
153
				$this->sousServiceNom = 'observation';
146
				} else {
-
 
147
					if (isset($this->ressources[1])) {
154
			}
148
						$presenceRessourceService = $this->avoirRessourceSousService();
-
 
149
					}
-
 
150
				} 
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
		}
151
			}
161
 
152
		} else {
162
		if ($this->sousServiceNom == null) {
153
			if (isset($this->parametres['retour.format']) && $this->parametres['retour.format'] == 'widget') {
163
			$this->lancerMessageErreurRessource();
Line 154... Line 164...
154
				$this->methode = 'consulter';
164
		}
155
				$presenceRessourceService = true;
165
	}
-
 
166
 
-
 
167
	private function etreRessourceIdentifiant($num) {
-
 
168
		$presenceId = false;
-
 
169
		if (isset($this->ressources[$num]) && is_numeric($this->ressources[$num])) {
-
 
170
			$presenceId = true;
-
 
171
		}
156
				$this->serviceNom = 'liste-observations-widget';
172
		return $presenceId;
-
 
173
	}
157
			} else {
174
 
158
				$presenceRessourceService = true;
175
	private function verifierRessourceValeur($num, $valeur) {
-
 
176
		$ok = false;
-
 
177
		if (isset($this->ressources[$num]) && $this->ressources[$num] == $valeur) {
-
 
178
			$ok = true;
-
 
179
		}
159
				$this->serviceNom = 'liste-observations';
180
		return $ok;
-
 
181
	}
160
			}
182
 
161
		}
183
	private function verifierParametreValeur($cle, $valeur) {
162
		return $presenceRessourceService;
184
		$ok = false;
163
	}
185
		if (isset($this->parametres[$cle]) && $this->ressources[$cle] == $valeur) {
164
	
186
			$ok = true;
-
 
187
		}
165
	private function avoirRessourceSousService() {
188
		return $ok;
166
		$presenceRessourceService = false;
189
	}
167
		$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
190
 
168
		if ($this->avoirRessourceIdentifiant(1)) {
-
 
169
			$service = $this->ressources[2];
-
 
170
			if (in_array($service, $servicesDispo)) {
-
 
Line 171... Line 191...
171
				$presenceRessourceService = true;
191
	private function lancerMessageErreurRessource() {
172
				$this->serviceNom = 'vote-observation';
-
 
173
			} else {
-
 
174
				$message = "Le service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
192
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
175
							"Les services disponibles sont : ".implode(', ', $servicesDispo);
193
		$message = "La ressource demandée '$ressource' ".
176
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
194
			"n'est pas disponible pour le service ".$this->serviceNom." !\n".
177
				throw new Exception($message, $code);
195
			"Les URLs disponibles sont : \n".
178
			}
196
			" - en GET : observations, observations/#idObs/#idProposition/vote \n".
179
		}
197
			" - en POST : observations/#id, observations/#idObs/#idProposition/vote \n".
180
		return $presenceRessourceService;
198
			" - en PUT : observations/#idObs/#idProposition/vote \n";
181
	}
199
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
182
 
200
		throw new Exception($message, $code);
183
	private function initialiserService() {
201
	}
184
		//$this->chargerNomDuService();
202
 
185
		
203
	private function initialiserService() {
186
		$classe = $this->obtenirNomClasseService($this->serviceNom);
204
		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
187
		$chemins = array();
205
		$chemins = array();
Line 203... Line 221...
203
				}
221
				}
204
			}
222
			}
205
		}
223
		}
Line 206... Line 224...
206
		
224
 
-
 
225
		if (is_null($service)) {
207
		if (is_null($service)) {
226
			$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
-
 
227
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
208
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
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;
Line 213... Line 233...
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;
-
 
218
	}
-
 
219
 
237
		return $classeNom;
220
 
-
 
221
}
238
	}