578 |
aurelien |
1 |
<?php
|
|
|
2 |
// declare(encoding='UTF-8');
|
1111 |
mathias |
3 |
// declare(tension='220v');
|
|
|
4 |
// declare(couleur_du_ciel='bleu');
|
578 |
aurelien |
5 |
/**
|
|
|
6 |
* Classe implémentant l'API d'eFlore concernant les statuts de protection
|
|
|
7 |
*
|
|
|
8 |
* @see http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=EfloreApi01Status
|
|
|
9 |
*
|
|
|
10 |
* @package eFlore/services
|
|
|
11 |
* @author Aurélien PERONNET <aurelien@tela-botanica.org>
|
1111 |
mathias |
12 |
* * @author Mathias CHOUET <mathias@tela-botanica.org>
|
578 |
aurelien |
13 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
14 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
15 |
* @version 1.0
|
1111 |
mathias |
16 |
* @copyright 1999-2014 Tela Botanica (accueil@tela-botanica.org)
|
578 |
aurelien |
17 |
*/
|
|
|
18 |
// TODO : Config et Outils sont des classes statiques qui doivent poser des pb pour les tests...
|
|
|
19 |
class Statuts extends Commun {
|
|
|
20 |
|
|
|
21 |
protected $parametres = array();
|
|
|
22 |
protected $ressources = array();
|
1111 |
mathias |
23 |
|
578 |
aurelien |
24 |
private $bdd;
|
613 |
aurelien |
25 |
private $retour_format = 'complet';
|
|
|
26 |
private $retours_formats_autorises = array('complet','zone_geo');
|
1111 |
mathias |
27 |
protected $nn_demande;
|
|
|
28 |
protected $navigation_depart;
|
|
|
29 |
protected $navigation_limite;
|
578 |
aurelien |
30 |
|
|
|
31 |
public function __construct($config = null) {
|
|
|
32 |
$this->config = $config;
|
|
|
33 |
$this->bdd = $this->getBdd();
|
|
|
34 |
}
|
1111 |
mathias |
35 |
|
578 |
aurelien |
36 |
public function consulter($ressources, $parametres) {
|
|
|
37 |
|
|
|
38 |
$this->parametres = $parametres;
|
|
|
39 |
$this->ressources = $ressources;
|
|
|
40 |
|
1111 |
mathias |
41 |
$this->affecterParametres();
|
578 |
aurelien |
42 |
|
|
|
43 |
$resultat = new ResultatService();
|
|
|
44 |
$resultat->corps = $this->obtenirStatuts();
|
|
|
45 |
|
|
|
46 |
return $resultat;
|
|
|
47 |
}
|
|
|
48 |
|
1111 |
mathias |
49 |
/**
|
|
|
50 |
* Affecte et vérifie les paramètres
|
|
|
51 |
* @throws Exception
|
|
|
52 |
*/
|
|
|
53 |
private function affecterParametres() {
|
578 |
aurelien |
54 |
$erreurs = array();
|
1111 |
mathias |
55 |
|
|
|
56 |
if (isset($this->parametres['retour.format'])) {
|
|
|
57 |
if(!in_array($this->retour_format, $this->retours_formats_autorises)) {
|
|
|
58 |
$erreurs[] = 'la valeur '.$this->retour_format.' est inconnue';
|
|
|
59 |
}
|
|
|
60 |
$this->retour_format = $this->parametres['retour.format'];
|
578 |
aurelien |
61 |
}
|
1111 |
mathias |
62 |
if (isset($this->parametres['masque.nn']) && ($this->parametres['masque.nn'] != '')) {
|
|
|
63 |
if(!is_numeric($this->parametres['masque.nn'])) {
|
|
|
64 |
$erreurs[] = 'la valeur pour masque.nn doit être un entier';
|
|
|
65 |
}
|
|
|
66 |
$this->nn_demande = $this->parametres['masque.nn'];
|
578 |
aurelien |
67 |
}
|
1111 |
mathias |
68 |
if (isset($this->parametres['masque.zone']) && ($this->parametres['masque.zone'] != '')) {
|
|
|
69 |
if(!is_numeric($this->parametres['masque.zone']) || !($this->parametres['masque.zone'] > 0 && $this->parametres['masque.zone'] <= 64)) {
|
|
|
70 |
$erreurs[] = 'la valeur pour masque.zone doit être un entier compris entre 1 et 64';
|
|
|
71 |
}
|
|
|
72 |
$this->zone = $this->parametres['masque.zone'];
|
613 |
aurelien |
73 |
}
|
1111 |
mathias |
74 |
if (isset($this->parametres['navigation.depart']) && ($this->parametres['navigation.depart'] != '')) {
|
|
|
75 |
if (!is_numeric($this->parametres['navigation.depart'])) {
|
|
|
76 |
$erreurs[] = 'la valeur pour navigation.depart doit être un entier';
|
|
|
77 |
}
|
|
|
78 |
$this->navigation_depart = max(0, intval($this->parametres['navigation.depart']));
|
|
|
79 |
} else {
|
|
|
80 |
$this->navigation_depart = 0;
|
|
|
81 |
}
|
|
|
82 |
if (isset($this->parametres['navigation.limite']) && ($this->parametres['navigation.limite'] != '')) {
|
|
|
83 |
if (!is_numeric($this->parametres['navigation.limite'])) {
|
|
|
84 |
$erreurs[] = 'la valeur pour navigation.limite doit être un entier';
|
|
|
85 |
}
|
|
|
86 |
$this->navigation_limite = intval($this->parametres['navigation.limite']);
|
|
|
87 |
} else {
|
|
|
88 |
$this->navigation_limite = 100;
|
|
|
89 |
}
|
|
|
90 |
|
578 |
aurelien |
91 |
if (count($erreurs) > 0) {
|
|
|
92 |
$message = implode('<br />', $erreurs);
|
|
|
93 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
94 |
throw new Exception($message, $code);
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
private function obtenirLois(Array $id_lois) {
|
|
|
99 |
$id_lois = array_map(array($this->bdd, 'proteger'), $id_lois);
|
678 |
delphine |
100 |
$requete = "SELECT * FROM ".Config::get('bdd_table_lois').' '.
|
578 |
aurelien |
101 |
"WHERE id IN (".implode(',',$id_lois).") ";
|
|
|
102 |
return $this->bdd->recupererTous($requete);
|
|
|
103 |
}
|
|
|
104 |
|
613 |
aurelien |
105 |
private function obtenirLoisZoneGeo(Array $id_lois) {
|
|
|
106 |
$id_lois = array_map(array($this->bdd, 'proteger'), $id_lois);
|
678 |
delphine |
107 |
$requete = "SELECT DISTINCT zone_application, code_zone_application FROM ".Config::get('bdd_table_lois').' '.
|
613 |
aurelien |
108 |
"WHERE id IN (".implode(',',$id_lois).") ";
|
|
|
109 |
return $this->bdd->recupererTous($requete);
|
|
|
110 |
}
|
|
|
111 |
|
1111 |
mathias |
112 |
private function obtenirStatuts() {
|
|
|
113 |
if ($this->nn_demande != null) {
|
|
|
114 |
$conditions_taxons = array();
|
|
|
115 |
$conditions_taxons = $this->obtenirNumNomTaxonsSuperieurs(Config::get('referentiel'), $this->nn_demande);
|
|
|
116 |
$conditions_taxons[] = $this->bdd->proteger($this->nn_demande);
|
|
|
117 |
}
|
640 |
aurelien |
118 |
|
1111 |
mathias |
119 |
$requete = "SELECT * FROM ".Config::get('bdd_table_especes');
|
|
|
120 |
if ($this->nn_demande != null) {
|
|
|
121 |
$requete .= " WHERE num_nom_retenu IN (".implode(', ', $conditions_taxons).") OR "
|
|
|
122 |
. "num_nom IN (".implode(', ', $conditions_taxons).") ";
|
|
|
123 |
}
|
|
|
124 |
// pagination
|
|
|
125 |
$requete .= " LIMIT " . $this->navigation_depart . ", " . $this->navigation_limite;
|
|
|
126 |
$requete .= ' -- ' . __FILE__ . ':' . __LINE__;
|
|
|
127 |
//echo "REQ: $requete\n";
|
640 |
aurelien |
128 |
|
578 |
aurelien |
129 |
$statuts = $this->bdd->recuperer($requete);
|
1111 |
mathias |
130 |
//echo "STATUTS: " . print_r($statuts, true) . "\n";
|
|
|
131 |
//echo "CPT: " . count($statuts) . "\n";
|
613 |
aurelien |
132 |
$statuts = $this->formaterRetour($statuts);
|
578 |
aurelien |
133 |
return $statuts;
|
940 |
aurelien |
134 |
}
|
578 |
aurelien |
135 |
//+---------------------------FONCTIONS DE FORMATAGE---------------------------------------------------------+
|
613 |
aurelien |
136 |
|
|
|
137 |
private function formaterRetour($statuts_taxon) {
|
|
|
138 |
switch($this->retour_format) {
|
|
|
139 |
case 'zone_geo':
|
|
|
140 |
$retour = $this->formaterStatutsTaxonZoneGeo($statuts_taxon);
|
|
|
141 |
break;
|
|
|
142 |
|
|
|
143 |
case 'complet':
|
|
|
144 |
$retour = $this->formaterStatutsTaxon($statuts_taxon);
|
|
|
145 |
break;
|
|
|
146 |
|
|
|
147 |
default:
|
|
|
148 |
$retour = $this->formaterStatutsTaxon();
|
|
|
149 |
break;
|
|
|
150 |
}
|
|
|
151 |
return $retour;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
private function formaterStatutsTaxonZoneGeo($statuts_taxon) {
|
|
|
155 |
$lois_statuts = array();
|
768 |
raphael |
156 |
// cas ou la requête (obtenirStatuts()) ne retourne rien
|
|
|
157 |
if(!$statuts_taxon) return array();
|
|
|
158 |
|
613 |
aurelien |
159 |
foreach ($statuts_taxon as $champ => $statut) {
|
|
|
160 |
if($statut == "1") {
|
|
|
161 |
$lois_statuts[] = $champ;
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
$zones_geo_lois = (!empty($lois_statuts)) ? $this->obtenirLoisZoneGeo($lois_statuts) : array();
|
|
|
166 |
|
|
|
167 |
return $zones_geo_lois;
|
|
|
168 |
}
|
|
|
169 |
|
578 |
aurelien |
170 |
private function formaterStatutsTaxon($statuts_taxon) {
|
|
|
171 |
$statuts_formates = array();
|
|
|
172 |
$lois_statuts = array();
|
640 |
aurelien |
173 |
|
|
|
174 |
if(is_array($statuts_taxon)) {
|
|
|
175 |
unset($statuts_taxon['num_nom']);
|
|
|
176 |
unset($statuts_taxon['num_nom_retenu']);
|
|
|
177 |
unset($statuts_taxon['nom_sci']);
|
|
|
178 |
foreach ($statuts_taxon as $champ => $statut) {
|
|
|
179 |
if($statut == "1") {
|
|
|
180 |
$lois_statuts[] = $champ;
|
|
|
181 |
}
|
613 |
aurelien |
182 |
}
|
578 |
aurelien |
183 |
}
|
613 |
aurelien |
184 |
|
578 |
aurelien |
185 |
$statuts_formates = (!empty($lois_statuts)) ? $this->obtenirLois($lois_statuts) : array();
|
613 |
aurelien |
186 |
|
578 |
aurelien |
187 |
return $statuts_formates;
|
|
|
188 |
}
|
|
|
189 |
}
|
|
|
190 |
?>
|