Subversion Repositories eFlore/Applications.del

Rev

Rev 1795 | Details | Compare with Previous | 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->analyserRessources();
75
		$retour = $this->initialiserService();
1144 aurelien 76
		return $retour;
77
	}
78
 
1700 jpm 79
	private function analyserRessources() {
80
		if ($this->methode == 'consulter') {
81
			if ($this->ressources[0] == 'images-determinations-probables') {
82
				$this->sousServiceNom = 'liste-images-determinations-probables';
1144 aurelien 83
			}
1700 jpm 84
		} else if ($this->methode == 'modifier') {
85
			if ($this->ressources[0] == 'valider-determination') {
86
				$this->sousServiceNom = 'valider-determination';
87
			}
1144 aurelien 88
		}
1700 jpm 89
 
90
		if ($this->sousServiceNom == null) {
91
			$this->lancerMessageErreurRessource();
92
		}
1144 aurelien 93
	}
1700 jpm 94
 
95
	private function lancerMessageErreurRessource() {
96
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
97
		$message = "La ressource demandée '$ressource' ".
98
			"n'est pas disponible pour le service ".$this->serviceNom." !\n".
99
			"Les URLs disponibles sont : \n".
100
			" - en GET : determinations/images-determinations-probables \n".
101
			" - en POST : determinations/valider-determination";
1144 aurelien 102
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
103
		throw new Exception($message, $code);
104
	}
105
 
106
	private function initialiserService() {
1700 jpm 107
		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
1144 aurelien 108
		$chemins = array();
1700 jpm 109
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
1144 aurelien 110
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
111
		$retour = '';
112
		$service = null;
113
		foreach ($chemins as $chemin) {
114
			if (file_exists($chemin)) {
115
				require_once $chemin;
116
				$service = new $classe($this->conteneur);
117
				if ($this->methode == 'consulter') {
1813 jpm 118
					$retour = $service->consulter($this->parametres);
1144 aurelien 119
				} elseif ($this->methode == 'ajouter') {
120
					$retour = $service->ajouter($this->ressources, $this->parametres);
121
				} elseif ($this->methode == 'modifier') {
122
					$retour = $service->modifier($this->ressources, $this->parametres);
1700 jpm 123
				} elseif ($this->methode == 'supprimer') {
124
					$retour = $service->supprimer($this->ressources);
1144 aurelien 125
				}
126
			}
127
		}
1700 jpm 128
 
1144 aurelien 129
		if (is_null($service)) {
1700 jpm 130
			$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
131
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
132
				"n'existe pas dans le service '{$this->serviceNom}' !";
1144 aurelien 133
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
134
			throw new Exception($message, $code);
135
		}
136
		return $retour;
137
	}
138
 
139
	private function obtenirNomClasseService($mot) {
140
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
141
		return $classeNom;
142
	}
1700 jpm 143
}