Subversion Repositories eFlore/Applications.del

Rev

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

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