Subversion Repositories eFlore/Applications.del

Rev

Rev 972 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
858 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 Commentaires extends RestService {
18
 
19
 
20
	private $parametres = array();
21
	private $ressources = array();
22
	private $methode = null;
23
	private $projetNom = array();
24
	private $serviceNom = array();
25
	private $cheminCourant = null;
26
 
27
	private $conteneur;
28
 
29
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
30
	protected $utilisationParametresBruts = true;
31
 
32
	public function __construct() {
33
		$this->cheminCourant = dirname(__FILE__).DS;
34
	}
35
 
36
	public function consulter($ressources, $parametres) {
37
		$this->methode = 'consulter';
38
		$resultat = '';
39
		$reponseHttp = new ReponseHttp();
40
		try {
41
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
42
			$this->conteneur = new Conteneur($this->parametres);
43
			$resultat = $this->traiterRessources();
44
			$reponseHttp->setResultatService($resultat);
45
		} catch (Exception $e) {
46
			$reponseHttp->ajouterErreur($e);
47
		}
48
		$reponseHttp->emettreLesEntetes();
49
		$corps = $reponseHttp->getCorps();
50
		return $corps;
51
	}
52
 
53
 
54
	public function ajouter($ressources, $parametres) {
55
		$this->methode = 'ajouter';
56
		$resultat = '';
57
		$reponseHttp = new ReponseHttp();
883 aurelien 58
 
858 gduche 59
		try {
60
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
61
			$this->conteneur = new Conteneur($this->parametres);
62
			$resultat = $this->traiterRessources();
63
			$reponseHttp->setResultatService($resultat);
64
		} catch (Exception $e) {
65
			$reponseHttp->ajouterErreur($e);
66
		}
67
		$corps = $reponseHttp->getCorps();
68
		return $corps;
69
	}
70
 
864 gduche 71
	public function supprimer($ressources) {
72
		$this->methode = 'supprimer';
73
		$resultat = '';
74
		$reponseHttp = new ReponseHttp();
75
		try {
76
			$this->ressources = $ressources;
77
			$this->conteneur = new Conteneur();
78
			$resultat = $this->traiterRessources();
79
			$reponseHttp->setResultatService($resultat);
80
		} catch (Exception $e) {
81
			$reponseHttp->ajouterErreur($e);
972 aurelien 82
			$reponseHttp->emettreLesEntetes();
83
			echo $reponseHttp->getCorps();
864 gduche 84
		}
85
	}
86
 
858 gduche 87
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
88
		$this->ressources = $ressources;
89
		$this->parametres = $parametres;
90
	}
91
 
92
	private function traiterRessources() {
93
		$retour = '';
94
		$this->initialiserProjet();
95
		$retour = $this->initialiserService();
96
		return $retour;
97
	}
98
 
99
 
100
	/*------------------------------------------------------------------------------------------------------------------
101
										CONFIGURATION DU PROJET
102
	------------------------------------------------------------------------------------------------------------------*/
103
	private function initialiserProjet() {
104
		$this->projetNom = 'commentaires';
105
		$this->chargerConfigProjet();
106
	}
107
 
108
	private function chargerConfigProjet() {
109
		$projet = $this->projetNom;
110
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
111
		Config::charger($chemin);
112
	}
113
 
114
	/*------------------------------------------------------------------------------------------------------------------
115
								CONFIGURATION DU SERVICE
116
	------------------------------------------------------------------------------------------------------------------*/
117
	private function initialiserService() {
118
		$this->chargerNomService();
119
 
120
		$classe = $this->obtenirNomClasseService($this->serviceNom);
121
		$chemins = array();
122
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
123
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
124
		$retour = '';
125
		$service = null;
126
		foreach ($chemins as $chemin) {
127
			if (file_exists($chemin)) {
128
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
129
 
130
				require_once $chemin;
131
				$service = new $classe($this->conteneur);
132
				if ($this->methode == 'consulter') {
133
					$retour = $service->consulter($this->ressources, $this->parametres);
134
				} elseif ($this->methode == 'ajouter') {
883 aurelien 135
					$retour = $service->ajouter($this->ressources, $this->parametres);
864 gduche 136
				} elseif ($this->methode == 'supprimer') {
137
					$retour = $service->supprimer($this->ressources, $this->parametres);
858 gduche 138
				}
139
			}
140
		}
141
 
142
		if (is_null($service)) {
143
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
144
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
145
			throw new Exception($message, $code);
146
		}
147
		return $retour;
148
	}
149
 
150
	private function chargerNomService() {
151
		// si la méthode est POST, on ajouter un commentaire
152
		if ($this->methode == 'ajouter') {
153
			$this->serviceNom = 'ajouter-commentaire';
154
		} else if ($this->methode == 'supprimer') {
155
			$this->serviceNom = 'supprimer-commentaire';
156
		}
157
		else {
158
			//S'il n'y a pas de ressources => tous les commentaires
159
			if (!isset($this->ressources) || empty($this->ressources)) {
160
				$this->serviceNom = 'liste-commentaires';
161
			} else {
162
				if (!is_numeric($this->ressources[0])) {
163
					$message = "La première ressource doit être un identifiant";
164
					$code = RestServeur::HTTP_CODE_ERREUR;
165
					throw new Exception($message, $code);
166
				} else {
167
					$this->serviceNom = 'consulter-commentaire';
168
				}
169
			}
170
		}
171
	}
172
 
173
	private function obtenirNomClasseService($mot) {
174
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
175
		return $classeNom;
176
	}
177
}
178
?>