Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 127 | Rev 140 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 127 Rev 138
Line 18... Line 18...
18
 
18
 
19
	private $parametres = array();
19
	private $parametres = array();
20
	private $ressources = array();
20
	private $ressources = array();
Line -... Line 21...
-
 
21
	private $Bdd;
-
 
22
 
-
 
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';
21
	private $Bdd;
26
	const MIME_JPEG = 'image/jpeg';
22
 
27
	const MIME_JSON = 'application/json';
-
 
28
	private $formats_supportes = array(self::MIME_JPEG, self::MIME_JSON);
-
 
29
	private $ref_tax_demande = array();
-
 
30
	private $infosImages = array();
-
 
31
	private $nbreImages = 0;
23
	private $formats_supportes = array('image/jpeg');
32
	private $Utilisateurs = null;
24
	private $ref_tax_demande = array();
33
	private $intitulesAuteurs = array();
25
 
34
 
-
 
35
	public function __construct(Bdd $bdd = null, Utilisateurs $utilisateurs = null) {
26
	public function __construct(Bdd $bdd) {
36
		$this->Bdd = is_null($bdd) ? new Bdd() : $bdd;
Line 27... Line 37...
27
		$this->Bdd = $bdd;
37
		$this->Utilisateurs = is_null($utilisateurs) ? new Utilisateurs() : $utilisateurs;
28
	}
38
	}
29
 
39
 
Line 30... Line 40...
30
	public function consulter($ressources, $parametres) {
40
	public function consulter($ressources, $parametres) {
31
		$this->parametres = $parametres;
41
		$this->parametres = $parametres;
32
		$this->ressources = $ressources;
42
		$this->ressources = $ressources;
33
 
-
 
34
		$this->definirValeurParDefautDesParametres();
-
 
Line 35... Line 43...
35
		$this->verifierParametres();
43
 
-
 
44
		$this->definirValeurParDefautDesParametres();
-
 
45
		$this->verifierParametres();
-
 
46
		$this->analyserMasqueNn();
-
 
47
 
-
 
48
		$resultat = new ResultatService();
-
 
49
		if ($this->parametres['retour'] == self::MIME_JPEG) {
36
		$this->analyserMasqueNn();
50
			$id_image_a_renvoyer = $this->obtenirIdImageAuHasard();
-
 
51
			$resultat->corps = $this->recupererImageBinaire($id_image_a_renvoyer);
37
		$id_image_a_renvoyer = $this->obtenirIdImageAuHasardPourNumNom();
52
		} else if ($this->parametres['retour'] == self::MIME_JSON) {
-
 
53
			$this->chargerListeImages();
38
		$image = $this->recupererImageBinaire($id_image_a_renvoyer);
54
			$this->chargerNbreImagesTotal();
39
 
55
			$resultat->corps = $infos = $this->formaterListeImages();
Line 40... Line 56...
40
		$resultat = new ResultatService();
56
		}
41
		$resultat->mime = 'image/jpeg';
57
		$resultat->mime = $this->parametres['retour'];
42
		$resultat->corps = $image;
58
 
43
		return $resultat;
59
		return $resultat;
44
	}
60
	}
45
 
61
 
46
	private function definirValeurParDefautDesParametres() {
62
	private function definirValeurParDefautDesParametres() {
-
 
63
		if (isset($this->parametres['retour']) == false) {
-
 
64
			$this->parametres['retour'] = self::MIME_JSON;
-
 
65
		}
-
 
66
		if (isset($this->parametres['retour.format']) == false) {
-
 
67
			$this->parametres['retour.format'] = 'M';
-
 
68
		}
47
		if (isset($this->parametres['retour']) == false) {
69
		if (isset($this->parametres['navigation.depart']) == false) {
Line 48... Line 70...
48
			$this->parametres['retour'] = 'image/jpeg';
70
			$this->parametres['navigation.depart'] = 0;
49
		}
71
		}
Line 50... Line 72...
50
		if (isset($this->parametres['retour.format']) == false) {
72
		if (isset($this->parametres['navigation.limite']) == false) {
51
			$this->parametres['retour.format'] = 'M';
73
			$this->parametres['navigation.limite'] = 100;
52
		}
74
		}
53
	}
75
	}
54
 
76
 
55
	private function verifierParametres() {
77
	private function verifierParametres() {
56
		$erreurs = array();
78
		$erreurs = array();
57
 
79
 
58
		if (isset($this->parametres['masque.nn']) == false) {
80
		if (isset($this->parametres['masque.nn']) == false) {
59
			$erreurs[] = "Le paramètre masque.nn est obligatoire.";
81
			$erreurs[] = "Le paramètre masque.nn est obligatoire.";
Line 74... Line 96...
74
			$erreurs[] = "Le paramètre de format de retour 'retour.format' est obligatoire.";
96
			$erreurs[] = "Le paramètre de format de retour 'retour.format' est obligatoire.";
75
		}
97
		}
76
		if ($this->verifierValeurParametreFormat() == false) {
98
		if ($this->verifierValeurParametreFormat() == false) {
77
			$erreurs[] = "Le type de format '{$this->parametres['retour.format']}' n'est pas supporté";
99
			$erreurs[] = "Le type de format '{$this->parametres['retour.format']}' n'est pas supporté";
78
		}
100
		}
-
 
101
		if ($this->verifierValeurParametreNavigationDepart() == false) {
-
 
102
			$erreurs[] = "Le paramètre 'navigation.depart' doit possèder un valeur numérique.";
-
 
103
		}
-
 
104
		if ($this->verifierValeurParametreNavigationLimite() == false) {
-
 
105
			$erreurs[] = "Le paramètre 'navigation.limite' doit possèder un valeur numérique supérieure à 0.";
-
 
106
		}
Line 79... Line 107...
79
 
107
 
80
		if (count($erreurs) > 0) {
108
		if (count($erreurs) > 0) {
81
			$message = implode('<br />', $erreurs);
109
			$message = implode('<br />', $erreurs);
82
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
110
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
83
			throw new Exception($message, $code);
111
			throw new Exception($message, $code);
84
		}
112
		}
Line -... Line 113...
-
 
113
	}
-
 
114
 
-
 
115
	private function verifierMasqueNnAutoriseActuellement() {
-
 
116
		$ok = true;
-
 
117
		$masque = $this->parametres['masque.nn'];
-
 
118
		$retour = $this->parametres['retour'];
-
 
119
		if ($retour == self::MIME_JPEG) {
-
 
120
			$ok = is_numeric($masque) ? true : false;
-
 
121
		} else if ($retour == self::MIME_JSON) {
-
 
122
			$listeNn = explode(',', $masque);
-
 
123
			if ($listeNn == false || count($listeNn) == 0) {
-
 
124
				$ok = false;
-
 
125
			} else {
-
 
126
				foreach ($listeNn as $nn) {
-
 
127
					if (is_numeric($nn) == false) {
-
 
128
						$ok = false;
-
 
129
						break;
-
 
130
					}
-
 
131
				}
-
 
132
			}
-
 
133
		}
-
 
134
		return $ok;
-
 
135
	}
-
 
136
 
-
 
137
	private function verifierValeurParametreMasqueNn() {
-
 
138
		$nn = $this->parametres['masque.nn'];
-
 
139
		$projetPattern = '(?:(?:[A-Z0-9]+:)?(?:[0-9]+,)*[0-9]+)';
-
 
140
		$patternComplet = "/$projetPattern(?:;$projetPattern)*/i";
-
 
141
		$ok = preg_match($patternComplet, $nn) ? true : false;
-
 
142
		return $ok;
85
	}
143
	}
86
 
144
 
87
	private function verifierValeurParametreRetour() {
145
	private function verifierValeurParametreRetour() {
Line 88... Line 146...
88
		return in_array($this->parametres['retour'], $this->formats_supportes);
146
		return in_array($this->parametres['retour'], $this->formats_supportes);
89
	}
147
	}
90
 
148
 
91
	private function verifierValeurParametreFormat() {
149
	private function verifierValeurParametreFormat() {
92
		$formats = Outils::recupererTableauConfig('Images.formats');
150
		$formats = Outils::recupererTableauConfig('Images.formats');
Line 93... Line 151...
93
		$ok = array_key_exists($this->parametres['retour.format'], $formats);
151
		$ok = array_key_exists($this->parametres['retour.format'], $formats);
-
 
152
		return $ok;
94
		return $ok;
153
	}
95
	}
154
 
96
 
155
	private function verifierValeurParametreNavigationDepart() {
Line 97... Line 156...
97
	private function verifierMasqueNnAutoriseActuellement() {
156
		$depart = $this->parametres['navigation.depart'];
98
		$ok = is_numeric($this->parametres['masque.nn']) ? true : false;
157
		$ok = is_numeric($depart) ? true : false;
99
		return $ok;
-
 
100
	}
-
 
101
 
158
		return $ok;
102
	private function verifierValeurParametreMasqueNn() {
159
	}
103
		$nn = $this->parametres['masque.nn'];
160
 
Line 104... Line 161...
104
		$projetPattern = '(?:(?:[A-Z0-9]+:)?(?:[0-9]+,)*[0-9]+)';
161
	private function verifierValeurParametreNavigationLimite() {
105
		$patternComplet = "/$projetPattern(?:;$projetPattern)*/i";
162
		$limite = $this->parametres['navigation.limite'];
Line 114... Line 171...
114
		} else {
171
		} else {
115
			// ceci contient potentiellement des formes ref_tax1:nn1,nn2;ref_tax2:nn3,nn4
172
			// ceci contient potentiellement des formes ref_tax1:nn1,nn2;ref_tax2:nn3,nn4
116
			$projetsListeEtNumNoms = explode(';', $nn);
173
			$projetsListeEtNumNoms = explode(';', $nn);
117
			if (count($projetsListeEtNumNoms) > 0) {
174
			if (count($projetsListeEtNumNoms) > 0) {
118
				foreach ($projetsListeEtNumNoms as $projetEtNumNoms) {
175
				foreach ($projetsListeEtNumNoms as $projetEtNumNoms) {
-
 
176
					$projetEtNumNoms = (strpos($projetEtNumNoms, ':')) ? $projetEtNumNoms : 'bdtfx:'.$projetEtNumNoms;
119
					list($projet, $numNoms) = explode(':', $projetEtNumNoms);
177
					list($projet, $numNoms) = explode(':', $projetEtNumNoms);
120
					$this->ref_tax_demande[$projet] = explode(',', $numNoms);
178
					$this->ref_tax_demande[$projet] = explode(',', $numNoms);
121
				}
179
				}
122
			}
180
			}
123
		}
181
		}
124
	}
182
	}
Line 125... Line 183...
125
 
183
 
-
 
184
	private function obtenirIdImageAuHasard() {
126
	private function obtenirIdImageAuHasardPourNumNom() {
185
		$refTax = self::CODE_REFTAX_DEFAUT;
Line 127... Line 186...
127
		$numNom = $this->Bdd->proteger($this->ref_tax_demande['bdtfx']);
186
		$numNom = $this->Bdd->proteger($this->ref_tax_demande[$refTax]);
128
 
187
 
129
		//TODO: modifier la requete lors du passage à la nouvelle base de données pour faire quelque chose
188
		//TODO: modifier la requete lors du passage à la nouvelle base de données pour faire quelque chose
130
		// du numéro nomenclatural + modifier les champs appelés pour le nouveau format
189
		// du numéro nomenclatural + modifier les champs appelés pour le nouveau format
Line 184... Line 243...
184
		$codeImage = wordwrap($codeImage, 3, '_', true);
243
		$codeImage = wordwrap($codeImage, 3, '_', true);
185
		$format = $this->parametres['retour.format'];
244
		$format = $this->parametres['retour.format'];
186
		$codeImage .= '_'.$format;
245
		$codeImage .= '_'.$format;
187
		return $codeImage;
246
		return $codeImage;
188
	}
247
	}
-
 
248
 
-
 
249
	private function chargerListeImages() {
-
 
250
		$refTax = self::CODE_REFTAX_DEFAUT;
-
 
251
		$numNomListe = implode(',', $this->ref_tax_demande[$refTax]);
-
 
252
		$depart = $this->parametres['navigation.depart'];
-
 
253
		$limite = $this->parametres['navigation.limite'];
-
 
254
 
-
 
255
		//TODO: modifier la requete lors du passage à la nouvelle base de données pour faire quelque chose
-
 
256
		// du numéro nomenclatural + modifier les champs appelés pour le nouveau format
-
 
257
		$requete = 	'SELECT SQL_CALC_FOUND_ROWS '.
-
 
258
			' ci.ci_id_image AS id_img, co.id AS id_obs, '.
-
 
259
			' identifiant AS utilisateur_courriel, '.
-
 
260
			' nom_sel, num_nom_sel, '.
-
 
261
			' location, id_location, lieudit, station, milieu, '.
-
 
262
		    ' ci.ci_meta_date AS date '.
-
 
263
			'FROM cel_images AS ci'.
-
 
264
			'	LEFT JOIN cel_obs_images AS coi  '.
-
 
265
			'		ON (coi.coi_ce_image = ci.ci_id_image) '.
-
 
266
			'	LEFT JOIN cel_inventory AS co '.
-
 
267
			'		ON (coi.coi_ce_observation = co.ordre AND coi.coi_ce_utilisateur = co.identifiant) '.
-
 
268
			'WHERE co.transmission = 1 '.
-
 
269
			"	AND co.num_nom_ret IN ($numNomListe) ".
-
 
270
			'GROUP BY id_img '.
-
 
271
			"LIMIT $depart,$limite ";
-
 
272
 
-
 
273
		$this->infosImages = $this->Bdd->recupererTous($requete);
-
 
274
	}
-
 
275
 
-
 
276
	private function chargerNbreImagesTotal() {
-
 
277
		$requete = 'SELECT FOUND_ROWS() AS nbre ';
-
 
278
		$resultats = $this->Bdd->recuperer($requete);
-
 
279
		$this->nbreImages = (int) $resultats['nbre'];
-
 
280
	}
-
 
281
 
-
 
282
	private function formaterListeImages() {
-
 
283
		$entete = $this->construireEntete();
-
 
284
		$resultats = $this->construireResultats();
-
 
285
 
-
 
286
		$resultat = array('entete' => $entete, 'resultats' => $resultats);
-
 
287
		return $resultat;
-
 
288
	}
-
 
289
 
-
 
290
	private function construireEntete() {
-
 
291
		$entete = array('masque' => '', 'depart' => 0, 'limite' => 100, 'total' => 0);
-
 
292
 
-
 
293
		$entete['masque'] = $this->recupererMasque();
-
 
294
		$entete['depart'] = (int) $this->parametres['navigation.depart'];
-
 
295
		$entete['limite'] = (int) $this->parametres['navigation.limite'];
-
 
296
		$entete['total'] = $this->nbreImages;
-
 
297
		if ($hrefPrecedent = $this->recupererHrefPrecedent()) {
-
 
298
			$entete['href.precedent'] = $hrefPrecedent;
-
 
299
		}
-
 
300
		if ($hrefSuivant = $this->recupererHrefSuivant()) {
-
 
301
			$entete['href.suivant'] = $hrefSuivant;
-
 
302
		}
-
 
303
		return $entete;
-
 
304
	}
-
 
305
 
-
 
306
	private function recupererMasque() {
-
 
307
		$masqueEntete = '';
-
 
308
		if ($masqueNn = $this->parametres['masque.nn']) {
-
 
309
			$masqueEntete = "nn=$masqueNn";
-
 
310
		}
-
 
311
		return $masqueEntete;
-
 
312
	}
-
 
313
 
-
 
314
	private function recupererHrefPrecedent() {
-
 
315
		$departActuel = $this->parametres['navigation.depart'];
-
 
316
		$limite = $this->parametres['navigation.limite'];
-
 
317
		$departPrecedent = $departActuel - $limite;
-
 
318
		$url = null;
-
 
319
		if ($departPrecedent >= 0) {
-
 
320
			$url = $this->obtenirUrlNavigation($departPrecedent, $limite);
-
 
321
		}
-
 
322
		return $url;
-
 
323
	}
-
 
324
 
-
 
325
	private function recupererHrefSuivant() {
-
 
326
		$departActuel = $this->parametres['navigation.depart'];
-
 
327
		$limite = $this->parametres['navigation.limite'];
-
 
328
		$departSuivant = $departActuel + $limite;
-
 
329
		$url = null;
-
 
330
		if ($departSuivant < $this->nbreImages) {
-
 
331
			$url = $this->obtenirUrlNavigation($departSuivant, $limite);
-
 
332
		}
-
 
333
		return $url;
-
 
334
	}
-
 
335
 
-
 
336
	private function obtenirUrlNavigation($depart, $limite) {
-
 
337
		$urlNavigation = new Url(Config::get('serviceImageUrl'));
-
 
338
		$parametres = array(
-
 
339
			'navigation.depart' => $depart,
-
 
340
			'navigation.limite' => $limite);
-
 
341
		if (isset($this->parametres['masque.nn'])) {
-
 
342
			$parametres['masque.nn'] = $this->parametres['masque.nn'];
-
 
343
		}
-
 
344
		$urlNavigation->setRequete($parametres);
-
 
345
		$url = $urlNavigation->getURL();
-
 
346
		return $url;
-
 
347
	}
-
 
348
 
-
 
349
	private function construireResultats() {
-
 
350
		$resultats = array();
-
 
351
		$this->extraireIntitulesAuteurs();
-
 
352
		foreach ($this->infosImages as $img) {
-
 
353
			$id = $img['id_img'];
-
 
354
 
-
 
355
			$info = array();
-
 
356
			$info['date'] = $img['date'];
-
 
357
			$info['mime'] = self::MIME_JPEG;
-
 
358
			$info['auteur'] = $this->formaterAuteur($img);
-
 
359
			$info['binaire.href'] = $this->formaterUrlImage($id);
-
 
360
			$info['determination'] = $this->formaterDetermination($img);
-
 
361
			$info['determination.nom_sci'] = $img['nom_sel'];
-
 
362
			$info['determination.nom_sci.code'] = $this->formaterNomSciCode($img);
-
 
363
			$info['station'] = $this->formaterStation($img);
-
 
364
 
-
 
365
			if (!isset($resultats[$id])) {
-
 
366
				$resultats[$id] = $info;
-
 
367
			} else {
-
 
368
				throw new Exception("Double : $id");
-
 
369
			}
-
 
370
		}
-
 
371
		return $resultats;
-
 
372
	}
-
 
373
 
-
 
374
	private function extraireIntitulesAuteurs() {
-
 
375
		$courriels = array();
-
 
376
		foreach ($this->infosImages as $img) {
-
 
377
			$courriels[] = $img['utilisateur_courriel'];
-
 
378
		}
-
 
379
		$utilisateurs = new Utilisateurs($courriels);
-
 
380
		$this->intitulesAuteurs = $utilisateurs->getIntitules();
-
 
381
	}
-
 
382
 
-
 
383
	private function formaterUrlImage($infos) {
-
 
384
		$format = self::TAILLE_IMG_DEFAUT;
-
 
385
		$id = sprintf('%09s', $infos['id_img']).$format;
-
 
386
		$url = sprintf(self::TPL_URL_IMG, $id);
-
 
387
		return $url;
-
 
388
	}
-
 
389
 
-
 
390
	private function formaterDetermination($infos) {
-
 
391
		return $infos['nom_sel'].'[Dét. : '.$this->intitulesAuteurs[$infos['utilisateur_courriel']].']';
-
 
392
	}
-
 
393
 
-
 
394
	private function formaterNomSciCode($infos) {
-
 
395
		return self::CODE_REFTAX_DEFAUT.'.'.$infos['num_nom_sel'];
-
 
396
	}
-
 
397
 
-
 
398
	private function formaterStation($infos) {
-
 
399
		$station = array();
-
 
400
		if ($commune = $this->formaterCommune($infos)) {
-
 
401
			$station[] = $commune;
-
 
402
		}
-
 
403
		if ($this->avoirContenu($infos['lieudit'])) {
-
 
404
			$station[] = $infos['lieudit'];
-
 
405
		}
-
 
406
		if ($this->avoirContenu($infos['station'])) {
-
 
407
			$station[] = $infos['station'];
-
 
408
		}
-
 
409
 
-
 
410
		if ($this->avoirContenu($infos['milieu'])) {
-
 
411
			$station[] = $infos['milieu'];
-
 
412
		}
-
 
413
		return implode(' > ', $station);
-
 
414
	}
-
 
415
 
-
 
416
	private function formaterCommune($infos) {
-
 
417
		$commune = array();
-
 
418
		if ($this->avoirContenu($infos['location'])) {
-
 
419
			$commune[] = $infos['location'];
-
 
420
		}
-
 
421
		if ($this->avoirContenu($infos['id_location'])) {
-
 
422
			$commune[] = '('.$infos['id_location'].')';
-
 
423
		}
-
 
424
		return implode(' ', $commune);
-
 
425
	}
-
 
426
 
-
 
427
	private function formaterAuteur($infos) {
-
 
428
		$auteur = $this->intitulesAuteurs[$infos['utilisateur_courriel']];
-
 
429
		return $auteur;
-
 
430
	}
-
 
431
 
-
 
432
	private function avoirContenu($info) {
-
 
433
		$vide = true;
-
 
434
		if ($info == null || $info == '' || $info == '000null') {
-
 
435
			$vide = false;
-
 
436
		}
-
 
437
		return $vide;
-
 
438
	}
189
}
439
}
190
?>
440
?>
191
441