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;
|
638 |
aurelien |
72 |
case 'referentiel' :
|
|
|
73 |
// ce paramètre est ignoré dans le cas de coste
|
|
|
74 |
// car seul le cel le prend en compte pour le moment
|
|
|
75 |
// on l'indique ici dans le switch car ce sont les mêmes url
|
|
|
76 |
// qui sont appelées pour le cel et coste (à retirer donc si ça change)
|
|
|
77 |
break;
|
490 |
mathilde |
78 |
case 'navigation.depart' :
|
|
|
79 |
$this->limite_requete['depart'] = $val;
|
|
|
80 |
break;
|
|
|
81 |
case 'navigation.limite' :
|
|
|
82 |
$this->limite_requete['limite'] = $val;
|
|
|
83 |
break;
|
|
|
84 |
default :
|
|
|
85 |
$e = 'Erreur dans les paramètres de recherche de votre requête : </br> Le parametre " '
|
|
|
86 |
.$param.' " n\'existe pas.';
|
|
|
87 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE,$e);
|
|
|
88 |
break;
|
6 |
jpm |
89 |
}
|
490 |
mathilde |
90 |
}
|
6 |
jpm |
91 |
}
|
|
|
92 |
|
|
|
93 |
|
490 |
mathilde |
94 |
|
6 |
jpm |
95 |
|
490 |
mathilde |
96 |
private function analyserMasque($valeur, $type_masque) {
|
|
|
97 |
$this->masque[] = "$type_masque=$valeur";
|
|
|
98 |
if (empty($valeur)) {
|
|
|
99 |
$e = 'renseignez une valeur pour masque.nn';
|
|
|
100 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE,$e);
|
|
|
101 |
} elseif (preg_match('/^[0-9]+$/', $valeur)) {
|
|
|
102 |
$this->requete_condition[] = " flore_bdtfx_".$type_masque." = $valeur";
|
|
|
103 |
} elseif (preg_match('/[0-9]+/', $valeur)){
|
571 |
aurelien |
104 |
// suppression des valeurs vides
|
|
|
105 |
while(strstr($valeur,',,')) {
|
|
|
106 |
$valeur = str_replace(',,',',',$valeur);
|
6 |
jpm |
107 |
}
|
611 |
delphine |
108 |
$condition = " flore_bdtfx_".$type_masque." IN ($valeur) AND flore_bdtfx_".$type_masque." not in (0, '')";
|
490 |
mathilde |
109 |
$this->requete_condition[] = $condition;
|
|
|
110 |
} else {
|
|
|
111 |
$e = "valeur incorrecte pour masque.".$type_masque;
|
|
|
112 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE,$e);
|
6 |
jpm |
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
|
558 |
mathilde |
119 |
|
490 |
mathilde |
120 |
//+-------------------------------------------formatage résultats---------------------------------------------+
|
6 |
jpm |
121 |
|
496 |
mathilde |
122 |
public function formaterResultat($resultat) {
|
|
|
123 |
$versionResultat['entete'] = $this->formaterEnteteResultat();;
|
|
|
124 |
if ($resultat == '') {
|
|
|
125 |
$message = 'La requête SQL formée comporte une erreur!';
|
|
|
126 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
127 |
throw new Exception($message, $code);
|
|
|
128 |
} elseif ($resultat) {
|
534 |
mathilde |
129 |
$versionResultat = $this->retournerResultatFormate($resultat);
|
496 |
mathilde |
130 |
} else {
|
|
|
131 |
$versionResultat['resultats'] = array();
|
|
|
132 |
}
|
|
|
133 |
return $versionResultat;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
public function retournerResultatFormate($resultat) {
|
490 |
mathilde |
137 |
$resultat_json = array();
|
496 |
mathilde |
138 |
foreach ($resultat as $tab) {
|
|
|
139 |
$num_coste = $tab['num_nom'];
|
|
|
140 |
unset($tab['num_nom']);
|
|
|
141 |
$resultat_json['resultats'][$num_coste]['num_nomenclatural'] = $tab['flore_bdtfx_nn'];
|
|
|
142 |
$resultat_json['resultats'][$num_coste]['num_taxonomique'] = $tab['flore_bdtfx_nt'];
|
|
|
143 |
$resultat_json['resultats'][$num_coste]['binaire.href'] = $this->formaterUrlImage($tab['image']);
|
|
|
144 |
$resultat_json['resultats'][$num_coste]['mime'] = "images/png";
|
|
|
145 |
}
|
490 |
mathilde |
146 |
return $resultat_json;
|
6 |
jpm |
147 |
}
|
|
|
148 |
|
490 |
mathilde |
149 |
public function formaterUrlImage($fichier) {
|
|
|
150 |
$chemin = config::get('donnees')."2.00/img/$fichier";
|
|
|
151 |
return $chemin;
|
6 |
jpm |
152 |
}
|
|
|
153 |
|
490 |
mathilde |
154 |
|
6 |
jpm |
155 |
|
558 |
mathilde |
156 |
|
490 |
mathilde |
157 |
public function formaterEnteteResultat() {
|
|
|
158 |
$entete['depart'] = $this->limite_requete['depart'];
|
|
|
159 |
$entete['limite'] = $this->limite_requete['limite'];
|
|
|
160 |
$entete['total'] = $this->total_resultat;
|
|
|
161 |
$entete['masque'] = empty($this->masque) ? 'aucun' : implode('&', $this->masque);
|
|
|
162 |
$url = $this->formulerUrl($this->total_resultat, '/images');
|
|
|
163 |
if (isset($url['precedent']) && $url['precedent'] != '') {
|
|
|
164 |
$entete['href.precedent'] = $url['precedent'];
|
6 |
jpm |
165 |
}
|
490 |
mathilde |
166 |
if (isset($url['suivant']) && $url['suivant'] != '') {
|
|
|
167 |
$entete['href.suivant'] = $url['suivant'];
|
6 |
jpm |
168 |
}
|
490 |
mathilde |
169 |
return $entete;
|
6 |
jpm |
170 |
}
|
|
|
171 |
|
490 |
mathilde |
172 |
//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
|
6 |
jpm |
173 |
|
490 |
mathilde |
174 |
public function assemblerLaRequete() {
|
|
|
175 |
$requete = ' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
|
|
|
176 |
.$this->retournerRequeteCondition().' '
|
|
|
177 |
.$this->delimiterResultatsRequete();
|
|
|
178 |
return $requete;
|
6 |
jpm |
179 |
}
|
|
|
180 |
|
490 |
mathilde |
181 |
|
6 |
jpm |
182 |
|
490 |
mathilde |
183 |
public function retournerRequeteCondition() {
|
|
|
184 |
$condition = '';
|
|
|
185 |
if (empty($this->requete_condition) == false) {
|
|
|
186 |
$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
|
|
|
187 |
}
|
|
|
188 |
return $condition;
|
6 |
jpm |
189 |
}
|
|
|
190 |
|
490 |
mathilde |
191 |
|
6 |
jpm |
192 |
|
490 |
mathilde |
193 |
public function calculerTotalResultat() {
|
|
|
194 |
$requete = 'SELECT count(*) as nombre FROM '.$this->table.' '
|
|
|
195 |
.$this->retournerRequeteCondition();
|
|
|
196 |
$res = $this->getBdd()->recuperer($requete);
|
6 |
jpm |
197 |
if ($res) {
|
490 |
mathilde |
198 |
$this->total_resultat = $res['nombre'];
|
|
|
199 |
} else {
|
|
|
200 |
$this->total_resultat = 0;
|
|
|
201 |
$e = 'Données introuvables dans la base';
|
|
|
202 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
|
6 |
jpm |
203 |
}
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
|
490 |
mathilde |
207 |
public function delimiterResultatsRequete() {
|
|
|
208 |
$this->calculerTotalResultat();
|
|
|
209 |
$requete_limite = '';
|
|
|
210 |
if ((count($this->ressources)) == 0) {
|
534 |
mathilde |
211 |
if (($this->limite_requete['depart'] < $this->total_resultat) &&
|
|
|
212 |
(($this->limite_requete['limite'] + $this->limite_requete['depart'] )
|
|
|
213 |
< $this->total_resultat )) {
|
490 |
mathilde |
214 |
$requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
|
|
|
215 |
.$this->limite_requete['limite'];
|
534 |
mathilde |
216 |
}
|
6 |
jpm |
217 |
}
|
490 |
mathilde |
218 |
return $requete_limite;
|
6 |
jpm |
219 |
}
|
|
|
220 |
|
|
|
221 |
}
|
|
|
222 |
?>
|