Subversion Repositories eFlore/Applications.del

Rev

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

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