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