106 |
aurelien |
1 |
<?php
|
118 |
jpm |
2 |
// declare(encoding='UTF-8');
|
|
|
3 |
/**
|
|
|
4 |
* Classe implémentant l'API d'eFlore Images pour le projet CEL.
|
|
|
5 |
*
|
|
|
6 |
* @see http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=EfloreApi01Images
|
|
|
7 |
*
|
|
|
8 |
* @package eFlore/services
|
|
|
9 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
10 |
* @author Aurélien PERONNET <aurelien@tela-botanica.org>
|
|
|
11 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
12 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
13 |
* @version 1.0
|
|
|
14 |
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
|
|
|
15 |
*/
|
|
|
16 |
// TODO : Config et Outils sont des classes statiques qui doivent poser des pb pour les tests...
|
539 |
aurelien |
17 |
class Images extends Commun {
|
116 |
jpm |
18 |
|
539 |
aurelien |
19 |
protected $parametres = array();
|
|
|
20 |
protected $ressources = array();
|
106 |
aurelien |
21 |
private $Bdd;
|
116 |
jpm |
22 |
|
138 |
jpm |
23 |
const CODE_REFTAX_DEFAUT = 'bdtfx';
|
|
|
24 |
const TAILLE_IMG_DEFAUT = 'M';
|
|
|
25 |
const MIME_JPEG = 'image/jpeg';
|
|
|
26 |
const MIME_JSON = 'application/json';
|
140 |
jpm |
27 |
|
1044 |
aurelien |
28 |
private $tpl_url_img;
|
1107 |
mathias |
29 |
protected $config = array();
|
140 |
jpm |
30 |
private $cheminImagesBase = '';
|
138 |
jpm |
31 |
private $formats_supportes = array(self::MIME_JPEG, self::MIME_JSON);
|
330 |
delphine |
32 |
private $tris_supportes = array('date');
|
538 |
aurelien |
33 |
private $retour_champs = array('determination.nom_sci' => 'nom_sel', 'determination.nom_sci.code' => 'nom_sel_nn',
|
378 |
delphine |
34 |
'station.lieudit' => 'lieudit', 'station', 'milieu');
|
638 |
aurelien |
35 |
private $code_ref_tax_demande = null;
|
116 |
jpm |
36 |
private $ref_tax_demande = array();
|
138 |
jpm |
37 |
private $infosImages = array();
|
|
|
38 |
private $nbreImages = 0;
|
|
|
39 |
private $Utilisateurs = null;
|
140 |
jpm |
40 |
private $UrlNavigation = null;
|
116 |
jpm |
41 |
|
140 |
jpm |
42 |
public function __construct(Bdd $bdd = null, Array $config = null, Utilisateurs $utilisateurs = null, Url $url = null) {
|
|
|
43 |
$this->config = is_null($config) ? Config::get('Images') : $config;
|
138 |
jpm |
44 |
$this->Bdd = is_null($bdd) ? new Bdd() : $bdd;
|
|
|
45 |
$this->Utilisateurs = is_null($utilisateurs) ? new Utilisateurs() : $utilisateurs;
|
140 |
jpm |
46 |
$this->UrlNavigation = is_null($url) ? new Url($this->config['urlService']) : $url;
|
|
|
47 |
$this->cheminImagesBase = $this->config['chemin'];
|
1044 |
aurelien |
48 |
$this->tpl_url_img = $this->config['urlImagesTpl'];
|
116 |
jpm |
49 |
}
|
|
|
50 |
|
106 |
aurelien |
51 |
public function consulter($ressources, $parametres) {
|
140 |
jpm |
52 |
//$tpsDebut = microtime(true);
|
116 |
jpm |
53 |
$this->parametres = $parametres;
|
|
|
54 |
$this->ressources = $ressources;
|
|
|
55 |
|
|
|
56 |
$this->definirValeurParDefautDesParametres();
|
|
|
57 |
$this->verifierParametres();
|
|
|
58 |
|
|
|
59 |
$resultat = new ResultatService();
|
138 |
jpm |
60 |
if ($this->parametres['retour'] == self::MIME_JPEG) {
|
949 |
raphael |
61 |
if (@$this->parametres['retour.tri'] == 'date') { // recherche la plus vieille image du CEL
|
330 |
delphine |
62 |
$id_image_a_renvoyer = $this->obtenirIdPremiereImage();
|
1267 |
killian |
63 |
} else {
|
330 |
delphine |
64 |
$id_image_a_renvoyer = $this->obtenirIdImageAuHasard();
|
|
|
65 |
}
|
138 |
jpm |
66 |
$resultat->corps = $this->recupererImageBinaire($id_image_a_renvoyer);
|
|
|
67 |
} else if ($this->parametres['retour'] == self::MIME_JSON) {
|
375 |
delphine |
68 |
if (isset($this->ressources[0])) {
|
|
|
69 |
$this->chargerInfosImage();
|
386 |
jpm |
70 |
$this->extraireIdentitesAuteurs();
|
375 |
delphine |
71 |
$resultat->corps = $this->formaterInfosImage($this->infosImages[0]);
|
1267 |
killian |
72 |
} elseif (substr($this->parametres['masque.nn'],0,4) === 'top:') {
|
1265 |
killian |
73 |
$resultat->corps = $this->chargerTopImages();
|
375 |
delphine |
74 |
} else {
|
|
|
75 |
$this->chargerListeImages();
|
|
|
76 |
$this->chargerNbreImagesTotal();
|
|
|
77 |
$resultat->corps = $this->formaterListeImages();
|
|
|
78 |
}
|
138 |
jpm |
79 |
}
|
|
|
80 |
$resultat->mime = $this->parametres['retour'];
|
|
|
81 |
|
116 |
jpm |
82 |
return $resultat;
|
106 |
aurelien |
83 |
}
|
375 |
delphine |
84 |
//+---------------------------FONCTION D'ANALYSE DES PARAMETRES---------------------------------------------------------+
|
116 |
jpm |
85 |
private function definirValeurParDefautDesParametres() {
|
|
|
86 |
if (isset($this->parametres['retour']) == false) {
|
138 |
jpm |
87 |
$this->parametres['retour'] = self::MIME_JSON;
|
106 |
aurelien |
88 |
}
|
116 |
jpm |
89 |
if (isset($this->parametres['retour.format']) == false) {
|
|
|
90 |
$this->parametres['retour.format'] = 'M';
|
106 |
aurelien |
91 |
}
|
138 |
jpm |
92 |
if (isset($this->parametres['navigation.depart']) == false) {
|
|
|
93 |
$this->parametres['navigation.depart'] = 0;
|
|
|
94 |
}
|
|
|
95 |
if (isset($this->parametres['navigation.limite']) == false) {
|
|
|
96 |
$this->parametres['navigation.limite'] = 100;
|
|
|
97 |
}
|
638 |
aurelien |
98 |
if (isset($this->parametres['referentiel']) == false) {
|
|
|
99 |
$this->parametres['referentiel'] = self::CODE_REFTAX_DEFAUT;
|
|
|
100 |
}
|
106 |
aurelien |
101 |
}
|
116 |
jpm |
102 |
|
|
|
103 |
private function verifierParametres() {
|
|
|
104 |
$erreurs = array();
|
|
|
105 |
|
375 |
delphine |
106 |
if (!isset($this->ressources[0])) {
|
|
|
107 |
if (isset($this->parametres['masque.nn']) == false) {
|
|
|
108 |
$erreurs[] = "Le paramètre masque.nn est obligatoire.";
|
|
|
109 |
} else {
|
|
|
110 |
$this->analyserMasqueNn();
|
|
|
111 |
if ($this->verifierMasqueNnAutorisePourRetourJPEG() == false) {
|
|
|
112 |
$erreurs[] = "Le paramètre masque.nn peut contenir une seule valeur numérique pour l'instant pour le format de retour image/jpeg.";
|
|
|
113 |
} else if ($this->verifierValeurParametreMasqueNn() == false) {
|
|
|
114 |
$erreurs[] = "Le paramètre masque.nn est mal formé.";
|
|
|
115 |
}
|
106 |
aurelien |
116 |
}
|
|
|
117 |
}
|
116 |
jpm |
118 |
if (isset($this->parametres['retour']) == false) {
|
|
|
119 |
$erreurs[] = "Le paramètre type de retour 'retour' est obligatoire.";
|
106 |
aurelien |
120 |
}
|
116 |
jpm |
121 |
if ($this->verifierValeurParametreRetour() == false) {
|
|
|
122 |
$erreurs[] = "Le type de retour '{$this->parametres['retour']}' n'est pas supporté";
|
106 |
aurelien |
123 |
}
|
116 |
jpm |
124 |
if (isset($this->parametres['retour.format']) == false) {
|
|
|
125 |
$erreurs[] = "Le paramètre de format de retour 'retour.format' est obligatoire.";
|
106 |
aurelien |
126 |
}
|
116 |
jpm |
127 |
if ($this->verifierValeurParametreFormat() == false) {
|
|
|
128 |
$erreurs[] = "Le type de format '{$this->parametres['retour.format']}' n'est pas supporté";
|
|
|
129 |
}
|
330 |
delphine |
130 |
if (isset($this->parametres['retour.tri'])){
|
|
|
131 |
if ($this->verifierValeurParametreTri() == false) {
|
|
|
132 |
$erreurs[] = "Le type de tri '{$this->parametres['retour.tri']}' n'est pas supporté";
|
|
|
133 |
}
|
|
|
134 |
}
|
386 |
jpm |
135 |
|
378 |
delphine |
136 |
if (isset($this->parametres['retour.champs'])){
|
|
|
137 |
if ($this->verifierValeurParametreRetourChamps() == false) {
|
|
|
138 |
$erreurs[] = "Le champs '{$this->parametres['retour.champs']}' n'existe pas";
|
|
|
139 |
}
|
|
|
140 |
}
|
386 |
jpm |
141 |
|
138 |
jpm |
142 |
if ($this->verifierValeurParametreNavigationDepart() == false) {
|
|
|
143 |
$erreurs[] = "Le paramètre 'navigation.depart' doit possèder un valeur numérique.";
|
|
|
144 |
}
|
|
|
145 |
if ($this->verifierValeurParametreNavigationLimite() == false) {
|
|
|
146 |
$erreurs[] = "Le paramètre 'navigation.limite' doit possèder un valeur numérique supérieure à 0.";
|
|
|
147 |
}
|
116 |
jpm |
148 |
|
|
|
149 |
if (count($erreurs) > 0) {
|
|
|
150 |
$message = implode('<br />', $erreurs);
|
|
|
151 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
152 |
throw new Exception($message, $code);
|
|
|
153 |
}
|
106 |
aurelien |
154 |
}
|
116 |
jpm |
155 |
|
162 |
jpm |
156 |
private function verifierMasqueNnAutorisePourRetourJPEG() {
|
138 |
jpm |
157 |
$ok = true;
|
|
|
158 |
$masque = $this->parametres['masque.nn'];
|
|
|
159 |
$retour = $this->parametres['retour'];
|
162 |
jpm |
160 |
|
138 |
jpm |
161 |
if ($retour == self::MIME_JPEG) {
|
|
|
162 |
$ok = is_numeric($masque) ? true : false;
|
|
|
163 |
}
|
|
|
164 |
return $ok;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
private function verifierValeurParametreMasqueNn() {
|
|
|
168 |
$nn = $this->parametres['masque.nn'];
|
|
|
169 |
$projetPattern = '(?:(?:[A-Z0-9]+:)?(?:[0-9]+,)*[0-9]+)';
|
162 |
jpm |
170 |
$patternComplet = "/^$projetPattern(?:;$projetPattern)*$/i";
|
138 |
jpm |
171 |
$ok = preg_match($patternComplet, $nn) ? true : false;
|
|
|
172 |
return $ok;
|
|
|
173 |
}
|
|
|
174 |
|
116 |
jpm |
175 |
private function verifierValeurParametreRetour() {
|
|
|
176 |
return in_array($this->parametres['retour'], $this->formats_supportes);
|
106 |
aurelien |
177 |
}
|
116 |
jpm |
178 |
|
|
|
179 |
private function verifierValeurParametreFormat() {
|
118 |
jpm |
180 |
$formats = Outils::recupererTableauConfig('Images.formats');
|
116 |
jpm |
181 |
$ok = array_key_exists($this->parametres['retour.format'], $formats);
|
|
|
182 |
return $ok;
|
106 |
aurelien |
183 |
}
|
377 |
jpm |
184 |
|
330 |
delphine |
185 |
private function verifierValeurParametreTri() {
|
|
|
186 |
return in_array($this->parametres['retour.tri'], $this->tris_supportes);
|
|
|
187 |
}
|
386 |
jpm |
188 |
|
378 |
delphine |
189 |
private function verifierValeurParametreRetourChamps() {
|
|
|
190 |
$ok = false;
|
|
|
191 |
$liste_champs = preg_split(',', $this->parametres['retour.champs']);
|
|
|
192 |
foreach ($liste_champs as $champs) {
|
|
|
193 |
$ok[$champs] = array_key_exists($champs, $this->retour_champs);
|
|
|
194 |
}
|
|
|
195 |
return $ok;
|
|
|
196 |
}
|
116 |
jpm |
197 |
|
138 |
jpm |
198 |
private function verifierValeurParametreNavigationDepart() {
|
|
|
199 |
$depart = $this->parametres['navigation.depart'];
|
|
|
200 |
$ok = is_numeric($depart) ? true : false;
|
116 |
jpm |
201 |
return $ok;
|
106 |
aurelien |
202 |
}
|
116 |
jpm |
203 |
|
138 |
jpm |
204 |
private function verifierValeurParametreNavigationLimite() {
|
|
|
205 |
$limite = $this->parametres['navigation.limite'];
|
|
|
206 |
$ok = (is_numeric($limite) && $limite != 0) ? true : false;
|
116 |
jpm |
207 |
return $ok;
|
106 |
aurelien |
208 |
}
|
116 |
jpm |
209 |
|
|
|
210 |
private function analyserMasqueNn() {
|
|
|
211 |
$nn = $this->parametres['masque.nn'];
|
142 |
jpm |
212 |
if (preg_match('/^[0-9]+$/', $nn)) {
|
638 |
aurelien |
213 |
$this->ref_tax_demande[$this->parametres['referentiel']][] = $nn;
|
106 |
aurelien |
214 |
} else {
|
116 |
jpm |
215 |
// ceci contient potentiellement des formes ref_tax1:nn1,nn2;ref_tax2:nn3,nn4
|
|
|
216 |
$projetsListeEtNumNoms = explode(';', $nn);
|
|
|
217 |
if (count($projetsListeEtNumNoms) > 0) {
|
|
|
218 |
foreach ($projetsListeEtNumNoms as $projetEtNumNoms) {
|
638 |
aurelien |
219 |
$projetEtNumNoms = (strpos($projetEtNumNoms, ':')) ? $projetEtNumNoms : $this->parametres['referentiel'].':'.$projetEtNumNoms;
|
116 |
jpm |
220 |
list($projet, $numNoms) = explode(':', $projetEtNumNoms);
|
|
|
221 |
$this->ref_tax_demande[$projet] = explode(',', $numNoms);
|
106 |
aurelien |
222 |
}
|
|
|
223 |
}
|
|
|
224 |
}
|
|
|
225 |
}
|
116 |
jpm |
226 |
|
377 |
jpm |
227 |
|
375 |
delphine |
228 |
//+---------------------------------------- REQUETES ---------------------------------------------------------------+
|
138 |
jpm |
229 |
private function obtenirIdImageAuHasard() {
|
638 |
aurelien |
230 |
$refTax = $this->parametres['referentiel'];
|
142 |
jpm |
231 |
$numNom = $this->Bdd->proteger($this->ref_tax_demande[$refTax][0]);
|
116 |
jpm |
232 |
|
1120 |
aurelien |
233 |
$requete = 'SELECT ci.id_image AS id_image '.
|
1286 |
delphine |
234 |
'FROM cel_images_export AS ci '.
|
|
|
235 |
' LEFT JOIN cel_export AS co '.
|
1267 |
killian |
236 |
'ON (ci.ce_observation = co.id_observation) '.
|
1286 |
delphine |
237 |
'WHERE '.
|
|
|
238 |
" co.nom_ret_nn IN ($numNom)";
|
767 |
raphael |
239 |
" AND co.nom_referentiel = ".$this->Bdd->proteger($refTax) . ' -- ' . __FILE__ . ':' . __LINE__;
|
116 |
jpm |
240 |
|
|
|
241 |
$resultat = $this->Bdd->recupererTous($requete);
|
|
|
242 |
|
|
|
243 |
if (!is_array($resultat) || count($resultat) <= 0) {
|
126 |
jpm |
244 |
$message = "Aucune image ne correspond au numéro numenclatural $refTax:$numNom";
|
116 |
jpm |
245 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
246 |
throw new Exception($message, $code);
|
106 |
aurelien |
247 |
}
|
116 |
jpm |
248 |
|
|
|
249 |
$id_image_hasard = $resultat[array_rand($resultat)]['id_image'];
|
106 |
aurelien |
250 |
return $id_image_hasard;
|
|
|
251 |
}
|
377 |
jpm |
252 |
|
330 |
delphine |
253 |
private function obtenirIdPremiereImage() {
|
638 |
aurelien |
254 |
$refTax = $this->parametres['referentiel'];
|
330 |
delphine |
255 |
$numNom = $this->Bdd->proteger($this->ref_tax_demande[$refTax][0]);
|
538 |
aurelien |
256 |
$requete = 'SELECT ci.id_image AS id_image '.
|
1286 |
delphine |
257 |
'FROM cel_images_export AS ci'.
|
|
|
258 |
' LEFT JOIN cel_export AS co '.
|
1120 |
aurelien |
259 |
' ON (ci.ce_observation = co.id_observation) '.
|
1286 |
delphine |
260 |
'WHERE '.
|
|
|
261 |
" co.nom_ret_nn IN ($numNom) ".
|
1120 |
aurelien |
262 |
' AND ci.date_prise_de_vue != "0000-00-00" '.
|
1300 |
delphine |
263 |
' AND co.nom_referentiel = '.$this->Bdd->proteger($refTax).' '.
|
1120 |
aurelien |
264 |
' ORDER BY ci.date_prise_de_vue ASC '.
|
767 |
raphael |
265 |
'LIMIT 1' . ' -- ' . __FILE__ . ':' . __LINE__;
|
377 |
jpm |
266 |
|
330 |
delphine |
267 |
$resultat = $this->Bdd->recupererTous($requete);
|
377 |
jpm |
268 |
|
330 |
delphine |
269 |
if (!is_array($resultat) || count($resultat) <= 0) {
|
|
|
270 |
$message = "Aucune image ne correspond au numéro numenclatural $refTax:$numNom";
|
|
|
271 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
272 |
throw new Exception($message, $code);
|
|
|
273 |
}
|
377 |
jpm |
274 |
|
330 |
delphine |
275 |
$id_image_hasard = $resultat[0]['id_image'];
|
|
|
276 |
return $id_image_hasard;
|
|
|
277 |
}
|
377 |
jpm |
278 |
|
375 |
delphine |
279 |
private function chargerInfosImage() {
|
|
|
280 |
$requete = 'SELECT SQL_CALC_FOUND_ROWS '.
|
638 |
aurelien |
281 |
' ci.id_image AS id_img, co.date_observation AS date, '.
|
538 |
aurelien |
282 |
' co.id_observation AS id_obs, '.
|
1287 |
delphine |
283 |
' co.courriel_utilisateur AS utilisateur_courriel, '.
|
538 |
aurelien |
284 |
' nom_sel, nom_sel_nn, '.
|
|
|
285 |
' zone_geo, ce_zone_geo, lieudit, station, milieu '.
|
1286 |
delphine |
286 |
'FROM cel_images_export AS ci'.
|
|
|
287 |
' LEFT JOIN cel_export AS co '.
|
1120 |
aurelien |
288 |
' ON (ci.ce_observation = co.id_observation) '.
|
726 |
raphael |
289 |
'WHERE ci.id_image = '.$this->ressources[0].
|
767 |
raphael |
290 |
' AND co.id_observation IS NOT NULL' . ' -- ' . __FILE__ . ':' . __LINE__;
|
377 |
jpm |
291 |
|
375 |
delphine |
292 |
$this->infosImages = $this->Bdd->recupererTous($requete);
|
|
|
293 |
}
|
116 |
jpm |
294 |
|
375 |
delphine |
295 |
private function chargerListeImages() {
|
638 |
aurelien |
296 |
$refTax = $this->parametres['referentiel'];
|
375 |
delphine |
297 |
$numNomListe = implode(',', $this->ref_tax_demande[$refTax]);
|
|
|
298 |
$depart = $this->parametres['navigation.depart'];
|
|
|
299 |
$limite = $this->parametres['navigation.limite'];
|
377 |
jpm |
300 |
|
375 |
delphine |
301 |
//TODO: modifier la requete lors du passage à la nouvelle base de données pour faire quelque chose
|
386 |
jpm |
302 |
// du numéro nomenclatural + modifier les champs appelés pour le nouveau format
|
375 |
delphine |
303 |
$requete = 'SELECT SQL_CALC_FOUND_ROWS '.
|
538 |
aurelien |
304 |
' co.id_observation AS id_obs, co.courriel_utilisateur AS utilisateur_courriel, co.zone_geo, co.ce_zone_geo, '.
|
|
|
305 |
' co.nom_sel, co.nom_sel_nn, '.
|
638 |
aurelien |
306 |
' ci.id_image AS id_img, co.date_observation AS date '.
|
386 |
jpm |
307 |
(isset($this->parametres['retour.champs']) ? ', '.$this->parametres['retour.champs'] : '').
|
1286 |
delphine |
308 |
'FROM cel_images_export AS ci'.
|
1300 |
delphine |
309 |
' JOIN cel_export AS co '.
|
1120 |
aurelien |
310 |
' ON (ci.ce_observation = co.id_observation) '.
|
386 |
jpm |
311 |
$this->formerRequeteConditions($numNomListe).' '.
|
1300 |
delphine |
312 |
$this->formerRequeteTri().
|
767 |
raphael |
313 |
"LIMIT $depart,$limite " . ' -- ' . __FILE__ . ':' . __LINE__;
|
1267 |
killian |
314 |
|
375 |
delphine |
315 |
$this->infosImages = $this->Bdd->recupererTous($requete);
|
|
|
316 |
}
|
386 |
jpm |
317 |
|
375 |
delphine |
318 |
private function formerRequeteConditions($numNomListe) {
|
638 |
aurelien |
319 |
$refTax = $this->parametres['referentiel'];
|
1288 |
delphine |
320 |
$where[] = " co.nom_ret_nn IN ($numNomListe) ";
|
1300 |
delphine |
321 |
$where[] = " co.nom_referentiel = ".$this->Bdd->proteger($refTax).' ';
|
638 |
aurelien |
322 |
|
375 |
delphine |
323 |
return ' WHERE '.implode(' AND ', $where);
|
|
|
324 |
}
|
377 |
jpm |
325 |
|
375 |
delphine |
326 |
private function formerRequeteTri() {
|
386 |
jpm |
327 |
$order = '';
|
375 |
delphine |
328 |
if (isset($this->parametres['retour.tri']) && $this->parametres['retour.tri'] == 'date') {
|
638 |
aurelien |
329 |
$order = ' ORDER BY co.date_observation ASC ';
|
375 |
delphine |
330 |
}
|
|
|
331 |
return $order;
|
|
|
332 |
}
|
377 |
jpm |
333 |
|
375 |
delphine |
334 |
private function chargerNbreImagesTotal() {
|
|
|
335 |
$requete = 'SELECT FOUND_ROWS() AS nbre ';
|
|
|
336 |
$resultats = $this->Bdd->recuperer($requete);
|
|
|
337 |
$this->nbreImages = (int) $resultats['nbre'];
|
|
|
338 |
}
|
|
|
339 |
|
|
|
340 |
//+---------------------------------------CHEMIN ET CONVERSION--------------------------------------------------------+
|
116 |
jpm |
341 |
private function recupererImageBinaire($id_image) {
|
|
|
342 |
$image = '';
|
|
|
343 |
$chemin = $this->obtenirCheminImage($id_image);
|
118 |
jpm |
344 |
$image = file_get_contents($chemin);
|
|
|
345 |
if ($image === false) {
|
116 |
jpm |
346 |
$message = "L'image demandée est introuvable sur le serveur : $chemin";
|
|
|
347 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
348 |
throw new Exception($message, $code);
|
106 |
aurelien |
349 |
}
|
116 |
jpm |
350 |
return $image;
|
106 |
aurelien |
351 |
}
|
116 |
jpm |
352 |
|
127 |
jpm |
353 |
private function obtenirCheminImage($idImage) {
|
116 |
jpm |
354 |
$nom = $this->convertirIdImageVersNomFichier($idImage);
|
|
|
355 |
$dossier = $this->convertirIdImageVersChemin($idImage);
|
106 |
aurelien |
356 |
return $dossier.'/'.$nom;
|
|
|
357 |
}
|
116 |
jpm |
358 |
|
127 |
jpm |
359 |
private function convertirIdImageVersNomFichier($idImage) {
|
116 |
jpm |
360 |
$codeImage = $this->construireCodeImage($idImage);
|
|
|
361 |
$nom_fichier = $codeImage.'.jpg';
|
106 |
aurelien |
362 |
return $nom_fichier;
|
|
|
363 |
}
|
116 |
jpm |
364 |
|
|
|
365 |
private function convertirIdImageVersChemin($idImage) {
|
|
|
366 |
$codeImage = $this->construireCodeImage($idImage);
|
|
|
367 |
list($dossierNiveau1, $dossierNiveau2, $idFichier, $format) = explode('_', $codeImage);
|
140 |
jpm |
368 |
$chemin_sur_serveur = $this->cheminImagesBase.'/'.$dossierNiveau1.'/'.$dossierNiveau2.'/'.$format;
|
116 |
jpm |
369 |
return $chemin_sur_serveur;
|
106 |
aurelien |
370 |
}
|
116 |
jpm |
371 |
|
|
|
372 |
private function construireCodeImage($idImage) {
|
|
|
373 |
$codeImage = sprintf('%09s', $idImage);
|
|
|
374 |
$codeImage = wordwrap($codeImage, 3, '_', true);
|
|
|
375 |
$format = $this->parametres['retour.format'];
|
|
|
376 |
$codeImage .= '_'.$format;
|
|
|
377 |
return $codeImage;
|
106 |
aurelien |
378 |
}
|
377 |
jpm |
379 |
|
375 |
delphine |
380 |
//+------------------------------------FORMATAGE LISTE----------------------------------------------------------------+
|
138 |
jpm |
381 |
private function formaterListeImages() {
|
|
|
382 |
$entete = $this->construireEntete();
|
|
|
383 |
$resultats = $this->construireResultats();
|
|
|
384 |
|
|
|
385 |
$resultat = array('entete' => $entete, 'resultats' => $resultats);
|
|
|
386 |
return $resultat;
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
private function construireEntete() {
|
|
|
390 |
$entete = array('masque' => '', 'depart' => 0, 'limite' => 100, 'total' => 0);
|
|
|
391 |
|
|
|
392 |
$entete['masque'] = $this->recupererMasque();
|
|
|
393 |
$entete['depart'] = (int) $this->parametres['navigation.depart'];
|
|
|
394 |
$entete['limite'] = (int) $this->parametres['navigation.limite'];
|
|
|
395 |
$entete['total'] = $this->nbreImages;
|
|
|
396 |
if ($hrefPrecedent = $this->recupererHrefPrecedent()) {
|
|
|
397 |
$entete['href.precedent'] = $hrefPrecedent;
|
|
|
398 |
}
|
|
|
399 |
if ($hrefSuivant = $this->recupererHrefSuivant()) {
|
|
|
400 |
$entete['href.suivant'] = $hrefSuivant;
|
|
|
401 |
}
|
|
|
402 |
return $entete;
|
|
|
403 |
}
|
|
|
404 |
|
|
|
405 |
private function recupererMasque() {
|
|
|
406 |
$masqueEntete = '';
|
|
|
407 |
if ($masqueNn = $this->parametres['masque.nn']) {
|
|
|
408 |
$masqueEntete = "nn=$masqueNn";
|
|
|
409 |
}
|
|
|
410 |
return $masqueEntete;
|
|
|
411 |
}
|
|
|
412 |
|
|
|
413 |
private function recupererHrefPrecedent() {
|
|
|
414 |
$departActuel = $this->parametres['navigation.depart'];
|
|
|
415 |
$limite = $this->parametres['navigation.limite'];
|
|
|
416 |
$departPrecedent = $departActuel - $limite;
|
|
|
417 |
$url = null;
|
|
|
418 |
if ($departPrecedent >= 0) {
|
|
|
419 |
$url = $this->obtenirUrlNavigation($departPrecedent, $limite);
|
|
|
420 |
}
|
|
|
421 |
return $url;
|
|
|
422 |
}
|
|
|
423 |
|
|
|
424 |
private function recupererHrefSuivant() {
|
|
|
425 |
$departActuel = $this->parametres['navigation.depart'];
|
|
|
426 |
$limite = $this->parametres['navigation.limite'];
|
|
|
427 |
$departSuivant = $departActuel + $limite;
|
|
|
428 |
$url = null;
|
|
|
429 |
if ($departSuivant < $this->nbreImages) {
|
|
|
430 |
$url = $this->obtenirUrlNavigation($departSuivant, $limite);
|
|
|
431 |
}
|
|
|
432 |
return $url;
|
|
|
433 |
}
|
|
|
434 |
|
|
|
435 |
private function obtenirUrlNavigation($depart, $limite) {
|
|
|
436 |
$parametres = array(
|
|
|
437 |
'navigation.depart' => $depart,
|
|
|
438 |
'navigation.limite' => $limite);
|
|
|
439 |
if (isset($this->parametres['masque.nn'])) {
|
|
|
440 |
$parametres['masque.nn'] = $this->parametres['masque.nn'];
|
|
|
441 |
}
|
140 |
jpm |
442 |
$this->UrlNavigation->setRequete($parametres);
|
|
|
443 |
$url = $this->UrlNavigation->getURL();
|
138 |
jpm |
444 |
return $url;
|
|
|
445 |
}
|
|
|
446 |
|
|
|
447 |
private function construireResultats() {
|
|
|
448 |
$resultats = array();
|
386 |
jpm |
449 |
$this->extraireIdentitesAuteurs();
|
138 |
jpm |
450 |
foreach ($this->infosImages as $img) {
|
375 |
delphine |
451 |
$info = $this->formaterInfosImage($img);
|
142 |
jpm |
452 |
$id = $img['id_img'];
|
378 |
delphine |
453 |
$info['href'] = $this->config['urlService'].'/'.$id;
|
138 |
jpm |
454 |
if (!isset($resultats[$id])) {
|
|
|
455 |
$resultats[$id] = $info;
|
|
|
456 |
} else {
|
|
|
457 |
throw new Exception("Double : $id");
|
|
|
458 |
}
|
|
|
459 |
}
|
|
|
460 |
return $resultats;
|
|
|
461 |
}
|
|
|
462 |
|
375 |
delphine |
463 |
private function formaterInfosImage($img) {
|
|
|
464 |
$info = array();
|
|
|
465 |
$info['date'] = $img['date'];
|
|
|
466 |
$info['mime'] = self::MIME_JPEG;
|
386 |
jpm |
467 |
$info['auteur.libelle'] = $this->Utilisateurs->getIntitule($img['utilisateur_courriel']);
|
|
|
468 |
$info['auteur.id'] = $this->Utilisateurs->getId($img['utilisateur_courriel']);
|
375 |
delphine |
469 |
$info['binaire.href'] = $this->formaterUrlImage($img);
|
393 |
jpm |
470 |
$info['observation.id'] = $img['id_obs'];
|
378 |
delphine |
471 |
if (isset($img['nom_sel'])) {
|
|
|
472 |
$info['determination.libelle'] = $this->formaterDetermination($img);
|
|
|
473 |
$info['determination.nom_sci'] = $img['nom_sel'];
|
|
|
474 |
$info['determination.nom_sci.code'] = $this->formaterNomSciCode($img);
|
|
|
475 |
}
|
375 |
delphine |
476 |
$info = array_merge($info, $this->formaterStation($img));
|
|
|
477 |
return $info;
|
|
|
478 |
}
|
377 |
jpm |
479 |
|
386 |
jpm |
480 |
private function extraireIdentitesAuteurs() {
|
138 |
jpm |
481 |
$courriels = array();
|
|
|
482 |
foreach ($this->infosImages as $img) {
|
|
|
483 |
$courriels[] = $img['utilisateur_courriel'];
|
|
|
484 |
}
|
846 |
raphael |
485 |
// pour Acer monspessulanum L. Sapindaceae, cela divise par 9 le nombre
|
|
|
486 |
// de courriels (213 => 23)
|
|
|
487 |
$courriels = array_values(array_unique($courriels, SORT_STRING));
|
386 |
jpm |
488 |
$this->Utilisateurs->setCourriels($courriels);
|
846 |
raphael |
489 |
// XXX: webservices: /service:annuaire:utilisateur/identite-par-courriel/ [/bibliotheque/Utilisateurs.php]
|
386 |
jpm |
490 |
$this->Utilisateurs->chargerIdentites();
|
138 |
jpm |
491 |
}
|
|
|
492 |
|
|
|
493 |
private function formaterUrlImage($infos) {
|
143 |
jpm |
494 |
$format = $this->parametres['retour.format'];
|
138 |
jpm |
495 |
$id = sprintf('%09s', $infos['id_img']).$format;
|
1044 |
aurelien |
496 |
$url = sprintf($this->tpl_url_img, $id);
|
138 |
jpm |
497 |
return $url;
|
|
|
498 |
}
|
|
|
499 |
|
|
|
500 |
private function formaterDetermination($infos) {
|
386 |
jpm |
501 |
$auteur = $this->Utilisateurs->getIntitule($infos['utilisateur_courriel']);
|
|
|
502 |
return $infos['nom_sel']."[Dét. : $auteur]";
|
138 |
jpm |
503 |
}
|
|
|
504 |
|
|
|
505 |
private function formaterNomSciCode($infos) {
|
638 |
aurelien |
506 |
return $this->parametres['referentiel'].'.'.$infos['nom_sel_nn'];
|
138 |
jpm |
507 |
}
|
|
|
508 |
|
|
|
509 |
private function formaterStation($infos) {
|
|
|
510 |
$station = array();
|
538 |
aurelien |
511 |
if (isset($infos['zone_geo']) && $commune = $this->formaterCommune($infos)) {
|
375 |
delphine |
512 |
$station['station.commune'] = $commune;
|
138 |
jpm |
513 |
}
|
378 |
delphine |
514 |
if (isset($infos['lieudit']) && $this->avoirContenu($infos['lieudit'])) {
|
375 |
delphine |
515 |
$station['station.lieudit'] = $infos['lieudit'];
|
138 |
jpm |
516 |
}
|
378 |
delphine |
517 |
if (isset($infos['station']) && $this->avoirContenu($infos['station'])) {
|
375 |
delphine |
518 |
$station['station.station'] = $infos['station'];
|
138 |
jpm |
519 |
}
|
|
|
520 |
|
378 |
delphine |
521 |
if (isset($infos['milieu']) && $this->avoirContenu($infos['milieu'])) {
|
375 |
delphine |
522 |
$station['station.milieu'] = $infos['milieu'];
|
138 |
jpm |
523 |
}
|
666 |
aurelien |
524 |
if (count($station) >= 1) {
|
378 |
delphine |
525 |
$station['station.libelle'] = implode(' > ', $station);
|
|
|
526 |
}
|
375 |
delphine |
527 |
return $station;
|
138 |
jpm |
528 |
}
|
|
|
529 |
|
|
|
530 |
private function formaterCommune($infos) {
|
|
|
531 |
$commune = array();
|
538 |
aurelien |
532 |
if ($this->avoirContenu($infos['zone_geo'])) {
|
|
|
533 |
$commune[] = $infos['zone_geo'];
|
138 |
jpm |
534 |
}
|
1300 |
delphine |
535 |
if (isset($infos['ce_zone_geo']) && $this->avoirContenu($infos['ce_zone_geo']) && $infos['ce_zone_geo'] != '') {
|
538 |
aurelien |
536 |
$commune[] = '('.substr(str_replace('INSEE-C:','',$infos['ce_zone_geo']), 0, 2).')';
|
138 |
jpm |
537 |
}
|
|
|
538 |
return implode(' ', $commune);
|
|
|
539 |
}
|
|
|
540 |
|
|
|
541 |
private function avoirContenu($info) {
|
638 |
aurelien |
542 |
return !($info == null || $info == '' || $info == '000null');
|
138 |
jpm |
543 |
}
|
1265 |
killian |
544 |
|
|
|
545 |
/* autres fonctions */
|
|
|
546 |
private function chargerTopImages() {
|
|
|
547 |
$nn = substr($this->parametres['masque.nn'], strpos($this->parametres['masque.nn'], ':') + 1);
|
1268 |
killian |
548 |
$referentiel = isset($this->parametres['masque.referentiel']) ? $this->parametres['masque.referentiel'] : '';
|
1267 |
killian |
549 |
$and = $referentiel ? ' AND referentiel = ' . $this->Bdd->proteger($referentiel) : '';
|
|
|
550 |
|
|
|
551 |
$requete = 'SELECT referentiel, ce_image as id_img, organe '.
|
1300 |
delphine |
552 |
'FROM tb_del.del_image_top '.
|
1267 |
killian |
553 |
'WHERE nn = '. $this->Bdd->proteger($nn) . $and . ' -- ' . __FILE__ . ':' . __LINE__;
|
1265 |
killian |
554 |
|
|
|
555 |
$this->infosImages = $this->Bdd->recupererTous($requete);
|
|
|
556 |
|
|
|
557 |
$resultat = array();
|
|
|
558 |
foreach ($this->infosImages as $image) {
|
1267 |
killian |
559 |
$resultat[$image['referentiel']][$image['organe']] = $this->formaterUrlImage($image);
|
1265 |
killian |
560 |
}
|
|
|
561 |
|
|
|
562 |
return array('resultat' => $resultat);
|
|
|
563 |
}
|
106 |
aurelien |
564 |
}
|
1265 |
killian |
565 |
?>
|