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)){
|
|
|
98 |
$nums = explode(',', $valeur);
|
|
|
99 |
$total = count($nums);
|
|
|
100 |
$condition = '';
|
|
|
101 |
for ($i = 0; $i < $total; $i++) {
|
|
|
102 |
$condition .= " flore_bdtfx_".$type_masque." = {$nums[$i]}";
|
|
|
103 |
if ($i < ($total-1) ) {
|
|
|
104 |
$condition .= " OR ";
|
6 |
jpm |
105 |
}
|
|
|
106 |
}
|
490 |
mathilde |
107 |
$this->requete_condition[] = $condition;
|
|
|
108 |
} else {
|
|
|
109 |
$e = "valeur incorrecte pour masque.".$type_masque;
|
|
|
110 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE,$e);
|
6 |
jpm |
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
|
116 |
|
558 |
mathilde |
117 |
|
490 |
mathilde |
118 |
//+-------------------------------------------formatage résultats---------------------------------------------+
|
6 |
jpm |
119 |
|
496 |
mathilde |
120 |
public function formaterResultat($resultat) {
|
|
|
121 |
$versionResultat['entete'] = $this->formaterEnteteResultat();;
|
|
|
122 |
if ($resultat == '') {
|
|
|
123 |
$message = 'La requête SQL formée comporte une erreur!';
|
|
|
124 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
125 |
throw new Exception($message, $code);
|
|
|
126 |
} elseif ($resultat) {
|
534 |
mathilde |
127 |
$versionResultat = $this->retournerResultatFormate($resultat);
|
496 |
mathilde |
128 |
} else {
|
|
|
129 |
$versionResultat['resultats'] = array();
|
|
|
130 |
}
|
|
|
131 |
return $versionResultat;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
public function retournerResultatFormate($resultat) {
|
490 |
mathilde |
135 |
$resultat_json = array();
|
496 |
mathilde |
136 |
foreach ($resultat as $tab) {
|
|
|
137 |
$num_coste = $tab['num_nom'];
|
|
|
138 |
unset($tab['num_nom']);
|
|
|
139 |
$resultat_json['resultats'][$num_coste]['num_nomenclatural'] = $tab['flore_bdtfx_nn'];
|
|
|
140 |
$resultat_json['resultats'][$num_coste]['num_taxonomique'] = $tab['flore_bdtfx_nt'];
|
|
|
141 |
$resultat_json['resultats'][$num_coste]['binaire.href'] = $this->formaterUrlImage($tab['image']);
|
|
|
142 |
$resultat_json['resultats'][$num_coste]['mime'] = "images/png";
|
|
|
143 |
}
|
490 |
mathilde |
144 |
return $resultat_json;
|
6 |
jpm |
145 |
}
|
|
|
146 |
|
490 |
mathilde |
147 |
public function formaterUrlImage($fichier) {
|
|
|
148 |
$chemin = config::get('donnees')."2.00/img/$fichier";
|
|
|
149 |
return $chemin;
|
6 |
jpm |
150 |
}
|
|
|
151 |
|
490 |
mathilde |
152 |
|
6 |
jpm |
153 |
|
558 |
mathilde |
154 |
|
490 |
mathilde |
155 |
public function formaterEnteteResultat() {
|
|
|
156 |
$entete['depart'] = $this->limite_requete['depart'];
|
|
|
157 |
$entete['limite'] = $this->limite_requete['limite'];
|
|
|
158 |
$entete['total'] = $this->total_resultat;
|
|
|
159 |
$entete['masque'] = empty($this->masque) ? 'aucun' : implode('&', $this->masque);
|
|
|
160 |
$url = $this->formulerUrl($this->total_resultat, '/images');
|
|
|
161 |
if (isset($url['precedent']) && $url['precedent'] != '') {
|
|
|
162 |
$entete['href.precedent'] = $url['precedent'];
|
6 |
jpm |
163 |
}
|
490 |
mathilde |
164 |
if (isset($url['suivant']) && $url['suivant'] != '') {
|
|
|
165 |
$entete['href.suivant'] = $url['suivant'];
|
6 |
jpm |
166 |
}
|
490 |
mathilde |
167 |
return $entete;
|
6 |
jpm |
168 |
}
|
|
|
169 |
|
490 |
mathilde |
170 |
//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
|
6 |
jpm |
171 |
|
490 |
mathilde |
172 |
public function assemblerLaRequete() {
|
|
|
173 |
$requete = ' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
|
|
|
174 |
.$this->retournerRequeteCondition().' '
|
|
|
175 |
.$this->delimiterResultatsRequete();
|
|
|
176 |
return $requete;
|
6 |
jpm |
177 |
}
|
|
|
178 |
|
490 |
mathilde |
179 |
|
6 |
jpm |
180 |
|
490 |
mathilde |
181 |
public function retournerRequeteCondition() {
|
|
|
182 |
$condition = '';
|
|
|
183 |
if (empty($this->requete_condition) == false) {
|
|
|
184 |
$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
|
|
|
185 |
}
|
|
|
186 |
return $condition;
|
6 |
jpm |
187 |
}
|
|
|
188 |
|
490 |
mathilde |
189 |
|
6 |
jpm |
190 |
|
490 |
mathilde |
191 |
public function calculerTotalResultat() {
|
|
|
192 |
$requete = 'SELECT count(*) as nombre FROM '.$this->table.' '
|
|
|
193 |
.$this->retournerRequeteCondition();
|
|
|
194 |
$res = $this->getBdd()->recuperer($requete);
|
6 |
jpm |
195 |
if ($res) {
|
490 |
mathilde |
196 |
$this->total_resultat = $res['nombre'];
|
|
|
197 |
} else {
|
|
|
198 |
$this->total_resultat = 0;
|
|
|
199 |
$e = 'Données introuvables dans la base';
|
|
|
200 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
|
6 |
jpm |
201 |
}
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
|
490 |
mathilde |
205 |
public function delimiterResultatsRequete() {
|
|
|
206 |
$this->calculerTotalResultat();
|
|
|
207 |
$requete_limite = '';
|
|
|
208 |
if ((count($this->ressources)) == 0) {
|
534 |
mathilde |
209 |
if (($this->limite_requete['depart'] < $this->total_resultat) &&
|
|
|
210 |
(($this->limite_requete['limite'] + $this->limite_requete['depart'] )
|
|
|
211 |
< $this->total_resultat )) {
|
490 |
mathilde |
212 |
$requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
|
|
|
213 |
.$this->limite_requete['limite'];
|
534 |
mathilde |
214 |
}
|
6 |
jpm |
215 |
}
|
490 |
mathilde |
216 |
return $requete_limite;
|
6 |
jpm |
217 |
}
|
|
|
218 |
|
|
|
219 |
}
|
|
|
220 |
?>
|