Subversion Repositories eFlore/Applications.del

Rev

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

Rev 1158 Rev 1462
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
	
72
	
73
	public function supprimer($ressources) {
73
	public function supprimer($ressources) {
74
		$this->methode = 'supprimer';
74
		$this->methode = 'supprimer';
75
		$resultat = '';
75
		$resultat = '';
76
		$reponseHttp = new ReponseHttp();
76
		$reponseHttp = new ReponseHttp();
77
		try {
77
		try {
78
			$this->ressources = $ressources;
78
			$this->ressources = $ressources;
79
			$this->conteneur = new Conteneur();
79
			$this->conteneur = new Conteneur();
80
			$resultat = $this->traiterRessources();
80
			$resultat = $this->traiterRessources();
81
			$reponseHttp->setResultatService($resultat);
81
			$reponseHttp->setResultatService($resultat);
82
		} catch (Exception $e) {
82
		} catch (Exception $e) {
83
			$reponseHttp->ajouterErreur($e);
83
			$reponseHttp->ajouterErreur($e);
84
			$reponseHttp->emettreLesEntetes();
84
			$reponseHttp->emettreLesEntetes();
85
			echo $reponseHttp->getCorps();
85
			echo $reponseHttp->getCorps();
86
		}
86
		}
87
	}
87
	}
88
	
88
	
89
	public function modifier($ressources, $requeteDonnees) {
89
	public function modifier($ressources, $requeteDonnees) {
90
		
90
		
91
	}
91
	}
92
 
92
 
93
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
93
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
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
		$retour = '';
99
		$retour = '';
100
		$this->initialiserProjet();
100
		$this->initialiserProjet();
101
		$retour = $this->initialiserService();
101
		$retour = $this->initialiserService();
102
		return $retour;
102
		return $retour;
103
	}
103
	}
104
	
104
	
105
 
105
 
106
	/*------------------------------------------------------------------------------------------------------------------
106
	/*------------------------------------------------------------------------------------------------------------------
107
										CONFIGURATION DU PROJET
107
										CONFIGURATION DU PROJET
108
	------------------------------------------------------------------------------------------------------------------*/
108
	------------------------------------------------------------------------------------------------------------------*/
109
	private function initialiserProjet() {
109
	private function initialiserProjet() {
110
		$this->chargerNomDuProjet();
110
		$this->chargerNomDuProjet();
111
		$this->chargerConfigProjet();
111
		$this->chargerConfigProjet();
112
 
112
 
113
	}
113
	}
114
 
114
 
115
	private function chargerNomDuProjet() {
115
	private function chargerNomDuProjet() {
116
		$this->projetNom = 'motscles';
116
		$this->projetNom = 'motscles';
117
	}
117
	}
118
 
118
 
119
	private function chargerConfigProjet() {
119
	private function chargerConfigProjet() {
120
		$projet = $this->projetNom;
120
		$projet = $this->projetNom;
121
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
121
		$chemin = Config::get('chemin_configurations')."config_mots_cles.ini";
122
		Config::charger($chemin);
122
		Config::charger($chemin);
123
	}
123
	}
124
 
124
 
125
	/*------------------------------------------------------------------------------------------------------------------
125
	/*------------------------------------------------------------------------------------------------------------------
126
								CONFIGURATION DU SERVICE
126
								CONFIGURATION DU SERVICE
127
	------------------------------------------------------------------------------------------------------------------*/
127
	------------------------------------------------------------------------------------------------------------------*/
128
	private function chargerNomService() {
128
	private function chargerNomService() {
129
		// si la méthode est POST, on ajouter un commentaire
129
		// si la méthode est POST, on ajouter un commentaire
130
		if ($this->methode == 'ajouter') {
130
		if ($this->methode == 'ajouter') {
131
			$this->serviceNom = 'ajouter-mot-cle';
131
			$this->serviceNom = 'ajouter-mot-cle';
132
		} else if ($this->methode == 'supprimer') {
132
		} else if ($this->methode == 'supprimer') {
133
			$this->serviceNom = 'supprimer-mot-cle';
133
			$this->serviceNom = 'supprimer-mot-cle';
134
		}
134
		}
135
		else {
135
		else {
136
			//S'il n'y a pas de ressources => tous les commentaires
136
			//S'il n'y a pas de ressources => tous les commentaires
137
			if (!isset($this->ressources) || empty($this->ressources)) {
137
			if (!isset($this->ressources) || empty($this->ressources)) {
138
				$this->serviceNom = 'liste-mots-cles';
138
				$this->serviceNom = 'liste-mots-cles';
139
			} else {
139
			} else {
140
				if (!is_numeric($this->ressources[0])) {
140
				if (!is_numeric($this->ressources[0])) {
141
					$message = "La première ressource doit être un identifiant";
141
					$message = "La première ressource doit être un identifiant";
142
					$code = RestServeur::HTTP_CODE_ERREUR;
142
					$code = RestServeur::HTTP_CODE_ERREUR;
143
					throw new Exception($message, $code);
143
					throw new Exception($message, $code);
144
				} else {
144
				} else {
145
					$this->serviceNom = 'consulter-mots-cles';
145
					$this->serviceNom = 'consulter-mots-cles';
146
				}
146
				}
147
			}			
147
			}			
148
		}
148
		}
149
	}
149
	}
150
	
150
	
151
	private function editerMessageErreurRessource() {
151
	private function editerMessageErreurRessource() {
152
		$message = "Le service demandé '".$this->projetNom.'/'.implode('/', $this->ressources).
152
		$message = "Le service demandé '".$this->projetNom.'/'.implode('/', $this->ressources).
153
					"' n'est pas disponible pour le projet ".$this->projetNom." !\n".
153
					"' n'est pas disponible pour le projet ".$this->projetNom." !\n".
154
					"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";
155
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
155
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
156
		throw new Exception($message, $code);
156
		throw new Exception($message, $code);
157
	}
157
	}
158
	
158
	
159
	private function etreRessourceIdentifiant($num) {
159
	private function etreRessourceIdentifiant($num) {
160
		$presenceId = false;
160
		$presenceId = false;
161
		if (is_numeric($this->ressources[$num])) {
161
		if (is_numeric($this->ressources[$num])) {
162
			$presenceId = true;
162
			$presenceId = true;
163
		} else {
163
		} else {
164
			$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";
165
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
165
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
166
			throw new Exception($message, $code);
166
			throw new Exception($message, $code);
167
		}
167
		}
168
		return $presenceId;
168
		return $presenceId;
169
	}
169
	}
170
 
170
 
171
	private function initialiserService() {
171
	private function initialiserService() {
172
		$this->chargerNomService();
172
		$this->chargerNomService();
173
		
173
		
174
		$classe = $this->obtenirNomClasseService($this->serviceNom);
174
		$classe = $this->obtenirNomClasseService($this->serviceNom);
175
		$chemins = array();
175
		$chemins = array();
176
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
176
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
177
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
177
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
178
		
178
		
179
		$retour = '';
179
		$retour = '';
180
		$service = null;
180
		$service = null;
181
		foreach ($chemins as $chemin) {
181
		foreach ($chemins as $chemin) {
182
			if (file_exists($chemin)) {
182
			if (file_exists($chemin)) {
183
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
183
				$this->conteneur->chargerConfiguration('config_mots_cles.ini');
184
				require_once $chemin;
184
				require_once $chemin;
185
				$service = new $classe($this->conteneur);
185
				$service = new $classe($this->conteneur);
186
				if ($this->methode == 'consulter') {
186
				if ($this->methode == 'consulter') {
187
					$retour = $service->consulter($this->ressources, $this->parametres);
187
					$retour = $service->consulter($this->ressources, $this->parametres);
188
				} elseif ($this->methode == 'ajouter') {
188
				} elseif ($this->methode == 'ajouter') {
189
					$retour = $service->ajouter($this->ressources, $this->parametres);
189
					$retour = $service->ajouter($this->ressources, $this->parametres);
190
				} elseif ($this->methode == 'modifier') {
190
				} elseif ($this->methode == 'modifier') {
191
					$retour = $service->modifier($this->ressources, $this->parametres);
191
					$retour = $service->modifier($this->ressources, $this->parametres);
192
				} elseif ($this->methode == 'supprimer') {
192
				} elseif ($this->methode == 'supprimer') {
193
					$retour = $service->supprimer($this->ressources, $this->parametres);
193
					$retour = $service->supprimer($this->ressources, $this->parametres);
194
				}
194
				}
195
			}
195
			}
196
		}
196
		}
197
		
197
		
198
		if (is_null($service)) {
198
		if (is_null($service)) {
199
			$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} !";
200
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
200
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
201
			throw new Exception($message, $code);
201
			throw new Exception($message, $code);
202
		}
202
		}
203
		return $retour;
203
		return $retour;
204
	}
204
	}
205
 
205
 
206
	private function obtenirNomClasseService($mot) {
206
	private function obtenirNomClasseService($mot) {
207
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
207
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
208
		return $classeNom;
208
		return $classeNom;
209
	}
209
	}
210
}
210
}
211
?>
211
?>