Subversion Repositories eFlore/Applications.del

Rev

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

Rev 864 Rev 883
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 eflore-projets
8
* @package eflore-projets
9
* @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
9
* @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
10
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
10
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
11
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
12
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
12
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
13
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
13
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
14
* @version 0.1
14
* @version 0.1
15
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
15
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
16
*/
16
*/
17
class Commentaires extends RestService {
17
class Commentaires extends RestService {
18
 
18
 
19
	
19
	
20
	private $parametres = array();
20
	private $parametres = array();
21
	private $ressources = array();
21
	private $ressources = array();
22
	private $methode = null;
22
	private $methode = null;
23
	private $projetNom = array();
23
	private $projetNom = array();
24
	private $serviceNom = array();
24
	private $serviceNom = array();
25
	private $cheminCourant = null;
25
	private $cheminCourant = null;
26
 
26
 
27
	private $conteneur;
27
	private $conteneur;
28
	
28
	
29
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
29
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
30
	protected $utilisationParametresBruts = true;
30
	protected $utilisationParametresBruts = true;
31
 
31
 
32
	public function __construct() {
32
	public function __construct() {
33
		$this->cheminCourant = dirname(__FILE__).DS;
33
		$this->cheminCourant = dirname(__FILE__).DS;
34
	}
34
	}
35
 
35
 
36
	public function consulter($ressources, $parametres) {
36
	public function consulter($ressources, $parametres) {
37
		$this->methode = 'consulter';
37
		$this->methode = 'consulter';
38
		$resultat = '';
38
		$resultat = '';
39
		$reponseHttp = new ReponseHttp();
39
		$reponseHttp = new ReponseHttp();
40
		try {
40
		try {
41
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
41
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
42
			$this->conteneur = new Conteneur($this->parametres);
42
			$this->conteneur = new Conteneur($this->parametres);
43
			$resultat = $this->traiterRessources();
43
			$resultat = $this->traiterRessources();
44
			$reponseHttp->setResultatService($resultat);
44
			$reponseHttp->setResultatService($resultat);
45
		} catch (Exception $e) {
45
		} catch (Exception $e) {
46
			$reponseHttp->ajouterErreur($e);
46
			$reponseHttp->ajouterErreur($e);
47
		}
47
		}
48
		$reponseHttp->emettreLesEntetes();
48
		$reponseHttp->emettreLesEntetes();
49
		$corps = $reponseHttp->getCorps();
49
		$corps = $reponseHttp->getCorps();
50
		return $corps;
50
		return $corps;
51
	}
51
	}
52
	
52
	
53
	
53
	
54
	public function ajouter($ressources, $parametres) {
54
	public function ajouter($ressources, $parametres) {
55
		$this->methode = 'ajouter';
55
		$this->methode = 'ajouter';
56
		$resultat = '';
56
		$resultat = '';
57
		$reponseHttp = new ReponseHttp();
57
		$reponseHttp = new ReponseHttp();
-
 
58
		
58
		try {
59
		try {
59
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
60
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
60
			$this->conteneur = new Conteneur($this->parametres);
61
			$this->conteneur = new Conteneur($this->parametres);
61
			$resultat = $this->traiterRessources();
62
			$resultat = $this->traiterRessources();
62
			$reponseHttp->setResultatService($resultat);
63
			$reponseHttp->setResultatService($resultat);
63
		} catch (Exception $e) {
64
		} catch (Exception $e) {
64
			$reponseHttp->ajouterErreur($e);
65
			$reponseHttp->ajouterErreur($e);
65
		}
66
		}
66
		$reponseHttp->emettreLesEntetes();
-
 
67
		$corps = $reponseHttp->getCorps();
67
		$corps = $reponseHttp->getCorps();
68
		return $corps;
68
		return $corps;
69
	}
69
	}
70
	
70
	
71
	public function supprimer($ressources) {
71
	public function supprimer($ressources) {
72
		$this->methode = 'supprimer';
72
		$this->methode = 'supprimer';
73
		$resultat = '';
73
		$resultat = '';
74
		$reponseHttp = new ReponseHttp();
74
		$reponseHttp = new ReponseHttp();
75
		try {
75
		try {
76
			$this->ressources = $ressources;
76
			$this->ressources = $ressources;
77
			$this->conteneur = new Conteneur();
77
			$this->conteneur = new Conteneur();
78
			$resultat = $this->traiterRessources();
78
			$resultat = $this->traiterRessources();
79
			$reponseHttp->setResultatService($resultat);
79
			$reponseHttp->setResultatService($resultat);
80
		} catch (Exception $e) {
80
		} catch (Exception $e) {
81
			$reponseHttp->ajouterErreur($e);
81
			$reponseHttp->ajouterErreur($e);
82
		}
82
		}
83
		$reponseHttp->emettreLesEntetes();
83
		$reponseHttp->emettreLesEntetes();
84
		$corps = $reponseHttp->getCorps();
84
		$corps = $reponseHttp->getCorps();
85
		return $corps;
85
		return $corps;
86
	}
86
	}
87
	
87
	
88
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
88
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
89
		$this->ressources = $ressources;
89
		$this->ressources = $ressources;
90
		$this->parametres = $parametres;
90
		$this->parametres = $parametres;
91
	}
91
	}
92
 
92
 
93
	private function traiterRessources() {
93
	private function traiterRessources() {
94
		$retour = '';
94
		$retour = '';
95
		$this->initialiserProjet();
95
		$this->initialiserProjet();
96
		$retour = $this->initialiserService();
96
		$retour = $this->initialiserService();
97
		return $retour;
97
		return $retour;
98
	}
98
	}
99
	
99
	
100
 
100
 
101
	/*------------------------------------------------------------------------------------------------------------------
101
	/*------------------------------------------------------------------------------------------------------------------
102
										CONFIGURATION DU PROJET
102
										CONFIGURATION DU PROJET
103
	------------------------------------------------------------------------------------------------------------------*/
103
	------------------------------------------------------------------------------------------------------------------*/
104
	private function initialiserProjet() {
104
	private function initialiserProjet() {
105
		$this->projetNom = 'commentaires';
105
		$this->projetNom = 'commentaires';
106
		$this->chargerConfigProjet();
106
		$this->chargerConfigProjet();
107
	}
107
	}
108
 
108
 
109
	private function chargerConfigProjet() {
109
	private function chargerConfigProjet() {
110
		$projet = $this->projetNom;
110
		$projet = $this->projetNom;
111
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
111
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
112
		Config::charger($chemin);
112
		Config::charger($chemin);
113
	}
113
	}
114
 
114
 
115
	/*------------------------------------------------------------------------------------------------------------------
115
	/*------------------------------------------------------------------------------------------------------------------
116
								CONFIGURATION DU SERVICE
116
								CONFIGURATION DU SERVICE
117
	------------------------------------------------------------------------------------------------------------------*/
117
	------------------------------------------------------------------------------------------------------------------*/
118
	private function initialiserService() {
118
	private function initialiserService() {
119
		$this->chargerNomService();
119
		$this->chargerNomService();
120
				
120
				
121
		$classe = $this->obtenirNomClasseService($this->serviceNom);
121
		$classe = $this->obtenirNomClasseService($this->serviceNom);
122
		$chemins = array();
122
		$chemins = array();
123
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
123
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
124
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
124
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
125
		$retour = '';
125
		$retour = '';
126
		$service = null;
126
		$service = null;
127
		foreach ($chemins as $chemin) {
127
		foreach ($chemins as $chemin) {
128
			if (file_exists($chemin)) {
128
			if (file_exists($chemin)) {
129
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
129
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
130
				
130
				
131
				require_once $chemin;
131
				require_once $chemin;
132
				$service = new $classe($this->conteneur);
132
				$service = new $classe($this->conteneur);
133
				if ($this->methode == 'consulter') {
133
				if ($this->methode == 'consulter') {
134
					$retour = $service->consulter($this->ressources, $this->parametres);
134
					$retour = $service->consulter($this->ressources, $this->parametres);
135
				} elseif ($this->methode == 'ajouter') {
135
				} elseif ($this->methode == 'ajouter') {
136
					$retour = $service->ajouter($this->ressources, $this->parametres);				
136
					$retour = $service->ajouter($this->ressources, $this->parametres);			
137
				} elseif ($this->methode == 'supprimer') {
137
				} elseif ($this->methode == 'supprimer') {
138
					$retour = $service->supprimer($this->ressources, $this->parametres);
138
					$retour = $service->supprimer($this->ressources, $this->parametres);
139
				}
139
				}
140
			}
140
			}
141
		}
141
		}
142
		
142
		
143
		if (is_null($service)) {
143
		if (is_null($service)) {
144
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
144
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
145
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
145
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
146
			throw new Exception($message, $code);
146
			throw new Exception($message, $code);
147
		}
147
		}
148
		return $retour;
148
		return $retour;
149
	}
149
	}
150
	
150
	
151
	private function chargerNomService() {
151
	private function chargerNomService() {
152
		// si la méthode est POST, on ajouter un commentaire
152
		// si la méthode est POST, on ajouter un commentaire
153
		if ($this->methode == 'ajouter') {
153
		if ($this->methode == 'ajouter') {
154
			$this->serviceNom = 'ajouter-commentaire';
154
			$this->serviceNom = 'ajouter-commentaire';
155
		} else if ($this->methode == 'supprimer') {
155
		} else if ($this->methode == 'supprimer') {
156
			$this->serviceNom = 'supprimer-commentaire';
156
			$this->serviceNom = 'supprimer-commentaire';
157
		}
157
		}
158
		else {
158
		else {
159
			//S'il n'y a pas de ressources => tous les commentaires
159
			//S'il n'y a pas de ressources => tous les commentaires
160
			if (!isset($this->ressources) || empty($this->ressources)) {
160
			if (!isset($this->ressources) || empty($this->ressources)) {
161
				$this->serviceNom = 'liste-commentaires';
161
				$this->serviceNom = 'liste-commentaires';
162
			} else {
162
			} else {
163
				if (!is_numeric($this->ressources[0])) {
163
				if (!is_numeric($this->ressources[0])) {
164
					$message = "La première ressource doit être un identifiant";
164
					$message = "La première ressource doit être un identifiant";
165
					$code = RestServeur::HTTP_CODE_ERREUR;
165
					$code = RestServeur::HTTP_CODE_ERREUR;
166
					throw new Exception($message, $code);
166
					throw new Exception($message, $code);
167
				} else {
167
				} else {
168
					$this->serviceNom = 'consulter-commentaire';
168
					$this->serviceNom = 'consulter-commentaire';
169
				}
169
				}
170
			}			
170
			}			
171
		}
171
		}
172
	}
172
	}
173
 
173
 
174
	private function obtenirNomClasseService($mot) {
174
	private function obtenirNomClasseService($mot) {
175
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
175
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
176
		return $classeNom;
176
		return $classeNom;
177
	}
177
	}
178
 
-
 
179
 
-
 
180
}
178
}
181
?>
179
?>