Subversion Repositories eFlore/Applications.del

Rev

Rev 813 | Rev 1817 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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