| Line 1... |
Line 1... |
| 1 |
<?php
|
1 |
<?php
|
| Line 2... |
Line 2... |
| 2 |
|
2 |
|
| 3 |
/**
|
3 |
/**
|
| 4 |
* Classe Informations.php interroge la table Index
|
4 |
* Classe Informations.php interroge la table Index
|
| 5 |
* et redirige vers une autre classe si besoin
|
5 |
* et redirige vers une autre classe si besoin
|
| 6 |
* d'interroger une autre table (comme baseflor ou baseflor_rang_sup)
|
6 |
* d'interroger une autre table (comme baseflor ou baseflor_rang_sup)
|
| 7 |
* elle traite également le retour du web service : /informations
|
7 |
* elle traite également le retour du web service : /informations
|
| 8 |
*
|
8 |
*
|
| 9 |
* Encodage en entrée : utf8
|
9 |
* Encodage en entrée : utf8
|
| 10 |
* Encodage en sortie : utf8
|
10 |
* Encodage en sortie : utf8
|
| 11 |
* @package eflore-projets
|
11 |
* @package eflore-projets
|
| 12 |
* @author Mathilde SALTHUN-LASSALLE <mathilde@tela-botanica.org>
|
12 |
* @author Mathilde SALTHUN-LASSALLE <mathilde@tela-botanica.org>
|
| 13 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
13 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
| 14 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
14 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
| 15 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
15 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
| 16 |
* @version 1.0
|
16 |
* @version 1.0
|
| 17 |
* @copyright 1999-2012 Tela Botanica (accueil@tela-botanica.org)
|
17 |
* @copyright 1999-2012 Tela Botanica (accueil@tela-botanica.org)
|
| 18 |
*
|
18 |
*
|
| 19 |
*
|
19 |
*
|
| 20 |
*/
|
- |
|
| 21 |
|
- |
|
| Line 22... |
Line 20... |
| 22 |
|
20 |
*/
|
| Line 23... |
Line 21... |
| 23 |
|
21 |
|
| 24 |
class Informations extends Commun {
|
22 |
class Informations extends Commun {
|
| Line 31... |
Line 29... |
| 31 |
private $total_resultat;
|
29 |
private $total_resultat;
|
| 32 |
protected $serviceNom = 'informations';
|
30 |
protected $serviceNom = 'informations';
|
| 33 |
private $masque;
|
31 |
private $masque;
|
| Line 34... |
Line 32... |
| 34 |
|
32 |
|
| 35 |
public function consulter($ressources, $parametres) {
|
33 |
public function consulter($ressources, $parametres) {
|
| 36 |
$this->ressources = $ressources;
|
34 |
$this->ressources = $ressources;
|
| 37 |
$this->parametres = $parametres;
|
35 |
$this->parametres = $parametres;
|
| 38 |
$versionResultat = null;
|
36 |
$versionResultat = null;
|
| 39 |
if (empty($this->ressources)){
|
37 |
if (empty($this->ressources)){
|
| 40 |
$this->traiterParametres();
|
38 |
$this->traiterParametres();
|
| 41 |
}
|
39 |
}
|
| 42 |
$this->definirTables(array ('index' => config::get('bdd_table_index'),
|
40 |
$this->definirTables(array ('index' => config::get('bdd_table_index'),
|
| 43 |
'baseflor' => config::get('bdd_table'),
|
41 |
'baseflor' => config::get('bdd_table'),
|
| 44 |
'rangSup' => config::get('bdd_table_rang_sup')
|
42 |
'rangSup' => config::get('bdd_table_rang_sup')
|
| 45 |
)
|
43 |
)
|
| 46 |
);
|
44 |
);
|
| 47 |
$this->traiterRessources();
|
45 |
$this->traiterRessources();
|
| 48 |
$requete = $this->assemblerLaRequete();
|
46 |
$requete = $this->assemblerLaRequete();
|
| 49 |
// exemple:
|
47 |
// exemple:
|
| 50 |
// SELECT * FROM baseflor_index_v2012_12_31 WHERE `bdnt.nn` = 'bdtfx.nn:XXXXX';
|
48 |
// SELECT * FROM baseflor_index_v2012_12_31 WHERE `bdnt.nn` = 'bdtfx.nn:XXXXX';
|
| 51 |
$resultat = $this->Bdd->recupererTous($requete . ' -- ' . __FILE__ . ':' .__LINE__);
|
49 |
$resultat = $this->Bdd->recupererTous($requete . ' -- ' . __FILE__ . ':' .__LINE__);
|
| 52 |
$versionResultat = $this->renvoyerResultat($resultat);
|
50 |
$versionResultat = $this->renvoyerResultat($resultat);
|
| 53 |
if ($versionResultat == null) {
|
- |
|
| 54 |
$message = 'Les données recherchées sont introuvables.';
|
51 |
if ($versionResultat === null) {
|
| 55 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
- |
|
| 56 |
throw new Exception($message, $code);
|
52 |
throw new Exception('Les données recherchées sont introuvables.', RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
|
| 57 |
}
|
53 |
}
|
| 58 |
return $versionResultat;
|
54 |
return $versionResultat;
|
| Line 59... |
Line 55... |
| 59 |
}
|
55 |
}
|
| 60 |
|
56 |
|
| 61 |
public function __construct() {
|
57 |
public function __construct() {
|
| 62 |
$Conteneur = new Conteneur();
|
58 |
$Conteneur = new Conteneur();
|
| Line 63... |
Line 59... |
| 63 |
$this->Bdd = $Conteneur ->getBdd();
|
59 |
$this->Bdd = $Conteneur ->getBdd();
|
| 64 |
}
|
60 |
}
|
| 65 |
|
61 |
|
| 66 |
public function renvoyerResultat($resultat) {
|
62 |
public function renvoyerResultat($resultat) {
|
| 67 |
$res_formate = array();
|
- |
|
| 68 |
if (empty($this->ressources)) {
|
63 |
if (empty($this->ressources)) return $this->retournerResultatFormate($resultat);
|
| - |
|
64 |
if(empty($resultat[0]['baseflor'])) {
|
| - |
|
65 |
if(@$this->parametres['categorie'] == 'description') {
|
| 69 |
$res_formate = $this->retournerResultatFormate($resultat);
|
66 |
// non-supporté par InformationsTaxonsSup.php...
|
| 70 |
} else {
|
67 |
return array();
|
| 71 |
$classe = empty($resultat[0]['baseflor']) ? 'InformationsTaxonsSup' : 'InformationsBaseflor';
|
68 |
}
|
| - |
|
69 |
$info = new InformationsTaxonsSup(new Conteneur());
|
| - |
|
70 |
return $info->consulter($this->ressources, $this->parametres);
|
| 72 |
$info = new $classe(new Conteneur());
|
71 |
}
|
| 73 |
$res_formate = $info->consulter($this->ressources, $this->parametres);
|
72 |
|
| Line 74... |
Line 73... |
| 74 |
}
|
73 |
$info = new InformationsBaseflor(new Conteneur());
|
| Line 112... |
Line 111... |
| 112 |
|
111 |
|
| 113 |
public function traiterParametres() {
|
112 |
public function traiterParametres() {
|
| 114 |
if (isset($this->parametres) && !empty($this->parametres) ) {
|
113 |
if (isset($this->parametres) && !empty($this->parametres) ) {
|
| 115 |
foreach ($this->parametres as $param => $valeur) {
|
114 |
foreach ($this->parametres as $param => $valeur) {
|
| 116 |
switch ($param) {
|
115 |
switch ($param) {
|
| 117 |
case 'navigation.depart' :
|
116 |
case 'navigation.depart' :
|
| 118 |
if(!isset($this->parametres['navigation.limite']))
|
117 |
if(!isset($this->parametres['navigation.limite']))
|
| 119 |
throw new Exception("indiquez également la valeur pour le paramètre navigation.limite.",
|
118 |
throw new Exception("indiquez également la valeur pour le paramètre navigation.limite.",
|
| 120 |
RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
119 |
RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
| 121 |
$this->definirNavigationDepart($valeur);
|
120 |
$this->definirNavigationDepart($valeur);
|
| 122 |
break;
|
121 |
break;
|
| 123 |
case 'navigation.limite' :
|
122 |
case 'navigation.limite' :
|
| 124 |
if(!isset($this->parametres['navigation.depart']))
|
123 |
if(!isset($this->parametres['navigation.depart']))
|
| 125 |
throw new Exception("indiquez également la valeur pour le paramètre navigation.depart.",
|
124 |
throw new Exception("indiquez également la valeur pour le paramètre navigation.depart.",
|
| 126 |
RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
125 |
RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
| 127 |
$this->definirNavigationLimite($valeur);
|
126 |
$this->definirNavigationLimite($valeur);
|
| 128 |
break;
|
127 |
break;
|
| 129 |
case 'version.projet' :
|
128 |
case 'version.projet' :
|
| 130 |
$this->traiterVersion($valeur);
|
129 |
$this->traiterVersion($valeur);
|
| 131 |
break;
|
130 |
break;
|
| 132 |
case 'masque.cat' :
|
131 |
case 'masque.cat' :
|
| 133 |
$this->masque = 'catminat='.str_replace('-','/',$valeur);
|
132 |
$this->masque = 'catminat='.str_replace('-','/',$valeur);
|
| 134 |
$this->requete_condition[] = " catminat_code = '".str_replace('-','/',$valeur)."' ";
|
133 |
$this->requete_condition[] = " catminat_code = '".str_replace('-','/',$valeur)."' ";
|
| 135 |
break;
|
134 |
break;
|
| 136 |
default :
|
135 |
default :
|
| 137 |
$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
|
136 |
$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
|
| 138 |
.$param.' " n\'existe pas.';
|
137 |
.$param.' " n\'existe pas.';
|
| 139 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e); break;
|
138 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e); break;
|
| 140 |
}
|
139 |
}
|
| 141 |
}
|
140 |
}
|
| 142 |
}
|
141 |
}
|
| Line 203... |
Line 202... |
| 203 |
|
202 |
|
| 204 |
if ($resultat_json['resultats'][$num]['num_nomen'] != 0) {
|
203 |
if ($resultat_json['resultats'][$num]['num_nomen'] != 0) {
|
| 205 |
$bdnt = strtolower($resultat_json['resultats'][$num]['bdnt']);
|
204 |
$bdnt = strtolower($resultat_json['resultats'][$num]['bdnt']);
|
| 206 |
$nn = $resultat_json['resultats'][$num]['num_nomen'];
|
205 |
$nn = $resultat_json['resultats'][$num]['num_nomen'];
|
| 207 |
$resultat_json['resultats'][$num]['href'] =
|
206 |
$resultat_json['resultats'][$num]['href'] =
|
| 208 |
$this->ajouterHref('informations',$bdnt.".nn:".$nn);
|
207 |
$this->ajouterHref('informations',$bdnt.".nn:".$nn);
|
| 209 |
}
|
208 |
}
|
| 210 |
}
|
209 |
}
|
| 211 |
return $resultat_json;
|
210 |
return $resultat_json;
|
| Line 212... |
Line 211... |
| 212 |
}
|
211 |
}
|
| 213 |
|
212 |
|
| 214 |
|
213 |
|
| 215 |
public function ajouterEnteteResultat() {
|
214 |
public function ajouterEnteteResultat() {
|
| 216 |
$entete['depart'] = $this->limite_requete['depart'];
|
215 |
$entete['depart'] = $this->limite_requete['depart'];
|
| 217 |
$entete['limite'] = $this->limite_requete['limite'];
|
216 |
$entete['limite'] = $this->limite_requete['limite'];
|
| 218 |
$entete['total'] = $this->total_resultat;
|
217 |
$entete['total'] = $this->total_resultat;
|
| 219 |
if ($this->masque) {
|
218 |
if ($this->masque) {
|
| 220 |
$entete['masque'] = $this->masque;
|
219 |
$entete['masque'] = $this->masque;
|
| 221 |
}
|
220 |
}
|
| 222 |
$entete['version'] = $this->version_projet;
|
221 |
$entete['version'] = $this->version_projet;
|
| 223 |
$url = $this->formulerUrl($this->total_resultat, '/informations');
|
222 |
$url = $this->formulerUrl($this->total_resultat, '/informations');
|
| 224 |
if (isset($url['precedent']) && $url['precedent'] != '') {
|
223 |
if (isset($url['precedent']) && $url['precedent'] != '') {
|
| 225 |
$entete['href.precedent'] = $url['precedent'];
|
224 |
$entete['href.precedent'] = $url['precedent'];
|
| 226 |
}
|
225 |
}
|
| 227 |
if (isset($url['suivant']) && $url['suivant'] != '') {
|
226 |
if (isset($url['suivant']) && $url['suivant'] != '') {
|
| 228 |
$entete['href.suivant'] = $url['suivant'];
|
227 |
$entete['href.suivant'] = $url['suivant'];
|
| Line 229... |
Line 228... |
| 229 |
}
|
228 |
}
|
| Line 230... |
Line 229... |
| 230 |
return $entete;
|
229 |
return $entete;
|
| 231 |
}
|
230 |
}
|
| 232 |
|
231 |
|
| 233 |
|
232 |
|
| 234 |
//+-------------------------------------assemblage de requête------------------------------------//
|
233 |
//+-------------------------------------assemblage de requête------------------------------------//
|
| 235 |
|
234 |
|
| 236 |
|
235 |
|
| 237 |
public function assemblerLaRequete() {
|
236 |
public function assemblerLaRequete() {
|
| Line 238... |
Line 237... |
| 238 |
$requete = ' SELECT '.$this->champs_recherches.
|
237 |
$requete = ' SELECT '.$this->champs_recherches.
|
| 239 |
' FROM '.$this->tables['index'].' '
|
238 |
' FROM '.$this->tables['index'].' '
|
| 240 |
.self::retournerRequeteJointure($this->requete_jointure).' '
|
239 |
.self::retournerRequeteJointure($this->requete_jointure).' '
|
| Line 241... |
Line 240... |
| 241 |
.self::retournerRequeteCondition($this->requete_condition).' '
|
240 |
.self::retournerRequeteCondition($this->requete_condition).' '
|
| 242 |
.$this->delimiterResultatsRequete();
|
241 |
.$this->delimiterResultatsRequete();
|
| 243 |
return $requete;
|
242 |
return $requete;
|
| 244 |
}
|
243 |
}
|
| 245 |
|
244 |
|
| 246 |
static function retournerRequeteCondition($cond) {
|
245 |
static function retournerRequeteCondition($cond) {
|
| 247 |
return $cond ? (' WHERE '.implode(' AND ', $cond)) : '';
|
246 |
return $cond ? (' WHERE '.implode(' AND ', $cond)) : '';
|
| 248 |
}
|
247 |
}
|
| 249 |
|
248 |
|
| 250 |
|
249 |
|
| 251 |
static function calculerTotalResultat($db, $table, Array $join, Array $cond) {
|
250 |
static function calculerTotalResultat($db, $table, Array $join, Array $cond) {
|
| 252 |
$requete = sprintf(
|
251 |
$requete = sprintf(
|
| Line 253... |
Line 252... |
| 253 |
'SELECT count(*) as nombre FROM %s %s %s -- %s:%d',
|
252 |
'SELECT count(*) as nombre FROM %s %s %s -- %s:%d',
|
| 254 |
$table,
|
253 |
$table,
|
| 255 |
$join ? implode(' ', $join) : '',
|
254 |
$join ? implode(' ', $join) : '',
|
| 256 |
$cond ? (' WHERE '.implode(' AND ', $cond)) : '',
|
255 |
$cond ? (' WHERE '.implode(' AND ', $cond)) : '',
|
| 257 |
__FILE__,
|
256 |
__FILE__,
|
| 258 |
__LINE__);
|
257 |
__LINE__);
|
| 259 |
$res = $db->recuperer($requete);
|
258 |
$res = $db->recuperer($requete);
|
| 260 |
if ($res && $res['nombre']) return $res['nombre'];
|
259 |
if ($res && $res['nombre']) return $res['nombre'];
|
| 261 |
throw new Exception('Données introuvables dans la base', RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
|
260 |
throw new Exception('Données introuvables dans la base', RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
|
| 262 |
}
|
261 |
}
|
| 263 |
|
262 |
|
| 264 |
public function delimiterResultatsRequete() {
|
263 |
public function delimiterResultatsRequete() {
|
| 265 |
$this->total_resultat = self::calculerTotalResultat(
|
264 |
$this->total_resultat = self::calculerTotalResultat(
|
| 266 |
$this->getBdd(),
|
265 |
$this->getBdd(),
|
| 267 |
$this->tables['index'],
|
266 |
$this->tables['index'],
|
| 268 |
$this->requete_jointure,
|
267 |
$this->requete_jointure,
|
| 269 |
$this->requete_condition);
|
268 |
$this->requete_condition);
|
| 270 |
$requete_limite = '';
|
269 |
$requete_limite = '';
|
| 271 |
if (($this->limite_requete['depart'] <= $this->total_resultat) ){
|
270 |
if (($this->limite_requete['depart'] <= $this->total_resultat) ){
|
| Line 272... |
Line 271... |
| 272 |
if ( $this->limite_requete['depart'] < $this->total_resultat ){
|
271 |
if ( $this->limite_requete['depart'] < $this->total_resultat ){
|
| 273 |
$requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
|
272 |
$requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
|
| 274 |
.$this->limite_requete['limite'];
|
273 |
.$this->limite_requete['limite'];
|
| Line 275... |
Line 274... |
| 275 |
}
|
274 |
}
|
| 276 |
} else {
|
275 |
} else {
|