Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 578 | Rev 640 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 578 Rev 613
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
	private $bdd;
21
	private $bdd;
-
 
22
	private $retour_format = 'complet';
-
 
23
	private $retours_formats_autorises = array('complet','zone_geo');
-
 
24
	protected $table_version = null;
-
 
25
	
21
 
26
 
22
	public function __construct($config = null) {
27
	public function __construct($config = null) {
23
		$this->config = $config;
28
		$this->config = $config;
24
		$this->bdd = $this->getBdd();
29
		$this->bdd = $this->getBdd();
-
 
30
		$this->table_version = Config::get('table_version');
25
	}
31
	}
26
	
32
	
27
	public function consulter($ressources, $parametres) {
33
	public function consulter($ressources, $parametres) {
28
 
34
 
29
		$this->parametres = $parametres;
35
		$this->parametres = $parametres;
30
		$this->ressources = $ressources;
36
		$this->ressources = $ressources;
-
 
37
 
31
 
38
		$this->affecterParametresParDefaut();
32
		$this->verifierParametres();
39
		$this->verifierParametres();
33
 
40
 
34
		$resultat = new ResultatService();
41
		$resultat = new ResultatService();
35
		$resultat->corps = $this->obtenirStatuts();
42
		$resultat->corps = $this->obtenirStatuts();
36
 
43
 
37
		return $resultat;
44
		return $resultat;
38
	}
45
	}
39
//+---------------------------FONCTION D'ANALYSE DES PARAMETRES---------------------------------------------------------+
46
//+---------------------------FONCTION D'ANALYSE DES PARAMETRES---------------------------------------------------------+
-
 
47
 
-
 
48
	private function affecterParametresParDefaut() {
-
 
49
		$this->retour_format = isset($this->parametres['retour.format']) ? $this->parametres['retour.format'] : $this->retour_format;
-
 
50
	}
40
 
51
	
41
	private function verifierParametres() {
52
	private function verifierParametres() {
42
		$erreurs = array();
53
		$erreurs = array();
43
		
54
		
44
		if (empty($this->parametres['masque.nn'])) {
55
		if (empty($this->parametres['masque.nn'])) {
45
			$erreurs[] = 'renseignez une valeur pour masque.nn';
56
			$erreurs[] = 'renseignez une valeur pour masque.nn';
46
		}
57
		}
47
		
58
		
48
		if(!is_numeric($this->parametres['masque.nn'])) {
59
		if(!is_numeric($this->parametres['masque.nn'])) {
49
			$erreurs[] = 'la valeur pour masque.nn doit être un entier';
60
			$erreurs[] = 'la valeur pour masque.nn doit être un entier';
50
		}
61
		}
-
 
62
		
-
 
63
		if(!in_array($this->retour_format, $this->retours_formats_autorises)) {
-
 
64
			$erreurs[] = 'la valeur '.$this->retour_format.' est inconnue';
-
 
65
		}
51
		
66
		
52
		if (count($erreurs) > 0) {
67
		if (count($erreurs) > 0) {
53
			$message = implode('<br />', $erreurs);
68
			$message = implode('<br />', $erreurs);
54
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
69
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
55
			throw new Exception($message, $code);
70
			throw new Exception($message, $code);
56
		}
71
		}
57
	}
72
	}
58
	
73
	
59
//+---------------------------FONCTIONS DE REQUETE---------------------------------------------------------+
74
//+---------------------------FONCTIONS DE REQUETE---------------------------------------------------------+
60
 
75
 
61
	private function obtenirLois(Array $id_lois) {
76
	private function obtenirLois(Array $id_lois) {
62
		$id_lois = array_map(array($this->bdd, 'proteger'), $id_lois);
77
		$id_lois = array_map(array($this->bdd, 'proteger'), $id_lois);
63
		$requete = "SELECT * FROM sptb_lois_v2012 ".
78
		$requete = "SELECT * FROM sptb_lois_v".$this->table_version.' '.
-
 
79
		           "WHERE id IN (".implode(',',$id_lois).") ";   
-
 
80
		return $this->bdd->recupererTous($requete);
-
 
81
	}
-
 
82
	
-
 
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.' '.
64
		           "WHERE id IN (".implode(',',$id_lois).") ";   
86
			           "WHERE id IN (".implode(',',$id_lois).") ";   
65
		return $this->bdd->recupererTous($requete);
87
		return $this->bdd->recupererTous($requete);
66
	}
88
	}
67
	
89
	
68
	private function obtenirStatuts() {		
90
	private function obtenirStatuts() {		
69
		$requete = "SELECT * FROM sptb_especes_v2012 ".
91
		$requete = "SELECT * FROM sptb_especes_v".$this->table_version.' '.
70
		           "WHERE ".
92
		           "WHERE ".
71
		           "num_nom = ".$this->bdd->proteger($this->parametres['masque.nn'])."";
93
		           "num_nom = ".$this->bdd->proteger($this->parametres['masque.nn'])."";
72
		
94
		
73
		$statuts = $this->bdd->recuperer($requete);
95
		$statuts = $this->bdd->recuperer($requete);
74
		$statuts = $this->formaterStatutsTaxon($statuts);
96
		$statuts = $this->formaterRetour($statuts);
75
		return $statuts;           
97
		return $statuts;           
76
	}
98
	}
77
	
99
	
78
//+---------------------------FONCTIONS DE FORMATAGE---------------------------------------------------------+
100
//+---------------------------FONCTIONS DE FORMATAGE---------------------------------------------------------+
-
 
101
 
-
 
102
	private function formaterRetour($statuts_taxon) {
-
 
103
		switch($this->retour_format) {
-
 
104
			case 'zone_geo':
-
 
105
				$retour = $this->formaterStatutsTaxonZoneGeo($statuts_taxon);
-
 
106
			break;
-
 
107
					
-
 
108
			case 'complet':
-
 
109
				$retour = $this->formaterStatutsTaxon($statuts_taxon);
-
 
110
			break;
-
 
111
					
-
 
112
			default:
-
 
113
				$retour = $this->formaterStatutsTaxon();
-
 
114
			break;
-
 
115
		}
-
 
116
		return $retour;
-
 
117
	}
-
 
118
	
-
 
119
	private function formaterStatutsTaxonZoneGeo($statuts_taxon) {
-
 
120
		$lois_statuts = array();
-
 
121
		foreach ($statuts_taxon as $champ => $statut) {
-
 
122
			if($statut == "1") {
-
 
123
				$lois_statuts[] = $champ;
-
 
124
			}			
-
 
125
		}
-
 
126
		
-
 
127
		$zones_geo_lois = (!empty($lois_statuts)) ? $this->obtenirLoisZoneGeo($lois_statuts) : array();
-
 
128
		
-
 
129
		return $zones_geo_lois;
-
 
130
	}
-
 
131
	
79
	private function formaterStatutsTaxon($statuts_taxon) {
132
	private function formaterStatutsTaxon($statuts_taxon) {
80
		$statuts_formates = array();
133
		$statuts_formates = array();
81
		unset($statuts_taxon['num_nom']);
134
		unset($statuts_taxon['num_nom']);
82
		unset($statuts_taxon['nom_sci']);
135
		unset($statuts_taxon['nom_sci']);
83
		$lois_statuts = array();
136
		$lois_statuts = array();
84
		foreach ($statuts_taxon as $champ => $statut) {
137
		foreach ($statuts_taxon as $champ => $statut) {
85
			if($statut == "1") {
138
			if($statut == "1") {
86
				$lois_statuts[] = $champ;
139
				$lois_statuts[] = $champ;
87
			}			
140
			}
88
		}
141
		}
89
		
142
	
90
		$statuts_formates = (!empty($lois_statuts)) ? $this->obtenirLois($lois_statuts) : array();
143
		$statuts_formates = (!empty($lois_statuts)) ? $this->obtenirLois($lois_statuts) : array();
91
		
144
	
92
		return $statuts_formates;
145
		return $statuts_formates;
93
	}
146
	}
94
}
147
}
95
?>
148
?>