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 TPL_URL_IMG = 'http://www.tela-botanica.org/appli:cel-img:%s.jpg';
|
|
|
25 |
const TAILLE_IMG_DEFAUT = 'M';
|
|
|
26 |
const MIME_JPEG = 'image/jpeg';
|
|
|
27 |
const MIME_JSON = 'application/json';
|
140 |
jpm |
28 |
|
|
|
29 |
private $config = array();
|
|
|
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'];
|
116 |
jpm |
48 |
}
|
|
|
49 |
|
106 |
aurelien |
50 |
public function consulter($ressources, $parametres) {
|
140 |
jpm |
51 |
//$tpsDebut = microtime(true);
|
116 |
jpm |
52 |
$this->parametres = $parametres;
|
|
|
53 |
$this->ressources = $ressources;
|
|
|
54 |
|
|
|
55 |
$this->definirValeurParDefautDesParametres();
|
|
|
56 |
$this->verifierParametres();
|
|
|
57 |
|
|
|
58 |
$resultat = new ResultatService();
|
138 |
jpm |
59 |
if ($this->parametres['retour'] == self::MIME_JPEG) {
|
378 |
delphine |
60 |
if ($this->parametres['retour.tri'] == 'date') { // recherche la plus vieille image du CEL
|
330 |
delphine |
61 |
$id_image_a_renvoyer = $this->obtenirIdPremiereImage();
|
|
|
62 |
} else {
|
|
|
63 |
$id_image_a_renvoyer = $this->obtenirIdImageAuHasard();
|
|
|
64 |
}
|
138 |
jpm |
65 |
$resultat->corps = $this->recupererImageBinaire($id_image_a_renvoyer);
|
|
|
66 |
} else if ($this->parametres['retour'] == self::MIME_JSON) {
|
375 |
delphine |
67 |
if (isset($this->ressources[0])) {
|
|
|
68 |
$this->chargerInfosImage();
|
386 |
jpm |
69 |
$this->extraireIdentitesAuteurs();
|
375 |
delphine |
70 |
$resultat->corps = $this->formaterInfosImage($this->infosImages[0]);
|
|
|
71 |
} else {
|
|
|
72 |
$this->chargerListeImages();
|
|
|
73 |
$this->chargerNbreImagesTotal();
|
|
|
74 |
$resultat->corps = $this->formaterListeImages();
|
|
|
75 |
}
|
138 |
jpm |
76 |
}
|
|
|
77 |
$resultat->mime = $this->parametres['retour'];
|
|
|
78 |
|
116 |
jpm |
79 |
return $resultat;
|
106 |
aurelien |
80 |
}
|
375 |
delphine |
81 |
//+---------------------------FONCTION D'ANALYSE DES PARAMETRES---------------------------------------------------------+
|
116 |
jpm |
82 |
private function definirValeurParDefautDesParametres() {
|
|
|
83 |
if (isset($this->parametres['retour']) == false) {
|
138 |
jpm |
84 |
$this->parametres['retour'] = self::MIME_JSON;
|
106 |
aurelien |
85 |
}
|
116 |
jpm |
86 |
if (isset($this->parametres['retour.format']) == false) {
|
|
|
87 |
$this->parametres['retour.format'] = 'M';
|
106 |
aurelien |
88 |
}
|
138 |
jpm |
89 |
if (isset($this->parametres['navigation.depart']) == false) {
|
|
|
90 |
$this->parametres['navigation.depart'] = 0;
|
|
|
91 |
}
|
|
|
92 |
if (isset($this->parametres['navigation.limite']) == false) {
|
|
|
93 |
$this->parametres['navigation.limite'] = 100;
|
|
|
94 |
}
|
638 |
aurelien |
95 |
if (isset($this->parametres['referentiel']) == false) {
|
|
|
96 |
$this->parametres['referentiel'] = self::CODE_REFTAX_DEFAUT;
|
|
|
97 |
}
|
106 |
aurelien |
98 |
}
|
116 |
jpm |
99 |
|
|
|
100 |
private function verifierParametres() {
|
|
|
101 |
$erreurs = array();
|
|
|
102 |
|
375 |
delphine |
103 |
if (!isset($this->ressources[0])) {
|
|
|
104 |
if (isset($this->parametres['masque.nn']) == false) {
|
|
|
105 |
$erreurs[] = "Le paramètre masque.nn est obligatoire.";
|
|
|
106 |
} else {
|
|
|
107 |
$this->analyserMasqueNn();
|
|
|
108 |
if ($this->verifierMasqueNnAutorisePourRetourJPEG() == false) {
|
|
|
109 |
$erreurs[] = "Le paramètre masque.nn peut contenir une seule valeur numérique pour l'instant pour le format de retour image/jpeg.";
|
|
|
110 |
} else if ($this->verifierValeurParametreMasqueNn() == false) {
|
|
|
111 |
$erreurs[] = "Le paramètre masque.nn est mal formé.";
|
|
|
112 |
}
|
106 |
aurelien |
113 |
}
|
|
|
114 |
}
|
116 |
jpm |
115 |
if (isset($this->parametres['retour']) == false) {
|
|
|
116 |
$erreurs[] = "Le paramètre type de retour 'retour' est obligatoire.";
|
106 |
aurelien |
117 |
}
|
116 |
jpm |
118 |
if ($this->verifierValeurParametreRetour() == false) {
|
|
|
119 |
$erreurs[] = "Le type de retour '{$this->parametres['retour']}' n'est pas supporté";
|
106 |
aurelien |
120 |
}
|
116 |
jpm |
121 |
if (isset($this->parametres['retour.format']) == false) {
|
|
|
122 |
$erreurs[] = "Le paramètre de format de retour 'retour.format' est obligatoire.";
|
106 |
aurelien |
123 |
}
|
116 |
jpm |
124 |
if ($this->verifierValeurParametreFormat() == false) {
|
|
|
125 |
$erreurs[] = "Le type de format '{$this->parametres['retour.format']}' n'est pas supporté";
|
|
|
126 |
}
|
330 |
delphine |
127 |
if (isset($this->parametres['retour.tri'])){
|
|
|
128 |
if ($this->verifierValeurParametreTri() == false) {
|
|
|
129 |
$erreurs[] = "Le type de tri '{$this->parametres['retour.tri']}' n'est pas supporté";
|
|
|
130 |
}
|
|
|
131 |
}
|
386 |
jpm |
132 |
|
378 |
delphine |
133 |
if (isset($this->parametres['retour.champs'])){
|
|
|
134 |
if ($this->verifierValeurParametreRetourChamps() == false) {
|
|
|
135 |
$erreurs[] = "Le champs '{$this->parametres['retour.champs']}' n'existe pas";
|
|
|
136 |
}
|
|
|
137 |
}
|
386 |
jpm |
138 |
|
138 |
jpm |
139 |
if ($this->verifierValeurParametreNavigationDepart() == false) {
|
|
|
140 |
$erreurs[] = "Le paramètre 'navigation.depart' doit possèder un valeur numérique.";
|
|
|
141 |
}
|
|
|
142 |
if ($this->verifierValeurParametreNavigationLimite() == false) {
|
|
|
143 |
$erreurs[] = "Le paramètre 'navigation.limite' doit possèder un valeur numérique supérieure à 0.";
|
|
|
144 |
}
|
116 |
jpm |
145 |
|
|
|
146 |
if (count($erreurs) > 0) {
|
|
|
147 |
$message = implode('<br />', $erreurs);
|
|
|
148 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
149 |
throw new Exception($message, $code);
|
|
|
150 |
}
|
106 |
aurelien |
151 |
}
|
116 |
jpm |
152 |
|
162 |
jpm |
153 |
private function verifierMasqueNnAutorisePourRetourJPEG() {
|
138 |
jpm |
154 |
$ok = true;
|
|
|
155 |
$masque = $this->parametres['masque.nn'];
|
|
|
156 |
$retour = $this->parametres['retour'];
|
162 |
jpm |
157 |
|
138 |
jpm |
158 |
if ($retour == self::MIME_JPEG) {
|
|
|
159 |
$ok = is_numeric($masque) ? true : false;
|
|
|
160 |
}
|
|
|
161 |
return $ok;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
private function verifierValeurParametreMasqueNn() {
|
|
|
165 |
$nn = $this->parametres['masque.nn'];
|
|
|
166 |
$projetPattern = '(?:(?:[A-Z0-9]+:)?(?:[0-9]+,)*[0-9]+)';
|
162 |
jpm |
167 |
$patternComplet = "/^$projetPattern(?:;$projetPattern)*$/i";
|
138 |
jpm |
168 |
$ok = preg_match($patternComplet, $nn) ? true : false;
|
|
|
169 |
return $ok;
|
|
|
170 |
}
|
|
|
171 |
|
116 |
jpm |
172 |
private function verifierValeurParametreRetour() {
|
|
|
173 |
return in_array($this->parametres['retour'], $this->formats_supportes);
|
106 |
aurelien |
174 |
}
|
116 |
jpm |
175 |
|
|
|
176 |
private function verifierValeurParametreFormat() {
|
118 |
jpm |
177 |
$formats = Outils::recupererTableauConfig('Images.formats');
|
116 |
jpm |
178 |
$ok = array_key_exists($this->parametres['retour.format'], $formats);
|
|
|
179 |
return $ok;
|
106 |
aurelien |
180 |
}
|
377 |
jpm |
181 |
|
330 |
delphine |
182 |
private function verifierValeurParametreTri() {
|
|
|
183 |
return in_array($this->parametres['retour.tri'], $this->tris_supportes);
|
|
|
184 |
}
|
386 |
jpm |
185 |
|
378 |
delphine |
186 |
private function verifierValeurParametreRetourChamps() {
|
|
|
187 |
$ok = false;
|
|
|
188 |
$liste_champs = preg_split(',', $this->parametres['retour.champs']);
|
|
|
189 |
foreach ($liste_champs as $champs) {
|
|
|
190 |
$ok[$champs] = array_key_exists($champs, $this->retour_champs);
|
|
|
191 |
}
|
|
|
192 |
return $ok;
|
|
|
193 |
}
|
116 |
jpm |
194 |
|
138 |
jpm |
195 |
private function verifierValeurParametreNavigationDepart() {
|
|
|
196 |
$depart = $this->parametres['navigation.depart'];
|
|
|
197 |
$ok = is_numeric($depart) ? true : false;
|
116 |
jpm |
198 |
return $ok;
|
106 |
aurelien |
199 |
}
|
116 |
jpm |
200 |
|
138 |
jpm |
201 |
private function verifierValeurParametreNavigationLimite() {
|
|
|
202 |
$limite = $this->parametres['navigation.limite'];
|
|
|
203 |
$ok = (is_numeric($limite) && $limite != 0) ? true : false;
|
116 |
jpm |
204 |
return $ok;
|
106 |
aurelien |
205 |
}
|
116 |
jpm |
206 |
|
|
|
207 |
private function analyserMasqueNn() {
|
|
|
208 |
$nn = $this->parametres['masque.nn'];
|
142 |
jpm |
209 |
if (preg_match('/^[0-9]+$/', $nn)) {
|
638 |
aurelien |
210 |
$this->ref_tax_demande[$this->parametres['referentiel']][] = $nn;
|
106 |
aurelien |
211 |
} else {
|
116 |
jpm |
212 |
// ceci contient potentiellement des formes ref_tax1:nn1,nn2;ref_tax2:nn3,nn4
|
|
|
213 |
$projetsListeEtNumNoms = explode(';', $nn);
|
|
|
214 |
if (count($projetsListeEtNumNoms) > 0) {
|
|
|
215 |
foreach ($projetsListeEtNumNoms as $projetEtNumNoms) {
|
638 |
aurelien |
216 |
$projetEtNumNoms = (strpos($projetEtNumNoms, ':')) ? $projetEtNumNoms : $this->parametres['referentiel'].':'.$projetEtNumNoms;
|
116 |
jpm |
217 |
list($projet, $numNoms) = explode(':', $projetEtNumNoms);
|
|
|
218 |
$this->ref_tax_demande[$projet] = explode(',', $numNoms);
|
106 |
aurelien |
219 |
}
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
}
|
116 |
jpm |
223 |
|
377 |
jpm |
224 |
|
375 |
delphine |
225 |
//+---------------------------------------- REQUETES ---------------------------------------------------------------+
|
138 |
jpm |
226 |
private function obtenirIdImageAuHasard() {
|
638 |
aurelien |
227 |
$refTax = $this->parametres['referentiel'];
|
142 |
jpm |
228 |
$numNom = $this->Bdd->proteger($this->ref_tax_demande[$refTax][0]);
|
116 |
jpm |
229 |
|
538 |
aurelien |
230 |
$requete = 'SELECT coi.id_image AS id_image '.
|
116 |
jpm |
231 |
'FROM cel_obs_images AS coi '.
|
538 |
aurelien |
232 |
' LEFT JOIN cel_obs AS co '.
|
|
|
233 |
'ON (coi.id_observation = co.id_observation) '.
|
116 |
jpm |
234 |
'WHERE ci.transmission = 1 '.
|
538 |
aurelien |
235 |
" AND ci.nom_ret_nn IN ($numNom)";
|
767 |
raphael |
236 |
" AND co.nom_referentiel = ".$this->Bdd->proteger($refTax) . ' -- ' . __FILE__ . ':' . __LINE__;
|
116 |
jpm |
237 |
|
|
|
238 |
$resultat = $this->Bdd->recupererTous($requete);
|
|
|
239 |
|
|
|
240 |
if (!is_array($resultat) || count($resultat) <= 0) {
|
126 |
jpm |
241 |
$message = "Aucune image ne correspond au numéro numenclatural $refTax:$numNom";
|
116 |
jpm |
242 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
243 |
throw new Exception($message, $code);
|
106 |
aurelien |
244 |
}
|
116 |
jpm |
245 |
|
|
|
246 |
$id_image_hasard = $resultat[array_rand($resultat)]['id_image'];
|
106 |
aurelien |
247 |
return $id_image_hasard;
|
|
|
248 |
}
|
377 |
jpm |
249 |
|
330 |
delphine |
250 |
private function obtenirIdPremiereImage() {
|
638 |
aurelien |
251 |
$refTax = $this->parametres['referentiel'];
|
330 |
delphine |
252 |
$numNom = $this->Bdd->proteger($this->ref_tax_demande[$refTax][0]);
|
538 |
aurelien |
253 |
$requete = 'SELECT ci.id_image AS id_image '.
|
330 |
delphine |
254 |
'FROM cel_images AS ci'.
|
|
|
255 |
' LEFT JOIN cel_obs_images AS coi '.
|
538 |
aurelien |
256 |
' ON (coi.id_image = ci.id_image) '.
|
|
|
257 |
' LEFT JOIN cel_obs AS co '.
|
|
|
258 |
' ON (coi.id_observation = co.id_observation) '.
|
330 |
delphine |
259 |
'WHERE co.transmission = 1 '.
|
538 |
aurelien |
260 |
" AND co.nom_ret_nn IN ($numNom) ".
|
626 |
aurelien |
261 |
' AND ci.date_prise_de_vue != "0000-00-00" ORDER BY ci.date_prise_de_vue ASC '.
|
638 |
aurelien |
262 |
' AND co.nom_referentiel LIKE '.$this->proteger($refTax.'%').' '.
|
767 |
raphael |
263 |
'LIMIT 1' . ' -- ' . __FILE__ . ':' . __LINE__;
|
377 |
jpm |
264 |
|
330 |
delphine |
265 |
$resultat = $this->Bdd->recupererTous($requete);
|
377 |
jpm |
266 |
|
330 |
delphine |
267 |
if (!is_array($resultat) || count($resultat) <= 0) {
|
|
|
268 |
$message = "Aucune image ne correspond au numéro numenclatural $refTax:$numNom";
|
|
|
269 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
270 |
throw new Exception($message, $code);
|
|
|
271 |
}
|
377 |
jpm |
272 |
|
330 |
delphine |
273 |
$id_image_hasard = $resultat[0]['id_image'];
|
|
|
274 |
return $id_image_hasard;
|
|
|
275 |
}
|
377 |
jpm |
276 |
|
375 |
delphine |
277 |
private function chargerInfosImage() {
|
|
|
278 |
$requete = 'SELECT SQL_CALC_FOUND_ROWS '.
|
638 |
aurelien |
279 |
' ci.id_image AS id_img, co.date_observation AS date, '.
|
538 |
aurelien |
280 |
' co.id_observation AS id_obs, '.
|
539 |
aurelien |
281 |
' ci.courriel_utilisateur AS utilisateur_courriel, '.
|
538 |
aurelien |
282 |
' nom_sel, nom_sel_nn, '.
|
|
|
283 |
' zone_geo, ce_zone_geo, lieudit, station, milieu '.
|
375 |
delphine |
284 |
'FROM cel_images AS ci'.
|
|
|
285 |
' LEFT JOIN cel_obs_images AS coi '.
|
539 |
aurelien |
286 |
' ON (coi.id_image = ci.id_image) '.
|
538 |
aurelien |
287 |
' LEFT JOIN cel_obs AS co '.
|
|
|
288 |
' ON (coi.id_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'] : '').
|
|
|
308 |
'FROM cel_images AS ci'.
|
|
|
309 |
' LEFT JOIN cel_obs_images AS coi '.
|
538 |
aurelien |
310 |
' ON (coi.id_image = ci.id_image) '.
|
|
|
311 |
' LEFT JOIN cel_obs AS co '.
|
|
|
312 |
' ON (coi.id_observation = co.id_observation) '.
|
386 |
jpm |
313 |
$this->formerRequeteConditions($numNomListe).' '.
|
|
|
314 |
'GROUP BY id_img '.
|
|
|
315 |
$this->formerRequeteTri().
|
767 |
raphael |
316 |
"LIMIT $depart,$limite " . ' -- ' . __FILE__ . ':' . __LINE__;
|
377 |
jpm |
317 |
|
375 |
delphine |
318 |
$this->infosImages = $this->Bdd->recupererTous($requete);
|
|
|
319 |
}
|
386 |
jpm |
320 |
|
375 |
delphine |
321 |
private function formerRequeteConditions($numNomListe) {
|
638 |
aurelien |
322 |
$refTax = $this->parametres['referentiel'];
|
538 |
aurelien |
323 |
$where[] = " co.transmission = 1 AND co.nom_ret_nn IN ($numNomListe) ";
|
638 |
aurelien |
324 |
$where[] = " co.nom_referentiel LIKE ".$this->Bdd->proteger($refTax."%").' ';
|
|
|
325 |
|
375 |
delphine |
326 |
return ' WHERE '.implode(' AND ', $where);
|
|
|
327 |
}
|
377 |
jpm |
328 |
|
375 |
delphine |
329 |
private function formerRequeteTri() {
|
386 |
jpm |
330 |
$order = '';
|
375 |
delphine |
331 |
if (isset($this->parametres['retour.tri']) && $this->parametres['retour.tri'] == 'date') {
|
638 |
aurelien |
332 |
$order = ' ORDER BY co.date_observation ASC ';
|
375 |
delphine |
333 |
}
|
|
|
334 |
return $order;
|
|
|
335 |
}
|
377 |
jpm |
336 |
|
375 |
delphine |
337 |
private function chargerNbreImagesTotal() {
|
|
|
338 |
$requete = 'SELECT FOUND_ROWS() AS nbre ';
|
|
|
339 |
$resultats = $this->Bdd->recuperer($requete);
|
|
|
340 |
$this->nbreImages = (int) $resultats['nbre'];
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
//+---------------------------------------CHEMIN ET CONVERSION--------------------------------------------------------+
|
116 |
jpm |
344 |
private function recupererImageBinaire($id_image) {
|
|
|
345 |
$image = '';
|
|
|
346 |
$chemin = $this->obtenirCheminImage($id_image);
|
118 |
jpm |
347 |
$image = file_get_contents($chemin);
|
|
|
348 |
if ($image === false) {
|
116 |
jpm |
349 |
$message = "L'image demandée est introuvable sur le serveur : $chemin";
|
|
|
350 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
351 |
throw new Exception($message, $code);
|
106 |
aurelien |
352 |
}
|
116 |
jpm |
353 |
return $image;
|
106 |
aurelien |
354 |
}
|
116 |
jpm |
355 |
|
127 |
jpm |
356 |
private function obtenirCheminImage($idImage) {
|
116 |
jpm |
357 |
$nom = $this->convertirIdImageVersNomFichier($idImage);
|
|
|
358 |
$dossier = $this->convertirIdImageVersChemin($idImage);
|
106 |
aurelien |
359 |
return $dossier.'/'.$nom;
|
|
|
360 |
}
|
116 |
jpm |
361 |
|
127 |
jpm |
362 |
private function convertirIdImageVersNomFichier($idImage) {
|
116 |
jpm |
363 |
$codeImage = $this->construireCodeImage($idImage);
|
|
|
364 |
$nom_fichier = $codeImage.'.jpg';
|
106 |
aurelien |
365 |
return $nom_fichier;
|
|
|
366 |
}
|
116 |
jpm |
367 |
|
|
|
368 |
private function convertirIdImageVersChemin($idImage) {
|
|
|
369 |
$codeImage = $this->construireCodeImage($idImage);
|
|
|
370 |
list($dossierNiveau1, $dossierNiveau2, $idFichier, $format) = explode('_', $codeImage);
|
140 |
jpm |
371 |
$chemin_sur_serveur = $this->cheminImagesBase.'/'.$dossierNiveau1.'/'.$dossierNiveau2.'/'.$format;
|
116 |
jpm |
372 |
return $chemin_sur_serveur;
|
106 |
aurelien |
373 |
}
|
116 |
jpm |
374 |
|
|
|
375 |
private function construireCodeImage($idImage) {
|
|
|
376 |
$codeImage = sprintf('%09s', $idImage);
|
|
|
377 |
$codeImage = wordwrap($codeImage, 3, '_', true);
|
|
|
378 |
$format = $this->parametres['retour.format'];
|
|
|
379 |
$codeImage .= '_'.$format;
|
|
|
380 |
return $codeImage;
|
106 |
aurelien |
381 |
}
|
377 |
jpm |
382 |
|
375 |
delphine |
383 |
//+------------------------------------FORMATAGE LISTE----------------------------------------------------------------+
|
138 |
jpm |
384 |
private function formaterListeImages() {
|
|
|
385 |
$entete = $this->construireEntete();
|
|
|
386 |
$resultats = $this->construireResultats();
|
|
|
387 |
|
|
|
388 |
$resultat = array('entete' => $entete, 'resultats' => $resultats);
|
|
|
389 |
return $resultat;
|
|
|
390 |
}
|
|
|
391 |
|
|
|
392 |
private function construireEntete() {
|
|
|
393 |
$entete = array('masque' => '', 'depart' => 0, 'limite' => 100, 'total' => 0);
|
|
|
394 |
|
|
|
395 |
$entete['masque'] = $this->recupererMasque();
|
|
|
396 |
$entete['depart'] = (int) $this->parametres['navigation.depart'];
|
|
|
397 |
$entete['limite'] = (int) $this->parametres['navigation.limite'];
|
|
|
398 |
$entete['total'] = $this->nbreImages;
|
|
|
399 |
if ($hrefPrecedent = $this->recupererHrefPrecedent()) {
|
|
|
400 |
$entete['href.precedent'] = $hrefPrecedent;
|
|
|
401 |
}
|
|
|
402 |
if ($hrefSuivant = $this->recupererHrefSuivant()) {
|
|
|
403 |
$entete['href.suivant'] = $hrefSuivant;
|
|
|
404 |
}
|
|
|
405 |
return $entete;
|
|
|
406 |
}
|
|
|
407 |
|
|
|
408 |
private function recupererMasque() {
|
|
|
409 |
$masqueEntete = '';
|
|
|
410 |
if ($masqueNn = $this->parametres['masque.nn']) {
|
|
|
411 |
$masqueEntete = "nn=$masqueNn";
|
|
|
412 |
}
|
|
|
413 |
return $masqueEntete;
|
|
|
414 |
}
|
|
|
415 |
|
|
|
416 |
private function recupererHrefPrecedent() {
|
|
|
417 |
$departActuel = $this->parametres['navigation.depart'];
|
|
|
418 |
$limite = $this->parametres['navigation.limite'];
|
|
|
419 |
$departPrecedent = $departActuel - $limite;
|
|
|
420 |
$url = null;
|
|
|
421 |
if ($departPrecedent >= 0) {
|
|
|
422 |
$url = $this->obtenirUrlNavigation($departPrecedent, $limite);
|
|
|
423 |
}
|
|
|
424 |
return $url;
|
|
|
425 |
}
|
|
|
426 |
|
|
|
427 |
private function recupererHrefSuivant() {
|
|
|
428 |
$departActuel = $this->parametres['navigation.depart'];
|
|
|
429 |
$limite = $this->parametres['navigation.limite'];
|
|
|
430 |
$departSuivant = $departActuel + $limite;
|
|
|
431 |
$url = null;
|
|
|
432 |
if ($departSuivant < $this->nbreImages) {
|
|
|
433 |
$url = $this->obtenirUrlNavigation($departSuivant, $limite);
|
|
|
434 |
}
|
|
|
435 |
return $url;
|
|
|
436 |
}
|
|
|
437 |
|
|
|
438 |
private function obtenirUrlNavigation($depart, $limite) {
|
|
|
439 |
$parametres = array(
|
|
|
440 |
'navigation.depart' => $depart,
|
|
|
441 |
'navigation.limite' => $limite);
|
|
|
442 |
if (isset($this->parametres['masque.nn'])) {
|
|
|
443 |
$parametres['masque.nn'] = $this->parametres['masque.nn'];
|
|
|
444 |
}
|
140 |
jpm |
445 |
$this->UrlNavigation->setRequete($parametres);
|
|
|
446 |
$url = $this->UrlNavigation->getURL();
|
138 |
jpm |
447 |
return $url;
|
|
|
448 |
}
|
|
|
449 |
|
|
|
450 |
private function construireResultats() {
|
|
|
451 |
$resultats = array();
|
386 |
jpm |
452 |
$this->extraireIdentitesAuteurs();
|
138 |
jpm |
453 |
foreach ($this->infosImages as $img) {
|
375 |
delphine |
454 |
$info = $this->formaterInfosImage($img);
|
142 |
jpm |
455 |
$id = $img['id_img'];
|
378 |
delphine |
456 |
$info['href'] = $this->config['urlService'].'/'.$id;
|
138 |
jpm |
457 |
if (!isset($resultats[$id])) {
|
|
|
458 |
$resultats[$id] = $info;
|
|
|
459 |
} else {
|
|
|
460 |
throw new Exception("Double : $id");
|
|
|
461 |
}
|
|
|
462 |
}
|
|
|
463 |
return $resultats;
|
|
|
464 |
}
|
|
|
465 |
|
375 |
delphine |
466 |
private function formaterInfosImage($img) {
|
|
|
467 |
$info = array();
|
|
|
468 |
$info['date'] = $img['date'];
|
|
|
469 |
$info['mime'] = self::MIME_JPEG;
|
386 |
jpm |
470 |
$info['auteur.libelle'] = $this->Utilisateurs->getIntitule($img['utilisateur_courriel']);
|
|
|
471 |
$info['auteur.id'] = $this->Utilisateurs->getId($img['utilisateur_courriel']);
|
375 |
delphine |
472 |
$info['binaire.href'] = $this->formaterUrlImage($img);
|
393 |
jpm |
473 |
$info['observation.id'] = $img['id_obs'];
|
378 |
delphine |
474 |
if (isset($img['nom_sel'])) {
|
|
|
475 |
$info['determination.libelle'] = $this->formaterDetermination($img);
|
|
|
476 |
$info['determination.nom_sci'] = $img['nom_sel'];
|
|
|
477 |
$info['determination.nom_sci.code'] = $this->formaterNomSciCode($img);
|
|
|
478 |
}
|
375 |
delphine |
479 |
$info = array_merge($info, $this->formaterStation($img));
|
|
|
480 |
return $info;
|
|
|
481 |
}
|
377 |
jpm |
482 |
|
386 |
jpm |
483 |
private function extraireIdentitesAuteurs() {
|
138 |
jpm |
484 |
$courriels = array();
|
|
|
485 |
foreach ($this->infosImages as $img) {
|
|
|
486 |
$courriels[] = $img['utilisateur_courriel'];
|
|
|
487 |
}
|
386 |
jpm |
488 |
$this->Utilisateurs->setCourriels($courriels);
|
|
|
489 |
$this->Utilisateurs->chargerIdentites();
|
138 |
jpm |
490 |
}
|
|
|
491 |
|
|
|
492 |
private function formaterUrlImage($infos) {
|
143 |
jpm |
493 |
$format = $this->parametres['retour.format'];
|
138 |
jpm |
494 |
$id = sprintf('%09s', $infos['id_img']).$format;
|
|
|
495 |
$url = sprintf(self::TPL_URL_IMG, $id);
|
|
|
496 |
return $url;
|
|
|
497 |
}
|
|
|
498 |
|
|
|
499 |
private function formaterDetermination($infos) {
|
386 |
jpm |
500 |
$auteur = $this->Utilisateurs->getIntitule($infos['utilisateur_courriel']);
|
|
|
501 |
return $infos['nom_sel']."[Dét. : $auteur]";
|
138 |
jpm |
502 |
}
|
|
|
503 |
|
|
|
504 |
private function formaterNomSciCode($infos) {
|
638 |
aurelien |
505 |
return $this->parametres['referentiel'].'.'.$infos['nom_sel_nn'];
|
138 |
jpm |
506 |
}
|
|
|
507 |
|
|
|
508 |
private function formaterStation($infos) {
|
|
|
509 |
$station = array();
|
538 |
aurelien |
510 |
if (isset($infos['zone_geo']) && $commune = $this->formaterCommune($infos)) {
|
375 |
delphine |
511 |
$station['station.commune'] = $commune;
|
138 |
jpm |
512 |
}
|
378 |
delphine |
513 |
if (isset($infos['lieudit']) && $this->avoirContenu($infos['lieudit'])) {
|
375 |
delphine |
514 |
$station['station.lieudit'] = $infos['lieudit'];
|
138 |
jpm |
515 |
}
|
378 |
delphine |
516 |
if (isset($infos['station']) && $this->avoirContenu($infos['station'])) {
|
375 |
delphine |
517 |
$station['station.station'] = $infos['station'];
|
138 |
jpm |
518 |
}
|
|
|
519 |
|
378 |
delphine |
520 |
if (isset($infos['milieu']) && $this->avoirContenu($infos['milieu'])) {
|
375 |
delphine |
521 |
$station['station.milieu'] = $infos['milieu'];
|
138 |
jpm |
522 |
}
|
666 |
aurelien |
523 |
if (count($station) >= 1) {
|
378 |
delphine |
524 |
$station['station.libelle'] = implode(' > ', $station);
|
|
|
525 |
}
|
375 |
delphine |
526 |
return $station;
|
138 |
jpm |
527 |
}
|
|
|
528 |
|
|
|
529 |
private function formaterCommune($infos) {
|
|
|
530 |
$commune = array();
|
538 |
aurelien |
531 |
if ($this->avoirContenu($infos['zone_geo'])) {
|
|
|
532 |
$commune[] = $infos['zone_geo'];
|
138 |
jpm |
533 |
}
|
638 |
aurelien |
534 |
if (isset($infos['ce_zone_geo']) && $this->avoirContenu($infos['ce_zone_geo']) && $infos['ce_zone_geo'] != 'INSEE-C:') {
|
538 |
aurelien |
535 |
$commune[] = '('.substr(str_replace('INSEE-C:','',$infos['ce_zone_geo']), 0, 2).')';
|
138 |
jpm |
536 |
}
|
|
|
537 |
return implode(' ', $commune);
|
|
|
538 |
}
|
|
|
539 |
|
|
|
540 |
private function avoirContenu($info) {
|
638 |
aurelien |
541 |
return !($info == null || $info == '' || $info == '000null');
|
138 |
jpm |
542 |
}
|
106 |
aurelien |
543 |
}
|
|
|
544 |
?>
|