Subversion Repositories eFlore/Applications.del

Rev

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

Rev 813 Rev 1795
1
<?php
1
<?php
2
/**
2
/**
3
* Description :
-
 
4
* Classe principale de chargement des services d'eFlore.
3
 * Classe principale de chargement des sous-services de Protocoles.
5
*
4
 *
6
* Encodage en entrée : utf8
5
 * @category   DEL
7
* Encodage en sortie : utf8
6
 * @package    Services
8
* @package eflore-projets
7
 * @subpackage Protocoles
-
 
8
 * @version    0.1
9
* @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
9
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
10
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
10
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
 * @author     Aurelien PERONNET <aurelien@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
-
 
15
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
14
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
16
*/
15
 */
17
class Protocoles extends RestService {
16
class Protocoles extends RestService {
18
 
17
 
19
	
18
 
20
	/*
19
	/*
21
	* url possibles :
20
	* url possibles :
22
	* http://localhost/del/services/0.1/observations/ => toutes les observations (infos obs, infos images, infos propositions, infos nb commentaires)
21
	* http://localhost/del/services/0.1/observations/ => toutes les observations (infos obs, infos images, infos propositions, infos nb commentaires)
23
	* http://localhost/del/services/0.1/observations/#id => une observation donnée et ses images, SANS LES propositions & nombre de commentaire
22
	* http://localhost/del/services/0.1/observations/#id => une observation donnée et ses images, SANS LES propositions & nombre de commentaire
24
	* */
23
	* */
25
	
24
 
26
	private $parametres = array();
25
	private $parametres = array();
27
	private $ressources = array();
26
	private $ressources = array();
28
	private $methode = null;
27
	private $methode = null;
29
	private $projetNom = array();
28
	private $projetNom = array();
30
	private $serviceNom = array();
29
	private $serviceNom = array();
31
	private $cheminCourant = null;
30
	private $cheminCourant = null;
32
 
31
 
33
	private $conteneur;
32
	private $conteneur;
34
	
33
 
35
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
34
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
36
	protected $utilisationParametresBruts = true;
35
	protected $utilisationParametresBruts = true;
37
 
36
 
38
	public function __construct() {
37
	public function __construct() {
39
		$this->cheminCourant = dirname(__FILE__).DS;
38
		$this->cheminCourant = dirname(__FILE__).DS;
40
	}
39
	}
41
 
40
 
42
	public function consulter($ressources, $parametres) {
41
	public function consulter($ressources, $parametres) {
43
		$this->methode = 'consulter';
42
		$this->methode = 'consulter';
44
		$resultat = '';
43
		$resultat = '';
45
		$reponseHttp = new ReponseHttp();
44
		$reponseHttp = new ReponseHttp();
46
		try {
45
		try {
47
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
46
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
48
			$this->conteneur = new Conteneur($this->parametres);
47
			$this->conteneur = new Conteneur($this->parametres);
49
			$resultat = $this->traiterRessources();
48
			$resultat = $this->traiterRessources();
50
			$reponseHttp->setResultatService($resultat);
49
			$reponseHttp->setResultatService($resultat);
51
		} catch (Exception $e) {
50
		} catch (Exception $e) {
52
			$reponseHttp->ajouterErreur($e);
51
			$reponseHttp->ajouterErreur($e);
53
		}
52
		}
54
		$reponseHttp->emettreLesEntetes();
53
		$reponseHttp->emettreLesEntetes();
55
		$corps = $reponseHttp->getCorps();
54
		$corps = $reponseHttp->getCorps();
56
		return $corps;
55
		return $corps;
57
	}
56
	}
58
	
57
 
59
	public function ajouter($ressources, $requeteDonnees) {
58
	public function ajouter($ressources, $requeteDonnees) {
60
		$this->methode = 'ajouter';
59
		$this->methode = 'ajouter';
61
		$resultat = '';
60
		$resultat = '';
62
		try {
61
		try {
63
			$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
62
			$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
64
			$this->conteneur = new Conteneur($this->parametres);
63
			$this->conteneur = new Conteneur($this->parametres);
65
			$resultat = $this->traiterRessources();
64
			$resultat = $this->traiterRessources();
66
		} catch (Exception $e) {
65
		} catch (Exception $e) {
67
			$reponseHttp->ajouterErreur($e);
66
			$reponseHttp->ajouterErreur($e);
68
		}
67
		}
69
	}
68
	}
70
 
69
 
71
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
70
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
72
		$this->ressources = $ressources;
71
		$this->ressources = $ressources;
73
		$this->parametres = $parametres;
72
		$this->parametres = $parametres;
74
	}
73
	}
75
 
74
 
76
	private function traiterRessources() {
75
	private function traiterRessources() {
77
		$retour = '';
76
		$retour = '';
78
		$this->initialiserProjet();
77
		$this->initialiserProjet();
79
		if ($this->avoirRessourceService()) {
78
		if ($this->avoirRessourceService()) {
80
			$retour = $this->initialiserService();
79
			$retour = $this->initialiserService();
81
		}
80
		}
82
		return $retour;
81
		return $retour;
83
	}
82
	}
84
	
83
 
85
	private function avoirRessourceIdentifiant($num) {
84
	private function avoirRessourceIdentifiant($num) {
86
		$presenceId = false;
85
		$presenceId = false;
87
			if (is_numeric($this->ressources[$num])) {
86
			if (is_numeric($this->ressources[$num])) {
88
				$presenceId = true;
87
				$presenceId = true;
89
			} else {
88
			} else {
90
				$message = "Le service demandé '$service' nécessite d'avoir un identifiant d'image valide";
89
				$message = "Le service demandé '$service' nécessite d'avoir un identifiant d'image valide";
91
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
90
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
92
				throw new Exception($message, $code);
91
				throw new Exception($message, $code);
93
			}
92
			}
94
		return $presenceId;
93
		return $presenceId;
95
	}
94
	}
96
	/*------------------------------------------------------------------------------------------------------------------
95
	/*------------------------------------------------------------------------------------------------------------------
97
										CONFIGURATION DU PROJET
96
										CONFIGURATION DU PROJET
98
	------------------------------------------------------------------------------------------------------------------*/
97
	------------------------------------------------------------------------------------------------------------------*/
99
	private function initialiserProjet() {
98
	private function initialiserProjet() {
100
		$this->chargerNomDuProjet();
99
		$this->chargerNomDuProjet();
101
		$this->chargerConfigProjet();
100
		$this->chargerConfigProjet();
102
 
101
 
103
	}
102
	}
104
 
103
 
105
	private function chargerNomDuProjet() {
104
	private function chargerNomDuProjet() {
106
		$this->projetNom = 'protocoles';
105
		$this->projetNom = 'protocoles';
107
	}
106
	}
108
 
107
 
109
	private function chargerConfigProjet() {
108
	private function chargerConfigProjet() {
110
		$projet = $this->projetNom;
109
		$projet = $this->projetNom;
111
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
110
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
112
		Config::charger($chemin);
111
		Config::charger($chemin);
113
	}
112
	}
114
 
113
 
115
	/*------------------------------------------------------------------------------------------------------------------
114
	/*------------------------------------------------------------------------------------------------------------------
116
								CONFIGURATION DU SERVICE
115
								CONFIGURATION DU SERVICE
117
	------------------------------------------------------------------------------------------------------------------*/
116
	------------------------------------------------------------------------------------------------------------------*/
118
	private function avoirRessourceService() {
117
	private function avoirRessourceService() {
119
		/*
118
		/*
120
		* url possibles :
119
		* url possibles :
121
		* http://localhost/del/services/0.1/observations/ => toutes les observations (infos obs, infos images, infos propositions, infos nb commentaires)
120
		* http://localhost/del/services/0.1/observations/ => toutes les observations (infos obs, infos images, infos propositions, infos nb commentaires)
122
		* http://localhost/del/services/0.1/observations/#id => une observation donnée et ses images, SANS LES propositions & nombre de commentaire
121
		* http://localhost/del/services/0.1/observations/#id => une observation donnée et ses images, SANS LES propositions & nombre de commentaire
123
		* */
122
		* */
124
		$presenceRessourceService = false;
123
		$presenceRessourceService = false;
125
		if (isset($this->ressources[0])) {
124
		if (isset($this->ressources[0])) {
126
			if ($this->avoirRessourceIdentifiant(0)) {
125
			if ($this->avoirRessourceIdentifiant(0)) {
127
				if (sizeof($this->ressources) == 1) {
126
				if (sizeof($this->ressources) == 1) {
128
					$presenceRessourceService = true;
127
					$presenceRessourceService = true;
129
					$this->serviceNom = 'observation';
128
					$this->serviceNom = 'observation';
130
				} else {
129
				} else {
131
					if (isset($this->ressources[1])) {
130
					if (isset($this->ressources[1])) {
132
						$presenceRessourceService = $this->avoirRessourceSousService();
131
						$presenceRessourceService = $this->avoirRessourceSousService();
133
					}
132
					}
134
				} 
133
				}
135
			} 
134
			}
136
		} else {
135
		} else {
137
			$presenceRessourceService = true;
136
			$presenceRessourceService = true;
138
			$this->serviceNom = 'liste-protocoles';
137
			$this->serviceNom = 'liste-protocoles';
139
		}
138
		}
140
		return $presenceRessourceService;
139
		return $presenceRessourceService;
141
	}
140
	}
142
	
141
 
143
	private function avoirRessourceSousService() {
142
	private function avoirRessourceSousService() {
144
		$presenceRessourceService = false;
143
		$presenceRessourceService = false;
145
		$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
144
		$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
146
		if ($this->avoirRessourceIdentifiant(1)) {
145
		if ($this->avoirRessourceIdentifiant(1)) {
147
			$service = $this->ressources[2];
146
			$service = $this->ressources[2];
148
			if (in_array($service, $servicesDispo)) {
147
			if (in_array($service, $servicesDispo)) {
149
				$presenceRessourceService = true;
148
				$presenceRessourceService = true;
150
				$this->serviceNom = 'vote-observation';
149
				$this->serviceNom = 'vote-observation';
151
			} else {
150
			} else {
152
				$message = "Le service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
151
				$message = "Le service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
153
							"Les services disponibles sont : ".implode(', ', $servicesDispo);
152
							"Les services disponibles sont : ".implode(', ', $servicesDispo);
154
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
153
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
155
				throw new Exception($message, $code);
154
				throw new Exception($message, $code);
156
			}
155
			}
157
		}
156
		}
158
		return $presenceRessourceService;
157
		return $presenceRessourceService;
159
	}
158
	}
160
 
159
 
161
	private function initialiserService() {
160
	private function initialiserService() {
162
		//$this->chargerNomDuService();
161
		//$this->chargerNomDuService();
163
		
162
 
164
		$classe = $this->obtenirNomClasseService($this->serviceNom);
163
		$classe = $this->obtenirNomClasseService($this->serviceNom);
165
		$chemins = array();
164
		$chemins = array();
166
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
165
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
167
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
166
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
168
		$retour = '';
167
		$retour = '';
169
		$service = null;
168
		$service = null;
170
		foreach ($chemins as $chemin) {
169
		foreach ($chemins as $chemin) {
171
			if (file_exists($chemin)) {
170
			if (file_exists($chemin)) {
172
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
171
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
173
				require_once $chemin;
172
				require_once $chemin;
174
				$service = new $classe($this->conteneur);
173
				$service = new $classe($this->conteneur);
175
				if ($this->methode == 'consulter') {
174
				if ($this->methode == 'consulter') {
176
					$retour = $service->consulter($this->ressources, $this->parametres);
175
					$retour = $service->consulter($this->ressources, $this->parametres);
177
				} elseif ($this->methode == 'ajouter') {
176
				} elseif ($this->methode == 'ajouter') {
178
					$retour = $service->ajouter($this->ressources, $this->parametres);
177
					$retour = $service->ajouter($this->ressources, $this->parametres);
179
				}
178
				}
180
			}
179
			}
181
		}
180
		}
182
		
181
 
183
		if (is_null($service)) {
182
		if (is_null($service)) {
184
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
183
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
185
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
184
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
186
			throw new Exception($message, $code);
185
			throw new Exception($message, $code);
187
		}
186
		}
188
		return $retour;
187
		return $retour;
189
	}
188
	}
190
 
189
 
191
	private function obtenirNomClasseService($mot) {
190
	private function obtenirNomClasseService($mot) {
192
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
191
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
193
		return $classeNom;
192
		return $classeNom;
194
	}
193
	}
195
}
194
}
196
?>
195
?>