Subversion Repositories eFlore/Applications.del

Rev

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

Rev 2217 Rev 2218
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 sous-services "images" de DEL.
-
 
5
 *
-
 
6
 * Cette classe se charge toujours de :
-
 
7
 *  - vérifier l'existance des ressources (services) demandés
-
 
8
 *  - vérifier la cohérence et le format des paramêtres passées dans l'url
-
 
9
 * En fonction, de la compléxité du service, elle peut :
-
 
10
 *  - dans un premier temps, exécuter directement les actions : consulter, ajouter, modifier, supprimer.
-
 
11
 *  - dans un second temps, charger dynamiquement d'éventuelles sous-classes du service en fonction des ressources présentes dans l'URL.
5
 *
12
 *
6
 * URLs possibles :
13
 * URLs possibles :
7
 * GET :
14
 * GET :
8
 * http://localhost/service:del:0.1/observations
-
 
9
 * toutes les observations (infos obs, infos images, infos propositions, infos nb commentaires)
-
 
10
 *
-
 
11
 * http://localhost/service:del:0.1/observations?retour.format=widget
15
 * http://localhost/del/services/0.1/images/ => toutes les images : classe ListeImages
12
 * toutes les infos des observations pour le Widget DEL
-
 
13
 *
-
 
14
 * http://localhost/service:del:0.1/observations/#idObs
-
 
15
 * une observation donnée et ses images, SANS LES propositions & nombre de commentaire*
16
 * http://localhost/del/services/0.1/images/#idImg/votes => tous les votes d'une image (#idImg) classés par protocole : classe VotesImage
16
 *
17
 *
17
 * http://localhost/service:del:0.1/observations/#idObs/#idVote/vote
18
 * Non Implémenté : http://localhost/del/services/0.1/images/#id => une image donnée => en test pour remplacer les appels à eflore/cel
18
 * toutes les infos sur les votes d'une proposition
19
 * Non Implémenté : http://localhost/del/services/0.1/images/#id/votes?protocole=#id => tous les votes d'une image et d'un protocole donné
19
 *
20
 *
20
 * PUT :
21
 * PUT :
21
 * http://localhost/service:del:0.1/observations/#idObs/#idCommentaire/vote
22
 * http://localhost/del/services/0.1/images/#idImg => ajouter un vote sur une image donnée (#idImg)
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
-
 
26
 * utilisé seulement par les admins pour modifier une obs depuis DEL (dépublication des obs)
25
 * http://localhost/del/services/0.1/images/#idImg => modifier un vote sur une image donnée (#idImg)
27
 *
26
 *
-
 
27
 * DELETE :
28
 * http://localhost/service:del:0.1/observations/#idObs/#idCommentaire/vote
28
 * http://localhost/del/services/0.1/images/#idImg => supprimer une image donnée (#idImg)
29
 * modifie un vote (+ ou -) pour une obs et une proposition donnée
29
 * http://localhost/del/services/0.1/images/#idImg/votes/#idVote => supprimer un vote (#idVote) d'une image donnée (#idImg)
30
 *
30
 *
31
 * @category   DEL
31
 * @category   DEL
32
 * @package    Services
32
 * @package    Services
33
 * @subpackage Observations
33
 * @subpackage Images
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
restore_error_handler();
-
 
43
restore_exception_handler();
-
 
44
error_reporting(E_ALL);
42
 
45
 
43
class Tepik extends RestService {
46
class Tepik extends RestService {
-
 
47
 
-
 
48
	private $conteneur;
44
 
49
	private $cheminCourant;
45
	private $parametres = array();
50
	private $parametres = array();
46
	private $ressources = array();
51
	private $ressources = array();
47
	private $methode = null;
52
	private $methode;
48
	private $serviceNom = 'tepik';
53
	private $serviceNom = 'tepik';
49
	private $sousServiceNom = null;
-
 
50
	private $cheminCourant = null;
-
 
51
 
-
 
52
	private $conteneur;
54
	private $sousServiceNom;
53
 
55
 
54
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
56
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
55
	protected $utilisationParametresBruts = true;
57
	protected $utilisationParametresBruts = true;
56
 
58
 
57
	public function __construct() {
59
	public function __construct() {
-
 
60
		$this->conteneur = new Conteneur();
58
		$this->cheminCourant = dirname(__FILE__).DS;
61
		$this->cheminCourant = dirname(__FILE__).DS;
59
	}
62
	}
60
 
63
 
61
	public function consulter($ressources, $parametres) {
64
	public function consulter($ressources, $parametres) {
62
		$this->methode = 'consulter';
65
		$this->methode = 'consulter';
63
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
66
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
64
		return $this->executerService();
67
		return $this->executerService();
65
	}
68
	}
66
 
69
 
-
 
70
 
-
 
71
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
-
 
72
		$this->ressources = $ressources;
-
 
73
		$this->parametres = $parametres;
67
 
74
	}
68
 
-
 
69
	private function executerService() {
-
 
70
		$reponseHttp = new ReponseHttp();
-
 
71
		try {
75
 
72
			$this->conteneur = new Conteneur($this->parametres);
76
	private function executerService() {
73
			$resultat = $this->traiterRessources();
-
 
74
			$reponseHttp->setResultatService($resultat);
77
		$resultat = $this->traiterRessources();
75
		} catch (Exception $e) {
78
		if ($resultat === true || $resultat === false) {
76
			$reponseHttp->ajouterErreur($e);
-
 
77
		}
-
 
78
		$reponseHttp->emettreLesEntetes();
-
 
79
		$corps = $reponseHttp->getCorps();
-
 
-
 
79
			return $resultat;
80
		return $corps;
80
		}
81
	}
81
 
82
 
82
		$reponseHttp = new ReponseHttp();
83
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
83
		$reponseHttp->setResultatService($resultat);
84
		$this->ressources = $ressources;
84
		$reponseHttp->emettreLesEntetes();
85
		$this->parametres = $parametres;
85
		return $reponseHttp->getCorps();
86
	}
86
	}
87
 
87
 
88
	private function traiterRessources() {
88
	private function traiterRessources() {
89
		$this->analyserRessources();
89
		$this->analyserRessources();
90
		$retour = $this->initialiserService();
90
		$retour = $this->initialiserService();
91
		return $retour;
91
		return $retour;
92
	}
92
	}
93
 
93
 
94
	private function analyserRessources() {
94
	private function analyserRessources() {
95
		if ($this->methode == 'consulter') {
95
		if ($this->methode == 'consulter') {
96
			$this->analyserRessoucesConsultation();
96
			$this->analyserRessoucesConsultation();
97
		}  
-
 
98
	}
-
 
99
 
-
 
100
	private function analyserRessoucesConsultation() {
97
		} else if ($this->methode == 'modifier' || $this->methode == 'ajouter') {
101
		if (count($this->ressources) == 0) {
-
 
102
			// http://localhost/service:del:0.1/observations
-
 
103
			$this->sousServiceNom = 'liste-observations';
98
			$this->analyserRessoucesModification();
104
		} else if (count($this->ressources) == 1) {
99
		} else if ($this->methode == 'supprimer') {
105
			if ($this->etreRessourceIdentifiant(0)) {
100
			$this->analyserRessoucesSuppression();
106
				// http://localhost/service:del:0.1/observations/#idObs
-
 
107
				$this->sousServiceNom = 'observation-details';
-
 
108
			}
-
 
109
		} 
101
		}
110
		if ($this->sousServiceNom == null) {
102
		if ($this->sousServiceNom == null) {
111
			$this->lancerMessageErreurRessource();
103
			$this->lancerMessageErreurRessource();
112
		}
104
		}
113
	}
105
	}
114
 
106
 
115
	private function analyserRessoucesModification() {
107
	private function analyserRessoucesConsultation() {
-
 
108
		if (count($this->ressources) == 0) {
116
		if (count($this->ressources) == 1) {
109
			$this->sousServiceNom = 'liste-images';
117
			if ($this->methode == 'modifier' && $this->etreRessourceIdentifiant(0)) {
110
		} else if (count($this->ressources) == 2) {
118
				// http://localhost/service:del:0.1/observations/#idObs
111
			if ($this->etreRessourceIdentifiant(0) && $this->verifierRessourceValeur(1, 'votes')) {
119
				$this->sousServiceNom = 'observation-details';
112
				$this->sousServiceNom = 'votes-image';
120
			}
113
			}
121
		}
-
 
122
		
-
 
123
		if ($this->sousServiceNom == null) {
-
 
124
			$this->lancerMessageErreurRessource();
-
 
125
		}
114
		}
-
 
115
	}
126
	}
116
 
127
 
117
 
128
	private function etreRessourceIdentifiant($num) {
118
	private function etreRessourceIdentifiant($num) {
129
		$presenceId = false;
119
		$presenceId = false;
130
		if (isset($this->ressources[$num]) && is_numeric($this->ressources[$num])) {
120
		if (isset($this->ressources[$num]) && is_numeric($this->ressources[$num])) {
131
			$presenceId = true;
121
			$presenceId = true;
132
		}
122
		}
133
		return $presenceId;
123
		return $presenceId;
134
	}
124
	}
135
 
125
 
136
	private function verifierRessourceValeur($num, $valeur) {
126
	private function verifierRessourceValeur($num, $valeur) {
137
		$ok = false;
127
		$ok = false;
138
		if (isset($this->ressources[$num]) && $this->ressources[$num] == $valeur) {
128
		if (isset($this->ressources[$num]) && $this->ressources[$num] == $valeur) {
139
			$ok = true;
129
			$ok = true;
140
		}
130
		}
141
		return $ok;
131
		return $ok;
142
	}
132
	}
143
 
-
 
144
	private function verifierParametreValeur($cle, $valeur) {
-
 
145
		$ok = false;
-
 
146
		if (isset($this->parametres[$cle]) && $this->ressources[$cle] == $valeur) {
-
 
147
			$ok = true;
-
 
148
		}
-
 
149
		return $ok;
-
 
150
	}
-
 
151
 
133
 
152
	private function lancerMessageErreurRessource() {
134
	private function lancerMessageErreurRessource() {
153
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
135
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
154
		$message = "La ressource demandée '$ressource' ".
136
		$message = "La ressource demandée '$ressource' ".
155
			"n'est pas disponible pour le service ".$this->serviceNom." !\n".
137
			"n'est pas disponible pour le service ".$this->serviceNom." !\n".
156
			"Les URLs disponibles sont : \n".
138
			$this->getDoc();
157
			" - en GET : observations, observations/#idObs/#idProposition/vote \n".
-
 
158
			" - en POST : observations/#id, observations/#idObs/#idProposition/vote \n".
-
 
159
			" - en PUT : observations/#idObs/#idProposition/vote \n";
-
 
160
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
139
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
161
		throw new Exception($message, $code);
140
		throw new Exception($message, $code);
162
	}
141
	}
-
 
142
 
-
 
143
	public function getDoc() {
-
 
144
		return "Les URLs disponibles sont : \n".
-
 
145
			" * en GET : \n".
-
 
146
			"  - images => toutes les images\n".
-
 
147
			"  - images/#idImg/votes => tous les votes d'une image (#idImg) classés par protocole\n";
-
 
148
	}
163
 
149
 
164
	private function initialiserService() {
150
	private function initialiserService() {
165
		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
151
		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
166
		$chemins = array();
152
		$chemins = array();
167
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
153
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
168
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
154
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
169
		$retour = '';
155
		$retour = '';
170
		$service = null;
156
		$service = null;
171
		foreach ($chemins as $chemin) {
157
		foreach ($chemins as $chemin) {
172
			if (file_exists($chemin)) {
158
			if (file_exists($chemin)) {
173
				require_once $chemin;
159
				require_once $chemin;
174
				$service = new $classe($this->conteneur);
160
				$service = new $classe($this->conteneur);
175
				if ($this->methode == 'consulter') {
161
				if ($this->methode == 'consulter') {
176
					$retour = $service->consulter($this->ressources, $this->parametres);
162
					$retour = $service->consulter($this->ressources, $this->parametres);
177
				} else {
-
 
178
					$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
-
 
179
						"ne possède pas de méthode '{$this->methode}' !";
-
 
180
					$code = RestServeur::HTTP_NON_IMPLEMENTE;
-
 
181
					throw new Exception($message, $code);
-
 
182
				}
163
				} 
183
			}
164
			}
184
		}
165
		}
185
 
166
 
186
		if (is_null($service)) {
167
		if (is_null($service)) {
187
			$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
168
			$ressource = implode('/', $this->ressources);
188
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
169
			$msg = "Le classe '$classe' correspondant à la ressource '$ressource' ".
189
				"n'existe pas dans le service '{$this->serviceNom}' !";
170
				"n'existe pas dans le service '{$this->serviceNom}' !\n".$this->getDoc();
190
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
171
			throw new Exception($msg, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
191
			throw new Exception($message, $code);
-
 
192
		}
172
		}
193
		return $retour;
173
		return $retour;
194
	}
174
	}
195
 
175
 
196
	private function obtenirNomClasseService($mot) {
176
	private function obtenirNomClasseService($mot) {
197
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
177
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
198
		return $classeNom;
178
		return $classeNom;
199
	}
179
	}
200
}
180
}