Subversion Repositories eFlore/Applications.del

Rev

Rev 864 | Go to most recent revision | Details | 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();
58
		try {
59
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
60
			$this->conteneur = new Conteneur($this->parametres);
61
			$resultat = $this->traiterRessources();
62
			$reponseHttp->setResultatService($resultat);
63
		} catch (Exception $e) {
64
			$reponseHttp->ajouterErreur($e);
65
		}
66
		$reponseHttp->emettreLesEntetes();
67
		$corps = $reponseHttp->getCorps();
68
		return $corps;
69
	}
70
 
71
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
72
		$this->ressources = $ressources;
73
		$this->parametres = $parametres;
74
	}
75
 
76
	private function traiterRessources() {
77
		$retour = '';
78
		$this->initialiserProjet();
79
		$retour = $this->initialiserService();
80
		return $retour;
81
	}
82
 
83
 
84
	/*------------------------------------------------------------------------------------------------------------------
85
										CONFIGURATION DU PROJET
86
	------------------------------------------------------------------------------------------------------------------*/
87
	private function initialiserProjet() {
88
		$this->projetNom = 'commentaires';
89
		$this->chargerConfigProjet();
90
	}
91
 
92
	private function chargerConfigProjet() {
93
		$projet = $this->projetNom;
94
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
95
		Config::charger($chemin);
96
	}
97
 
98
	/*------------------------------------------------------------------------------------------------------------------
99
								CONFIGURATION DU SERVICE
100
	------------------------------------------------------------------------------------------------------------------*/
101
	private function initialiserService() {
102
		$this->chargerNomService();
103
 
104
		$classe = $this->obtenirNomClasseService($this->serviceNom);
105
		$chemins = array();
106
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
107
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
108
		$retour = '';
109
		$service = null;
110
		foreach ($chemins as $chemin) {
111
			if (file_exists($chemin)) {
112
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
113
 
114
				require_once $chemin;
115
				$service = new $classe($this->conteneur);
116
				if ($this->methode == 'consulter') {
117
					$retour = $service->consulter($this->ressources, $this->parametres);
118
				} elseif ($this->methode == 'ajouter') {
119
					$retour = $service->ajouter($this->ressources, $this->parametres);
120
				}
121
			}
122
		}
123
 
124
		if (is_null($service)) {
125
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
126
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
127
			throw new Exception($message, $code);
128
		}
129
		return $retour;
130
	}
131
 
132
	private function chargerNomService() {
133
		// si la méthode est POST, on ajouter un commentaire
134
		if ($this->methode == 'ajouter') {
135
			$this->serviceNom = 'ajouter-commentaire';
136
		} else if ($this->methode == 'supprimer') {
137
			// ??
138
			$this->serviceNom = 'supprimer-commentaire';
139
		}
140
		else {
141
			//S'il n'y a pas de ressources => tous les commentaires
142
			if (!isset($this->ressources) || empty($this->ressources)) {
143
				$this->serviceNom = 'liste-commentaires';
144
			} else {
145
				if (!is_numeric($this->ressources[0])) {
146
					$message = "La première ressource doit être un identifiant";
147
					$code = RestServeur::HTTP_CODE_ERREUR;
148
					throw new Exception($message, $code);
149
				} else {
150
					$this->serviceNom = 'consulter-commentaire';
151
				}
152
			}
153
		}
154
	}
155
 
156
	private function obtenirNomClasseService($mot) {
157
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
158
		return $classeNom;
159
	}
160
 
161
 
162
}
163
?>