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