Subversion Repositories eFlore/Applications.del

Rev

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

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