Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 245 | Rev 536 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 245 Rev 274
1
<?php
1
<?php
2
/**
2
/**
3
* Description :
3
* Description :
4
* Classe principale de chargement des services d'eFlore.
4
* Classe principale de chargement des services d'eFlore.
5
*
5
*
6
* Encodage en entrée : utf8
6
* Encodage en entrée : utf8
7
* Encodage en sortie : utf8
7
* Encodage en sortie : utf8
8
* @package eflore-projets
8
* @package eflore-projets
9
* @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
9
* @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
10
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
10
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
11
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
12
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
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>
13
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
14
* @version 0.1
14
* @version 0.1
15
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
15
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
16
*/
16
*/
17
class Projets extends RestService {
17
class Projets extends RestService {
18
 
18
 
19
	/** Contients les paramètres.*/
19
	/** Contients les paramètres.*/
20
	private $parametres = array();
20
	private $parametres = array();
21
	/** Contients les ressources.*/
21
	/** Contients les ressources.*/
22
	private $ressources = array();
22
	private $ressources = array();
23
 
23
 
24
	/** Nom du projet courrant. */
24
	/** Nom du projet courrant. */
25
	private $projetNom = array();
25
	private $projetNom = array();
26
	/** Nom du service demandé. */
26
	/** Nom du service demandé. */
27
	private $serviceNom = array();
27
	private $serviceNom = array();
28
	/** Chemin vers le dossier courrant. */
28
	/** Chemin vers le dossier courrant. */
29
	private $cheminCourrant = null;
29
	private $cheminCourrant = null;
30
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres brutes. */
30
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres brutes. */
31
	protected $utilisationParametresBruts = true;
31
	protected $utilisationParametresBruts = true;
32
 
32
 
33
	public function __construct() {
33
	public function __construct() {
34
		$this->cheminCourrant = dirname(__FILE__).DS;
34
		$this->cheminCourrant = dirname(__FILE__).DS;
35
	}
35
	}
36
 
36
 
37
	public function consulter($ressources, $parametres) {
37
	public function consulter($ressources, $parametres) {
38
		$resultat = '';
38
		$resultat = '';
39
		$reponseHttp = new ReponseHttp();
39
		$reponseHttp = new ReponseHttp();
40
		try {
40
		try {
41
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
41
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
42
			$resultat = $this->traiterRessources();
42
			$resultat = $this->traiterRessources();
43
			$reponseHttp->setResultatService($resultat);
43
			$reponseHttp->setResultatService($resultat);
44
		} catch (Exception $e) {
44
		} catch (Exception $e) {
45
			$reponseHttp->ajouterErreur($e);
45
			$reponseHttp->ajouterErreur($e);
46
		}
46
		}
47
		$reponseHttp->emettreLesEntetes();
47
		$reponseHttp->emettreLesEntetes();
48
		$corps = $reponseHttp->getCorps();
48
		$corps = $reponseHttp->getCorps();
49
		return $corps;
49
		return $corps;
50
	}
50
	}
51
 
51
 
52
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
52
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
53
		$this->ressources = $ressources;
53
		$this->ressources = $ressources;
54
		$this->parametres = $parametres;
54
		$this->parametres = $parametres;
55
	}
55
	}
56
 
56
 
57
	private function traiterRessources() {
57
	private function traiterRessources() {
58
		$retour = '';
58
		$retour = '';
59
		if ($this->avoirRessources()) {
59
		if ($this->avoirRessources()) {
60
			if ($this->avoirRessourceProjet()) {
60
			if ($this->avoirRessourceProjet()) {
61
				$this->initialiserProjet();
61
				$this->initialiserProjet();
62
				if ($this->avoirRessourceService()) {
62
				if ($this->avoirRessourceService()) {
63
					$retour = $this->initialiserService();
63
					$retour = $this->initialiserService();
64
				}
64
				}
65
			}
65
			}
66
		}
66
		}
67
		return $retour;
67
		return $retour;
68
	}
68
	}
69
 
69
 
70
	private function avoirRessources() {
70
	private function avoirRessources() {
71
		$presenceDeRessources = false;
71
		$presenceDeRessources = false;
72
		if (isset($this->ressources) && count($this->ressources) > 0) {
72
		if (isset($this->ressources) && count($this->ressources) > 0) {
73
			$presenceDeRessources = true;
73
			$presenceDeRessources = true;
74
		} else {
74
		} else {
75
			$message = "Aucune ressource n'a été indiquée.\n".
75
			$message = "Aucune ressource n'a été indiquée.\n".
76
				"Veuillez indiquer au moins un code de projet et un type de service.";
76
				"Veuillez indiquer au moins un code de projet et un type de service.";
77
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
77
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
78
			throw new Exception($message, $code);
78
			throw new Exception($message, $code);
79
		}
79
		}
80
		return $presenceDeRessources;
80
		return $presenceDeRessources;
81
	}
81
	}
82
 
82
 
83
	private function avoirRessourceProjet() {
83
	private function avoirRessourceProjet() {
84
		$presenceRessourceProjet = false;
84
		$presenceRessourceProjet = false;
85
		$projet = $this->ressources[0];
85
		$projet = $this->ressources[0];
86
		$projetsDispo = Outils::recupererTableauConfig('projetsDispo');
86
		$projetsDispo = Outils::recupererTableauConfig('projetsDispo');
87
		if (in_array($projet, $projetsDispo)) {
87
		if (in_array($projet, $projetsDispo)) {
88
			$presenceRessourceProjet = true;
88
			$presenceRessourceProjet = true;
89
		} else {
89
		} else {
90
			$message = "La ressource '$projet' n'indique pas un projet existant.\n".
90
			$message = "La ressource '$projet' n'indique pas un projet existant.\n".
91
				"Les projets existant sont :\n".implode(', ', $projetsDispo);
91
				"Les projets existant sont :\n".implode(', ', $projetsDispo);
92
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
92
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
93
			throw new Exception($message, $code);
93
			throw new Exception($message, $code);
94
		}
94
		}
95
		return $presenceRessourceProjet;
95
		return $presenceRessourceProjet;
96
	}
96
	}
97
 
97
 
98
	private function initialiserProjet() {
98
	private function initialiserProjet() {
99
		$this->chargerNomDuProjet();
99
		$this->chargerNomDuProjet();
100
		$this->chargerConfigProjet();
100
		$this->chargerConfigProjet();
101
 
101
 
102
		// php5.3 : Enregistrement en première position des autoload de la méthode gérant les classes des services
102
		// php5.3 : Enregistrement en première position des autoload de la méthode gérant les classes des services
103
		if (phpversion() < 5.3) {
103
		if (phpversion() < 5.3) {
104
			spl_autoload_register(array($this, 'chargerClasseProjet'));
104
			spl_autoload_register(array($this, 'chargerClasseProjet'));
105
		} else {
105
		} else {
106
			spl_autoload_register(array($this, 'chargerClasseProjet'), true , true);
106
			spl_autoload_register(array($this, 'chargerClasseProjet'), true , true);
107
		}
107
		}
108
	}
108
	}
109
 
109
 
110
	private function chargerNomDuProjet() {
110
	private function chargerNomDuProjet() {
111
		$this->projetNom = $this->ressources[0];
111
		$this->projetNom = $this->ressources[0];
112
	}
112
	}
113
 
113
 
114
	private function chargerConfigProjet() {
114
	private function chargerConfigProjet() {
115
		$projet = $this->projetNom;
115
		$projet = $this->projetNom;
116
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
116
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
117
		Config::charger($chemin);
117
		Config::charger($chemin);
118
	}
118
	}
119
 
119
 
120
	private function chargerClasseProjet($classe) {
120
	private function chargerClasseProjet($classe) {
121
		if (class_exists($classe)) {
121
		if (class_exists($classe)) {
122
			return null;
122
			return null;
123
		}
123
		}
-
 
124
 
124
 
125
		$cheminBiblio = Config::get('chemin_bibliotheque');
125
		$chemins = array();
126
		$chemins = array();
126
		$chemins[] = $this->cheminCourrant.$this->projetNom.DS;
127
		$chemins[] = $this->cheminCourrant.$this->projetNom.DS;
127
		$chemins[] = $this->cheminCourrant.'commun'.DS;
128
		$chemins[] = $this->cheminCourrant.'commun'.DS;
-
 
129
		$chemins[] = $cheminBiblio;
128
		$chemins[] = Config::get('chemin_bibliotheque');
130
		$chemins[] = $cheminBiblio.'robots'.DS;
129
 
131
 
130
		foreach ($chemins as $chemin) {
132
		foreach ($chemins as $chemin) {
131
			$chemin = $chemin.$classe.'.php';
133
			$chemin = $chemin.$classe.'.php';
132
			if (file_exists($chemin)) {
134
			if (file_exists($chemin)) {
133
				require_once $chemin;
135
				require_once $chemin;
134
				break;
136
				break;
135
			}
137
			}
136
		}
138
		}
137
	}
139
	}
138
 
140
 
139
	private function avoirRessourceService() {
141
	private function avoirRessourceService() {
140
		$presenceRessourceService = false;
142
		$presenceRessourceService = false;
141
		$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
143
		$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
142
		if (isset($this->ressources[1])) {
144
		if (isset($this->ressources[1])) {
143
			$service = $this->ressources[1];
145
			$service = $this->ressources[1];
144
			if (in_array($service, $servicesDispo)) {
146
			if (in_array($service, $servicesDispo)) {
145
				$presenceRessourceService = true;
147
				$presenceRessourceService = true;
146
			} else {
148
			} else {
147
				$message = "La service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
149
				$message = "La service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
148
					"Les services disponibles sont : ".implode(', ', $servicesDispo);
150
					"Les services disponibles sont : ".implode(', ', $servicesDispo);
149
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
151
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
150
				throw new Exception($message, $code);
152
				throw new Exception($message, $code);
151
			}
153
			}
152
		} else {
154
		} else {
153
			$message = "Vous n'avez pas indiqué de service pour le projet {$this->projetNom} !\n".
155
			$message = "Vous n'avez pas indiqué de service pour le projet {$this->projetNom} !\n".
154
				"Les services disponibles sont : ".implode(', ', $servicesDispo);
156
				"Les services disponibles sont : ".implode(', ', $servicesDispo);
155
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
157
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
156
			throw new Exception($message, $code);
158
			throw new Exception($message, $code);
157
		}
159
		}
158
		return $presenceRessourceService;
160
		return $presenceRessourceService;
159
	}
161
	}
160
 
162
 
161
	private function initialiserService() {
163
	private function initialiserService() {
162
		$this->chargerNomDuService();
164
		$this->chargerNomDuService();
163
		$classe = $this->obtenirNomClasseService($this->serviceNom);
165
		$classe = $this->obtenirNomClasseService($this->serviceNom);
164
		$chemins = array();
166
		$chemins = array();
165
		$chemins[] = $this->cheminCourrant.$this->projetNom.DS.$classe.'.php';
167
		$chemins[] = $this->cheminCourrant.$this->projetNom.DS.$classe.'.php';
166
		$chemins[] = $this->cheminCourrant.'commun'.DS.$classe.'.php';
168
		$chemins[] = $this->cheminCourrant.'commun'.DS.$classe.'.php';
167
 
169
 
168
		$retour = '';
170
		$retour = '';
169
		$service = null;
171
		$service = null;
170
		foreach ($chemins as $chemin) {
172
		foreach ($chemins as $chemin) {
171
			if (file_exists($chemin)) {
173
			if (file_exists($chemin)) {
172
				$service = new $classe($this->getBdd());
174
				$service = new $classe($this->getBdd());
173
				$ressourcesPourService = $this->filtrerRessourcesPourService();
175
				$ressourcesPourService = $this->filtrerRessourcesPourService();
174
				$retour = $service->consulter($ressourcesPourService, $this->parametres);
176
				$retour = $service->consulter($ressourcesPourService, $this->parametres);
175
			}
177
			}
176
		}
178
		}
177
		if (is_null($service)) {
179
		if (is_null($service)) {
178
			$message = "La service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
180
			$message = "La service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
179
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
181
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
180
			throw new Exception($message, $code);
182
			throw new Exception($message, $code);
181
		}
183
		}
182
		return $retour;
184
		return $retour;
183
	}
185
	}
184
 
186
 
185
	private function chargerNomDuService() {
187
	private function chargerNomDuService() {
186
		$this->serviceNom = $this->ressources[1];
188
		$this->serviceNom = $this->ressources[1];
187
	}
189
	}
188
 
190
 
189
	private function obtenirNomClasseService($mot) {
191
	private function obtenirNomClasseService($mot) {
190
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
192
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
191
		return $classeNom;
193
		return $classeNom;
192
	}
194
	}
193
 
195
 
194
	private function filtrerRessourcesPourService() {
196
	private function filtrerRessourcesPourService() {
195
		$ressourcesPourService = array();
197
		$ressourcesPourService = array();
196
		$nbreDeRessources = count($this->ressources);
198
		$nbreDeRessources = count($this->ressources);
197
		for ($i = 2; $i < $nbreDeRessources; $i++) {
199
		for ($i = 2; $i < $nbreDeRessources; $i++) {
198
			$ressourcesPourService[] = $this->ressources[$i];
200
			$ressourcesPourService[] = $this->ressources[$i];
199
		}
201
		}
200
		return $ressourcesPourService;
202
		return $ressourcesPourService;
201
	}
203
	}
202
}
204
}
203
?>
205
?>