379 |
mathilde |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Classe Informations.php permet de faire des requetes de baseflor en fonction d'un référentiel
|
|
|
5 |
*( BDTFX, BDAFX, BDBFX ) et d'un numéro nomenclatural ( différent de 0 ).
|
|
|
6 |
* fin d'url possibles :
|
|
|
7 |
* /informations/#bdnt.nn:#num_nomen --> retourne tous les champs pour un BDNT et un num_nomen
|
|
|
8 |
* /informations/#bdnt.nn:#num_nomen?test=description --> retourne champs description pour un BDNT et un num_nomen
|
|
|
9 |
* /informations/#bdnt.nn:#num_nomen?test=ecologie --> retourne champs ecologiques pour un BDNT et un num_nomen
|
|
|
10 |
* /informations --> retourne les 10 premiers résultats
|
|
|
11 |
* /informations?navigation.limite=..&navigation.depart=.. --> retourne les 10 premiers résultats avec limites
|
|
|
12 |
*
|
|
|
13 |
* Encodage en entrée : utf8
|
|
|
14 |
* Encodage en sortie : utf8
|
|
|
15 |
* @package eflore-projets
|
|
|
16 |
* @author Mathilde SALTHUN-LASSALLE <mathilde@tela-botanica.org>
|
|
|
17 |
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
|
|
|
18 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
19 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
20 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
21 |
* @version 1.0
|
|
|
22 |
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
class Informations extends Commun{
|
|
|
26 |
|
|
|
27 |
protected $table="";
|
|
|
28 |
private $requete_condition = "";
|
|
|
29 |
/** Permet de stocker la requete formulée: /informations/bdnt.nn | */
|
|
|
30 |
private $format_reponse = 'informations';
|
|
|
31 |
private $total_resultat;
|
395 |
mathilde |
32 |
private $retour_format = 'max';
|
391 |
mathilde |
33 |
protected $limite_requete = array( 'depart' => 0, 'limite' => 10);
|
395 |
mathilde |
34 |
private $champs_recherches = '*';
|
|
|
35 |
private $champs_ontologiques = array ('ve_lumiere' => 'VEL', 've_temperature' => 'VET',
|
|
|
36 |
've_continentalite' => 'VEC', 've_humidite_atmos' => 'VEHA',
|
|
|
37 |
've_humidite_edaph' => 'VEHE', 've_reaction_sol' => 'VER' ,
|
|
|
38 |
've_nutriments_sol' => 'VEN', 've_salinite'=> 'VES' ,
|
|
|
39 |
've_texture_sol' => 'VETX', 've_mat_org_sol' => 'VEMO');
|
|
|
40 |
|
|
|
41 |
//+--------------------------traitement ressources ou paramètres -------------------------------------------+
|
379 |
mathilde |
42 |
public function traiterParametres() {
|
|
|
43 |
if (isset($this->parametres) && !empty($this->parametres)) {
|
|
|
44 |
foreach ($this->parametres as $param => $valeur) {
|
|
|
45 |
switch ($param) {
|
|
|
46 |
case 'navigation.depart' :
|
|
|
47 |
$this->limite_requete['depart'] = $valeur; break;
|
|
|
48 |
case 'navigation.limite' : $this->limite_requete['limite'] = $valeur; break;
|
|
|
49 |
case 'test' :
|
|
|
50 |
if ($valeur == "description") {
|
|
|
51 |
$this->champs_recherches="cle, chorologie, inflorescence, sexualite, ordre_maturation, "
|
|
|
52 |
."pollinisation, fruit, dissemination, couleur_fleur, macule, floraison, type_bio, "
|
|
|
53 |
."form_vegetale ";
|
|
|
54 |
}elseif ($valeur == "ecologie"){
|
|
|
55 |
$this->champs_recherches="cle, ve_lumiere , ve_temperature, ve_continentalite,
|
|
|
56 |
ve_humidite_atmos, ve_humidite_edaph, ve_reaction_sol, ve_nutriments_sol, ve_salinite,
|
|
|
57 |
ve_texture_sol, ve_mat_org_sol ";
|
|
|
58 |
}else {
|
|
|
59 |
$e = 'Valeur de paramètre inconnue pour \'test\'. ';
|
|
|
60 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
|
|
|
61 |
}
|
|
|
62 |
break;
|
395 |
mathilde |
63 |
case 'retour.format' :
|
|
|
64 |
$this->retour_format = $valeur;
|
|
|
65 |
break;
|
379 |
mathilde |
66 |
default :
|
|
|
67 |
$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
|
|
|
68 |
.$param.' " n\'existe pas.';
|
|
|
69 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e); break;
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
public function traiterRessources() {
|
|
|
76 |
if (isset($this->ressources) && !empty($this->ressources[0])) {
|
|
|
77 |
if(preg_match('/^(.+)\.nn:([0-9]+)$/', $this->ressources[0], $retour)==1){
|
|
|
78 |
switch ($retour[1]) {
|
385 |
mathilde |
79 |
case 'bdtfx' :
|
379 |
mathilde |
80 |
$this->requete_condition[] = "num_nomen = ".$retour[2]." AND BDNT = 'BDTFX' ";
|
|
|
81 |
|
|
|
82 |
break;
|
385 |
mathilde |
83 |
case 'bdafx' :
|
379 |
mathilde |
84 |
$this->requete_condition[] = "num_nomen = ".$retour[2]." AND BDNT = 'BDAFX' ";
|
|
|
85 |
break;
|
385 |
mathilde |
86 |
case 'bdbfx' :
|
379 |
mathilde |
87 |
$this->requete_condition[] = "num_nomen = ".$retour[2]." AND BDNT = 'BDBFX' ";
|
|
|
88 |
break;
|
|
|
89 |
default :
|
|
|
90 |
$e = 'Erreur dans l\'url de votre requête : </br> La ressource " '
|
|
|
91 |
.$retour[1].' " n\'existe pas.';
|
|
|
92 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
|
|
|
93 |
break;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
}
|
|
|
97 |
} else {
|
|
|
98 |
$this->champs_recherches=" cle, catminat_code, BDNT, num_taxon, num_nomen ";
|
|
|
99 |
}
|
|
|
100 |
}
|
395 |
mathilde |
101 |
|
|
|
102 |
//+--------------------------formatages de resultats -------------------------------------------+
|
379 |
mathilde |
103 |
public function retournerResultatFormate($resultat) {
|
395 |
mathilde |
104 |
if ((count($this->ressources)) == 0) {
|
390 |
mathilde |
105 |
$table_retour_json['resultat'] = $this->formaterResultats($resultat);
|
|
|
106 |
} else {
|
395 |
mathilde |
107 |
$table_retour_json = $this->formaterResultatsInformationsBDNTnn($resultat);
|
379 |
mathilde |
108 |
}
|
390 |
mathilde |
109 |
return $table_retour_json;
|
|
|
110 |
}
|
|
|
111 |
|
395 |
mathilde |
112 |
public function formaterResultatsInformationsBDNTnn($resultat){
|
|
|
113 |
$resultat_json=array();
|
|
|
114 |
$resultat_json = $resultat[0];
|
|
|
115 |
if ($this->retour_format == 'max'){
|
|
|
116 |
if ($resultat_json['type_bio'] != ""){
|
|
|
117 |
$tab_ontologie = $this->recupererOntologiePrTypeBio($resultat_json['type_bio']);
|
|
|
118 |
unset($resultat_json['type_bio']);
|
|
|
119 |
$resultat_json= array_merge($resultat_json,$tab_ontologie);
|
|
|
120 |
}
|
|
|
121 |
foreach ($this->champs_ontologiques as $cle => $valeur){
|
|
|
122 |
if ($resultat_json[$cle] != ""){
|
|
|
123 |
$tab_ontologie = $this->recupererOntologies($resultat_json[$cle], $cle);
|
|
|
124 |
unset($resultat_json[$cle]);
|
|
|
125 |
$resultat_json= array_merge($resultat_json,$tab_ontologie);
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
return $resultat_json;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
public function formaterResultatsInformations($resultat) {
|
|
|
133 |
$resultat_json=array();
|
|
|
134 |
$resultat_json['entete'] = $this->ajouterEnteteResultat();
|
379 |
mathilde |
135 |
foreach ($resultat as $tab) {
|
|
|
136 |
$num = $tab['cle'];
|
|
|
137 |
unset($tab['cle']);
|
|
|
138 |
foreach ($tab as $param => $valeur) {
|
|
|
139 |
$resultat_json[$num][$param] = $valeur;
|
|
|
140 |
}
|
391 |
mathilde |
141 |
$resultat_json[$num]['href'] = $this->ajouterHref('ontologies', $num);
|
379 |
mathilde |
142 |
}
|
390 |
mathilde |
143 |
return $resultat_json;
|
379 |
mathilde |
144 |
}
|
|
|
145 |
public function ajouterEnteteResultat() {
|
|
|
146 |
$entete['depart'] = $this->limite_requete['depart'];
|
|
|
147 |
$entete['limite'] = $this->limite_requete['limite'];
|
|
|
148 |
$entete['total'] = $this->total_resultat;
|
|
|
149 |
$entete['version'] = $this->version_projet;
|
391 |
mathilde |
150 |
$url = $this->formulerUrl($this->total_resultat, '/informations');
|
|
|
151 |
if (isset($url['precedent']) && $url['precedent'] != '') {
|
|
|
152 |
$entete['href.precedent'] = $url['precedent'];
|
|
|
153 |
}
|
|
|
154 |
if (isset($url['suivant']) && $url['suivant'] != '') {
|
|
|
155 |
$entete['href.suivant'] = $url['suivant'];
|
|
|
156 |
}
|
379 |
mathilde |
157 |
return $entete;
|
|
|
158 |
}
|
|
|
159 |
|
395 |
mathilde |
160 |
|
|
|
161 |
//+--------------------------traitement ontologies -------------------------------------------+
|
|
|
162 |
public function recupererOntologies($valeur, $champs){
|
|
|
163 |
$url = $this->ajouterHref('ontologies',$this->champs_ontologiques[$champs].':'.urlencode(urlencode($valeur)));
|
|
|
164 |
$val = $this->consulterHref($url);
|
|
|
165 |
$resultat_json[$champs.'.libelle'] = $val->nom;
|
|
|
166 |
$resultat_json[$champs.'.code'] = $valeur;
|
|
|
167 |
$resultat_json[$champs.'.href'] = $url;
|
|
|
168 |
return $resultat_json;
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
public function recupererOntologiePrTypeBio($type_a_traiter){
|
|
|
174 |
$resultat_typeBio = "";
|
|
|
175 |
if ($type_a_traiter != ""){
|
|
|
176 |
$resultat_typeBio['type_bio.code'] = $type_a_traiter;
|
|
|
177 |
$types_prim_sec = $this->separerTypesPrimairesSecondaires($type_a_traiter);
|
|
|
178 |
foreach ($types_prim_sec as $cle => $tb){
|
|
|
179 |
$types_sstypes[$cle]= $this->separerTypes_SousTypes($tb, $cle);
|
|
|
180 |
}
|
|
|
181 |
switch (count($types_sstypes)){
|
|
|
182 |
case 1 :
|
|
|
183 |
$types_sstypes = $types_sstypes['principal'];
|
|
|
184 |
break;
|
|
|
185 |
case 2 :
|
|
|
186 |
$types_sstypes = array_merge($types_sstypes['principal'],$types_sstypes['secondaire']);
|
|
|
187 |
break;
|
|
|
188 |
case 3 :
|
|
|
189 |
$types_sstypes = array_merge($types_sstypes['principal'],
|
|
|
190 |
$types_sstypes['secondaire'],
|
|
|
191 |
$types_sstypes['special']);
|
|
|
192 |
break;
|
|
|
193 |
default :
|
|
|
194 |
break;
|
|
|
195 |
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
foreach ($types_sstypes as $cle => $tb){
|
|
|
199 |
if (($cle == 'type.principal') || ($cle == 'type.secondaire')){
|
|
|
200 |
$url = $this->ajouterHref('ontologies','TB:'.urlencode(urlencode($tb)));
|
|
|
201 |
$val = $this->consulterHref($url);
|
|
|
202 |
$libelle[$cle]= $val->nom;
|
|
|
203 |
$detail[$cle]['libelle'] = $val->nom;
|
|
|
204 |
$detail[$cle]['code'] = $val->code;
|
|
|
205 |
$detail[$cle]['href'] = $url;
|
|
|
206 |
}
|
|
|
207 |
if ( ($cle == 'sous_type.principal') || ($cle == 'sous_type.secondaire') || ($cle == 'sous_type.special') ){
|
|
|
208 |
$i = 0;
|
|
|
209 |
foreach ($tb as $sstypes){
|
|
|
210 |
$url = $this->ajouterHref('ontologies','ssTT:'.urlencode(urlencode($sstypes)));
|
|
|
211 |
$val = $this->consulterHref($url);
|
|
|
212 |
$libelle[$cle]=$val->nom;
|
|
|
213 |
$detail[$cle][$i]['libelle'] = $val->nom;
|
|
|
214 |
$detail[$cle][$i]['code'] = $val->code;
|
|
|
215 |
$detail[$cle][$i]['href'] = $url;
|
|
|
216 |
$i++;
|
|
|
217 |
}
|
|
|
218 |
}
|
|
|
219 |
}
|
|
|
220 |
$resultat_typeBio['type_bio.libelle']= $this->formaterTypeBioPrLibelle($libelle);
|
|
|
221 |
$resultat_typeBio['type_bio.detail'] = $detail;
|
|
|
222 |
}
|
|
|
223 |
return $resultat_typeBio;
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
|
|
|
227 |
public function formaterTypeBioPrLibelle($info){
|
|
|
228 |
$libelle = $info['type.principal']." ";
|
|
|
229 |
$libelle .= isset($info['sous_type.principal']) ? " (".$info['sous_type.principal'].") " : "";
|
|
|
230 |
$libelle .= isset($info['type.secondaire']) ? ", ".$info['type.secondaire']." " : "";
|
|
|
231 |
$libelle .= isset($info['sous_type.secondaire']) ? "(".$info['sous_type.secondaire'].") " : "";
|
|
|
232 |
$libelle .= isset($info['sous_type.special']) ? "(".$info['sous_type.special'].") " : "";
|
|
|
233 |
return $libelle;
|
|
|
234 |
}
|
|
|
235 |
|
379 |
mathilde |
236 |
|
395 |
mathilde |
237 |
public function separerTypesPrimairesSecondaires($type){
|
|
|
238 |
$types = array();
|
|
|
239 |
//séparation des principaux et des secondaires
|
|
|
240 |
if (( preg_match("/(.+)\((.+)\)\-{0,1}(.+){0,1}/", $type,$retour) == 1 )){
|
|
|
241 |
$types['principal'] = $retour[1];
|
|
|
242 |
$types['secondaire'] = $retour[2];
|
|
|
243 |
if (isset($retour[3])){$types['special'] = $retour[3];}
|
|
|
244 |
}else {
|
|
|
245 |
$types['principal'] = $type;
|
|
|
246 |
}
|
|
|
247 |
return $types;
|
|
|
248 |
}
|
379 |
mathilde |
249 |
|
395 |
mathilde |
250 |
public function separerTypes_SousTypes($type_a_traiter, $typePrimOuSec){
|
|
|
251 |
$types = array();
|
|
|
252 |
if(( preg_match("/^([a-zA-Zé]+)\-(.+)$|^([a-zA-Zé]+[^\-])$/", $type_a_traiter, $retour) == 1 )){
|
|
|
253 |
//recupère le type unique
|
|
|
254 |
|
|
|
255 |
$type = (isset($retour[3]) && ($typePrimOuSec != 'special') ) ? $retour[3] : $retour[1] ;
|
|
|
256 |
$types['type.'.$typePrimOuSec]= $type;
|
|
|
257 |
//recupère le ou les sous-types
|
|
|
258 |
if ((isset($retour[3]) && ($typePrimOuSec == 'special') )){
|
|
|
259 |
$ss_types[0] = (isset($retour[3]) && ($typePrimOuSec == 'special') ) ? $retour[3] : "" ;
|
|
|
260 |
}elseif(isset($retour[2]) && ($retour[2] != "")){
|
|
|
261 |
$ss_types[0]= (isset($retour[2]) && ($retour[2] != "") ) ? $retour[2] : "" ;
|
|
|
262 |
}
|
|
|
263 |
if (isset($ss_types[0])){
|
|
|
264 |
if (( preg_match("/\-/", $ss_types[0])) == 1 ){
|
|
|
265 |
$ss_types = preg_split("/\-/", $ss_types[0]);
|
|
|
266 |
}
|
|
|
267 |
for($i = 0 ; $i < count($ss_types); $i++){
|
|
|
268 |
$types['sous_type.'.$typePrimOuSec][$i] = $ss_types[$i];
|
|
|
269 |
}
|
|
|
270 |
}
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
return $types;
|
|
|
274 |
}
|
379 |
mathilde |
275 |
|
|
|
276 |
|
395 |
mathilde |
277 |
//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
|
|
|
278 |
|
379 |
mathilde |
279 |
public function assemblerLaRequete() {
|
|
|
280 |
$requete = ' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
|
|
|
281 |
.$this->retournerRequeteCondition().' '
|
|
|
282 |
.$this->delimiterResultatsRequete();
|
|
|
283 |
return $requete;
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
|
|
|
287 |
|
|
|
288 |
public function retournerRequeteCondition() {
|
|
|
289 |
$condition = '';
|
|
|
290 |
if ($this->requete_condition !== "") {
|
|
|
291 |
$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
|
|
|
292 |
}
|
|
|
293 |
return $condition;
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
|
|
|
297 |
|
|
|
298 |
public function calculerTotalResultat() {
|
|
|
299 |
//on récupère le nombre total de résultats de la requete
|
|
|
300 |
$requete = 'SELECT count(*) as nombre FROM '.$this->table.' '
|
|
|
301 |
.$this->retournerRequeteCondition();
|
|
|
302 |
$res = $this->getBdd()->recuperer($requete);
|
|
|
303 |
if ($res) {
|
|
|
304 |
$this->total_resultat = $res['nombre'];
|
|
|
305 |
} else {
|
|
|
306 |
$this->total_resultat = 0;
|
|
|
307 |
$e = 'Données introuvables dans la base';
|
|
|
308 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
|
|
|
309 |
Debug::printr($requete);
|
|
|
310 |
}
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
public function delimiterResultatsRequete() {
|
|
|
314 |
$this->calculerTotalResultat();
|
|
|
315 |
if ((count($this->ressources)) == 0) {
|
|
|
316 |
if (($this->limite_requete['depart'] < $this->total_resultat) &&
|
|
|
317 |
(($this->limite_requete['depart'] + $this->limite_requete['limite'])
|
|
|
318 |
< $this->total_resultat )){
|
|
|
319 |
$this->requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
|
|
|
320 |
.$this->limite_requete['limite'];
|
|
|
321 |
}else {
|
|
|
322 |
$e = 'Données introuvables dans la base. ';
|
|
|
323 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
|
|
|
324 |
}
|
|
|
325 |
}else {
|
|
|
326 |
$this->requete_limite='';
|
|
|
327 |
}
|
|
|
328 |
return $this->requete_limite;
|
|
|
329 |
}
|
|
|
330 |
}
|