Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 640 | Rev 768 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
578 aurelien 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();
613 aurelien 20
 
578 aurelien 21
	private $bdd;
613 aurelien 22
	private $retour_format = 'complet';
23
	private $retours_formats_autorises = array('complet','zone_geo');
24
 
578 aurelien 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
 
613 aurelien 36
		$this->affecterParametresParDefaut();
578 aurelien 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
 
613 aurelien 46
	private function affecterParametresParDefaut() {
47
		$this->retour_format = isset($this->parametres['retour.format']) ? $this->parametres['retour.format'] : $this->retour_format;
48
	}
49
 
578 aurelien 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
 
613 aurelien 61
		if(!in_array($this->retour_format, $this->retours_formats_autorises)) {
62
			$erreurs[] = 'la valeur '.$this->retour_format.' est inconnue';
63
		}
64
 
578 aurelien 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);
678 delphine 76
		$requete = "SELECT * FROM ".Config::get('bdd_table_lois').' '.
578 aurelien 77
		           "WHERE id IN (".implode(',',$id_lois).") ";
78
		return $this->bdd->recupererTous($requete);
79
	}
80
 
613 aurelien 81
	private function obtenirLoisZoneGeo(Array $id_lois) {
82
		$id_lois = array_map(array($this->bdd, 'proteger'), $id_lois);
678 delphine 83
		$requete = "SELECT DISTINCT zone_application, code_zone_application FROM ".Config::get('bdd_table_lois').' '.
613 aurelien 84
			           "WHERE id IN (".implode(',',$id_lois).") ";
85
		return $this->bdd->recupererTous($requete);
86
	}
87
 
640 aurelien 88
	private function obtenirStatuts() {
89
		$nn_demande = $this->parametres['masque.nn'];
90
 
91
		$conditions_taxons = array();
92
		$conditions_taxons = $this->obtenirNumNomTaxonsSuperieurs($nn_demande);
93
		$conditions_taxons[] = $this->bdd->proteger($nn_demande);
94
 
678 delphine 95
		$requete = "SELECT * FROM ".Config::get('bdd_table_especes').' '.
640 aurelien 96
		           "WHERE num_nom_retenu IN (".implode(', ', $conditions_taxons).") OR ".
97
				   "num_nom IN (".implode(', ', $conditions_taxons).") ";
98
 
578 aurelien 99
		$statuts = $this->bdd->recuperer($requete);
613 aurelien 100
		$statuts = $this->formaterRetour($statuts);
578 aurelien 101
		return $statuts;
102
	}
103
 
640 aurelien 104
	private function obtenirNumNomTaxonsSuperieurs($nn_demande) {
105
		$nn_taxons_sup = array();
106
		// TODO: ceci ramène trop de champs alors que l'on a besoin que du numéro nomenclatural
107
		// et il y a peut-être un meilleur moyen que ramener la hierarchie des taxons supérieurs
108
		// mais pour le moment ça marche et c'est assez rapide
678 delphine 109
		$url = $this->ajouterHrefAutreProjet('taxons', $nn_demande, '/relations/superieurs',Config::get('referentiel'));
640 aurelien 110
		$classification = $this->consulterHref($url);
111
		$classification = is_object($classification) ? get_object_vars($classification) : array();
112
 
113
		if(isset($classification[$nn_demande])) {
114
			$classification_nn_demande = get_object_vars($classification[$nn_demande]);
115
			$tab_nn_demandes = array_keys($classification_nn_demande);
116
			$nn_taxons_sup = array_map(array($this->bdd,'proteger'), $tab_nn_demandes);
117
		}
118
		return $nn_taxons_sup;
119
	}
120
 
578 aurelien 121
//+---------------------------FONCTIONS DE FORMATAGE---------------------------------------------------------+
613 aurelien 122
 
123
	private function formaterRetour($statuts_taxon) {
124
		switch($this->retour_format) {
125
			case 'zone_geo':
126
				$retour = $this->formaterStatutsTaxonZoneGeo($statuts_taxon);
127
			break;
128
 
129
			case 'complet':
130
				$retour = $this->formaterStatutsTaxon($statuts_taxon);
131
			break;
132
 
133
			default:
134
				$retour = $this->formaterStatutsTaxon();
135
			break;
136
		}
137
		return $retour;
138
	}
139
 
140
	private function formaterStatutsTaxonZoneGeo($statuts_taxon) {
141
		$lois_statuts = array();
142
		foreach ($statuts_taxon as $champ => $statut) {
143
			if($statut == "1") {
144
				$lois_statuts[] = $champ;
145
			}
146
		}
147
 
148
		$zones_geo_lois = (!empty($lois_statuts)) ? $this->obtenirLoisZoneGeo($lois_statuts) : array();
149
 
150
		return $zones_geo_lois;
151
	}
152
 
578 aurelien 153
	private function formaterStatutsTaxon($statuts_taxon) {
154
		$statuts_formates = array();
155
		$lois_statuts = array();
640 aurelien 156
 
157
		if(is_array($statuts_taxon)) {
158
			unset($statuts_taxon['num_nom']);
159
			unset($statuts_taxon['num_nom_retenu']);
160
			unset($statuts_taxon['nom_sci']);
161
			foreach ($statuts_taxon as $champ => $statut) {
162
				if($statut == "1") {
163
					$lois_statuts[] = $champ;
164
				}
613 aurelien 165
			}
578 aurelien 166
		}
613 aurelien 167
 
578 aurelien 168
		$statuts_formates = (!empty($lois_statuts)) ? $this->obtenirLois($lois_statuts) : array();
613 aurelien 169
 
578 aurelien 170
		return $statuts_formates;
171
	}
172
}
173
?>