Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 768 | Rev 941 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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