Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev 517 Rev 518
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 $requete_condition = "";
27
	private $requete_condition = "";
28
	private $format_reponse = 'informations';
28
	private $format_reponse = 'informations';
29
	private $champs_recherches = '*';
29
	private $champs_recherches = '*';
30
	protected $serviceNom = 'informations';
30
	protected $serviceNom = 'informations';
31
	private $retour_format = 'max';
31
	private $retour_format = 'max';
32
	
32
	
33
	public function consulter($ressources, $parametres) {
33
	public function consulter($ressources, $parametres) {
34
		$this->ressources = $ressources;
34
		$this->ressources = $ressources;
35
		$this->parametres = $parametres;
35
		$this->parametres = $parametres;
36
		
-
 
37
		$this->traiterParametres();
36
		$this->traiterParametres();
38
		$this->definirVersion();
37
		$this->definirTables();
39
		$this->table = Config::get('bdd_table_rang_sup')."_v".$this->version;
-
 
40
		$resultats = '';
-
 
41
		$this->recupererNomChamp($this->table); 
-
 
42
		$this->traiterRessources();
38
		$this->traiterRessources();
-
 
39
		$resultats = '';
-
 
40
	foreach ($this->table_version as $version) {
-
 
41
			$this->table = $version;
43
		$requete = $this->assemblerLaRequete();
42
			$requete = $this->assemblerLaRequete();
44
		$resultat = $this->getBdd()->recupererTous($requete);
43
			$resultat = $this->getBdd()->recupererTous($requete);
45
		$versionResultat = $this->traiterResultat($resultat, $this->table, $requete);
44
			$versionResultat = $this->traiterResultat($resultat, $version);
-
 
45
			if (count($this->table_version) > 1) {
-
 
46
				$resultats[$version] = $versionResultat;
-
 
47
			} else {
-
 
48
				$resultats = $versionResultat;
-
 
49
			}
-
 
50
		}
46
		return $versionResultat;
51
		return $versionResultat;
47
	}
52
	}
48
	
53
	
49
	
54
	
50
	//+--------------------------traitement ressources ou paramètres  -------------------------------------------+
55
	//+--------------------------traitement ressources ou paramètres  -------------------------------------------+
51
		
56
		
52
	public function traiterRessources() {
57
	public function traiterRessources() {
53
		if (isset($this->ressources) && !empty($this->ressources[0])) {
58
		if (isset($this->ressources) && !empty($this->ressources[0])) {
54
			if(preg_match('/^(.+)\.nn:([0-9]+)$/', $this->ressources[0], $retour)==1){
59
			if(preg_match('/^(.+)\.nn:([0-9]+)$/', $this->ressources[0], $retour)==1){
55
				switch ($retour[1]) {
60
				switch ($retour[1]) {
56
					case 'bdtfx' :
61
					case 'bdtfx' :
57
						$this->requete_condition[] = "num_nomen = ".$retour[2]." AND bdnt = 'bdtfx' ";
62
						$this->requete_condition[] = "num_nomen = ".$retour[2]." AND bdnt = 'bdtfx' ";
58
						break;
63
						break;
59
					default :
64
					default :
60
						$e = 'Erreur dans l\'url de votre requête : </br> La ressource " '
65
						$e = 'Erreur dans l\'url de votre requête : </br> La ressource " '
61
					.$retour[1].' " n\'existe pas.';
66
					.$retour[1].' " n\'existe pas.';
62
					$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
67
					$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
63
					break;
68
					break;
64
				}
69
				}
65
	
70
	
66
			}
71
			}
67
		} 
72
		} 
68
	}
73
	}
69
	
74
	
70
	//+---- paramètres ----+
75
	//+---- paramètres ----+
71
	
76
	
72
	public function traiterParametres() {
77
	public function traiterParametres() {
73
		if (isset($this->parametres) && !empty($this->parametres)) {
78
		if (isset($this->parametres) && !empty($this->parametres)) {
74
			foreach ($this->parametres as $param => $valeur) {
79
			foreach ($this->parametres as $param => $valeur) {
75
				switch ($param) {
80
				switch ($param) {
76
					case 'categorie'  :
81
					case 'categorie'  :
77
						if ($valeur == "ecologie"){
82
						if ($valeur == "ecologie"){
78
							$this->champs_recherches = ' num_nomen, bdnt, ve_lumiere_min , ve_lumiere_max,'
83
							$this->champs_recherches = ' num_nomen, bdnt, ve_lumiere_min , ve_lumiere_max,'
79
							.' ve_temperature_min, ve_temperature_max, ve_continentalite_min,'
84
							.' ve_temperature_min, ve_temperature_max, ve_continentalite_min,'
80
							.' ve_continentalite_max, ve_humidite_atmos_min, ve_humidite_atmos_max,'
85
							.' ve_continentalite_max, ve_humidite_atmos_min, ve_humidite_atmos_max,'
81
							.' ve_humidite_edaph_min, ve_humidite_edaph_max, ve_reaction_sol_min,'
86
							.' ve_humidite_edaph_min, ve_humidite_edaph_max, ve_reaction_sol_min,'
82
							.' ve_reaction_sol_max, ve_nutriments_sol_min, ve_nutriments_sol_max,'
87
							.' ve_reaction_sol_max, ve_nutriments_sol_min, ve_nutriments_sol_max,'
83
							.' ve_salinite_min, ve_salinite_max, ve_texture_sol_min,ve_texture_sol_max,'
88
							.' ve_salinite_min, ve_salinite_max, ve_texture_sol_min,ve_texture_sol_max,'
84
							.' ve_mat_org_sol_min, ve_mat_org_sol_max ';
89
							.' ve_mat_org_sol_min, ve_mat_org_sol_max ';
85
						} else {
90
						} else {
86
							$e = "Valeur de paramètre inconnue pour 'categorie'.";
91
							$e = "Valeur de paramètre inconnue pour 'categorie'.";
87
							$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
92
							$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
88
						}
93
						}
89
						break;
94
						break;
90
					case 'retour.format'  :
95
					case 'retour.format'  :
91
						if ($valeur == 'min' || $valeur == 'max') {
96
						if ($valeur == 'min' || $valeur == 'max') {
92
							$this->retour_format = $valeur;
97
							$this->retour_format = $valeur;
93
							break;
98
							break;
94
						} else {
99
						} else {
95
							$e = "Valeur de paramètre inconnue pour 'retour.format'.";
100
							$e = "Valeur de paramètre inconnue pour 'retour.format'.";
96
							$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
101
							$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
97
						}
102
						}
-
 
103
					case 'version.projet' :
-
 
104
						$this->traiterVersion($valeur);
-
 
105
						break;
98
					default :
106
					default :
99
						$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
107
						$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
100
					.$param.' " n\'existe pas.';
108
					.$param.' " n\'existe pas.';
101
					$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);	break;
109
					$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);	break;
102
				}
110
				}
103
			}
111
			}
104
		}
112
		}
105
	}	
113
	}	
-
 
114
 
-
 
115
	//+++------------------------------traitement des versions----------------------------------------++
-
 
116
	
106
	private function definirVersion() {
117
	public function traiterVersion($valeur) {
-
 
118
		if (preg_match('/^[0-9]+(?:[._][0-9]+)*$/', $valeur) || $valeur == '*' || $valeur == '+') {
107
		$this->chargerVersions();
119
				$this->version_projet = $valeur;
-
 
120
			} else  {
-
 
121
				$e = "Erreur : La version est inconnue.";
108
		if( (!isset($this->parametres['version.projet']) ) || ((isset($this->parametres['version.projet']) )&&
122
				$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
-
 
123
			}
109
		(($this->parametres['version.projet'] == '+') || ($this->parametres['version.projet'] == '')))){
124
		if ($this->version_projet == '*' && $this->ressources == array()) {
110
			$this->version = $this->metadonnees[0]['version'];
125
			$message = "L'affichage de plusieurs versions ne fonctionne que pour les ressources de type /ressources/#id";
111
		} else {
126
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
112
			$this->version = $this->parametres['version.projet'];
127
			throw new Exception($message, $code);
113
		}
128
		}
114
	}
129
	}
115
 
130
 
-
 
131
	public function DefinirTables() {
116
	private function chargerVersions() {
132
		$table_num_version = $this->recupererVersionDisponible();
117
		$requete = "SELECT version FROM ".Config::get('bdd_table_meta')." ".
133
		$prefixe_table = config::get('bdd_table_rang_sup');
-
 
134
		if ( in_array($this->version_projet,$table_num_version) ) {
118
								"ORDER BY date_creation DESC ";
135
			$this->table_version[] = $prefixe_table.'_v'.$this->version_projet;
119
		$resultats = $this->getBdd()->recupererTous($requete);
136
		} elseif ($this->version_projet == '+') {
120
		if (!is_array($resultats) || count($resultats) <= 0) {
137
					$derniere_version = $table_num_version[count($table_num_version) - 1];
-
 
138
					$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $derniere_version);
121
			$message = "Les méta-données n'ont pu être chargée pour la ressource demandée";
139
		} elseif ($this->version_projet == '*') {
-
 
140
			foreach ($table_num_version as $num_version) {
-
 
141
				$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $num_version);
-
 
142
			}
122
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
143
		} else {
-
 
144
			$e = "Erreur : La version est inconnue.";
123
			throw new Exception($message, $code);
145
			$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
124
		}
-
 
125
	
-
 
126
		$this->metadonnees = $resultats;
146
		}
-
 
147
	}
127
	}
148
	
128
	
149
	
129
	//+--------------------------formatages de resultats  -------------------------------------------+
150
	//+--------------------------formatages de resultats  -------------------------------------------+
130
	
151
	
131
	
152
	
132
	public function retournerResultatFormate($resultat) {
153
	public function retournerResultatFormate($resultat) {
133
		$resultat_json = $resultat[0];
154
		$resultat_json = $resultat[0];
134
		if ($this->retour_format == 'max') {
155
		if ($this->retour_format == 'max') {
135
			$this->champs_ontologiques = $this->recupererTableauConfig('champs_ontologiques');
156
			$this->champs_ontologiques = $this->recupererTableauConfig('champs_ontologiques');
136
			$graphique_presence = false;
157
			$graphique_presence = false;
137
			foreach ($this->champs_ontologiques as $cle => $valeur){
158
			foreach ($this->champs_ontologiques as $cle => $valeur){
138
				if ($resultat_json[$cle.'_min'] != ""){
159
				if ($resultat_json[$cle.'_min'] != ""){
139
					$graphique_presence = true;
160
					$graphique_presence = true;
140
					$tab_ontologie = $this->recupererOntologies($resultat_json[$cle.'_min'], $cle.'_min');
161
					$tab_ontologie = $this->recupererOntologies($resultat_json[$cle.'_min'], $cle.'_min');
141
					unset($resultat_json[$cle.'_min']);
162
					unset($resultat_json[$cle.'_min']);
142
					$resultat_json= array_merge($resultat_json,$tab_ontologie);
163
					$resultat_json= array_merge($resultat_json,$tab_ontologie);
143
				}
164
				}
144
				if ($resultat_json[$cle.'_max'] != ""){
165
				if ($resultat_json[$cle.'_max'] != ""){
145
					$tab_ontologie = $this->recupererOntologies($resultat_json[$cle.'_max'], $cle.'_max');
166
					$tab_ontologie = $this->recupererOntologies($resultat_json[$cle.'_max'], $cle.'_max');
146
					unset($resultat_json[$cle.'_max']);
167
					unset($resultat_json[$cle.'_max']);
147
					$resultat_json= array_merge($resultat_json,$tab_ontologie);
168
					$resultat_json= array_merge($resultat_json,$tab_ontologie);
148
				}
169
				}
149
			}
170
			}
150
			if ($graphique_presence) {
171
			if ($graphique_presence) {
151
				$resultat_json['graphique_climat']['libelle'] = 'climat';
172
				$resultat_json['graphique_climat']['libelle'] = 'climat';
152
				$resultat_json['graphique_climat']['href'] = $this->ajouterHref('graphiques/climat', strtolower($resultat_json['bdnt']).'.nn:'.$resultat_json['num_nomen']);
173
				$resultat_json['graphique_climat']['href'] = $this->ajouterHref('graphiques/climat', strtolower($resultat_json['bdnt']).'.nn:'.$resultat_json['num_nomen']);
153
				$resultat_json['graphique_sol']['libelle'] = 'sol';
174
				$resultat_json['graphique_sol']['libelle'] = 'sol';
154
				$resultat_json['graphique_sol']['href'] = $this->ajouterHref('graphiques/sol', strtolower($resultat_json['bdnt']).'.nn:'.$resultat_json['num_nomen']);
175
				$resultat_json['graphique_sol']['href'] = $this->ajouterHref('graphiques/sol', strtolower($resultat_json['bdnt']).'.nn:'.$resultat_json['num_nomen']);
155
			}
176
			}
156
		}
177
		}
157
		return $resultat_json ;
178
		return $resultat_json ;
158
		
179
		
159
	}
180
	}
160
	
181
	
161
	//+--------------------------traitement ontologies -------------------------------------------+
182
	//+--------------------------traitement ontologies -------------------------------------------+
162
	
183
	
163
	public function recupererOntologies($valeur, $champs){
184
	public function recupererOntologies($valeur, $champs){
164
		$chps_sans = preg_replace("/_min|_max/", '', $champs);
185
		$chps_sans = preg_replace("/_min|_max/", '', $champs);
-
 
186
		$url = Config::get('url_service_base').Config::get('nom_projet').
165
		$url = $this->ajouterHref('ontologies',$this->champs_ontologiques[$chps_sans].':'.urlencode(urlencode($valeur)));
187
			'/ontologies/'.$this->champs_ontologiques[$chps_sans].':'.urlencode(urlencode($valeur));
166
		$val = $this->consulterHref($url);
188
		$val = $this->consulterHref($url);
167
		$resultat_json[$champs.'.libelle'] = $val->nom;
189
		$resultat_json[$champs.'.libelle'] = $val->nom;
168
		$resultat_json[$champs.'.code'] = $valeur;
190
		$resultat_json[$champs.'.code'] = $valeur;
169
		$resultat_json[$champs.'.href'] = $url;
191
		$resultat_json[$champs.'.href'] = $url;
170
		return $resultat_json;
192
		return $resultat_json;
171
	}
193
	}
172
	
194
	
173
	
195
	
174
	//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
196
	//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
175
	
197
	
176
	public function assemblerLaRequete() {
198
	public function assemblerLaRequete() {
177
		$requete = 	' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
199
		$requete = 	' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
178
		.$this->retournerRequeteCondition();
200
		.$this->retournerRequeteCondition();
179
		return $requete;
201
		return $requete;
180
	}
202
	}
181
	
203
	
182
	
204
	
183
	
205
	
184
	public  function retournerRequeteCondition() {
206
	public  function retournerRequeteCondition() {
185
		$condition = '';
207
		$condition = '';
186
		if ($this->requete_condition !== "") {
208
		if ($this->requete_condition !== "") {
187
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
209
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
188
		}
210
		}
189
		return $condition;
211
		return $condition;
190
	}
212
	}
191
	
213
	
192
}
214
}
193
?>
215
?>