Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev 618 Rev 712
1
<?php
1
<?php
2
 
2
 
3
/**
3
/**
4
* Classe InformationsTaxonsSup.php permet de faire des requetes  pour les rangs superieurs de baseflor
4
* Classe InformationsTaxonsSup.php permet de faire des requetes  pour les rangs superieurs de baseflor
5
*  du référentiel BDTFX  et avec un numéro nomenclatural ( différent de 0 ). 
5
*  du référentiel BDTFX  et avec un numéro nomenclatural ( différent de 0 ). 
6
*  fin d'url possibles :
6
*  fin d'url possibles :
7
*  
7
*  
8
*  /informations/#bdnt.nn:#num_nomen?champs=ecologie --> retourne champs ecologiques pour un BDNT et un num_nomen
8
*  /informations/#bdnt.nn:#num_nomen?champs=ecologie --> retourne champs ecologiques pour un BDNT et un num_nomen
9
*  
9
*  
10
*
10
*
11
* Encodage en entrée : utf8
11
* Encodage en entrée : utf8
12
* Encodage en sortie : utf8
12
* Encodage en sortie : utf8
13
* @package eflore-projets
13
* @package eflore-projets
14
* @author Mathilde SALTHUN-LASSALLE <mathilde@tela-botanica.org>
14
* @author Mathilde SALTHUN-LASSALLE <mathilde@tela-botanica.org>
15
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
15
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
16
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
16
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
17
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
17
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
18
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
18
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
19
* @version 1.0
19
* @version 1.0
20
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
20
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
21
*/
21
*/
22
 
22
 
23
class InformationsTaxonsSup extends Commun{
23
class InformationsTaxonsSup extends Commun{
24
	
24
	
25
	protected $table = "";
25
	protected $table = "";
26
	private $champs_ontologiques = array();
26
	private $champs_ontologiques = array();
27
	private $format_reponse = 'informations';
27
	private $format_reponse = 'informations';
28
	protected $serviceNom = 'informations';
28
	protected $serviceNom = 'informations';
29
	private $retour_format = 'max';
29
	private $retour_format = 'max';
30
	private $Bdd;
30
	private $Bdd;
31
	private $requete_condition = "";
31
	private $requete_condition = "";
32
	private $champs_recherches = '*';
32
	private $champs_recherches = '*';
33
	
33
	
34
	public function consulter($ressources, $parametres) {
34
	public function consulter($ressources, $parametres) {
35
		$this->ressources = $ressources;
35
		$this->ressources = $ressources;
36
		$this->parametres = $parametres;
36
		$this->parametres = $parametres;
37
		$this->traiterParametres();
37
		$this->traiterParametres();
38
		$this->definirTables();
38
		$this->definirTables();
39
		$this->traiterRessources();
39
		$this->traiterRessources();
40
		$resultats = '';
40
		$resultats = '';
41
	foreach ($this->table_version as $version) {
41
	foreach ($this->table_version as $version) {
42
			$this->table = $version;
42
			$this->table = $version;
43
			$requete = $this->assemblerLaRequete();
43
			$requete = $this->assemblerLaRequete();
44
			$resultat = $this->Bdd->recupererTous($requete);
44
			$resultat = $this->Bdd->recupererTous($requete);
45
			$versionResultat = $this->analyserResultat($resultat);
45
			$versionResultat = $this->analyserResultat($resultat);
46
			if (count($this->table_version) > 1) {
46
			if (count($this->table_version) > 1) {
47
				$resultats[$version] = $versionResultat;
47
				$resultats[$version] = $versionResultat;
48
			} else {
48
			} else {
49
				$resultats = $versionResultat;
49
				$resultats = $versionResultat;
50
			}
50
			}
51
		}
51
		}
52
		return $resultats;
52
		return $resultats;
53
	}
53
	}
54
	
54
	
55
	public function analyserResultat($resultat) {
55
	public function analyserResultat($resultat) {
56
		$versionResultat = null;
56
		$versionResultat = null;
57
		if ($resultat == '') {
57
		if ($resultat == '') {
58
			$message = 'La requête SQL formée comporte une erreur!';
58
			$message = 'La requête SQL formée comporte une erreur!';
59
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
59
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
60
			throw new Exception($message, $code);
60
			throw new Exception($message, $code);
61
		} elseif ($resultat) {
61
		} elseif ($resultat) {
62
			$versionResultat = $this->retournerResultatFormate($resultat);
62
			$versionResultat = $this->retournerResultatFormate($resultat);
63
		}
63
		}
64
		return $versionResultat;
64
		return $versionResultat;
65
	}
65
	}
66
	
66
	
67
	public function __construct(Conteneur $Conteneur) {
67
	public function __construct(Conteneur $Conteneur) {
68
		$this->Bdd = $Conteneur->getBdd();
68
		$this->Bdd = $Conteneur->getBdd();
69
	}
69
	}
70
	
70
	
71
	//+--------------------------traitement ressources ou paramètres  -------------------------------------------+
71
	//+--------------------------traitement ressources ou paramètres  -------------------------------------------+
72
		
72
		
73
	public function traiterRessources() {
73
	public function traiterRessources() {
74
			if(preg_match('/^(.+)\.nn:([0-9]+)$/', $this->ressources[0], $retour)==1){
74
			if(preg_match('/^(.+)\.nn:([0-9]+)$/', $this->ressources[0], $retour)==1){
75
				switch ($retour[1]) {
75
				switch ($retour[1]) {
76
					case 'bdtfx' :
76
					case 'bdtfx' :
77
						$this->requete_condition[] = "num_nomen = ".$retour[2]." AND bdnt = 'bdtfx' ";
77
						$this->requete_condition[] = "num_nomen = ".$retour[2]." AND bdnt = 'bdtfx' ";
78
						break;
78
						break;
79
					default :
79
					default :
80
						$e = 'Erreur dans l\'url de votre requête : </br> Le référentiel " '
80
						$e = 'Erreur dans l\'url de votre requête : </br> Le référentiel " '
81
					.$retour[1].' " n\'existe pas.';
81
					.$retour[1].' " n\'existe pas.';
82
					throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
82
					throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
83
					break;
83
					break;
84
				}
84
				}
85
	
85
	
86
			}
86
			}
87
			
87
			
88
	}
88
	}
89
	
89
	
90
	//+---- paramètres ----+
90
	//+---- paramètres ----+
91
	
91
	
92
	public function traiterParametres() {
92
	public function traiterParametres() {
93
		if (isset($this->parametres) && !empty($this->parametres) ) {
93
		if (isset($this->parametres) && !empty($this->parametres) ) {
94
			foreach ($this->parametres as $param => $valeur) {
94
			foreach ($this->parametres as $param => $valeur) {
95
				switch ($param) {
95
				switch ($param) {
96
					case 'categorie'  :
96
					case 'categorie'  :
97
						if ($valeur == "ecologie"){
97
						if ($valeur == "ecologie"){
98
							$this->champs_recherches = ' num_nomen, bdnt, ve_lumiere_min , ve_lumiere_max,'
98
							$this->champs_recherches = ' num_nomen, bdnt, ve_lumiere_min , ve_lumiere_max,'
99
							.' ve_temperature_min, ve_temperature_max, ve_continentalite_min,'
99
							.' ve_temperature_min, ve_temperature_max, ve_continentalite_min,'
100
							.' ve_continentalite_max, ve_humidite_atmos_min, ve_humidite_atmos_max,'
100
							.' ve_continentalite_max, ve_humidite_atmos_min, ve_humidite_atmos_max,'
101
							.' ve_humidite_edaph_min, ve_humidite_edaph_max, ve_reaction_sol_min,'
101
							.' ve_humidite_edaph_min, ve_humidite_edaph_max, ve_reaction_sol_min,'
102
							.' ve_reaction_sol_max, ve_nutriments_sol_min, ve_nutriments_sol_max,'
102
							.' ve_reaction_sol_max, ve_nutriments_sol_min, ve_nutriments_sol_max,'
103
							.' ve_salinite_min, ve_salinite_max, ve_texture_sol_min,ve_texture_sol_max,'
103
							.' ve_salinite_min, ve_salinite_max, ve_texture_sol_min,ve_texture_sol_max,'
104
							.' ve_mat_org_sol_min, ve_mat_org_sol_max ';
104
							.' ve_mat_org_sol_min, ve_mat_org_sol_max ';
105
						} else {
105
						} else {
106
							$e = "Valeur de paramètre inconnue pour 'categorie'. Ce paramètre n'est pas autorisé pour informations/#id/relations/catminat";
106
							$e = "Valeur de paramètre inconnue pour 'categorie'. Ce paramètre n'est pas autorisé pour informations/#id/relations/catminat";
107
							throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
107
							throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
108
						}
108
						}
109
						break;
109
						break;
110
					case 'retour.format'  :
110
					case 'retour.format'  :
111
						if ($valeur == 'min' || $valeur == 'max') {
111
						if ($valeur == 'min' || $valeur == 'max') {
112
							$this->retour_format = $valeur;
112
							$this->retour_format = $valeur;
113
							break;
113
							break;
114
						} else {
114
						} else {
115
							$e = "Valeur de paramètre inconnue pour 'retour.format'. Ce paramètre n'est pas autorisé pour informations/#id/relations/catminat";
115
							$e = "Valeur de paramètre inconnue pour 'retour.format'. Ce paramètre n'est pas autorisé pour informations/#id/relations/catminat";
116
							throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
116
							throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
117
						}
117
						}
118
					case 'version.projet' :
118
					case 'version.projet' :
119
						$this->traiterVersion($valeur);
119
						$this->traiterVersion($valeur);
120
						break;
120
						break;
121
					default :
121
					default :
122
						$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
122
						$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
123
					.$param.' " n\'existe pas.';
123
					.$param.' " n\'existe pas.';
124
					throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);	break;
124
					throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);	break;
125
				}
125
				}
126
			}
126
			}
127
		}
127
		}
128
	}	
128
	}	
129
 
129
 
130
	//+++------------------------------traitement des versions----------------------------------------++
130
	//+++------------------------------traitement des versions----------------------------------------++
131
	
131
	
132
	public function traiterVersion($valeur) {
132
	public function traiterVersion($valeur) {
133
		if (preg_match('/^[0-9]+(?:[._][0-9]+)*$/', $valeur) || $valeur == '*' || $valeur == '+') {
133
		if (preg_match('/^[0-9]+(?:[._][0-9]+)*$/', $valeur) || $valeur == '*' || $valeur == '+') {
134
				$this->version_projet = $valeur;
134
				$this->version_projet = $valeur;
135
			} else  {
135
			} else  {
136
				$e = "Erreur : La version est inconnue.";
136
				$e = "Erreur : La version est inconnue.";
137
				throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
137
				throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
138
			}
138
			}
139
		if ($this->version_projet == '*' && $this->ressources == array()) {
139
		if ($this->version_projet == '*' && $this->ressources == array()) {
140
			$message = "L'affichage de plusieurs versions ne fonctionne que pour les ressources de type /ressources/#id";
140
			$message = "L'affichage de plusieurs versions ne fonctionne que pour les ressources de type /ressources/#id";
141
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
141
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
142
			throw new Exception($message, $code);
142
			throw new Exception($message, $code);
143
		}
143
		}
144
	}
144
	}
145
 
145
 
146
	public function definirTables() {
146
	public function definirTables() {
147
		$table_num_version = $this->recupererVersionDisponible();
147
		$table_num_version = $this->recupererVersionDisponible();
148
		$prefixe_table = config::get('bdd_table_rang_sup');
148
		$prefixe_table = config::get('bdd_table_rang_sup');
149
		if ( in_array($this->version_projet,$table_num_version) ) {
149
		if ( in_array($this->version_projet,$table_num_version) ) {
150
			$this->table_version[] = $prefixe_table.'_v'.$this->version_projet;
150
			$this->table_version[] = $prefixe_table.'_v'.$this->version_projet;
151
		} elseif ($this->version_projet == '+') {
151
		} elseif ($this->version_projet == '+') {
152
					$derniere_version = $table_num_version[count($table_num_version) - 1];
152
					$derniere_version = $table_num_version[count($table_num_version) - 1];
153
					$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $derniere_version);
153
					$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $derniere_version);
154
		} elseif ($this->version_projet == '*') {
154
		} elseif ($this->version_projet == '*') {
155
			foreach ($table_num_version as $num_version) {
155
			foreach ($table_num_version as $num_version) {
156
				$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $num_version);
156
				$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $num_version);
157
			}
157
			}
158
		} else {
158
		} else {
159
			$e = "Erreur : La version est inconnue.";
159
			$e = "Erreur : La version est inconnue.";
160
			throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
160
			throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
161
		}
161
		}
162
	}
162
	}
163
	
163
	
164
	
164
	
165
	//+--------------------------formatages de resultats  -------------------------------------------+
165
	//+--------------------------formatages de resultats  -------------------------------------------+
166
	
166
	
167
	
167
	
168
	public function retournerResultatFormate($resultat) {
168
	public function retournerResultatFormate($resultat) {
169
		$this->resultat_json = $resultat[0];
169
		$this->resultat_json = $resultat[0];
170
		if ($this->retour_format == 'max') {
170
		if ($this->retour_format == 'max') {
171
			$graphique_presence = $this->traiterEcologie() ;
171
			$graphique_presence = $this->traiterEcologie() ;
172
			if ($graphique_presence) {
172
			if ($graphique_presence) {
173
				$this->ajouterLiensGraphique($graphique_presence);
173
				$this->ajouterLiensGraphique($graphique_presence);
174
			}
174
			}
175
		} 
175
		} 
176
		return $this->resultat_json ;
176
		return $this->resultat_json ;
177
		
177
		
178
	}
178
	}
179
	
179
	
180
	public function traiterEcologie() {
180
	public function traiterEcologie() {
181
		$donnees_presence =  false;
181
		$donnees_presence =  false;
182
		$this->champs_ontologiques = $this->recupererTableauConfig('champs_ontologiques');
182
		$this->champs_ontologiques = $this->recupererTableauConfig('champs_ontologiques');
183
		foreach ($this->champs_ontologiques as $cle => $valeur){
183
		foreach ($this->champs_ontologiques as $cle => $valeur){
184
			if ($this->resultat_json[$cle.'_min'] != ""){
184
			if ($this->resultat_json[$cle.'_min'] != ""){
185
				$donnees_presence[$this->getNomGraphique($valeur)] = true;
185
				$donnees_presence[$this->getNomGraphique($valeur)] = true;
186
				$tab_ontologie = $this->recupererOntologies($this->resultat_json[$cle.'_min'], $cle.'_min');
186
				$tab_ontologie = $this->recupererOntologies($this->resultat_json[$cle.'_min'], $cle.'_min');
187
				unset($this->resultat_json[$cle.'_min']);
187
				unset($this->resultat_json[$cle.'_min']);
188
			}
188
			}
189
			if ($this->resultat_json[$cle.'_max'] != ""){
189
			if ($this->resultat_json[$cle.'_max'] != ""){
190
				$this->recupererOntologies($this->resultat_json[$cle.'_max'], $cle.'_max');
190
				$this->recupererOntologies($this->resultat_json[$cle.'_max'], $cle.'_max');
191
				unset($this->resultat_json[$cle.'_max']);
191
				unset($this->resultat_json[$cle.'_max']);
192
			}
192
			}
193
		}
193
		}
194
		return $donnees_presence;
194
		return $donnees_presence;
195
	}
195
	}
196
	
196
	
197
	//donne le nom du graphique correspondant à un champ écologique
197
	//donne le nom du graphique correspondant à un champ écologique
198
	public function getNomGraphique($code_ecolo) {
198
	public function getNomGraphique($code_ecolo) {
199
		$graphique = null;
199
		$graphique = null;
200
		if (in_array($code_ecolo, explode(',',Config::get('Paramètres.climat')))) {
200
		if (in_array($code_ecolo, explode(',',Config::get('Paramètres.climat')))) {
201
			$graphique = 'climat';
201
			$graphique = 'climat';
202
		} elseif (in_array($code_ecolo, explode(',', Config::get('Paramètres.sol')) )) {
202
		} elseif (in_array($code_ecolo, explode(',', Config::get('Paramètres.sol')) )) {
203
			$graphique = 'sol';
203
			$graphique = 'sol';
204
		}
204
		}
205
		return $graphique;
205
		return $graphique;
206
	}
206
	}
207
	
207
	
208
	public function ajouterLiensGraphique($graphique_presence) {
208
	public function ajouterLiensGraphique($graphique_presence) {
209
		if ($graphique_presence['climat']) {
209
		if ($graphique_presence['climat']) {
210
			$this->resultat_json['graphique_climat']['libelle'] = 'climat';
210
			$this->resultat_json['graphique_climat']['libelle'] = 'climat';
211
			$this->resultat_json['graphique_climat']['href'] = $this->ajouterHref('graphiques/climat',
211
			$this->resultat_json['graphique_climat']['href'] = $this->ajouterHref('graphiques/climat',
212
			strtolower($this->resultat_json['bdnt']).'.nn:'.$this->resultat_json['num_nomen']);
212
			strtolower($this->resultat_json['bdnt']).'.nn:'.$this->resultat_json['num_nomen']);
213
		}
213
		}
214
		if ($graphique_presence['sol']) {
214
		if ($graphique_presence['sol']) {
215
			$this->resultat_json['graphique_sol']['libelle'] = 'sol';
215
			$this->resultat_json['graphique_sol']['libelle'] = 'sol';
216
			$this->resultat_json['graphique_sol']['href'] = $this->ajouterHref('graphiques/sol',
216
			$this->resultat_json['graphique_sol']['href'] = $this->ajouterHref('graphiques/sol',
217
			strtolower($this->resultat_json['bdnt']).'.nn:'.$this->resultat_json['num_nomen']);
217
			strtolower($this->resultat_json['bdnt']).'.nn:'.$this->resultat_json['num_nomen']);
218
		}
218
		}
219
	}
219
	}
220
	
220
	
221
	//+--------------------------traitement ontologies -------------------------------------------+
221
	//+--------------------------traitement ontologies -------------------------------------------+
222
	
222
	
223
	public function recupererOntologies($valeur, $champs){
223
	public function recupererOntologies($valeur, $champs){
224
		$chps_sans = preg_replace("/_min|_max/", '', $champs);
224
		$chps_sans = preg_replace("/_min|_max/", '', $champs);
225
		$url = Config::get('url_service_base').Config::get('nom_projet').
225
		$url = Config::get('url_service_base').Config::get('nom_projet').
226
			'/ontologies/'.$this->champs_ontologiques[$chps_sans].':'.urlencode(urlencode($valeur));
226
			'/ontologies/'.$this->champs_ontologiques[$chps_sans].':'.urlencode(urlencode($valeur));
-
 
227
		try {
227
		$val = $this->consulterHref($url);
228
			$val = $this->consulterHref($url);
228
		$this->resultat_json[$champs.'.libelle'] = $val->nom;
229
			$this->resultat_json[$champs.'.libelle'] = $val->nom;
229
		$this->resultat_json[$champs.'.code'] = $valeur;
230
			$this->resultat_json[$champs.'.code'] = $valeur;
230
		$this->resultat_json[$champs.'.href'] = $url;
231
			$this->resultat_json[$champs.'.href'] = $url;
-
 
232
		} catch (Exception $e) {
-
 
233
			$this->resultat_json[$champs.'.libelle'] = '';
-
 
234
			$this->resultat_json[$champs.'.code'] = '';
-
 
235
			$this->resultat_json[$champs.'.href'] = '';
-
 
236
		}
231
	}
237
	}
232
	
238
	
233
	
239
	
234
	//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
240
	//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
235
	
241
	
236
	public function assemblerLaRequete() {
242
	public function assemblerLaRequete() {
237
		$requete = 	' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
243
		$requete = 	' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
238
		.$this->retournerRequeteCondition();
244
		.$this->retournerRequeteCondition();
239
		return $requete;
245
		return $requete;
240
	}
246
	}
241
	
247
	
242
	
248
	
243
	
249
	
244
	public  function retournerRequeteCondition() {
250
	public  function retournerRequeteCondition() {
245
		$condition = '';
251
		$condition = '';
246
		if (empty($this->requete_condition) == false) {
252
		if (empty($this->requete_condition) == false) {
247
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
253
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
248
		}
254
		}
249
		return $condition;
255
		return $condition;
250
	}
256
	}
251
	
257
	
252
}
258
}
253
?>
259
?>