Subversion Repositories eFlore/Applications.del

Rev

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

Rev 1149 Rev 1158
1
<?php
1
<?php
2
/**
2
/**
3
* Description :
3
* Description :
4
* Classe principale de chargement des services d'eFlore.
4
* Classe principale de chargement des services d'eFlore.
5
*
5
*
6
* Encodage en entrée : utf8
6
* Encodage en entrée : utf8
7
* Encodage en sortie : utf8
7
* Encodage en sortie : utf8
8
* @package del
8
* @package del
9
* @author Aurélien Peronnet <aurelien@tela-botanica.org>
9
* @author Aurélien Peronnet <aurelien@tela-botanica.org>
10
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
10
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
11
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
11
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
12
* @version 0.1
12
* @version 0.1
13
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
13
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
14
*/
14
*/
15
class MotsCles extends RestService {
15
class MotsCles extends RestService {
16
 
16
 
17
	
17
	
18
	/*
18
	/*
19
	 * url possibles : 
19
	 * url possibles : 
20
	 * http://localhost/del/services/0.1/motscles/#id =>  les mots clés associés à une image donnée
20
	 * http://localhost/del/services/0.1/motscles/#id =>  les mots clés associés à une image donnée
21
	 * */
21
	 * */
22
	
22
	
23
	private $parametres = array();
23
	private $parametres = array();
24
	private $ressources = array();
24
	private $ressources = array();
25
	private $methode = null;
25
	private $methode = null;
26
	private $projetNom = array();
26
	private $projetNom = array();
27
	private $serviceNom = array();
27
	private $serviceNom = array();
28
	private $cheminCourant = null;
28
	private $cheminCourant = null;
29
 
29
 
30
	private $conteneur;
30
	private $conteneur;
31
	
31
	
32
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
32
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
33
	protected $utilisationParametresBruts = true;
33
	protected $utilisationParametresBruts = true;
34
 
34
 
35
	public function __construct() {
35
	public function __construct() {
36
		$this->cheminCourant = dirname(__FILE__).DS;
36
		$this->cheminCourant = dirname(__FILE__).DS;
37
	}
37
	}
38
 
38
 
39
	public function consulter($ressources, $parametres) {
39
	public function consulter($ressources, $parametres) {
40
		$this->methode = 'consulter';
40
		$this->methode = 'consulter';
41
		$resultat = '';
41
		$resultat = '';
42
		$reponseHttp = new ReponseHttp();
42
		$reponseHttp = new ReponseHttp();
43
		try {
43
		try {
44
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
44
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
45
			$this->conteneur = new Conteneur($this->parametres);
45
			$this->conteneur = new Conteneur($this->parametres);
46
			$resultat = $this->traiterRessources();
46
			$resultat = $this->traiterRessources();
47
			$reponseHttp->setResultatService($resultat);
47
			$reponseHttp->setResultatService($resultat);
48
		} catch (Exception $e) {
48
		} catch (Exception $e) {
49
			$reponseHttp->ajouterErreur($e);
49
			$reponseHttp->ajouterErreur($e);
50
		}
50
		}
51
		$reponseHttp->emettreLesEntetes();
51
		$reponseHttp->emettreLesEntetes();
52
		$corps = $reponseHttp->getCorps();
52
		$corps = $reponseHttp->getCorps();
53
		return $corps;
53
		return $corps;
54
	}
54
	}
55
	
55
	
56
	public function ajouter($ressources, $parametres) {
56
	public function ajouter($ressources, $parametres) {
57
		$this->methode = 'ajouter';
57
		$this->methode = 'ajouter';
58
		$resultat = '';
58
		$resultat = '';
59
		$reponseHttp = new ReponseHttp();
59
		$reponseHttp = new ReponseHttp();
60
		
60
		
61
		try {
61
		try {
62
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
62
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
63
			$this->conteneur = new Conteneur($this->parametres);
63
			$this->conteneur = new Conteneur($this->parametres);
64
			$resultat = $this->traiterRessources();
64
			$resultat = $this->traiterRessources();
65
			$reponseHttp->setResultatService($resultat);
65
			$reponseHttp->setResultatService($resultat);
66
		} catch (Exception $e) {
66
		} catch (Exception $e) {
67
			$reponseHttp->ajouterErreur($e);
67
			$reponseHttp->ajouterErreur($e);
68
		}
68
		}
69
		$corps = $reponseHttp->getCorps();
69
		$corps = $reponseHttp->getCorps();
70
		return $corps;
70
		return $corps;
71
	}
71
	}
-
 
72
	
-
 
73
	public function supprimer($ressources) {
-
 
74
		$this->methode = 'supprimer';
-
 
75
		$resultat = '';
-
 
76
		$reponseHttp = new ReponseHttp();
-
 
77
		try {
-
 
78
			$this->ressources = $ressources;
-
 
79
			$this->conteneur = new Conteneur();
-
 
80
			$resultat = $this->traiterRessources();
-
 
81
			$reponseHttp->setResultatService($resultat);
-
 
82
		} catch (Exception $e) {
-
 
83
			$reponseHttp->ajouterErreur($e);
-
 
84
			$reponseHttp->emettreLesEntetes();
-
 
85
			echo $reponseHttp->getCorps();
-
 
86
		}
-
 
87
	}
72
	
88
	
73
	public function modifier($ressources, $requeteDonnees) {
89
	public function modifier($ressources, $requeteDonnees) {
74
		
90
		
75
	}
91
	}
76
 
92
 
77
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
93
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
78
		$this->ressources = $ressources;
94
		$this->ressources = $ressources;
79
		$this->parametres = $parametres;
95
		$this->parametres = $parametres;
80
	}
96
	}
81
 
97
 
82
	private function traiterRessources() {
98
	private function traiterRessources() {
83
		$retour = '';
99
		$retour = '';
84
		$this->initialiserProjet();
100
		$this->initialiserProjet();
85
		$retour = $this->initialiserService();
101
		$retour = $this->initialiserService();
86
		return $retour;
102
		return $retour;
87
	}
103
	}
88
	
104
	
89
 
105
 
90
	/*------------------------------------------------------------------------------------------------------------------
106
	/*------------------------------------------------------------------------------------------------------------------
91
										CONFIGURATION DU PROJET
107
										CONFIGURATION DU PROJET
92
	------------------------------------------------------------------------------------------------------------------*/
108
	------------------------------------------------------------------------------------------------------------------*/
93
	private function initialiserProjet() {
109
	private function initialiserProjet() {
94
		$this->chargerNomDuProjet();
110
		$this->chargerNomDuProjet();
95
		$this->chargerConfigProjet();
111
		$this->chargerConfigProjet();
96
 
112
 
97
	}
113
	}
98
 
114
 
99
	private function chargerNomDuProjet() {
115
	private function chargerNomDuProjet() {
100
		$this->projetNom = 'motscles';
116
		$this->projetNom = 'motscles';
101
	}
117
	}
102
 
118
 
103
	private function chargerConfigProjet() {
119
	private function chargerConfigProjet() {
104
		$projet = $this->projetNom;
120
		$projet = $this->projetNom;
105
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
121
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
106
		Config::charger($chemin);
122
		Config::charger($chemin);
107
	}
123
	}
108
 
124
 
109
	/*------------------------------------------------------------------------------------------------------------------
125
	/*------------------------------------------------------------------------------------------------------------------
110
								CONFIGURATION DU SERVICE
126
								CONFIGURATION DU SERVICE
111
	------------------------------------------------------------------------------------------------------------------*/
127
	------------------------------------------------------------------------------------------------------------------*/
112
	private function chargerNomService() {
128
	private function chargerNomService() {
113
		// si la méthode est POST, on ajouter un commentaire
129
		// si la méthode est POST, on ajouter un commentaire
114
		if ($this->methode == 'ajouter') {
130
		if ($this->methode == 'ajouter') {
115
			$this->serviceNom = 'ajouter-mot-cle';
131
			$this->serviceNom = 'ajouter-mot-cle';
116
		} else if ($this->methode == 'supprimer') {
132
		} else if ($this->methode == 'supprimer') {
117
			$this->serviceNom = 'supprimer-mot-cle';
133
			$this->serviceNom = 'supprimer-mot-cle';
118
		}
134
		}
119
		else {
135
		else {
120
			//S'il n'y a pas de ressources => tous les commentaires
136
			//S'il n'y a pas de ressources => tous les commentaires
121
			if (!isset($this->ressources) || empty($this->ressources)) {
137
			if (!isset($this->ressources) || empty($this->ressources)) {
122
				$this->serviceNom = 'liste-mots-cles';
138
				$this->serviceNom = 'liste-mots-cles';
123
			} else {
139
			} else {
124
				if (!is_numeric($this->ressources[0])) {
140
				if (!is_numeric($this->ressources[0])) {
125
					$message = "La première ressource doit être un identifiant";
141
					$message = "La première ressource doit être un identifiant";
126
					$code = RestServeur::HTTP_CODE_ERREUR;
142
					$code = RestServeur::HTTP_CODE_ERREUR;
127
					throw new Exception($message, $code);
143
					throw new Exception($message, $code);
128
				} else {
144
				} else {
129
					$this->serviceNom = 'consulter-mots-cles';
145
					$this->serviceNom = 'consulter-mots-cles';
130
				}
146
				}
131
			}			
147
			}			
132
		}
148
		}
133
	}
149
	}
134
	
150
	
135
	private function editerMessageErreurRessource() {
151
	private function editerMessageErreurRessource() {
136
		$message = "Le service demandé '".$this->projetNom.'/'.implode('/', $this->ressources).
152
		$message = "Le service demandé '".$this->projetNom.'/'.implode('/', $this->ressources).
137
					"' n'est pas disponible pour le projet ".$this->projetNom." !\n".
153
					"' n'est pas disponible pour le projet ".$this->projetNom." !\n".
138
					"Les services disponibles sont : images, images/#id/votes, images/#id/vote/#id_vote";
154
					"Les services disponibles sont : images, images/#id/votes, images/#id/vote/#id_vote";
139
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
155
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
140
		throw new Exception($message, $code);
156
		throw new Exception($message, $code);
141
	}
157
	}
142
	
158
	
143
	private function etreRessourceIdentifiant($num) {
159
	private function etreRessourceIdentifiant($num) {
144
		$presenceId = false;
160
		$presenceId = false;
145
		if (is_numeric($this->ressources[$num])) {
161
		if (is_numeric($this->ressources[$num])) {
146
			$presenceId = true;
162
			$presenceId = true;
147
		} else {
163
		} else {
148
			$message = "Le service demandé '$service' nécessite d'avoir un identifiant d'image valide";
164
			$message = "Le service demandé '$service' nécessite d'avoir un identifiant d'image valide";
149
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
165
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
150
			throw new Exception($message, $code);
166
			throw new Exception($message, $code);
151
		}
167
		}
152
		return $presenceId;
168
		return $presenceId;
153
	}
169
	}
154
 
170
 
155
	private function initialiserService() {
171
	private function initialiserService() {
156
		$this->chargerNomService();
172
		$this->chargerNomService();
157
		
173
		
158
		$classe = $this->obtenirNomClasseService($this->serviceNom);
174
		$classe = $this->obtenirNomClasseService($this->serviceNom);
159
		$chemins = array();
175
		$chemins = array();
160
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
176
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
161
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
177
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
162
		
178
		
163
		$retour = '';
179
		$retour = '';
164
		$service = null;
180
		$service = null;
165
		foreach ($chemins as $chemin) {
181
		foreach ($chemins as $chemin) {
166
			if (file_exists($chemin)) {
182
			if (file_exists($chemin)) {
167
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
183
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
168
				require_once $chemin;
184
				require_once $chemin;
169
				$service = new $classe($this->conteneur);
185
				$service = new $classe($this->conteneur);
170
				if ($this->methode == 'consulter') {
186
				if ($this->methode == 'consulter') {
171
					$retour = $service->consulter($this->ressources, $this->parametres);
187
					$retour = $service->consulter($this->ressources, $this->parametres);
172
				} elseif ($this->methode == 'ajouter') {
188
				} elseif ($this->methode == 'ajouter') {
173
					$retour = $service->ajouter($this->ressources, $this->parametres);
189
					$retour = $service->ajouter($this->ressources, $this->parametres);
174
				} elseif ($this->methode == 'modifier') {
190
				} elseif ($this->methode == 'modifier') {
175
					$retour = $service->modifier($this->ressources, $this->parametres);
191
					$retour = $service->modifier($this->ressources, $this->parametres);
-
 
192
				} elseif ($this->methode == 'supprimer') {
-
 
193
					$retour = $service->supprimer($this->ressources, $this->parametres);
176
				}
194
				}
177
			}
195
			}
178
		}
196
		}
179
		
197
		
180
		if (is_null($service)) {
198
		if (is_null($service)) {
181
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
199
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
182
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
200
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
183
			throw new Exception($message, $code);
201
			throw new Exception($message, $code);
184
		}
202
		}
185
		return $retour;
203
		return $retour;
186
	}
204
	}
187
 
205
 
188
	private function obtenirNomClasseService($mot) {
206
	private function obtenirNomClasseService($mot) {
189
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
207
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
190
		return $classeNom;
208
		return $classeNom;
191
	}
209
	}
192
}
210
}
193
?>
211
?>