Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 516 | Rev 518 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
453 mathilde 1
<?php
2
 
3
/**
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 ).
6
*  fin d'url possibles :
7
*
8
*  /informations/#bdnt.nn:#num_nomen?champs=ecologie --> retourne champs ecologiques pour un BDNT et un num_nomen
9
*
10
*
11
* Encodage en entrée : utf8
12
* Encodage en sortie : utf8
13
* @package eflore-projets
14
* @author Mathilde SALTHUN-LASSALLE <mathilde@tela-botanica.org>
15
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
16
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
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>
19
* @version 1.0
20
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
21
*/
22
 
23
class InformationsTaxonsSup extends Commun{
24
 
25
	protected $table = "";
26
	private $champs_ontologiques = array();
27
	private $requete_condition = "";
28
	private $format_reponse = 'informations';
29
	private $champs_recherches = '*';
30
	protected $serviceNom = 'informations';
517 mathilde 31
	private $retour_format = 'max';
453 mathilde 32
 
33
	public function consulter($ressources, $parametres) {
34
		$this->ressources = $ressources;
35
		$this->parametres = $parametres;
36
 
37
		$this->traiterParametres();
38
		$this->definirVersion();
39
		$this->table = Config::get('bdd_table_rang_sup')."_v".$this->version;
40
		$resultats = '';
41
		$this->recupererNomChamp($this->table);
42
		$this->traiterRessources();
43
		$requete = $this->assemblerLaRequete();
44
		$resultat = $this->getBdd()->recupererTous($requete);
45
		$versionResultat = $this->traiterResultat($resultat, $this->table, $requete);
46
		return $versionResultat;
47
	}
48
 
49
 
50
	//+--------------------------traitement ressources ou paramètres  -------------------------------------------+
51
 
52
	public function traiterRessources() {
53
		if (isset($this->ressources) && !empty($this->ressources[0])) {
54
			if(preg_match('/^(.+)\.nn:([0-9]+)$/', $this->ressources[0], $retour)==1){
55
				switch ($retour[1]) {
56
					case 'bdtfx' :
57
						$this->requete_condition[] = "num_nomen = ".$retour[2]." AND bdnt = 'bdtfx' ";
58
						break;
59
					default :
60
						$e = 'Erreur dans l\'url de votre requête : </br> La ressource " '
61
					.$retour[1].' " n\'existe pas.';
62
					$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
63
					break;
64
				}
65
 
66
			}
67
		}
68
	}
69
 
70
	//+---- paramètres ----+
71
 
72
	public function traiterParametres() {
73
		if (isset($this->parametres) && !empty($this->parametres)) {
74
			foreach ($this->parametres as $param => $valeur) {
75
				switch ($param) {
516 mathilde 76
					case 'categorie'  :
453 mathilde 77
						if ($valeur == "ecologie"){
78
							$this->champs_recherches = ' num_nomen, bdnt, ve_lumiere_min , ve_lumiere_max,'
79
							.' ve_temperature_min, ve_temperature_max, ve_continentalite_min,'
80
							.' ve_continentalite_max, ve_humidite_atmos_min, ve_humidite_atmos_max,'
81
							.' 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,'
83
							.' 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 ';
85
						} else {
517 mathilde 86
							$e = "Valeur de paramètre inconnue pour 'categorie'.";
453 mathilde 87
							$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
88
						}
89
						break;
517 mathilde 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
						}
453 mathilde 98
					default :
99
						$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
100
					.$param.' " n\'existe pas.';
101
					$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);	break;
102
				}
103
			}
104
		}
105
	}
106
	private function definirVersion() {
107
		$this->chargerVersions();
108
		if( (!isset($this->parametres['version.projet']) ) || ((isset($this->parametres['version.projet']) )&&
109
		(($this->parametres['version.projet'] == '+') || ($this->parametres['version.projet'] == '')))){
110
			$this->version = $this->metadonnees[0]['version'];
111
		} else {
112
			$this->version = $this->parametres['version.projet'];
113
		}
114
	}
115
 
116
	private function chargerVersions() {
117
		$requete = "SELECT version FROM ".Config::get('bdd_table_meta')." ".
118
								"ORDER BY date_creation DESC ";
119
		$resultats = $this->getBdd()->recupererTous($requete);
120
		if (!is_array($resultats) || count($resultats) <= 0) {
121
			$message = "Les méta-données n'ont pu être chargée pour la ressource demandée";
122
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
123
			throw new Exception($message, $code);
124
		}
125
 
126
		$this->metadonnees = $resultats;
127
	}
128
 
129
	//+--------------------------formatages de resultats  -------------------------------------------+
130
 
131
 
132
	public function retournerResultatFormate($resultat) {
133
		$resultat_json = $resultat[0];
517 mathilde 134
		if ($this->retour_format == 'max') {
135
			$this->champs_ontologiques = $this->recupererTableauConfig('champs_ontologiques');
136
			$graphique_presence = false;
137
			foreach ($this->champs_ontologiques as $cle => $valeur){
138
				if ($resultat_json[$cle.'_min'] != ""){
139
					$graphique_presence = true;
140
					$tab_ontologie = $this->recupererOntologies($resultat_json[$cle.'_min'], $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']);
147
					$resultat_json= array_merge($resultat_json,$tab_ontologie);
148
				}
453 mathilde 149
			}
517 mathilde 150
			if ($graphique_presence) {
151
				$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']);
153
				$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']);
453 mathilde 155
			}
156
		}
157
		return $resultat_json ;
158
 
159
	}
160
 
161
	//+--------------------------traitement ontologies -------------------------------------------+
162
 
163
	public function recupererOntologies($valeur, $champs){
164
		$chps_sans = preg_replace("/_min|_max/", '', $champs);
165
		$url = $this->ajouterHref('ontologies',$this->champs_ontologiques[$chps_sans].':'.urlencode(urlencode($valeur)));
166
		$val = $this->consulterHref($url);
167
		$resultat_json[$champs.'.libelle'] = $val->nom;
168
		$resultat_json[$champs.'.code'] = $valeur;
169
		$resultat_json[$champs.'.href'] = $url;
170
		return $resultat_json;
171
	}
172
 
173
 
174
	//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
175
 
176
	public function assemblerLaRequete() {
177
		$requete = 	' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
178
		.$this->retournerRequeteCondition();
179
		return $requete;
180
	}
181
 
182
 
183
 
184
	public  function retournerRequeteCondition() {
185
		$condition = '';
186
		if ($this->requete_condition !== "") {
187
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
188
		}
189
		return $condition;
190
	}
191
 
192
}
193
?>