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