Subversion Repositories eFlore/Applications.del

Rev

Rev 1851 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1851 Rev 1881
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
 
43
class Observations extends RestService {
43
class Observations extends RestService {
44
 
44
 
45
	private $parametres = array();
45
	private $parametres = array();
46
	private $ressources = array();
46
	private $ressources = array();
47
	private $methode = null;
47
	private $methode = null;
48
	private $serviceNom = 'observations';
48
	private $serviceNom = 'observations';
49
	private $sousServiceNom = null;
49
	private $sousServiceNom = null;
50
	private $cheminCourant = null;
50
	private $cheminCourant = null;
51
 
51
 
52
	private $conteneur;
52
	private $conteneur;
53
 
53
 
54
	/** 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. */
55
	protected $utilisationParametresBruts = true;
55
	protected $utilisationParametresBruts = true;
56
 
56
 
57
	public function __construct() {
57
	public function __construct() {
58
		$this->cheminCourant = dirname(__FILE__).DS;
58
		$this->cheminCourant = dirname(__FILE__).DS;
59
	}
59
	}
60
 
60
 
61
	public function consulter($ressources, $parametres) {
61
	public function consulter($ressources, $parametres) {
62
		$this->methode = 'consulter';
62
		$this->methode = 'consulter';
63
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
63
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
64
		return $this->executerService();
64
		return $this->executerService();
65
	}
65
	}
66
 
66
 
67
	public function ajouter($ressources, $requeteDonnees) {
67
	public function ajouter($ressources, $requeteDonnees) {
68
		$this->methode = 'ajouter';
68
		$this->methode = 'ajouter';
69
		$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
69
		$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
70
		return $this->executerService();
70
		return $this->executerService();
71
	}
71
	}
72
 
72
 
73
	public function modifier($ressources, $requeteDonnees) {
73
	public function modifier($ressources, $requeteDonnees) {
74
		$this->methode = 'modifier';
74
		$this->methode = 'modifier';
75
		$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
75
		$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
76
		return $this->executerService();
76
		return $this->executerService();
77
	}
77
	}
78
 
78
 
79
	private function executerService() {
79
	private function executerService() {
80
		$reponseHttp = new ReponseHttp();
80
		$reponseHttp = new ReponseHttp();
81
		try {
81
		try {
82
			$this->conteneur = new Conteneur($this->parametres);
82
			$this->conteneur = new Conteneur($this->parametres);
83
			$resultat = $this->traiterRessources();
83
			$resultat = $this->traiterRessources();
84
			$reponseHttp->setResultatService($resultat);
84
			$reponseHttp->setResultatService($resultat);
85
		} catch (Exception $e) {
85
		} catch (Exception $e) {
86
			$reponseHttp->ajouterErreur($e);
86
			$reponseHttp->ajouterErreur($e);
87
		}
87
		}
88
		$reponseHttp->emettreLesEntetes();
88
		$reponseHttp->emettreLesEntetes();
89
		$corps = $reponseHttp->getCorps();
89
		$corps = $reponseHttp->getCorps();
90
		return $corps;
90
		return $corps;
91
	}
91
	}
92
 
92
 
93
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
93
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
94
		$this->ressources = $ressources;
94
		$this->ressources = $ressources;
95
		$this->parametres = $parametres;
95
		$this->parametres = $parametres;
96
	}
96
	}
97
 
97
 
98
	private function traiterRessources() {
98
	private function traiterRessources() {
99
		$this->analyserRessources();
99
		$this->analyserRessources();
100
		$retour = $this->initialiserService();
100
		$retour = $this->initialiserService();
101
		return $retour;
101
		return $retour;
102
	}
102
	}
103
 
103
 
104
	private function analyserRessources() {
104
	private function analyserRessources() {
105
		if ($this->methode == 'consulter') {
105
		if ($this->methode == 'consulter') {
106
			$this->analyserRessoucesConsultation();
106
			$this->analyserRessoucesConsultation();
107
		} else if ($this->methode == 'modifier' || $this->methode == 'ajouter') {
107
		} else if ($this->methode == 'modifier' || $this->methode == 'ajouter') {
108
			$this->analyserRessoucesModification();
108
			$this->analyserRessoucesModification();
109
		}
109
		}
110
	}
110
	}
111
 
111
 
112
	private function analyserRessoucesConsultation() {
112
	private function analyserRessoucesConsultation() {
113
		if (count($this->ressources) == 0) {
113
		if (count($this->ressources) == 0) {
114
			if ($this->verifierParametreValeur('retour.format', 'widget')) {
-
 
115
				// http://localhost/service:del:0.1/observations?retour.format=widget
-
 
116
				$this->sousServiceNom = 'liste-observations-widget';
-
 
117
			} else {
-
 
118
				// http://localhost/service:del:0.1/observations
114
			// http://localhost/service:del:0.1/observations
119
				$this->sousServiceNom = 'liste-observations';
115
			$this->sousServiceNom = 'liste-observations';
120
			}
-
 
121
		} else if (count($this->ressources) == 1) {
116
		} else if (count($this->ressources) == 1) {
122
			if ($this->etreRessourceIdentifiant(0)) {
117
			if ($this->etreRessourceIdentifiant(0)) {
123
				// http://localhost/service:del:0.1/observations/#idObs
118
				// http://localhost/service:del:0.1/observations/#idObs
124
				$this->sousServiceNom = 'observation-details';
119
				$this->sousServiceNom = 'observation-details';
125
			}
120
			}
126
		} else if (count($this->ressources) == 3) {
121
		} else if (count($this->ressources) == 3) {
127
			if ($this->etreRessourceIdentifiant(0) && $this->etreRessourceIdentifiant(1) && $this->verifierRessourceValeur(2, 'vote')) {
122
			if ($this->etreRessourceIdentifiant(0) && $this->etreRessourceIdentifiant(1) && $this->verifierRessourceValeur(2, 'vote')) {
128
				// http://localhost/service:del:0.1/observations/#idObs/#idProposition/vote/
123
				// http://localhost/service:del:0.1/observations/#idObs/#idProposition/vote/
129
				$this->sousServiceNom = 'vote-observation';
124
				$this->sousServiceNom = 'vote-observation';
130
			}
125
			}
131
		}
126
		}
132
 
127
 
133
		if ($this->sousServiceNom == null) {
128
		if ($this->sousServiceNom == null) {
134
			$this->lancerMessageErreurRessource();
129
			$this->lancerMessageErreurRessource();
135
		}
130
		}
136
	}
131
	}
137
 
132
 
138
	private function analyserRessoucesModification() {
133
	private function analyserRessoucesModification() {
139
		if (count($this->ressources) == 1) {
134
		if (count($this->ressources) == 1) {
140
			if ($this->methode == 'modifier' && $this->etreRessourceIdentifiant(0)) {
135
			if ($this->methode == 'modifier' && $this->etreRessourceIdentifiant(0)) {
141
				// http://localhost/service:del:0.1/observations/#idObs
136
				// http://localhost/service:del:0.1/observations/#idObs
142
				$this->sousServiceNom = 'observation-details';
137
				$this->sousServiceNom = 'observation-details';
143
			}
138
			}
144
		} else if (count($this->ressources) == 3) {
139
		} else if (count($this->ressources) == 3) {
145
			if ($this->etreRessourceIdentifiant(0) && $this->etreRessourceIdentifiant(1) && $this->verifierRessourceValeur(2, 'vote')) {
140
			if ($this->etreRessourceIdentifiant(0) && $this->etreRessourceIdentifiant(1) && $this->verifierRessourceValeur(2, 'vote')) {
146
				// http://localhost/service:del:0.1/observations/#idObs/#idProposition/vote/
141
				// http://localhost/service:del:0.1/observations/#idObs/#idProposition/vote/
147
				$this->sousServiceNom = 'vote-observation';
142
				$this->sousServiceNom = 'vote-observation';
148
			}
143
			}
149
		}
144
		}
150
 
145
 
151
		if ($this->sousServiceNom == null) {
146
		if ($this->sousServiceNom == null) {
152
			$this->lancerMessageErreurRessource();
147
			$this->lancerMessageErreurRessource();
153
		}
148
		}
154
	}
149
	}
155
 
150
 
156
	private function etreRessourceIdentifiant($num) {
151
	private function etreRessourceIdentifiant($num) {
157
		$presenceId = false;
152
		$presenceId = false;
158
		if (isset($this->ressources[$num]) && is_numeric($this->ressources[$num])) {
153
		if (isset($this->ressources[$num]) && is_numeric($this->ressources[$num])) {
159
			$presenceId = true;
154
			$presenceId = true;
160
		}
155
		}
161
		return $presenceId;
156
		return $presenceId;
162
	}
157
	}
163
 
158
 
164
	private function verifierRessourceValeur($num, $valeur) {
159
	private function verifierRessourceValeur($num, $valeur) {
165
		$ok = false;
160
		$ok = false;
166
		if (isset($this->ressources[$num]) && $this->ressources[$num] == $valeur) {
161
		if (isset($this->ressources[$num]) && $this->ressources[$num] == $valeur) {
167
			$ok = true;
162
			$ok = true;
168
		}
163
		}
169
		return $ok;
164
		return $ok;
170
	}
165
	}
171
 
166
 
172
	private function verifierParametreValeur($cle, $valeur) {
167
	private function verifierParametreValeur($cle, $valeur) {
173
		$ok = false;
168
		$ok = false;
174
		if (isset($this->parametres[$cle]) && $this->ressources[$cle] == $valeur) {
169
		if (isset($this->parametres[$cle]) && $this->ressources[$cle] == $valeur) {
175
			$ok = true;
170
			$ok = true;
176
		}
171
		}
177
		return $ok;
172
		return $ok;
178
	}
173
	}
179
 
174
 
180
	private function lancerMessageErreurRessource() {
175
	private function lancerMessageErreurRessource() {
181
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
176
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
182
		$message = "La ressource demandée '$ressource' ".
177
		$message = "La ressource demandée '$ressource' ".
183
			"n'est pas disponible pour le service ".$this->serviceNom." !\n".
178
			"n'est pas disponible pour le service ".$this->serviceNom." !\n".
184
			"Les URLs disponibles sont : \n".
179
			"Les URLs disponibles sont : \n".
185
			" - en GET : observations, observations/#idObs/#idProposition/vote \n".
180
			" - en GET : observations, observations/#idObs/#idProposition/vote \n".
186
			" - en POST : observations/#id, observations/#idObs/#idProposition/vote \n".
181
			" - en POST : observations/#id, observations/#idObs/#idProposition/vote \n".
187
			" - en PUT : observations/#idObs/#idProposition/vote \n";
182
			" - en PUT : observations/#idObs/#idProposition/vote \n";
188
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
183
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
189
		throw new Exception($message, $code);
184
		throw new Exception($message, $code);
190
	}
185
	}
191
 
186
 
192
	private function initialiserService() {
187
	private function initialiserService() {
193
		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
188
		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
194
		$chemins = array();
189
		$chemins = array();
195
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
190
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
196
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
191
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
197
		$retour = '';
192
		$retour = '';
198
		$service = null;
193
		$service = null;
199
		foreach ($chemins as $chemin) {
194
		foreach ($chemins as $chemin) {
200
			if (file_exists($chemin)) {
195
			if (file_exists($chemin)) {
201
				require_once $chemin;
196
				require_once $chemin;
202
				$service = new $classe($this->conteneur);
197
				$service = new $classe($this->conteneur);
203
				if ($this->methode == 'consulter') {
198
				if ($this->methode == 'consulter') {
204
					$retour = $service->consulter($this->ressources, $this->parametres);
199
					$retour = $service->consulter($this->ressources, $this->parametres);
205
				} elseif ($this->methode == 'ajouter') {
200
				} elseif ($this->methode == 'ajouter') {
206
					$retour = $service->ajouter($this->ressources, $this->parametres);
201
					$retour = $service->ajouter($this->ressources, $this->parametres);
207
				} elseif ($this->methode == 'modifier') {
202
				} elseif ($this->methode == 'modifier') {
208
					$retour = $service->modifier($this->ressources, $this->parametres);
203
					$retour = $service->modifier($this->ressources, $this->parametres);
209
				} else {
204
				} else {
210
					$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
205
					$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
211
						"ne possède pas de méthode '{$this->methode}' !";
206
						"ne possède pas de méthode '{$this->methode}' !";
212
					$code = RestServeur::HTTP_NON_IMPLEMENTE;
207
					$code = RestServeur::HTTP_NON_IMPLEMENTE;
213
					throw new Exception($message, $code);
208
					throw new Exception($message, $code);
214
				}
209
				}
215
			}
210
			}
216
		}
211
		}
217
 
212
 
218
		if (is_null($service)) {
213
		if (is_null($service)) {
219
			$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
214
			$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
220
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
215
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
221
				"n'existe pas dans le service '{$this->serviceNom}' !";
216
				"n'existe pas dans le service '{$this->serviceNom}' !";
222
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
217
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
223
			throw new Exception($message, $code);
218
			throw new Exception($message, $code);
224
		}
219
		}
225
		return $retour;
220
		return $retour;
226
	}
221
	}
227
 
222
 
228
	private function obtenirNomClasseService($mot) {
223
	private function obtenirNomClasseService($mot) {
229
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
224
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
230
		return $classeNom;
225
		return $classeNom;
231
	}
226
	}
232
}
227
}