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 |
|
1185 |
delphine |
98 |
private function obtenirLois(Array $id_lois) {
|
578 |
aurelien |
99 |
$id_lois = array_map(array($this->bdd, 'proteger'), $id_lois);
|
678 |
delphine |
100 |
$requete = "SELECT * FROM ".Config::get('bdd_table_lois').' '.
|
1190 |
delphine |
101 |
"WHERE cd_protection IN (".implode(',',$id_lois).") ORDER BY zone_application ASC";
|
1185 |
delphine |
102 |
$lois = $this->bdd->recupererTous($requete, 'ASSOC');
|
|
|
103 |
foreach ($lois as $loi) {
|
1190 |
delphine |
104 |
$retour[$loi['cd_protection']] = $loi;
|
1185 |
delphine |
105 |
}
|
|
|
106 |
return $retour;
|
578 |
aurelien |
107 |
}
|
|
|
108 |
|
613 |
aurelien |
109 |
private function obtenirLoisZoneGeo(Array $id_lois) {
|
|
|
110 |
$id_lois = array_map(array($this->bdd, 'proteger'), $id_lois);
|
1214 |
delphine |
111 |
$requete = "SELECT DISTINCT zone_application, code_zone_application, type_protection FROM ".Config::get('bdd_table_lois').' '.
|
1190 |
delphine |
112 |
"WHERE cd_protection IN (".implode(',',$id_lois).") ORDER BY zone_application ASC";
|
613 |
aurelien |
113 |
return $this->bdd->recupererTous($requete);
|
|
|
114 |
}
|
|
|
115 |
|
1111 |
mathias |
116 |
private function obtenirStatuts() {
|
|
|
117 |
if ($this->nn_demande != null) {
|
|
|
118 |
$conditions_taxons = array();
|
|
|
119 |
$conditions_taxons = $this->obtenirNumNomTaxonsSuperieurs(Config::get('referentiel'), $this->nn_demande);
|
1185 |
delphine |
120 |
$conditions_taxons[] = $this->nn_demande;
|
1111 |
mathias |
121 |
}
|
640 |
aurelien |
122 |
|
1111 |
mathias |
123 |
$requete = "SELECT * FROM ".Config::get('bdd_table_especes');
|
|
|
124 |
if ($this->nn_demande != null) {
|
|
|
125 |
$requete .= " WHERE num_nom_retenu IN (".implode(', ', $conditions_taxons).") OR "
|
|
|
126 |
. "num_nom IN (".implode(', ', $conditions_taxons).") ";
|
|
|
127 |
}
|
|
|
128 |
// pagination
|
|
|
129 |
$requete .= " LIMIT " . $this->navigation_depart . ", " . $this->navigation_limite;
|
|
|
130 |
$requete .= ' -- ' . __FILE__ . ':' . __LINE__;
|
|
|
131 |
//echo "REQ: $requete\n";
|
640 |
aurelien |
132 |
|
1185 |
delphine |
133 |
$statuts = $this->bdd->recupererTous($requete, 'ASSOC');
|
1111 |
mathias |
134 |
//echo "STATUTS: " . print_r($statuts, true) . "\n";
|
|
|
135 |
//echo "CPT: " . count($statuts) . "\n";
|
613 |
aurelien |
136 |
$statuts = $this->formaterRetour($statuts);
|
578 |
aurelien |
137 |
return $statuts;
|
940 |
aurelien |
138 |
}
|
578 |
aurelien |
139 |
//+---------------------------FONCTIONS DE FORMATAGE---------------------------------------------------------+
|
613 |
aurelien |
140 |
|
|
|
141 |
private function formaterRetour($statuts_taxon) {
|
1185 |
delphine |
142 |
$retour = "";
|
|
|
143 |
if ($statuts_taxon) {
|
|
|
144 |
foreach ($statuts_taxon as $nom) {
|
|
|
145 |
$f[$nom['cd_protection']][$nom['num_nom']] = $nom['nom_sci'];
|
|
|
146 |
$lois_statuts[] = $nom['cd_protection'];
|
|
|
147 |
}
|
|
|
148 |
switch($this->retour_format) {
|
|
|
149 |
case 'zone_geo':
|
|
|
150 |
$retour = $this->obtenirLoisZoneGeo($lois_statuts);
|
|
|
151 |
break;
|
|
|
152 |
|
|
|
153 |
case 'nom':
|
|
|
154 |
$lois = $this->obtenirLois($lois_statuts);
|
|
|
155 |
foreach ($statuts_taxon as $nom) {
|
|
|
156 |
$retour[$nom['num_nom']]['num_nom'] = $nom['num_nom'];
|
|
|
157 |
$retour[$nom['num_nom']]['nom_sci'] = $nom['nom_sci'];
|
|
|
158 |
$retour[$nom['num_nom']]['lois'][$nom['cd_protection']] = $lois[$nom['cd_protection']];
|
613 |
aurelien |
159 |
|
1185 |
delphine |
160 |
}
|
|
|
161 |
break;
|
|
|
162 |
case 'complet':
|
|
|
163 |
$lois = $this->obtenirLois($lois_statuts);
|
|
|
164 |
foreach ($lois as $id => $loi) {
|
|
|
165 |
$retour[$loi['zone_application']][$id] = $loi;
|
1188 |
delphine |
166 |
if (isset($f[$id])) {
|
|
|
167 |
foreach ($f[$id] as $num_nom => $nom_sci) {
|
|
|
168 |
$retour[$loi['zone_application']][$id]['nom_sci'][$num_nom] = $nom_sci;
|
|
|
169 |
}
|
1185 |
delphine |
170 |
}
|
|
|
171 |
}
|
|
|
172 |
break;
|
|
|
173 |
|
|
|
174 |
default:
|
|
|
175 |
$retour = $this->formaterStatutsTaxon();
|
|
|
176 |
break;
|
|
|
177 |
}
|
613 |
aurelien |
178 |
|
|
|
179 |
}
|
1185 |
delphine |
180 |
|
613 |
aurelien |
181 |
return $retour;
|
|
|
182 |
}
|
|
|
183 |
|
768 |
raphael |
184 |
|
578 |
aurelien |
185 |
}
|
|
|
186 |
?>
|