Subversion Repositories eFlore/Applications.del

Rev

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

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