6 |
jpm |
1 |
<?php
|
|
|
2 |
|
490 |
mathilde |
3 |
/**
|
|
|
4 |
* classe pour web service qui affiche les images de Coste.
|
|
|
5 |
* il n'existe qu'une seule version pour les images de Coste : 2.00
|
|
|
6 |
* retour en application/json
|
|
|
7 |
*
|
|
|
8 |
* exemple d'appel du service :
|
|
|
9 |
* .../service:eflore:0.1/coste/images?masque.nt=1053
|
|
|
10 |
* .../service:eflore:0.1/coste/images?masque.nn=39594,39601
|
|
|
11 |
* .../service:eflore:0.1/coste/images
|
|
|
12 |
*
|
|
|
13 |
* paramètres disponibles : navigation.depart, navigation.limite, masque.nn, masque.nt
|
|
|
14 |
*
|
|
|
15 |
* @package eFlore/services
|
|
|
16 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
17 |
* @author Mathilde Salthun-Lassalle <mathilde@tela-botanica.org>
|
|
|
18 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
19 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
20 |
* @version 1.0
|
|
|
21 |
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
|
|
|
22 |
*/
|
6 |
jpm |
23 |
|
|
|
24 |
class Images extends Commun {
|
|
|
25 |
|
490 |
mathilde |
26 |
protected $table ;
|
|
|
27 |
private $requete_condition = array(" image != '' ");
|
|
|
28 |
private $champs_recherches = " num_nom, image, flore_bdtfx_nn, flore_bdtfx_nt ";
|
|
|
29 |
private $total_resultat;
|
|
|
30 |
protected $limite_requete = array('depart' => 0, 'limite' => 100);
|
|
|
31 |
private $masque = array();
|
6 |
jpm |
32 |
|
527 |
mathilde |
33 |
|
490 |
mathilde |
34 |
|
6 |
jpm |
35 |
|
|
|
36 |
public function consulter($ressources, $parametres) {
|
490 |
mathilde |
37 |
$this->ressources = $ressources;
|
|
|
38 |
$this->parametres = $parametres;
|
|
|
39 |
$this->traiterRessources();
|
|
|
40 |
$this->traiterParametres();
|
|
|
41 |
$this->table = config::get('bdd_table').'_v2_00';
|
|
|
42 |
$requete = $this->assemblerLaRequete();
|
|
|
43 |
$resultat = $this->getBdd()->recupererTous($requete);
|
496 |
mathilde |
44 |
$resultats = $this->formaterResultat($resultat);
|
490 |
mathilde |
45 |
return $resultats;
|
6 |
jpm |
46 |
}
|
|
|
47 |
|
490 |
mathilde |
48 |
//+-----------------------------------traitement ressources et paramètres --------------------------------+
|
|
|
49 |
|
|
|
50 |
|
|
|
51 |
public function traiterRessources() {
|
|
|
52 |
if (empty($this->ressources) ) {
|
|
|
53 |
if (isset($this->parametres['masque.nn']) == false
|
|
|
54 |
&& isset($this->parametres['masque.nt']) == false) {
|
|
|
55 |
$this->requete_condition[] = " flore_bdtfx_nt != '' AND flore_bdtfx_nn != '' ";
|
|
|
56 |
}
|
6 |
jpm |
57 |
} else {
|
490 |
mathilde |
58 |
$e = "La ressource {$this->ressources[0]} n'existe pas.";
|
|
|
59 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE,$e);
|
6 |
jpm |
60 |
}
|
|
|
61 |
}
|
|
|
62 |
|
490 |
mathilde |
63 |
public function traiterParametres() {
|
|
|
64 |
foreach ($this->parametres as $param => $val ) {
|
|
|
65 |
switch ($param) {
|
|
|
66 |
case 'masque.nt' :
|
|
|
67 |
$this->analyserMasque($val,'nt');
|
|
|
68 |
break;
|
|
|
69 |
case 'masque.nn' :
|
|
|
70 |
$this->analyserMasque($val,'nn');
|
|
|
71 |
break;
|
|
|
72 |
case 'navigation.depart' :
|
|
|
73 |
$this->limite_requete['depart'] = $val;
|
|
|
74 |
break;
|
|
|
75 |
case 'navigation.limite' :
|
|
|
76 |
$this->limite_requete['limite'] = $val;
|
|
|
77 |
break;
|
|
|
78 |
default :
|
|
|
79 |
$e = 'Erreur dans les paramètres de recherche de votre requête : </br> Le parametre " '
|
|
|
80 |
.$param.' " n\'existe pas.';
|
|
|
81 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE,$e);
|
|
|
82 |
break;
|
6 |
jpm |
83 |
}
|
490 |
mathilde |
84 |
}
|
6 |
jpm |
85 |
}
|
|
|
86 |
|
|
|
87 |
|
490 |
mathilde |
88 |
|
6 |
jpm |
89 |
|
490 |
mathilde |
90 |
private function analyserMasque($valeur, $type_masque) {
|
|
|
91 |
$this->masque[] = "$type_masque=$valeur";
|
|
|
92 |
if (empty($valeur)) {
|
|
|
93 |
$e = 'renseignez une valeur pour masque.nn';
|
|
|
94 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE,$e);
|
|
|
95 |
} elseif (preg_match('/^[0-9]+$/', $valeur)) {
|
|
|
96 |
$this->requete_condition[] = " flore_bdtfx_".$type_masque." = $valeur";
|
|
|
97 |
} elseif (preg_match('/[0-9]+/', $valeur)){
|
571 |
aurelien |
98 |
// suppression des valeurs vides
|
|
|
99 |
while(strstr($valeur,',,')) {
|
|
|
100 |
$valeur = str_replace(',,',',',$valeur);
|
6 |
jpm |
101 |
}
|
611 |
delphine |
102 |
$condition = " flore_bdtfx_".$type_masque." IN ($valeur) AND flore_bdtfx_".$type_masque." not in (0, '')";
|
490 |
mathilde |
103 |
$this->requete_condition[] = $condition;
|
|
|
104 |
} else {
|
|
|
105 |
$e = "valeur incorrecte pour masque.".$type_masque;
|
|
|
106 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE,$e);
|
6 |
jpm |
107 |
}
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
|
558 |
mathilde |
113 |
|
490 |
mathilde |
114 |
//+-------------------------------------------formatage résultats---------------------------------------------+
|
6 |
jpm |
115 |
|
496 |
mathilde |
116 |
public function formaterResultat($resultat) {
|
|
|
117 |
$versionResultat['entete'] = $this->formaterEnteteResultat();;
|
|
|
118 |
if ($resultat == '') {
|
|
|
119 |
$message = 'La requête SQL formée comporte une erreur!';
|
|
|
120 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
121 |
throw new Exception($message, $code);
|
|
|
122 |
} elseif ($resultat) {
|
534 |
mathilde |
123 |
$versionResultat = $this->retournerResultatFormate($resultat);
|
496 |
mathilde |
124 |
} else {
|
|
|
125 |
$versionResultat['resultats'] = array();
|
|
|
126 |
}
|
|
|
127 |
return $versionResultat;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
public function retournerResultatFormate($resultat) {
|
490 |
mathilde |
131 |
$resultat_json = array();
|
496 |
mathilde |
132 |
foreach ($resultat as $tab) {
|
|
|
133 |
$num_coste = $tab['num_nom'];
|
|
|
134 |
unset($tab['num_nom']);
|
|
|
135 |
$resultat_json['resultats'][$num_coste]['num_nomenclatural'] = $tab['flore_bdtfx_nn'];
|
|
|
136 |
$resultat_json['resultats'][$num_coste]['num_taxonomique'] = $tab['flore_bdtfx_nt'];
|
|
|
137 |
$resultat_json['resultats'][$num_coste]['binaire.href'] = $this->formaterUrlImage($tab['image']);
|
|
|
138 |
$resultat_json['resultats'][$num_coste]['mime'] = "images/png";
|
|
|
139 |
}
|
490 |
mathilde |
140 |
return $resultat_json;
|
6 |
jpm |
141 |
}
|
|
|
142 |
|
490 |
mathilde |
143 |
public function formaterUrlImage($fichier) {
|
|
|
144 |
$chemin = config::get('donnees')."2.00/img/$fichier";
|
|
|
145 |
return $chemin;
|
6 |
jpm |
146 |
}
|
|
|
147 |
|
490 |
mathilde |
148 |
|
6 |
jpm |
149 |
|
558 |
mathilde |
150 |
|
490 |
mathilde |
151 |
public function formaterEnteteResultat() {
|
|
|
152 |
$entete['depart'] = $this->limite_requete['depart'];
|
|
|
153 |
$entete['limite'] = $this->limite_requete['limite'];
|
|
|
154 |
$entete['total'] = $this->total_resultat;
|
|
|
155 |
$entete['masque'] = empty($this->masque) ? 'aucun' : implode('&', $this->masque);
|
|
|
156 |
$url = $this->formulerUrl($this->total_resultat, '/images');
|
|
|
157 |
if (isset($url['precedent']) && $url['precedent'] != '') {
|
|
|
158 |
$entete['href.precedent'] = $url['precedent'];
|
6 |
jpm |
159 |
}
|
490 |
mathilde |
160 |
if (isset($url['suivant']) && $url['suivant'] != '') {
|
|
|
161 |
$entete['href.suivant'] = $url['suivant'];
|
6 |
jpm |
162 |
}
|
490 |
mathilde |
163 |
return $entete;
|
6 |
jpm |
164 |
}
|
|
|
165 |
|
490 |
mathilde |
166 |
//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
|
6 |
jpm |
167 |
|
490 |
mathilde |
168 |
public function assemblerLaRequete() {
|
|
|
169 |
$requete = ' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
|
|
|
170 |
.$this->retournerRequeteCondition().' '
|
|
|
171 |
.$this->delimiterResultatsRequete();
|
|
|
172 |
return $requete;
|
6 |
jpm |
173 |
}
|
|
|
174 |
|
490 |
mathilde |
175 |
|
6 |
jpm |
176 |
|
490 |
mathilde |
177 |
public function retournerRequeteCondition() {
|
|
|
178 |
$condition = '';
|
|
|
179 |
if (empty($this->requete_condition) == false) {
|
|
|
180 |
$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
|
|
|
181 |
}
|
|
|
182 |
return $condition;
|
6 |
jpm |
183 |
}
|
|
|
184 |
|
490 |
mathilde |
185 |
|
6 |
jpm |
186 |
|
490 |
mathilde |
187 |
public function calculerTotalResultat() {
|
|
|
188 |
$requete = 'SELECT count(*) as nombre FROM '.$this->table.' '
|
|
|
189 |
.$this->retournerRequeteCondition();
|
|
|
190 |
$res = $this->getBdd()->recuperer($requete);
|
6 |
jpm |
191 |
if ($res) {
|
490 |
mathilde |
192 |
$this->total_resultat = $res['nombre'];
|
|
|
193 |
} else {
|
|
|
194 |
$this->total_resultat = 0;
|
|
|
195 |
$e = 'Données introuvables dans la base';
|
|
|
196 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
|
6 |
jpm |
197 |
}
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
|
490 |
mathilde |
201 |
public function delimiterResultatsRequete() {
|
|
|
202 |
$this->calculerTotalResultat();
|
|
|
203 |
$requete_limite = '';
|
|
|
204 |
if ((count($this->ressources)) == 0) {
|
534 |
mathilde |
205 |
if (($this->limite_requete['depart'] < $this->total_resultat) &&
|
|
|
206 |
(($this->limite_requete['limite'] + $this->limite_requete['depart'] )
|
|
|
207 |
< $this->total_resultat )) {
|
490 |
mathilde |
208 |
$requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
|
|
|
209 |
.$this->limite_requete['limite'];
|
534 |
mathilde |
210 |
}
|
6 |
jpm |
211 |
}
|
490 |
mathilde |
212 |
return $requete_limite;
|
6 |
jpm |
213 |
}
|
|
|
214 |
|
|
|
215 |
}
|
|
|
216 |
?>
|