Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 1214 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1214 Rev 1277
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
2
// declare(encoding='UTF-8');
3
// declare(tension='220v');
3
// declare(tension='220v');
4
// declare(couleur_du_ciel='bleu');
4
// declare(couleur_du_ciel='bleu');
5
/**
5
/**
6
* Classe implémentant l'API d'eFlore concernant les statuts de protection
6
* Classe implémentant l'API d'eFlore concernant les statuts de protection
7
*
7
*
8
* @see http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=EfloreApi01Status
8
* @see http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=EfloreApi01Status
9
*
9
*
10
* @package eFlore/services
10
* @package eFlore/services
11
* @author Aurélien PERONNET <aurelien@tela-botanica.org>
11
* @author Aurélien PERONNET <aurelien@tela-botanica.org>
12
 * * @author Mathias CHOUET <mathias@tela-botanica.org>
12
 * * @author Mathias CHOUET <mathias@tela-botanica.org>
13
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
13
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
14
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
14
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
15
* @version 1.0
15
* @version 1.0
16
* @copyright 1999-2014 Tela Botanica (accueil@tela-botanica.org)
16
* @copyright 1999-2014 Tela Botanica (accueil@tela-botanica.org)
17
*/
17
*/
18
// TODO : Config et Outils sont des classes statiques qui doivent poser des pb pour les tests...
18
// TODO : Config et Outils sont des classes statiques qui doivent poser des pb pour les tests...
19
class Statuts extends Commun {
19
class Statuts extends Commun {
20
 
20
 
21
	protected $parametres = array();
21
	protected $parametres = array();
22
	protected $ressources = array();
22
	protected $ressources = array();
23
 
23
 
24
	private $bdd;
24
	private $bdd;
25
	private $retour_format = 'complet';
25
	private $retour_format = 'complet';
26
	private $retours_formats_autorises = array('complet','zone_geo');
26
	private $retours_formats_autorises = array('complet','zone_geo');
27
	protected $nn_demande;
27
	protected $nn_demande;
28
	protected $navigation_depart;
28
	protected $navigation_depart;
29
	protected $navigation_limite;
29
	protected $navigation_limite;
30
 
30
 
31
	public function __construct($config = null) {
31
	public function __construct($config = null) {
32
		$this->config = $config;
32
		$this->config = $config;
33
		$this->bdd = $this->getBdd();
33
		$this->bdd = $this->getBdd();
34
	}
34
	}
35
 
35
 
36
	public function consulter($ressources, $parametres) {
36
	public function consulter($ressources, $parametres) {
37
 
37
 
38
		$this->parametres = $parametres;
38
		$this->parametres = $parametres;
39
		$this->ressources = $ressources;
39
		$this->ressources = $ressources;
40
 
40
 
41
		$this->affecterParametres();
41
		$this->affecterParametres();
42
 
42
 
43
		$resultat = new ResultatService();
43
		$resultat = new ResultatService();
44
		$resultat->corps = $this->obtenirStatuts();
44
		$resultat->corps = $this->obtenirStatuts();
45
 
45
 
46
		return $resultat;
46
		return $resultat;
47
	}
47
	}
48
 
48
 
49
	/**
49
	/**
50
	 * Affecte et vérifie les paramètres
50
	 * Affecte et vérifie les paramètres
51
	 * @throws Exception
51
	 * @throws Exception
52
	 */
52
	 */
53
	private function affecterParametres() {
53
	private function affecterParametres() {
54
		$erreurs = array();
54
		$erreurs = array();
55
 
55
 
56
		if (isset($this->parametres['retour.format'])) {	
56
		if (isset($this->parametres['retour.format'])) {	
57
			if(!in_array($this->retour_format, $this->retours_formats_autorises)) {
57
			if(!in_array($this->retour_format, $this->retours_formats_autorises)) {
58
				$erreurs[] = 'la valeur '.$this->retour_format.' est inconnue';
58
				$erreurs[] = 'la valeur '.$this->retour_format.' est inconnue';
59
			}
59
			}
60
			$this->retour_format = $this->parametres['retour.format'];
60
			$this->retour_format = $this->parametres['retour.format'];
61
		}
61
		}
62
		if (isset($this->parametres['masque.nn']) && ($this->parametres['masque.nn'] != '')) {
62
		if (isset($this->parametres['masque.nn']) && ($this->parametres['masque.nn'] != '')) {
63
			if(!is_numeric($this->parametres['masque.nn'])) {
63
			if(!is_numeric($this->parametres['masque.nn'])) {
64
				$erreurs[] = 'la valeur pour masque.nn doit être un entier';
64
				$erreurs[] = 'la valeur pour masque.nn doit être un entier';
65
			}
65
			}
66
			$this->nn_demande = $this->parametres['masque.nn'];
66
			$this->nn_demande = $this->parametres['masque.nn'];
67
		}
67
		}
68
		if (isset($this->parametres['masque.zone']) && ($this->parametres['masque.zone'] != '')) {
68
		if (isset($this->parametres['masque.zone']) && ($this->parametres['masque.zone'] != '')) {
69
			if(!is_numeric($this->parametres['masque.zone']) || !($this->parametres['masque.zone'] > 0 && $this->parametres['masque.zone'] <= 64)) {
69
			if(!is_numeric($this->parametres['masque.zone']) || !($this->parametres['masque.zone'] > 0 && $this->parametres['masque.zone'] <= 64)) {
70
				$erreurs[] = 'la valeur pour masque.zone doit être un entier compris entre 1 et 64';
70
				$erreurs[] = 'la valeur pour masque.zone doit être un entier compris entre 1 et 64';
71
			}
71
			}
72
			$this->zone = $this->parametres['masque.zone'];
72
			$this->zone = $this->parametres['masque.zone'];
73
		}
73
		}
74
		if (isset($this->parametres['navigation.depart']) && ($this->parametres['navigation.depart'] != '')) {
74
		if (isset($this->parametres['navigation.depart']) && ($this->parametres['navigation.depart'] != '')) {
75
			if (!is_numeric($this->parametres['navigation.depart'])) {
75
			if (!is_numeric($this->parametres['navigation.depart'])) {
76
				$erreurs[] = 'la valeur pour navigation.depart doit être un entier';
76
				$erreurs[] = 'la valeur pour navigation.depart doit être un entier';
77
			}
77
			}
78
			$this->navigation_depart = max(0, intval($this->parametres['navigation.depart']));
78
			$this->navigation_depart = max(0, intval($this->parametres['navigation.depart']));
79
		} else {
79
		} else {
80
			$this->navigation_depart = 0;
80
			$this->navigation_depart = 0;
81
		}
81
		}
82
		if (isset($this->parametres['navigation.limite']) && ($this->parametres['navigation.limite'] != '')) {
82
		if (isset($this->parametres['navigation.limite']) && ($this->parametres['navigation.limite'] != '')) {
83
			if (!is_numeric($this->parametres['navigation.limite'])) {
83
			if (!is_numeric($this->parametres['navigation.limite'])) {
84
				$erreurs[] = 'la valeur pour navigation.limite doit être un entier';
84
				$erreurs[] = 'la valeur pour navigation.limite doit être un entier';
85
			}
85
			}
86
			$this->navigation_limite = intval($this->parametres['navigation.limite']);
86
			$this->navigation_limite = intval($this->parametres['navigation.limite']);
87
		} else {
87
		} else {
88
			$this->navigation_limite = 100;
88
			$this->navigation_limite = 100;
89
		}
89
		}
90
 
90
 
91
		if (count($erreurs) > 0) {
91
		if (count($erreurs) > 0) {
92
			$message = implode('<br />', $erreurs);
92
			$message = implode('<br />', $erreurs);
93
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
93
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
94
			throw new Exception($message, $code);
94
			throw new Exception($message, $code);
95
		}
95
		}
96
	}
96
	}
97
 
-
 
98
	private function obtenirLois(Array $id_lois) { 
-
 
99
		$id_lois = array_map(array($this->bdd, 'proteger'), $id_lois);
-
 
100
		$requete = "SELECT * FROM ".Config::get('bdd_table_lois').' '.
-
 
101
		           "WHERE cd_protection IN (".implode(',',$id_lois).") ORDER BY zone_application ASC";
-
 
102
		$lois = $this->bdd->recupererTous($requete, 'ASSOC');
-
 
103
		foreach ($lois as $loi) {
-
 
104
			$retour[$loi['cd_protection']] = $loi;
-
 
105
		}
-
 
106
		return $retour;
97
 
107
	}
98
 
108
	
99
	
109
	private function obtenirLoisZoneGeo(Array $id_lois) {
100
	private function obtenirLoisZoneGeo(Array $id_lois) {
110
		$id_lois = array_map(array($this->bdd, 'proteger'), $id_lois);
101
		$id_lois = array_map(array($this->bdd, 'proteger'), $id_lois);
111
		$requete = "SELECT DISTINCT zone_application, code_zone_application, type_protection FROM ".Config::get('bdd_table_lois').' '.
102
		$requete = "SELECT DISTINCT zone_application, code_zone_application, type_protection FROM ".Config::get('bdd_table_especes').' '.
112
			           "WHERE cd_protection IN (".implode(',',$id_lois).") ORDER BY zone_application ASC";   
103
			           "WHERE cd_protection IN (".implode(',',$id_lois).") ORDER BY zone_application ASC";   
113
		return $this->bdd->recupererTous($requete);
104
		return $this->bdd->recupererTous($requete);
114
	}
105
	}
115
	
106
	
116
	private function obtenirStatuts() {
107
	private function obtenirStatuts() {
117
		if ($this->nn_demande != null) {
108
		if ($this->nn_demande != null) {
118
			$conditions_taxons = array();
109
			$conditions_taxons = array();
119
			$conditions_taxons = $this->obtenirNumNomTaxonsSuperieurs(Config::get('referentiel'), $this->nn_demande);
110
			$conditions_taxons = $this->obtenirNumNomTaxonsSuperieurs(Config::get('referentiel'), $this->nn_demande);
120
			$conditions_taxons[] = $this->nn_demande;
111
			$conditions_taxons[] = $this->nn_demande;
121
		}
112
		}
122
		
113
		
123
		$requete = "SELECT * FROM ".Config::get('bdd_table_especes');
114
		$requete = "SELECT * FROM ".Config::get('bdd_table_especes');
124
		if ($this->nn_demande != null) {
115
		if ($this->nn_demande != null) {
125
			$requete .= " WHERE num_nom_retenu IN (".implode(', ', $conditions_taxons).") OR "
116
			$requete .= " WHERE num_nom_retenu IN (".implode(', ', $conditions_taxons).")";
126
				. "num_nom IN (".implode(', ', $conditions_taxons).") ";
-
 
127
		}
117
		}
128
		// pagination
118
		// pagination
129
		$requete .= " LIMIT " . $this->navigation_depart . ", " . $this->navigation_limite;
119
		$requete .= " LIMIT " . $this->navigation_depart . ", " . $this->navigation_limite;
130
		$requete .= ' -- ' . __FILE__ . ':' . __LINE__;
120
		$requete .= ' -- ' . __FILE__ . ':' . __LINE__;
131
		//echo "REQ: $requete\n";
121
		//echo "REQ: $requete\n";
132
				
122
				
133
		$statuts = $this->bdd->recupererTous($requete, 'ASSOC');
123
		$statuts = $this->bdd->recupererTous($requete, 'ASSOC');
134
		//echo "STATUTS: " . print_r($statuts, true) . "\n";
124
		//echo "STATUTS: " . print_r($statuts, true) . "\n";
135
		//echo "CPT: " . count($statuts) . "\n";
125
		//echo "CPT: " . count($statuts) . "\n";
136
		$statuts = $this->formaterRetour($statuts);
126
		$statuts = $this->formaterRetour($statuts);
137
		return $statuts;           
127
		return $statuts;           
138
	}	
128
	}	
139
//+---------------------------FONCTIONS DE FORMATAGE---------------------------------------------------------+
129
//+---------------------------FONCTIONS DE FORMATAGE---------------------------------------------------------+
140
 
130
 
141
	private function formaterRetour($statuts_taxon) {
131
	private function formaterRetour($statuts_taxon) {
142
		$retour = "";
132
		$retour = array();
143
		if ($statuts_taxon) {
133
		if ($statuts_taxon) {
144
			foreach ($statuts_taxon as $nom) {				
-
 
145
				$f[$nom['cd_protection']][$nom['num_nom']] = $nom['nom_sci'];
-
 
146
				$lois_statuts[] = $nom['cd_protection'];
-
 
147
			}
-
 
148
			switch($this->retour_format) {
134
			switch($this->retour_format) {
149
				case 'zone_geo':
135
			    case 'zone_geo': 
150
					$retour = $this->obtenirLoisZoneGeo($lois_statuts);
136
				    $retour = $this->obtenirLoisZoneGeo(array_column($statuts_taxon, "cd_protection"));
151
				break;
137
				break;
152
						
138
						
153
				case 'nom':
139
				case 'nom':
154
					$lois = $this->obtenirLois($lois_statuts);
-
 
155
					foreach ($statuts_taxon as $nom) {
140
				    foreach ($statuts_taxon as $nom) {//print_r($nom);
156
						$retour[$nom['num_nom']]['num_nom'] = $nom['num_nom'];
141
						$retour[$nom['num_nom']]['num_nom'] = $nom['num_nom_retenu'];
157
						$retour[$nom['num_nom']]['nom_sci'] = $nom['nom_sci'];
142
						$retour[$nom['num_nom']]['nom_sci'] = $nom['nom_sci'];
158
						$retour[$nom['num_nom']]['lois'][$nom['cd_protection']] = $lois[$nom['cd_protection']];
143
						$retour[$nom['num_nom']]['lois'][$nom['cd_protection']] = $nom;
159
					
144
					
160
					}
145
					}
161
				break;
146
				break;
162
				case 'complet':
147
				case 'complet':
163
					$lois = $this->obtenirLois($lois_statuts);
148
				    foreach ($statuts_taxon as $nom) { //print_r($nom);
-
 
149
				        $retour[$nom['zone_application']][$nom['cd_protection']]['cd_type_statut'] = $nom['cd_type_statut'];
-
 
150
				        $retour[$nom['zone_application']][$nom['cd_protection']]['type_protection'] = $nom['type_protection'];
-
 
151
				        $retour[$nom['zone_application']][$nom['cd_protection']]['rg_type_statut'] = $nom['rg_type_statut'];
164
					foreach ($lois as $id => $loi) {
152
				        $retour[$nom['zone_application']][$nom['cd_protection']]['cd_protection'] = $nom['cd_protection'];
165
						$retour[$loi['zone_application']][$id] = $loi;
153
				        $retour[$nom['zone_application']][$nom['cd_protection']]['intitule'] = $nom['intitule'];
166
						if (isset($f[$id])) {
154
				        $retour[$nom['zone_application']][$nom['cd_protection']]['rq_statut'] = $nom['rq_statut'];
-
 
155
				        $retour[$nom['zone_application']][$nom['cd_protection']]['zone_application'] = $nom['zone_application'];
167
							foreach ($f[$id] as $num_nom => $nom_sci) {
156
				        $retour[$nom['zone_application']][$nom['cd_protection']]['citation'] = $nom['citation'];
-
 
157
				        $retour[$nom['zone_application']][$nom['cd_protection']]['hyperlien'] = $nom['hyperlien'];
168
								$retour[$loi['zone_application']][$id]['nom_sci'][$num_nom] = $nom_sci;
158
				        $retour[$nom['zone_application']][$nom['cd_protection']]['nom_sci'][$nom['num_nom_retenu']] = $nom['nom_sci'];
169
							}
159
				    }
170
						}
-
 
171
					}
-
 
172
					break;
160
					break;
173
						
161
						
174
				default:
162
				default:
175
					$retour = $this->formaterStatutsTaxon();
163
					$retour = $this->formaterStatutsTaxon();
176
				break;
164
				break;
177
			}
165
			}
178
					
166
					
179
		}
167
		}
180
		
168
		
181
		return $retour;
169
		return $retour;
182
	}
170
	}
183
	
171
	
184
 
172
 
185
}
173
}
186
?>
174
?>