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 |
}
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
548 |
mathilde |
109 |
|
|
|
110 |
|
525 |
mathilde |
111 |
public function traiterParametres() {
|
|
|
112 |
if (isset($this->parametres) && !empty($this->parametres) ) {
|
|
|
113 |
foreach ($this->parametres as $param => $valeur) {
|
|
|
114 |
switch ($param) {
|
964 |
raphael |
115 |
case 'navigation.depart' :
|
|
|
116 |
if(!isset($this->parametres['navigation.limite']))
|
|
|
117 |
throw new Exception("indiquez également la valeur pour le paramètre navigation.limite.",
|
|
|
118 |
RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
|
|
119 |
$this->definirNavigationDepart($valeur);
|
|
|
120 |
break;
|
|
|
121 |
case 'navigation.limite' :
|
|
|
122 |
if(!isset($this->parametres['navigation.depart']))
|
|
|
123 |
throw new Exception("indiquez également la valeur pour le paramètre navigation.depart.",
|
|
|
124 |
RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
|
|
125 |
$this->definirNavigationLimite($valeur);
|
|
|
126 |
break;
|
|
|
127 |
case 'version.projet' :
|
|
|
128 |
$this->traiterVersion($valeur);
|
|
|
129 |
break;
|
|
|
130 |
case 'masque.cat' :
|
|
|
131 |
$this->masque = 'catminat='.str_replace('-','/',$valeur);
|
|
|
132 |
$this->requete_condition[] = " catminat_code = '".str_replace('-','/',$valeur)."' ";
|
|
|
133 |
break;
|
|
|
134 |
default :
|
|
|
135 |
$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
|
|
|
136 |
.$param.' " n\'existe pas.';
|
525 |
mathilde |
137 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e); break;
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
|
548 |
mathilde |
143 |
|
|
|
144 |
|
525 |
mathilde |
145 |
//+++------------------------------traitement des versions----------------------------------------++
|
|
|
146 |
|
|
|
147 |
public function traiterVersion($valeur) {
|
|
|
148 |
if (preg_match('/^[0-9]+(?:[._][0-9]+)*$/', $valeur) || $valeur == '*' || $valeur == '+') {
|
|
|
149 |
$this->version_projet = $valeur;
|
|
|
150 |
} else {
|
|
|
151 |
$e = "Erreur : La version est inconnue.";
|
|
|
152 |
throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
|
|
153 |
}
|
|
|
154 |
if ($this->version_projet == '*' && empty($this->ressources)) {
|
|
|
155 |
$message = "L'affichage de plusieurs versions ne fonctionne que pour les ressources de type /ressources/#id";
|
|
|
156 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
157 |
throw new Exception($message, $code);
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
|
596 |
mathilde |
161 |
public function definirTables($prefixe_table) {
|
525 |
mathilde |
162 |
$table_num_version = $this->recupererVersionDisponible();
|
|
|
163 |
foreach ($prefixe_table as $nom => $prefixe ) {
|
|
|
164 |
if ( in_array($this->version_projet,$table_num_version) ) {
|
|
|
165 |
$tables[$nom] = $prefixe.'_v'.$this->version_projet;
|
|
|
166 |
} elseif ($this->version_projet == '+') {
|
|
|
167 |
$derniere_version = $table_num_version[count($table_num_version) - 1];
|
|
|
168 |
$tables[$nom] = $prefixe.'_v'.str_replace('.', '_', $derniere_version);
|
|
|
169 |
} else {
|
|
|
170 |
$e = "Erreur : La version est inconnue.";
|
|
|
171 |
throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
|
|
172 |
}
|
|
|
173 |
}
|
596 |
mathilde |
174 |
$this->tables = $tables;
|
525 |
mathilde |
175 |
}
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
//+--------------------------formatages de resultats -------------------------------------------+
|
|
|
179 |
|
|
|
180 |
public function retournerResultatFormate($resultat) {
|
|
|
181 |
$resultat_json = array();
|
|
|
182 |
$resultat_json['entete'] = $this->ajouterEnteteResultat();
|
615 |
mathilde |
183 |
$resultat_json['resultats'] = array();
|
525 |
mathilde |
184 |
foreach ($resultat as $ligne => $tab) {
|
|
|
185 |
$num = $tab['index.cle'];
|
548 |
mathilde |
186 |
|
783 |
raphael |
187 |
$resultat_json['resultats'][$num]['num_nomen'] =
|
525 |
mathilde |
188 |
(empty ($tab['baseflor.nn'])) ? $tab['rangSup.nn'] : $tab['baseflor.nn'];
|
548 |
mathilde |
189 |
|
525 |
mathilde |
190 |
$resultat_json['resultats'][$num]['bdnt'] =
|
548 |
mathilde |
191 |
(empty ($tab['baseflor.bdnt'])) ? $tab['rangSup.bdnt'] : $tab['baseflor.bdnt'];
|
|
|
192 |
|
525 |
mathilde |
193 |
$resultat_json['resultats'][$num]['catminat_code'] =
|
548 |
mathilde |
194 |
(empty ($tab['catminat_code'])) ? '' : $tab['catminat_code'];
|
|
|
195 |
|
942 |
delphine |
196 |
$resultat_json['resultats'][$num]['nom_sci'] =
|
|
|
197 |
(empty ($tab['nom_sci'])) ? '' : $tab['nom_sci'];
|
614 |
mathilde |
198 |
|
525 |
mathilde |
199 |
$resultat_json['resultats'][$num]['num_taxon'] =
|
548 |
mathilde |
200 |
(empty ($tab['num_taxon'])) ? '' : $tab['num_taxon'];
|
525 |
mathilde |
201 |
|
548 |
mathilde |
202 |
if ($resultat_json['resultats'][$num]['num_nomen'] != 0) {
|
|
|
203 |
$bdnt = strtolower($resultat_json['resultats'][$num]['bdnt']);
|
|
|
204 |
$nn = $resultat_json['resultats'][$num]['num_nomen'];
|
|
|
205 |
$resultat_json['resultats'][$num]['href'] =
|
964 |
raphael |
206 |
$this->ajouterHref('informations',$bdnt.".nn:".$nn);
|
549 |
mathilde |
207 |
}
|
525 |
mathilde |
208 |
}
|
|
|
209 |
return $resultat_json;
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
|
|
|
213 |
public function ajouterEnteteResultat() {
|
548 |
mathilde |
214 |
$entete['depart'] = $this->limite_requete['depart'];
|
|
|
215 |
$entete['limite'] = $this->limite_requete['limite'];
|
964 |
raphael |
216 |
$entete['total'] = $this->total_resultat;
|
615 |
mathilde |
217 |
if ($this->masque) {
|
|
|
218 |
$entete['masque'] = $this->masque;
|
|
|
219 |
}
|
964 |
raphael |
220 |
$entete['version'] = $this->version_projet;
|
548 |
mathilde |
221 |
$url = $this->formulerUrl($this->total_resultat, '/informations');
|
525 |
mathilde |
222 |
if (isset($url['precedent']) && $url['precedent'] != '') {
|
|
|
223 |
$entete['href.precedent'] = $url['precedent'];
|
|
|
224 |
}
|
|
|
225 |
if (isset($url['suivant']) && $url['suivant'] != '') {
|
964 |
raphael |
226 |
$entete['href.suivant'] = $url['suivant'];
|
525 |
mathilde |
227 |
}
|
|
|
228 |
return $entete;
|
|
|
229 |
}
|
|
|
230 |
|
548 |
mathilde |
231 |
|
|
|
232 |
//+-------------------------------------assemblage de requête------------------------------------//
|
|
|
233 |
|
|
|
234 |
|
|
|
235 |
public function assemblerLaRequete() {
|
964 |
raphael |
236 |
$requete = ' SELECT '.$this->champs_recherches.
|
|
|
237 |
' FROM '.$this->tables['index'].' '
|
|
|
238 |
.self::retournerRequeteJointure($this->requete_jointure).' '
|
|
|
239 |
.self::retournerRequeteCondition($this->requete_condition).' '
|
|
|
240 |
.$this->delimiterResultatsRequete();
|
548 |
mathilde |
241 |
return $requete;
|
|
|
242 |
}
|
959 |
raphael |
243 |
|
|
|
244 |
static function retournerRequeteCondition($cond) {
|
964 |
raphael |
245 |
return $cond ? (' WHERE '.implode(' AND ', $cond)) : '';
|
548 |
mathilde |
246 |
}
|
|
|
247 |
|
|
|
248 |
|
959 |
raphael |
249 |
static function calculerTotalResultat($db, $table, Array $join, Array $cond) {
|
|
|
250 |
$requete = sprintf(
|
964 |
raphael |
251 |
'SELECT count(*) as nombre FROM %s %s %s -- %s:%d',
|
|
|
252 |
$table,
|
|
|
253 |
$join ? implode(' ', $join) : '',
|
|
|
254 |
$cond ? (' WHERE '.implode(' AND ', $cond)) : '',
|
|
|
255 |
__FILE__,
|
|
|
256 |
__LINE__);
|
959 |
raphael |
257 |
$res = $db->recuperer($requete);
|
|
|
258 |
if ($res && $res['nombre']) return $res['nombre'];
|
964 |
raphael |
259 |
throw new Exception('Données introuvables dans la base', RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
|
548 |
mathilde |
260 |
}
|
|
|
261 |
|
|
|
262 |
public function delimiterResultatsRequete() {
|
959 |
raphael |
263 |
$this->total_resultat = self::calculerTotalResultat(
|
964 |
raphael |
264 |
$this->getBdd(),
|
|
|
265 |
$this->tables['index'],
|
|
|
266 |
$this->requete_jointure,
|
|
|
267 |
$this->requete_condition);
|
548 |
mathilde |
268 |
$requete_limite = '';
|
964 |
raphael |
269 |
if (($this->limite_requete['depart'] <= $this->total_resultat) ){
|
|
|
270 |
if ( $this->limite_requete['depart'] < $this->total_resultat ){
|
|
|
271 |
$requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
|
548 |
mathilde |
272 |
.$this->limite_requete['limite'];
|
964 |
raphael |
273 |
}
|
|
|
274 |
} else {
|
|
|
275 |
$e = "Erreur : la valeur pour le paramètre navigation.départ est supérieure".
|
|
|
276 |
" au nombre total de résultats.";
|
|
|
277 |
throw new Exception($e, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
|
|
|
278 |
}
|
548 |
mathilde |
279 |
return $requete_limite;
|
|
|
280 |
}
|
|
|
281 |
|
959 |
raphael |
282 |
static function retournerRequeteJointure($join) {
|
964 |
raphael |
283 |
return $join ? implode(' ', $join) : '';
|
548 |
mathilde |
284 |
}
|
|
|
285 |
|
379 |
mathilde |
286 |
}
|