Subversion Repositories eFlore/Applications.del

Rev

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

Rev Author Line No. Line
1144 aurelien 1
<?php
2
/**
1700 jpm 3
 * Classe principale de chargement des services concernant les déterminations dans DEL.
4
 *
5
 * Encodage en entrée : utf8
6
 * Encodage en sortie : utf8
7
 * @package eflore-projets
8
 * @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
9
 * @author Delphine CAUQUIL <delphine@tela-botanica.org>
10
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
12
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
13
 * @version 0.1
14
 * @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
15
 */
1144 aurelien 16
class Determinations extends RestService {
17
 
18
	private $parametres = array();
19
	private $ressources = array();
20
	private $methode = null;
1700 jpm 21
	private $serviceNom = 'determinations';
22
	private $sousServiceNom = null;
1144 aurelien 23
	private $cheminCourant = null;
24
 
25
	private $conteneur;
1700 jpm 26
 
1144 aurelien 27
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
28
	protected $utilisationParametresBruts = true;
29
 
30
	public function __construct() {
31
		$this->cheminCourant = dirname(__FILE__).DS;
32
	}
33
 
34
	public function consulter($ressources, $parametres) {
35
		$this->methode = 'consulter';
1700 jpm 36
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
37
		return $this->executerService();
38
	}
39
 
40
	public function modifier($ressources, $requeteDonnees) {
41
		$this->methode = 'modifier';
42
		$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
43
		return $this->executerService();
44
	}
45
 
46
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
47
		$this->ressources = $ressources;
48
		$this->parametres = $parametres;
49
	}
50
 
51
	private function executerService() {
1144 aurelien 52
		$resultat = '';
53
		$reponseHttp = new ReponseHttp();
54
		try {
55
			$this->conteneur = new Conteneur($this->parametres);
56
			$resultat = $this->traiterRessources();
57
			$reponseHttp->setResultatService($resultat);
58
		} catch (Exception $e) {
59
			$reponseHttp->ajouterErreur($e);
60
		}
61
		$reponseHttp->emettreLesEntetes();
62
		$corps = $reponseHttp->getCorps();
63
		return $corps;
64
	}
65
 
66
	private function traiterRessources() {
1700 jpm 67
		$this->chargerConfigService();
68
		$this->analyserRessources();
69
		$retour = $this->initialiserService();
1144 aurelien 70
		return $retour;
71
	}
72
 
1700 jpm 73
	private function chargerConfigService() {
74
		$chemin = Config::get('chemin_configurations')."config_{$this->serviceNom}.ini";
1144 aurelien 75
		Config::charger($chemin);
76
	}
77
 
1700 jpm 78
	/**
79
	 * URLs possibles :
80
	 *
81
	 * GET :
82
	 * http://localhost/del/services/0.1/determinations/images-determinations-probables =>
83
	 *
84
	 *
85
	 * PUT :
86
	 *
87
	 *
88
	 * POST :
89
	 * http://localhost/del/services/0.1/determinations/valider-determination/#idProposition => Permet d'accepter une proposition donnée
90
	 *
91
	 * DELETE :
92
	 *
93
	 */
94
	private function analyserRessources() {
95
		if ($this->methode == 'consulter') {
96
			if ($this->ressources[0] == 'images-determinations-probables') {
97
				$this->sousServiceNom = 'liste-images-determinations-probables';
1144 aurelien 98
			}
1700 jpm 99
		} else if ($this->methode == 'modifier') {
100
			if ($this->ressources[0] == 'valider-determination') {
101
				$this->sousServiceNom = 'valider-determination';
102
			}
1144 aurelien 103
		}
1700 jpm 104
 
105
		if ($this->sousServiceNom == null) {
106
			$this->lancerMessageErreurRessource();
107
		}
1144 aurelien 108
	}
1700 jpm 109
 
110
	private function lancerMessageErreurRessource() {
111
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
112
		$message = "La ressource demandée '$ressource' ".
113
			"n'est pas disponible pour le service ".$this->serviceNom." !\n".
114
			"Les URLs disponibles sont : \n".
115
			" - en GET : determinations/images-determinations-probables \n".
116
			" - en POST : determinations/valider-determination";
1144 aurelien 117
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
118
		throw new Exception($message, $code);
119
	}
120
 
121
	private function initialiserService() {
1700 jpm 122
		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
123
		//echo $this->sousServiceNom.':'.$classe."\n";
124
		//echo 'Ressources :'.print_r($this->ressources, true);
125
		//echo 'Parametres :'.print_r($this->parametres, true);
1144 aurelien 126
		$chemins = array();
1700 jpm 127
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
1144 aurelien 128
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
129
		$retour = '';
130
		$service = null;
131
		foreach ($chemins as $chemin) {
132
			if (file_exists($chemin)) {
1700 jpm 133
				$this->conteneur->chargerConfiguration('config_'.$this->serviceNom.'.ini');
1144 aurelien 134
				require_once $chemin;
135
				$service = new $classe($this->conteneur);
136
				if ($this->methode == 'consulter') {
137
					$retour = $service->consulter($this->ressources, $this->parametres);
138
				} elseif ($this->methode == 'ajouter') {
139
					$retour = $service->ajouter($this->ressources, $this->parametres);
140
				} elseif ($this->methode == 'modifier') {
141
					$retour = $service->modifier($this->ressources, $this->parametres);
1700 jpm 142
				} elseif ($this->methode == 'supprimer') {
143
					$retour = $service->supprimer($this->ressources);
1144 aurelien 144
				}
145
			}
146
		}
1700 jpm 147
 
1144 aurelien 148
		if (is_null($service)) {
1700 jpm 149
			$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
150
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
151
				"n'existe pas dans le service '{$this->serviceNom}' !";
1144 aurelien 152
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
153
			throw new Exception($message, $code);
154
		}
155
		return $retour;
156
	}
157
 
158
	private function obtenirNomClasseService($mot) {
159
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
160
		return $classeNom;
161
	}
1700 jpm 162
}