Subversion Repositories eFlore/Applications.del

Rev

Rev 1454 | Rev 1706 | Go to most recent revision | Show entire file | Ignore 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;
32
 
52
 
33
	private $conteneur;
53
	private $conteneur;
34
	
54
 
Line 35... Line 55...
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
 
Line 38... Line 58...
38
	public function __construct() {
58
	public function __construct() {
39
		$this->cheminCourant = dirname(__FILE__).DS;
59
		$this->cheminCourant = dirname(__FILE__).DS;
40
	}
-
 
41
 
-
 
42
	public function consulter($ressources, $parametres) {
-
 
43
		$this->methode = 'consulter';
60
	}
44
		$resultat = '';
-
 
45
		$reponseHttp = new ReponseHttp();
61
 
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
		}
62
	public function consulter($ressources, $parametres) {
54
		$reponseHttp->emettreLesEntetes();
63
		$this->methode = 'consulter';
55
		$corps = $reponseHttp->getCorps();
64
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
56
		return $corps;
65
		return $this->executerService();
57
	}
-
 
58
	
-
 
59
	public function ajouter($ressources, $requeteDonnees) {
-
 
60
		$this->methode = 'ajouter';
66
	}
61
		$resultat = '';
-
 
62
		$reponseHttp = new ReponseHttp();
67
 
63
		try {
-
 
64
			$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
-
 
65
			$this->conteneur = new Conteneur($this->parametres);
-
 
66
			$resultat = $this->traiterRessources();
-
 
67
		} catch (Exception $e) {
68
	public function ajouter($ressources, $requeteDonnees) {
68
			$reponseHttp->ajouterErreur($e);
69
		$this->methode = 'ajouter';
69
			$reponseHttp->emettreLesEntetes();
70
		$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
70
		}
71
		return $this->executerService();
-
 
72
	}
71
	}
73
 
-
 
74
	public function modifier($ressources, $requeteDonnees) {
-
 
75
		$this->methode = 'modifier';
-
 
76
		$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
72
	
77
		return $this->executerService();
73
	public function modifier($ressources, $requeteDonnees) {
78
	}
74
		$this->methode = 'modifier';
-
 
75
		$resultat = '';
79
 
76
		$reponseHttp = new ReponseHttp();
80
	private function executerService() {
-
 
81
		$reponseHttp = new ReponseHttp();
77
		try {
82
		try {
78
			$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
83
			$this->conteneur = new Conteneur($this->parametres);
79
			$this->conteneur = new Conteneur($this->parametres);
-
 
80
			$resultat = $this->traiterRessources();
84
			$resultat = $this->traiterRessources();
-
 
85
			$reponseHttp->setResultatService($resultat);
-
 
86
		} catch (Exception $e) {
-
 
87
			$reponseHttp->ajouterErreur($e);
81
		} catch (Exception $e) {
88
		}
Line 82... Line 89...
82
			$reponseHttp->ajouterErreur($e);
89
		$reponseHttp->emettreLesEntetes();
83
			$reponseHttp->emettreLesEntetes();
90
		$corps = $reponseHttp->getCorps();
84
		}
91
		return $corps;
85
	}
92
	}
Line 86... Line 93...
86
 
93
 
87
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
-
 
88
		$this->ressources = $ressources;
94
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
89
		$this->parametres = $parametres;
95
		$this->ressources = $ressources;
90
	}
96
		$this->parametres = $parametres;
91
 
-
 
92
	private function traiterRessources() {
97
	}
93
		$retour = '';
98
 
94
		$this->initialiserProjet();
-
 
95
		if ($this->avoirRessourceService()) {
-
 
96
			$retour = $this->initialiserService();
-
 
97
		}
-
 
98
		return $retour;
-
 
99
	}
-
 
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();
-
 
Line 118... Line 99...
118
 
99
	private function traiterRessources() {
119
	}
-
 
120
 
100
		$this->chargerConfigService();
121
	private function chargerNomDuProjet() {
101
		$this->analyserRessources();
122
		$this->projetNom = 'observations';
102
		$retour = $this->initialiserService();
Line 123... Line 103...
123
	}
103
		return $retour;
124
 
104
	}
125
	private function chargerConfigProjet() {
105
 
126
		$projet = $this->projetNom;
-
 
127
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
106
	private function chargerConfigService() {
128
		Config::charger($chemin);
-
 
129
	}
-
 
130
 
-
 
131
	/*------------------------------------------------------------------------------------------------------------------
-
 
132
								CONFIGURATION DU SERVICE
107
		$chemin = Config::get('chemin_configurations')."config_{$this->serviceNom}.ini";
133
	------------------------------------------------------------------------------------------------------------------*/
108
		Config::charger($chemin);
134
	private function avoirRessourceService() {
109
	}
135
		/*
110
 
136
		* url possibles :
-
 
137
		* http://localhost/del/services/0.1/observations/ => toutes les observations (infos obs, infos images, infos propositions, infos nb commentaires)
111
	/*------------------------------------------------------------------------------------------------------------------
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])) {
112
								CONFIGURATION DU SERVICE
142
			if ($this->avoirRessourceIdentifiant(0)) {
113
	------------------------------------------------------------------------------------------------------------------*/
143
				if (sizeof($this->ressources) == 1) {
114
 
144
					$presenceRessourceService = true;
115
	private function analyserRessources() {
145
					$this->serviceNom = 'observation';
116
		if ($this->methode == 'consulter') {
146
				} else {
117
			$this->analyserRessoucesConsultation();
147
					if (isset($this->ressources[1])) {
118
		} else if ($this->methode == 'modifier' || $this->methode == 'ajouter') {
148
						$presenceRessourceService = $this->avoirRessourceSousService();
119
			$this->analyserRessoucesModification();
149
					}
120
		}
150
				} 
121
	}
151
			}
122
 
152
		} else {
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
-
 
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)) {
-
 
134
				// http://localhost/service:del:0.1/observations/#idObs
-
 
135
				$this->sousServiceNom = 'observation';
-
 
136
			}
-
 
137
		} else if (count($this->ressources) == 3) {
153
			if (isset($this->parametres['retour.format']) && $this->parametres['retour.format'] == 'widget') {
138
			if ($this->etreRessourceIdentifiant(0) && $this->etreRessourceIdentifiant(1) && $this->verifierRessourceValeur(2, 'vote')) {
154
				$this->methode = 'consulter';
-
 
155
				$presenceRessourceService = true;
139
				// http://localhost/service:del:0.1/observations/#idObs/#idProposition/vote/
156
				$this->serviceNom = 'liste-observations-widget';
140
				$this->sousServiceNom = 'vote-observation';
157
			} else {
141
			}
158
				$presenceRessourceService = true;
142
		}
159
				$this->serviceNom = 'liste-observations';
-
 
160
			}
143
 
161
		}
-
 
162
		return $presenceRessourceService;
144
		if ($this->sousServiceNom == null) {
163
	}
-
 
164
	
145
			$this->lancerMessageErreurRessource();
165
	private function avoirRessourceSousService() {
-
 
166
		$presenceRessourceService = false;
-
 
167
		$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
-
 
168
		if ($this->avoirRessourceIdentifiant(1)) {
-
 
169
			$service = $this->ressources[2];
-
 
170
			if (in_array($service, $servicesDispo)) {
146
		}
-
 
147
	}
-
 
148
 
-
 
149
	private function analyserRessoucesModification() {
-
 
150
		if (count($this->ressources) == 1) {
-
 
151
			if ($this->methode == 'modifier' && $this->etreRessourceIdentifiant(0)) {
-
 
152
				// http://localhost/service:del:0.1/observations/#idObs
-
 
153
				$this->sousServiceNom = 'observation';
-
 
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) {
171
				$presenceRessourceService = true;
163
			$this->lancerMessageErreurRessource();
172
				$this->serviceNom = 'vote-observation';
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;
-
 
171
		}
-
 
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);
173
			} else {
193
		$message = "La ressource demandée '$ressource' ".
Line 174... Line 194...
174
				$message = "Le service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
194
			"n'est pas disponible pour le service ".$this->serviceNom." !\n".
175
							"Les services disponibles sont : ".implode(', ', $servicesDispo);
-
 
176
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
-
 
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();
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 = '';
Line 201... Line 219...
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
	}
Line 214... Line 234...
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
	}
-
 
219
 
-
 
220
 
-
 
221
}
-
 
222
?>
238
	}
-
 
239
}
223
240