Subversion Repositories eFlore/Applications.del

Rev

Rev 787 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
755 gduche 1
<?php
2
/**
3
* Description :
4
* Classe principale de chargement des services d'eFlore.
5
*
6
* Encodage en entrée : utf8
7
* Encodage en sortie : utf8
8
* @package eflore-projets
9
* @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
10
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
11
* @author Jean-Pascal MILCENT <jpm@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
* @version 0.1
15
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
16
*/
17
class Observations extends RestService {
18
 
19
 
20
	/*
21
	* url possibles :
22
	* 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
24
	* */
25
 
26
	private $parametres = array();
27
	private $ressources = array();
28
	private $projetNom = array();
29
	private $serviceNom = array();
30
	private $cheminCourant = null;
31
 
32
	private $conteneur;
33
 
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
 
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
	}
57
 
58
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
59
		$this->ressources = $ressources;
60
		$this->parametres = $parametres;
61
	}
62
 
63
	private function traiterRessources() {
64
		$retour = '';
65
		$this->initialiserProjet();
66
		if ($this->avoirRessourceService()) {
67
			$retour = $this->initialiserService();
68
		}
69
		return $retour;
70
	}
71
 
72
	private function avoirRessourceIdentifiant() {
73
		$presenceId = false;
74
			if (is_numeric($this->ressources[0])) {
75
				$presenceId = true;
76
			} else {
77
				$message = "Le service demandé '$service' nécessite d'avoir un identifiant d'image valide";
78
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
79
				throw new Exception($message, $code);
80
			}
81
		return $presenceId;
82
	}
83
	/*------------------------------------------------------------------------------------------------------------------
84
										CONFIGURATION DU PROJET
85
	------------------------------------------------------------------------------------------------------------------*/
86
	private function initialiserProjet() {
87
		$this->chargerNomDuProjet();
88
		$this->chargerConfigProjet();
89
 
90
	}
91
 
92
	private function chargerNomDuProjet() {
93
		$this->projetNom = 'observations';
94
	}
95
 
96
	private function chargerConfigProjet() {
97
		$projet = $this->projetNom;
98
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
99
		Config::charger($chemin);
100
	}
101
 
102
	/*------------------------------------------------------------------------------------------------------------------
103
								CONFIGURATION DU SERVICE
104
	------------------------------------------------------------------------------------------------------------------*/
105
	private function avoirRessourceService() {
106
		/*
107
		* url possibles :
108
		* http://localhost/del/services/0.1/observations/ => toutes les observations (infos obs, infos images, infos propositions, infos nb commentaires)
109
		* http://localhost/del/services/0.1/observations/#id => une observation donnée et ses images, SANS LES propositions & nombre de commentaire
110
		* */
111
		$presenceRessourceService = false;
112
		if (isset($this->ressources[0])) {
113
			if ($this->avoirRessourceIdentifiant()) {
114
				if (sizeof($this->ressources) == 1) {
115
					$presenceRessourceService = true;
116
					$this->serviceNom = 'observation';
117
				} else {
118
					if (isset($this->ressources[1])) {
119
						$presenceRessourceService = $this->avoirRessourceSousService();
120
					}
121
				}
122
			}
123
		} else {
124
			$presenceRessourceService = true;
125
			$this->serviceNom = 'liste-observations';
126
		}
127
		return $presenceRessourceService;
128
	}
129
 
130
	private function avoirRessourceSousService() {
131
		$presenceRessourceService = false;
132
		$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
133
		$service = $this->ressources[1];
134
		if (in_array($service, $servicesDispo)) {
135
			$presenceRessourceService = true;
136
			$this->serviceNom = 'votes-image';
137
		} else {
138
			$message = "Le service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
139
						"Les services disponibles sont : ".implode(', ', $servicesDispo);
140
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
141
			throw new Exception($message, $code);
142
		}
143
		return $presenceRessourceService;
144
	}
145
 
146
	private function initialiserService() {
147
		//$this->chargerNomDuService();
148
 
149
		$classe = $this->obtenirNomClasseService($this->serviceNom);
150
		$chemins = array();
151
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
152
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
153
		$retour = '';
154
		$service = null;
155
		foreach ($chemins as $chemin) {
156
			if (file_exists($chemin)) {
157
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
158
				require_once $chemin;
159
				$service = new $classe($this->conteneur);
160
				$retour = $service->consulter($this->ressources, $this->parametres);
161
			}
162
		}
163
 
164
		if (is_null($service)) {
165
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
166
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
167
			throw new Exception($message, $code);
168
		}
169
		return $retour;
170
	}
171
 
172
	private function obtenirNomClasseService($mot) {
173
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
174
		return $classeNom;
175
	}
176
 
177
 
178
}
179
?>