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