685 |
delphine |
1 |
<?php
|
|
|
2 |
// declare(encoding='UTF-8');
|
|
|
3 |
/**
|
|
|
4 |
* Classe implémentant l'API d'eFlore concernant les statuts de protection
|
|
|
5 |
*
|
|
|
6 |
* @see http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=EfloreApi01Status
|
|
|
7 |
*
|
|
|
8 |
* @package eFlore/services
|
|
|
9 |
* @author Aurélien PERONNET <aurelien@tela-botanica.org>
|
|
|
10 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
11 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
12 |
* @version 1.0
|
|
|
13 |
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
|
|
|
14 |
*/
|
|
|
15 |
// TODO : Config et Outils sont des classes statiques qui doivent poser des pb pour les tests...
|
|
|
16 |
class Statuts extends Commun {
|
|
|
17 |
|
|
|
18 |
protected $parametres = array();
|
|
|
19 |
protected $ressources = array();
|
|
|
20 |
|
|
|
21 |
private $bdd;
|
|
|
22 |
private $retour_format = 'complet';
|
|
|
23 |
private $retours_formats_autorises = array('complet','zone_geo');
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
public function __construct($config = null) {
|
|
|
27 |
$this->config = $config;
|
|
|
28 |
$this->bdd = $this->getBdd();
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
public function consulter($ressources, $parametres) {
|
|
|
32 |
|
|
|
33 |
$this->parametres = $parametres;
|
|
|
34 |
$this->ressources = $ressources;
|
|
|
35 |
|
|
|
36 |
$this->affecterParametresParDefaut();
|
|
|
37 |
$this->verifierParametres();
|
|
|
38 |
|
|
|
39 |
$resultat = new ResultatService();
|
|
|
40 |
$resultat->corps = $this->obtenirStatuts();
|
|
|
41 |
|
|
|
42 |
return $resultat;
|
|
|
43 |
}
|
|
|
44 |
//+---------------------------FONCTION D'ANALYSE DES PARAMETRES---------------------------------------------------------+
|
|
|
45 |
|
|
|
46 |
private function affecterParametresParDefaut() {
|
|
|
47 |
$this->retour_format = isset($this->parametres['retour.format']) ? $this->parametres['retour.format'] : $this->retour_format;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
private function verifierParametres() {
|
|
|
51 |
$erreurs = array();
|
|
|
52 |
|
|
|
53 |
if (empty($this->parametres['masque.nn'])) {
|
|
|
54 |
$erreurs[] = 'renseignez une valeur pour masque.nn';
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
if(!is_numeric($this->parametres['masque.nn'])) {
|
|
|
58 |
$erreurs[] = 'la valeur pour masque.nn doit être un entier';
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
if(!in_array($this->retour_format, $this->retours_formats_autorises)) {
|
|
|
62 |
$erreurs[] = 'la valeur '.$this->retour_format.' est inconnue';
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
if (count($erreurs) > 0) {
|
|
|
66 |
$message = implode('<br />', $erreurs);
|
|
|
67 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
68 |
throw new Exception($message, $code);
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
//+---------------------------FONCTIONS DE REQUETE---------------------------------------------------------+
|
|
|
73 |
|
|
|
74 |
private function obtenirLois(Array $id_lois) {
|
|
|
75 |
$id_lois = array_map(array($this->bdd, 'proteger'), $id_lois);
|
|
|
76 |
$requete = "SELECT * FROM ".Config::get('bdd_table_lois').' '.
|
|
|
77 |
"WHERE id IN (".implode(',',$id_lois).") ";
|
|
|
78 |
return $this->bdd->recupererTous($requete);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
private function obtenirLoisZoneGeo(Array $id_lois) {
|
|
|
82 |
$id_lois = array_map(array($this->bdd, 'proteger'), $id_lois);
|
|
|
83 |
$requete = "SELECT DISTINCT zone_application, code_zone_application FROM ".Config::get('bdd_table_lois').' '.
|
|
|
84 |
"WHERE id IN (".implode(',',$id_lois).") ";
|
|
|
85 |
return $this->bdd->recupererTous($requete);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
private function obtenirStatuts() {
|
|
|
89 |
$nn_demande = $this->parametres['masque.nn'];
|
|
|
90 |
|
|
|
91 |
$conditions_taxons = array();
|
941 |
aurelien |
92 |
$conditions_taxons = $this->obtenirNumNomTaxonsSuperieurs(Config::get('referentiel'), $nn_demande);
|
685 |
delphine |
93 |
$conditions_taxons[] = $this->bdd->proteger($nn_demande);
|
|
|
94 |
|
|
|
95 |
$requete = "SELECT * FROM ".Config::get('bdd_table_especes').' '.
|
|
|
96 |
"WHERE num_nom_retenu IN (".implode(', ', $conditions_taxons).") OR ".
|
|
|
97 |
"num_nom IN (".implode(', ', $conditions_taxons).") ";
|
|
|
98 |
|
|
|
99 |
$statuts = $this->bdd->recuperer($requete);
|
|
|
100 |
$statuts = $this->formaterRetour($statuts);
|
|
|
101 |
return $statuts;
|
940 |
aurelien |
102 |
}
|
685 |
delphine |
103 |
//+---------------------------FONCTIONS DE FORMATAGE---------------------------------------------------------+
|
|
|
104 |
|
|
|
105 |
private function formaterRetour($statuts_taxon) {
|
|
|
106 |
switch($this->retour_format) {
|
|
|
107 |
case 'zone_geo':
|
|
|
108 |
$retour = $this->formaterStatutsTaxonZoneGeo($statuts_taxon);
|
|
|
109 |
break;
|
|
|
110 |
|
|
|
111 |
case 'complet':
|
|
|
112 |
$retour = $this->formaterStatutsTaxon($statuts_taxon);
|
|
|
113 |
break;
|
|
|
114 |
|
|
|
115 |
default:
|
|
|
116 |
$retour = $this->formaterStatutsTaxon();
|
|
|
117 |
break;
|
|
|
118 |
}
|
|
|
119 |
return $retour;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
private function formaterStatutsTaxonZoneGeo($statuts_taxon) {
|
|
|
123 |
$lois_statuts = array();
|
|
|
124 |
foreach ($statuts_taxon as $champ => $statut) {
|
|
|
125 |
if($statut == "1") {
|
|
|
126 |
$lois_statuts[] = $champ;
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
$zones_geo_lois = (!empty($lois_statuts)) ? $this->obtenirLoisZoneGeo($lois_statuts) : array();
|
|
|
131 |
|
|
|
132 |
return $zones_geo_lois;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
private function formaterStatutsTaxon($statuts_taxon) {
|
|
|
136 |
$statuts_formates = array();
|
|
|
137 |
$lois_statuts = array();
|
|
|
138 |
|
|
|
139 |
if(is_array($statuts_taxon)) {
|
|
|
140 |
unset($statuts_taxon['num_nom']);
|
|
|
141 |
unset($statuts_taxon['num_nom_retenu']);
|
|
|
142 |
unset($statuts_taxon['nom_sci']);
|
|
|
143 |
foreach ($statuts_taxon as $champ => $statut) {
|
|
|
144 |
if($statut == "1") {
|
|
|
145 |
$lois_statuts[] = $champ;
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
$statuts_formates = (!empty($lois_statuts)) ? $this->obtenirLois($lois_statuts) : array();
|
|
|
151 |
|
|
|
152 |
return $statuts_formates;
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
?>
|