Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 613 | Rev 678 | 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
	protected $table_version = null;
25
 
578 aurelien 26
 
27
	public function __construct($config = null) {
28
		$this->config = $config;
29
		$this->bdd = $this->getBdd();
613 aurelien 30
		$this->table_version = Config::get('table_version');
578 aurelien 31
	}
32
 
33
	public function consulter($ressources, $parametres) {
34
 
35
		$this->parametres = $parametres;
36
		$this->ressources = $ressources;
37
 
613 aurelien 38
		$this->affecterParametresParDefaut();
578 aurelien 39
		$this->verifierParametres();
40
 
41
		$resultat = new ResultatService();
42
		$resultat->corps = $this->obtenirStatuts();
43
 
44
		return $resultat;
45
	}
46
//+---------------------------FONCTION D'ANALYSE DES PARAMETRES---------------------------------------------------------+
47
 
613 aurelien 48
	private function affecterParametresParDefaut() {
49
		$this->retour_format = isset($this->parametres['retour.format']) ? $this->parametres['retour.format'] : $this->retour_format;
50
	}
51
 
578 aurelien 52
	private function verifierParametres() {
53
		$erreurs = array();
54
 
55
		if (empty($this->parametres['masque.nn'])) {
56
			$erreurs[] = 'renseignez une valeur pour masque.nn';
57
		}
58
 
59
		if(!is_numeric($this->parametres['masque.nn'])) {
60
			$erreurs[] = 'la valeur pour masque.nn doit être un entier';
61
		}
62
 
613 aurelien 63
		if(!in_array($this->retour_format, $this->retours_formats_autorises)) {
64
			$erreurs[] = 'la valeur '.$this->retour_format.' est inconnue';
65
		}
66
 
578 aurelien 67
		if (count($erreurs) > 0) {
68
			$message = implode('<br />', $erreurs);
69
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
70
			throw new Exception($message, $code);
71
		}
72
	}
73
 
74
//+---------------------------FONCTIONS DE REQUETE---------------------------------------------------------+
75
 
76
	private function obtenirLois(Array $id_lois) {
77
		$id_lois = array_map(array($this->bdd, 'proteger'), $id_lois);
613 aurelien 78
		$requete = "SELECT * FROM sptb_lois_v".$this->table_version.' '.
578 aurelien 79
		           "WHERE id IN (".implode(',',$id_lois).") ";
80
		return $this->bdd->recupererTous($requete);
81
	}
82
 
613 aurelien 83
	private function obtenirLoisZoneGeo(Array $id_lois) {
84
		$id_lois = array_map(array($this->bdd, 'proteger'), $id_lois);
85
		$requete = "SELECT DISTINCT zone_application, code_zone_application FROM sptb_lois_v".$this->table_version.' '.
86
			           "WHERE id IN (".implode(',',$id_lois).") ";
87
		return $this->bdd->recupererTous($requete);
88
	}
89
 
640 aurelien 90
	private function obtenirStatuts() {
91
		$nn_demande = $this->parametres['masque.nn'];
92
 
93
		$conditions_taxons = array();
94
		$conditions_taxons = $this->obtenirNumNomTaxonsSuperieurs($nn_demande);
95
		$conditions_taxons[] = $this->bdd->proteger($nn_demande);
96
 
613 aurelien 97
		$requete = "SELECT * FROM sptb_especes_v".$this->table_version.' '.
640 aurelien 98
		           "WHERE num_nom_retenu IN (".implode(', ', $conditions_taxons).") OR ".
99
				   "num_nom IN (".implode(', ', $conditions_taxons).") ";
100
 
578 aurelien 101
		$statuts = $this->bdd->recuperer($requete);
613 aurelien 102
		$statuts = $this->formaterRetour($statuts);
578 aurelien 103
		return $statuts;
104
	}
105
 
640 aurelien 106
	private function obtenirNumNomTaxonsSuperieurs($nn_demande) {
107
		$nn_taxons_sup = array();
108
		// TODO: ceci ramène trop de champs alors que l'on a besoin que du numéro nomenclatural
109
		// et il y a peut-être un meilleur moyen que ramener la hierarchie des taxons supérieurs
110
		// mais pour le moment ça marche et c'est assez rapide
111
		$url = $this->ajouterHrefAutreProjet('taxons', $nn_demande, '/relations/superieurs','bdtfx');
112
		$classification = $this->consulterHref($url);
113
		$classification = is_object($classification) ? get_object_vars($classification) : array();
114
 
115
		if(isset($classification[$nn_demande])) {
116
			$classification_nn_demande = get_object_vars($classification[$nn_demande]);
117
			$tab_nn_demandes = array_keys($classification_nn_demande);
118
			$nn_taxons_sup = array_map(array($this->bdd,'proteger'), $tab_nn_demandes);
119
		}
120
		return $nn_taxons_sup;
121
	}
122
 
578 aurelien 123
//+---------------------------FONCTIONS DE FORMATAGE---------------------------------------------------------+
613 aurelien 124
 
125
	private function formaterRetour($statuts_taxon) {
126
		switch($this->retour_format) {
127
			case 'zone_geo':
128
				$retour = $this->formaterStatutsTaxonZoneGeo($statuts_taxon);
129
			break;
130
 
131
			case 'complet':
132
				$retour = $this->formaterStatutsTaxon($statuts_taxon);
133
			break;
134
 
135
			default:
136
				$retour = $this->formaterStatutsTaxon();
137
			break;
138
		}
139
		return $retour;
140
	}
141
 
142
	private function formaterStatutsTaxonZoneGeo($statuts_taxon) {
143
		$lois_statuts = array();
144
		foreach ($statuts_taxon as $champ => $statut) {
145
			if($statut == "1") {
146
				$lois_statuts[] = $champ;
147
			}
148
		}
149
 
150
		$zones_geo_lois = (!empty($lois_statuts)) ? $this->obtenirLoisZoneGeo($lois_statuts) : array();
151
 
152
		return $zones_geo_lois;
153
	}
154
 
578 aurelien 155
	private function formaterStatutsTaxon($statuts_taxon) {
156
		$statuts_formates = array();
157
		$lois_statuts = array();
640 aurelien 158
 
159
		if(is_array($statuts_taxon)) {
160
			unset($statuts_taxon['num_nom']);
161
			unset($statuts_taxon['num_nom_retenu']);
162
			unset($statuts_taxon['nom_sci']);
163
			foreach ($statuts_taxon as $champ => $statut) {
164
				if($statut == "1") {
165
					$lois_statuts[] = $champ;
166
				}
613 aurelien 167
			}
578 aurelien 168
		}
613 aurelien 169
 
578 aurelien 170
		$statuts_formates = (!empty($lois_statuts)) ? $this->obtenirLois($lois_statuts) : array();
613 aurelien 171
 
578 aurelien 172
		return $statuts_formates;
173
	}
174
}
175
?>