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