Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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