379 |
mathilde |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
964 |
raphael |
4 |
* Classe Informations.php interroge la table Index
|
|
|
5 |
* et redirige vers une autre classe si besoin
|
|
|
6 |
* d'interroger une autre table (comme baseflor ou baseflor_rang_sup)
|
|
|
7 |
* elle traite également le retour du web service : /informations
|
|
|
8 |
*
|
|
|
9 |
* Encodage en entrée : utf8
|
|
|
10 |
* Encodage en sortie : utf8
|
|
|
11 |
* @package eflore-projets
|
|
|
12 |
* @author Mathilde SALTHUN-LASSALLE <mathilde@tela-botanica.org>
|
|
|
13 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
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>
|
|
|
16 |
* @version 1.0
|
|
|
17 |
* @copyright 1999-2012 Tela Botanica (accueil@tela-botanica.org)
|
|
|
18 |
*
|
|
|
19 |
*
|
|
|
20 |
*/
|
379 |
mathilde |
21 |
|
525 |
mathilde |
22 |
class Informations extends Commun {
|
379 |
mathilde |
23 |
|
525 |
mathilde |
24 |
protected $limite_requete = array( 'depart' => 0, 'limite' => 10);
|
596 |
mathilde |
25 |
private $tables;
|
525 |
mathilde |
26 |
private $champs_recherches = '*';
|
959 |
raphael |
27 |
private $requete_jointure = array();
|
|
|
28 |
private $requete_condition = array();
|
548 |
mathilde |
29 |
private $total_resultat;
|
525 |
mathilde |
30 |
protected $serviceNom = 'informations';
|
615 |
mathilde |
31 |
private $masque;
|
395 |
mathilde |
32 |
|
447 |
mathilde |
33 |
public function consulter($ressources, $parametres) {
|
964 |
raphael |
34 |
$this->ressources = $ressources;
|
|
|
35 |
$this->parametres = $parametres;
|
|
|
36 |
$versionResultat = null;
|
|
|
37 |
if (empty($this->ressources)){
|
|
|
38 |
$this->traiterParametres();
|
|
|
39 |
}
|
|
|
40 |
$this->definirTables(array ('index' => config::get('bdd_table_index'),
|
|
|
41 |
'baseflor' => config::get('bdd_table'),
|
|
|
42 |
'rangSup' => config::get('bdd_table_rang_sup')
|
|
|
43 |
)
|
|
|
44 |
);
|
|
|
45 |
$this->traiterRessources();
|
|
|
46 |
$requete = $this->assemblerLaRequete();
|
|
|
47 |
// exemple:
|
|
|
48 |
// SELECT * FROM baseflor_index_v2012_12_31 WHERE `bdnt.nn` = 'bdtfx.nn:XXXXX';
|
|
|
49 |
$resultat = $this->Bdd->recupererTous($requete . ' -- ' . __FILE__ . ':' .__LINE__);
|
|
|
50 |
$versionResultat = $this->renvoyerResultat($resultat);
|
|
|
51 |
if ($versionResultat === null) {
|
|
|
52 |
throw new Exception('Les données recherchées sont introuvables.', RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
|
|
|
53 |
}
|
447 |
mathilde |
54 |
return $versionResultat;
|
379 |
mathilde |
55 |
}
|
548 |
mathilde |
56 |
|
525 |
mathilde |
57 |
public function __construct() {
|
548 |
mathilde |
58 |
$Conteneur = new Conteneur();
|
|
|
59 |
$this->Bdd = $Conteneur ->getBdd();
|
525 |
mathilde |
60 |
}
|
379 |
mathilde |
61 |
|
548 |
mathilde |
62 |
public function renvoyerResultat($resultat) {
|
964 |
raphael |
63 |
if (empty($this->ressources)) return $this->retournerResultatFormate($resultat);
|
|
|
64 |
if(empty($resultat[0]['baseflor'])) {
|
|
|
65 |
if(@$this->parametres['categorie'] == 'description') {
|
|
|
66 |
// non-supporté par InformationsTaxonsSup.php...
|
|
|
67 |
return array();
|
|
|
68 |
}
|
|
|
69 |
$info = new InformationsTaxonsSup(new Conteneur());
|
|
|
70 |
return $info->consulter($this->ressources, $this->parametres);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
$info = new InformationsBaseflor(new Conteneur());
|
|
|
74 |
return $info->consulter($this->ressources, $this->parametres);
|
525 |
mathilde |
75 |
}
|
517 |
mathilde |
76 |
|
|
|
77 |
|
525 |
mathilde |
78 |
|
|
|
79 |
//+--------------------------traitement ressources ou paramètres -------------------------------------+
|
|
|
80 |
|
548 |
mathilde |
81 |
|
525 |
mathilde |
82 |
public function traiterRessources() {
|
548 |
mathilde |
83 |
if (empty($this->ressources)) {
|
|
|
84 |
$this->requete_jointure = array(
|
|
|
85 |
'LEFT JOIN '. $this->tables['baseflor'].' ON '
|
596 |
mathilde |
86 |
.$this->tables['baseflor'].".cle =".$this->tables['index'].".baseflor",
|
548 |
mathilde |
87 |
'LEFT JOIN '. $this->tables['rangSup'].' ON '
|
596 |
mathilde |
88 |
.$this->tables['rangSup'].".cle = ".$this->tables['index'].".RangSup");
|
548 |
mathilde |
89 |
|
|
|
90 |
$this->champs_recherches = " {$this->tables['baseflor']}.num_nomen as 'baseflor.nn',
|
|
|
91 |
{$this->tables['baseflor']}.BDNT as 'baseflor.bdnt',
|
|
|
92 |
{$this->tables['baseflor']}.catminat_code,
|
|
|
93 |
{$this->tables['baseflor']}.num_taxon ,
|
907 |
delphine |
94 |
{$this->tables['baseflor']}.nom_sci ,
|
548 |
mathilde |
95 |
{$this->tables['rangSup']}.num_nomen as 'rangSup.nn',
|
|
|
96 |
{$this->tables['rangSup']}.bdnt as 'rangSup.bdnt',
|
596 |
mathilde |
97 |
{$this->tables['index']}.cle as 'index.cle' ";
|
525 |
mathilde |
98 |
} else {
|
|
|
99 |
if(preg_match('/^(.+)\.nn:([0-9]+)$/', $this->ressources[0], $retour)){
|
548 |
mathilde |
100 |
$this->champs_recherches = " baseflor, rangSup ";
|
|
|
101 |
$this->requete_condition[] = " `bdnt.nn` = '{$retour[0]}' ";
|
525 |
mathilde |
102 |
} else {
|
|
|
103 |
$e = 'Erreur dans l\'url de votre requête : </br> La ressource demandée n\'existe pas.';
|
|
|
104 |
throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
|
|
105 |
break;
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
|
548 |
mathilde |
110 |
|
|
|
111 |
|
525 |
mathilde |
112 |
public function traiterParametres() {
|
|
|
113 |
if (isset($this->parametres) && !empty($this->parametres) ) {
|
|
|
114 |
foreach ($this->parametres as $param => $valeur) {
|
|
|
115 |
switch ($param) {
|
964 |
raphael |
116 |
case 'navigation.depart' :
|
|
|
117 |
if(!isset($this->parametres['navigation.limite']))
|
|
|
118 |
throw new Exception("indiquez également la valeur pour le paramètre navigation.limite.",
|
|
|
119 |
RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
|
|
120 |
$this->definirNavigationDepart($valeur);
|
|
|
121 |
break;
|
|
|
122 |
case 'navigation.limite' :
|
|
|
123 |
if(!isset($this->parametres['navigation.depart']))
|
|
|
124 |
throw new Exception("indiquez également la valeur pour le paramètre navigation.depart.",
|
|
|
125 |
RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
|
|
126 |
$this->definirNavigationLimite($valeur);
|
|
|
127 |
break;
|
|
|
128 |
case 'version.projet' :
|
|
|
129 |
$this->traiterVersion($valeur);
|
|
|
130 |
break;
|
|
|
131 |
case 'masque.cat' :
|
|
|
132 |
$this->masque = 'catminat='.str_replace('-','/',$valeur);
|
|
|
133 |
$this->requete_condition[] = " catminat_code = '".str_replace('-','/',$valeur)."' ";
|
|
|
134 |
break;
|
|
|
135 |
default :
|
|
|
136 |
$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
|
|
|
137 |
.$param.' " n\'existe pas.';
|
525 |
mathilde |
138 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e); break;
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
|
548 |
mathilde |
144 |
|
|
|
145 |
|
525 |
mathilde |
146 |
//+++------------------------------traitement des versions----------------------------------------++
|
|
|
147 |
|
|
|
148 |
public function traiterVersion($valeur) {
|
|
|
149 |
if (preg_match('/^[0-9]+(?:[._][0-9]+)*$/', $valeur) || $valeur == '*' || $valeur == '+') {
|
|
|
150 |
$this->version_projet = $valeur;
|
|
|
151 |
} else {
|
|
|
152 |
$e = "Erreur : La version est inconnue.";
|
|
|
153 |
throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
|
|
154 |
}
|
|
|
155 |
if ($this->version_projet == '*' && empty($this->ressources)) {
|
|
|
156 |
$message = "L'affichage de plusieurs versions ne fonctionne que pour les ressources de type /ressources/#id";
|
|
|
157 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
158 |
throw new Exception($message, $code);
|
|
|
159 |
}
|
|
|
160 |
}
|
|
|
161 |
|
596 |
mathilde |
162 |
public function definirTables($prefixe_table) {
|
525 |
mathilde |
163 |
$table_num_version = $this->recupererVersionDisponible();
|
|
|
164 |
foreach ($prefixe_table as $nom => $prefixe ) {
|
|
|
165 |
if ( in_array($this->version_projet,$table_num_version) ) {
|
|
|
166 |
$tables[$nom] = $prefixe.'_v'.$this->version_projet;
|
|
|
167 |
} elseif ($this->version_projet == '+') {
|
|
|
168 |
$derniere_version = $table_num_version[count($table_num_version) - 1];
|
|
|
169 |
$tables[$nom] = $prefixe.'_v'.str_replace('.', '_', $derniere_version);
|
|
|
170 |
} else {
|
|
|
171 |
$e = "Erreur : La version est inconnue.";
|
|
|
172 |
throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
|
|
173 |
}
|
|
|
174 |
}
|
596 |
mathilde |
175 |
$this->tables = $tables;
|
525 |
mathilde |
176 |
}
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
//+--------------------------formatages de resultats -------------------------------------------+
|
|
|
180 |
|
|
|
181 |
public function retournerResultatFormate($resultat) {
|
|
|
182 |
$resultat_json = array();
|
|
|
183 |
$resultat_json['entete'] = $this->ajouterEnteteResultat();
|
615 |
mathilde |
184 |
$resultat_json['resultats'] = array();
|
525 |
mathilde |
185 |
foreach ($resultat as $ligne => $tab) {
|
|
|
186 |
$num = $tab['index.cle'];
|
548 |
mathilde |
187 |
|
783 |
raphael |
188 |
$resultat_json['resultats'][$num]['num_nomen'] =
|
525 |
mathilde |
189 |
(empty ($tab['baseflor.nn'])) ? $tab['rangSup.nn'] : $tab['baseflor.nn'];
|
548 |
mathilde |
190 |
|
525 |
mathilde |
191 |
$resultat_json['resultats'][$num]['bdnt'] =
|
548 |
mathilde |
192 |
(empty ($tab['baseflor.bdnt'])) ? $tab['rangSup.bdnt'] : $tab['baseflor.bdnt'];
|
|
|
193 |
|
525 |
mathilde |
194 |
$resultat_json['resultats'][$num]['catminat_code'] =
|
548 |
mathilde |
195 |
(empty ($tab['catminat_code'])) ? '' : $tab['catminat_code'];
|
|
|
196 |
|
942 |
delphine |
197 |
$resultat_json['resultats'][$num]['nom_sci'] =
|
|
|
198 |
(empty ($tab['nom_sci'])) ? '' : $tab['nom_sci'];
|
614 |
mathilde |
199 |
|
525 |
mathilde |
200 |
$resultat_json['resultats'][$num]['num_taxon'] =
|
548 |
mathilde |
201 |
(empty ($tab['num_taxon'])) ? '' : $tab['num_taxon'];
|
525 |
mathilde |
202 |
|
548 |
mathilde |
203 |
if ($resultat_json['resultats'][$num]['num_nomen'] != 0) {
|
|
|
204 |
$bdnt = strtolower($resultat_json['resultats'][$num]['bdnt']);
|
|
|
205 |
$nn = $resultat_json['resultats'][$num]['num_nomen'];
|
|
|
206 |
$resultat_json['resultats'][$num]['href'] =
|
964 |
raphael |
207 |
$this->ajouterHref('informations',$bdnt.".nn:".$nn);
|
549 |
mathilde |
208 |
}
|
525 |
mathilde |
209 |
}
|
|
|
210 |
return $resultat_json;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
|
|
|
214 |
public function ajouterEnteteResultat() {
|
548 |
mathilde |
215 |
$entete['depart'] = $this->limite_requete['depart'];
|
|
|
216 |
$entete['limite'] = $this->limite_requete['limite'];
|
964 |
raphael |
217 |
$entete['total'] = $this->total_resultat;
|
615 |
mathilde |
218 |
if ($this->masque) {
|
|
|
219 |
$entete['masque'] = $this->masque;
|
|
|
220 |
}
|
964 |
raphael |
221 |
$entete['version'] = $this->version_projet;
|
548 |
mathilde |
222 |
$url = $this->formulerUrl($this->total_resultat, '/informations');
|
525 |
mathilde |
223 |
if (isset($url['precedent']) && $url['precedent'] != '') {
|
|
|
224 |
$entete['href.precedent'] = $url['precedent'];
|
|
|
225 |
}
|
|
|
226 |
if (isset($url['suivant']) && $url['suivant'] != '') {
|
964 |
raphael |
227 |
$entete['href.suivant'] = $url['suivant'];
|
525 |
mathilde |
228 |
}
|
|
|
229 |
return $entete;
|
|
|
230 |
}
|
|
|
231 |
|
548 |
mathilde |
232 |
|
|
|
233 |
//+-------------------------------------assemblage de requête------------------------------------//
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
public function assemblerLaRequete() {
|
964 |
raphael |
237 |
$requete = ' SELECT '.$this->champs_recherches.
|
|
|
238 |
' FROM '.$this->tables['index'].' '
|
|
|
239 |
.self::retournerRequeteJointure($this->requete_jointure).' '
|
|
|
240 |
.self::retournerRequeteCondition($this->requete_condition).' '
|
|
|
241 |
.$this->delimiterResultatsRequete();
|
548 |
mathilde |
242 |
return $requete;
|
|
|
243 |
}
|
959 |
raphael |
244 |
|
|
|
245 |
static function retournerRequeteCondition($cond) {
|
964 |
raphael |
246 |
return $cond ? (' WHERE '.implode(' AND ', $cond)) : '';
|
548 |
mathilde |
247 |
}
|
|
|
248 |
|
|
|
249 |
|
959 |
raphael |
250 |
static function calculerTotalResultat($db, $table, Array $join, Array $cond) {
|
|
|
251 |
$requete = sprintf(
|
964 |
raphael |
252 |
'SELECT count(*) as nombre FROM %s %s %s -- %s:%d',
|
|
|
253 |
$table,
|
|
|
254 |
$join ? implode(' ', $join) : '',
|
|
|
255 |
$cond ? (' WHERE '.implode(' AND ', $cond)) : '',
|
|
|
256 |
__FILE__,
|
|
|
257 |
__LINE__);
|
959 |
raphael |
258 |
$res = $db->recuperer($requete);
|
|
|
259 |
if ($res && $res['nombre']) return $res['nombre'];
|
964 |
raphael |
260 |
throw new Exception('Données introuvables dans la base', RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
|
548 |
mathilde |
261 |
}
|
|
|
262 |
|
|
|
263 |
public function delimiterResultatsRequete() {
|
959 |
raphael |
264 |
$this->total_resultat = self::calculerTotalResultat(
|
964 |
raphael |
265 |
$this->getBdd(),
|
|
|
266 |
$this->tables['index'],
|
|
|
267 |
$this->requete_jointure,
|
|
|
268 |
$this->requete_condition);
|
548 |
mathilde |
269 |
$requete_limite = '';
|
964 |
raphael |
270 |
if (($this->limite_requete['depart'] <= $this->total_resultat) ){
|
|
|
271 |
if ( $this->limite_requete['depart'] < $this->total_resultat ){
|
|
|
272 |
$requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
|
548 |
mathilde |
273 |
.$this->limite_requete['limite'];
|
964 |
raphael |
274 |
}
|
|
|
275 |
} else {
|
|
|
276 |
$e = "Erreur : la valeur pour le paramètre navigation.départ est supérieure".
|
|
|
277 |
" au nombre total de résultats.";
|
|
|
278 |
throw new Exception($e, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
|
|
|
279 |
}
|
548 |
mathilde |
280 |
return $requete_limite;
|
|
|
281 |
}
|
|
|
282 |
|
959 |
raphael |
283 |
static function retournerRequeteJointure($join) {
|
964 |
raphael |
284 |
return $join ? implode(' ', $join) : '';
|
548 |
mathilde |
285 |
}
|
|
|
286 |
|
379 |
mathilde |
287 |
}
|