Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 414 | Rev 517 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 414 Rev 447
Line 1... Line 1...
1
<?php
1
<?php
Line 2... Line 2...
2
 
2
 
3
/**
3
/**
4
* Classe Informations.php permet de faire des requetes de baseflor en fonction d'un référentiel
-
 
5
*( BDTFX, BDAFX, BDBFX ) et d'un numéro nomenclatural ( différent de 0 ). 
-
 
6
*  fin d'url possibles :
-
 
7
*  /informations/#bdnt.nn:#num_nomen --> retourne tous les champs pour un BDNT et un num_nomen
-
 
8
*  /informations/#bdnt.nn:#num_nomen?test=description --> retourne champs description pour un BDNT et un num_nomen
-
 
9
*  /informations/#bdnt.nn:#num_nomen?test=ecologie --> retourne champs ecologiques pour un BDNT et un num_nomen
-
 
10
*  /informations --> retourne les 10 premiers résultats
-
 
11
*  /informations?navigation.limite=..&navigation.depart=.. --> retourne les 10 premiers résultats avec limites
4
* Classe Informations.php redirige vers le web service pour baseflor ou pour les rangs supérieurs
12
*
5
*  
13
* Encodage en entrée : utf8
6
* Encodage en entrée : utf8
14
* Encodage en sortie : utf8
7
* Encodage en sortie : utf8
15
* @package eflore-projets
8
* @package eflore-projets
16
* @author Mathilde SALTHUN-LASSALLE <mathilde@tela-botanica.org>
-
 
17
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
9
* @author Mathilde SALTHUN-LASSALLE <mathilde@tela-botanica.org>
18
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
10
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
19
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
11
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
20
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
12
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
21
* @version 1.0
13
* @version 1.0
22
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
14
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
Line 23... Line 15...
23
*/
15
*/
Line 24... Line -...
24
 
-
 
25
class Informations extends Commun{
-
 
26
	
-
 
27
	protected $table="";
-
 
28
	private $requete_condition = "";
-
 
29
	/** Permet de stocker la requete formulée:  /informations/bdnt.nn |  */
-
 
30
	private $format_reponse = 'informations';
-
 
31
	private $total_resultat;
-
 
32
	private $retour_format = 'max';
16
 
33
	protected $limite_requete = array( 'depart' => 0, 'limite' => 10);
-
 
34
	private $champs_recherches = '*';
-
 
35
	private $champs_ontologiques =  array ('ve_lumiere' => 'VEL', 've_temperature' => 'VET', 
-
 
36
											've_continentalite' => 'VEC', 've_humidite_atmos' => 'VEHA',
-
 
37
											've_humidite_edaph' => 'VEHE', 've_reaction_sol' => 'VER' ,
-
 
38
											've_nutriments_sol' => 'VEN', 've_salinite'=> 'VES' , 
-
 
39
											've_texture_sol' => 'VETX', 've_mat_org_sol' => 'VEMO');
-
 
40
 
-
 
41
//+--------------------------traitement ressources ou paramètres  -------------------------------------------+	
-
 
42
	public function traiterParametres() {
-
 
43
		if (isset($this->parametres) && !empty($this->parametres)) {
-
 
44
			foreach ($this->parametres as $param => $valeur) {
-
 
45
				switch ($param) {
-
 
46
					case 'navigation.depart'  : 
-
 
47
						 $this->limite_requete['depart'] = $valeur;	 break;
-
 
48
					case 'navigation.limite'  :  $this->limite_requete['limite'] = $valeur;	  break;
-
 
49
					case 'test'  : 
-
 
50
						if ($valeur == "description") {
-
 
51
							$this->champs_recherches="cle, chorologie, inflorescence, sexualite, ordre_maturation, "
-
 
52
							."pollinisation, fruit, dissemination, couleur_fleur, macule, floraison, type_bio, "
-
 
53
						 	."form_vegetale ";	
-
 
54
						}elseif ($valeur == "ecologie"){
-
 
55
							$this->champs_recherches="cle, ve_lumiere , ve_temperature, ve_continentalite, 
-
 
56
							ve_humidite_atmos, ve_humidite_edaph, ve_reaction_sol, ve_nutriments_sol, ve_salinite,
-
 
57
							ve_texture_sol, ve_mat_org_sol ";
-
 
58
						}else {
-
 
59
							$e = 'Valeur de paramètre inconnue  pour \'test\'. ';
-
 
60
							$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
-
 
61
						}
-
 
62
						break;
-
 
63
					case 'retour.format'  :
-
 
64
						$this->retour_format = $valeur;
-
 
65
						break;
-
 
66
					default :
-
 
67
						$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
-
 
68
							.$param.' " n\'existe pas.';
-
 
69
						$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);	break;
-
 
70
				}
-
 
71
			}
-
 
72
		}
-
 
73
	}
-
 
74
 
-
 
75
	public function traiterRessources() {
-
 
76
		if (isset($this->ressources) && !empty($this->ressources[0])) {
-
 
77
			if(preg_match('/^(.+)\.nn:([0-9]+)$/', $this->ressources[0], $retour)==1){
-
 
78
				switch ($retour[1]) {
-
 
79
					case 'bdtfx' :
-
 
80
						$this->requete_condition[] = "num_nomen = ".$retour[2]." AND BDNT = 'BDTFX' ";
-
 
81
						 
-
 
82
						break;
-
 
83
					case  'bdafx' :
-
 
84
						$this->requete_condition[] = "num_nomen = ".$retour[2]." AND BDNT = 'BDAFX' "; 
-
 
85
						break;
-
 
86
					case  'bdbfx' :
-
 
87
						$this->requete_condition[] = "num_nomen = ".$retour[2]." AND BDNT = 'BDBFX' "; 
-
 
88
						break;
-
 
89
					default :
-
 
90
						$e = 'Erreur dans l\'url de votre requête : </br> La ressource " '
-
 
91
							.$retour[1].' " n\'existe pas.';
-
 
92
						$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);	
-
 
93
						break;
-
 
94
				}	
-
 
95
				
-
 
96
			}
-
 
97
		} else {
-
 
Line 98... Line -...
98
			$this->champs_recherches=" cle, catminat_code, BDNT, num_taxon, num_nomen ";
-
 
99
		}
17
class Informations extends Commun{
100
	}
-
 
101
	
-
 
102
//+--------------------------formatages de resultats  -------------------------------------------+
-
 
103
	public function retournerResultatFormate($resultat) {
-
 
104
		if ((count($this->ressources)) == 0) {
-
 
105
			$table_retour_json['resultat'] = $this->formaterResultatsInformations($resultat);
-
 
106
		} else {
-
 
107
			$table_retour_json = $this->formaterResultatsInformationsBDNTnn($resultat);
-
 
108
		}
-
 
109
		return $table_retour_json;
-
 
110
	}
-
 
111
	
-
 
112
	public function formaterResultatsInformationsBDNTnn($resultat){
-
 
113
	$resultat_json=array();
-
 
114
	$resultat_json = $resultat[0];
-
 
115
	if ($this->retour_format == 'max'){ 
-
 
116
		if ($resultat_json['type_bio'] != ""){
-
 
117
			$tab_ontologie = $this->recupererOntologiePrTypeBio($resultat_json['type_bio']);
-
 
118
			unset($resultat_json['type_bio']);
-
 
119
			$resultat_json= array_merge($resultat_json,$tab_ontologie);
-
 
120
		}
-
 
121
		foreach ($this->champs_ontologiques as $cle => $valeur){
-
 
122
			if ($resultat_json[$cle] != ""){
-
 
123
				$tab_ontologie = $this->recupererOntologies($resultat_json[$cle], $cle);
-
 
Line 124... Line -...
124
				unset($resultat_json[$cle]);
-
 
125
				$resultat_json= array_merge($resultat_json,$tab_ontologie);
-
 
126
			}
-
 
127
		}
-
 
128
		
-
 
129
	}
18
	
130
	$resultat_json['graphique_climat']['libelle']='climat';
-
 
131
	$resultat_json['graphique_climat']['href']=$this->ajouterHref('graphiques/climat', strtolower($resultat_json['BDNT']).'.nn:'.$resultat_json['num_nomen']);
-
 
132
	$resultat_json['graphique_sol']['libelle']='sol';
-
 
133
	$resultat_json['graphique_sol']['href']=$this->ajouterHref('graphiques/sol', strtolower($resultat_json['BDNT']).'.nn:'.$resultat_json['num_nomen']);
-
 
134
	return $resultat_json;
-
 
135
	}
19
	private $info_tables = array('InformationsBaseflor', 'InformationsTaxonsSup');
136
	
-
 
137
	public function formaterResultatsInformations($resultat) {
20
	
138
		$resultat_json=array();
-
 
139
		$resultat_json['entete'] = $this->ajouterEnteteResultat();
-
 
140
		foreach ($resultat as $tab) {
-
 
141
			if ($tab['BDNT'] != 'nc'){
-
 
142
				$num = $tab['cle'];
-
 
143
				unset($tab['cle']);
-
 
144
				foreach ($tab as $param => $valeur) {
-
 
145
					$resultat_json[$num][$param] = $valeur;
-
 
146
				}
-
 
147
				$resultat_json[$num]['href'] = $this->ajouterHref('informations',strtolower($tab['BDNT']).".nn:".$tab['num_nomen']);
-
 
148
			}
-
 
149
		}
-
 
150
		return $resultat_json;
-
 
151
	}
21
	public function consulter($ressources, $parametres) {
152
	
-
 
153
	
-
 
154
	
-
 
155
	public function ajouterEnteteResultat() {
-
 
156
		$entete['depart'] = $this->limite_requete['depart'];
-
 
157
		$entete['limite'] = $this->limite_requete['limite'];
-
 
158
		$entete['total']  =  $this->total_resultat;
-
 
159
		$entete['version']  = $this->version_projet;
-
 
160
		$url = $this->formulerUrl($this->total_resultat, '/informations');
-
 
161
		if (isset($url['precedent']) && $url['precedent'] != '') {
-
 
162
			$entete['href.precedent'] = $url['precedent'];
-
 
163
		}
-
 
164
		if (isset($url['suivant']) && $url['suivant'] != '') {
-
 
165
			$entete['href.suivant']   = $url['suivant'];
-
 
166
		}
-
 
167
		return $entete;
-
 
168
	}
-
 
169
	
-
 
170
	
-
 
171
//+--------------------------traitement ontologies -------------------------------------------+
-
 
172
	public function recupererOntologies($valeur, $champs){
-
 
173
		$url = $this->ajouterHref('ontologies',$this->champs_ontologiques[$champs].':'.urlencode(urlencode($valeur)));
-
 
174
		$val = $this->consulterHref($url);
-
 
175
		$resultat_json[$champs.'.libelle'] = $val->nom;
-
 
176
		$resultat_json[$champs.'.code'] = $valeur;
-
 
177
		$resultat_json[$champs.'.href'] = $url;
-
 
178
		return $resultat_json;
-
 
179
	}
-
 
180
	
22
		
181
	
-
 
182
	
-
 
183
	public function recupererOntologiePrTypeBio($type_a_traiter){
-
 
184
		$resultat_typeBio = "";
-
 
185
		if ($type_a_traiter != ""){
-
 
186
			$resultat_typeBio['type_bio.code'] = $type_a_traiter;
-
 
187
			$types_prim_sec = $this->separerTypesPrimairesSecondaires($type_a_traiter);
-
 
188
			foreach ($types_prim_sec as $cle => $tb){
-
 
189
				$types_sstypes[$cle]= $this->separerTypes_SousTypes($tb, $cle);
-
 
190
			}
-
 
191
			switch (count($types_sstypes)){
-
 
192
				case 1 :
-
 
193
					$types_sstypes = $types_sstypes['principal'];
-
 
194
					break;
-
 
195
				case 2 : 
-
 
196
					$types_sstypes = array_merge($types_sstypes['principal'],$types_sstypes['secondaire']);
-
 
197
					break;
23
		$versionResultat = null;
198
				case 3 : 
-
 
199
					$types_sstypes = array_merge($types_sstypes['principal'],
-
 
200
												$types_sstypes['secondaire'],
-
 
201
												$types_sstypes['special']);
24
		foreach ($this->info_tables as $info_tab) {			
202
					break;
-
 
203
			  default :
-
 
204
			  	break;
-
 
205
				 
-
 
206
			}
-
 
207
			
-
 
208
			foreach ($types_sstypes as $cle => $tb){
-
 
209
				if (($cle == 'type.principal') || ($cle == 'type.secondaire')){
-
 
210
					$url = $this->ajouterHref('ontologies','TB:'.urlencode(urlencode($tb)));
-
 
211
					$val = $this->consulterHref($url);
-
 
212
					$libelle[$cle]= $val->nom;
-
 
213
					$detail[$cle]['libelle'] = $val->nom;
-
 
214
					$detail[$cle]['code'] = $val->code;
-
 
215
					$detail[$cle]['href'] = $url;
-
 
216
				}
-
 
217
				if ( ($cle == 'sous_type.principal') || ($cle == 'sous_type.secondaire') || ($cle == 'sous_type.special') ){
-
 
218
					$i = 0; 
-
 
219
					foreach ($tb as $sstypes){
-
 
220
						$url = $this->ajouterHref('ontologies','ssTT:'.urlencode(urlencode($sstypes)));
-
 
221
						$val = $this->consulterHref($url);
-
 
222
						$libelle[$cle]=$val->nom;
-
 
223
						$detail[$cle][$i]['libelle'] = $val->nom;
-
 
224
						$detail[$cle][$i]['code'] = $val->code;
-
 
225
						$detail[$cle][$i]['href'] = $url;					
-
 
226
						$i++;
-
 
227
					}
-
 
228
				}
-
 
229
			}
-
 
230
		$resultat_typeBio['type_bio.libelle']= $this->formaterTypeBioPrLibelle($libelle);
-
 
231
		$resultat_typeBio['type_bio.detail'] = $detail;
-
 
232
		}
-
 
233
		return $resultat_typeBio;
-
 
234
	}
-
 
235
	
-
 
236
	
-
 
237
	public function formaterTypeBioPrLibelle($info){
-
 
238
		$libelle = $info['type.principal']." ";
-
 
239
		$libelle .= isset($info['sous_type.principal']) ? " (".$info['sous_type.principal'].") " : "";
-
 
240
		$libelle .= isset($info['type.secondaire']) ? ", ".$info['type.secondaire']." " : "";
-
 
241
		$libelle .= isset($info['sous_type.secondaire']) ? "(".$info['sous_type.secondaire'].") " : "";
-
 
242
		$libelle .= isset($info['sous_type.special']) ? "(".$info['sous_type.special'].") " : "";
-
 
243
		return $libelle;
-
 
244
	}
-
 
245
	
-
 
246
 
-
 
247
	public function separerTypesPrimairesSecondaires($type){
-
 
248
		$types = array(); 
-
 
249
		//séparation des principaux et des secondaires
-
 
250
		if (( preg_match("/(.+)\((.+)\)\-{0,1}(.+){0,1}/", $type,$retour) == 1  )){
-
 
251
			$types['principal'] = $retour[1]; 
25
			$info = new $info_tab();
252
			$types['secondaire'] = $retour[2];
-
 
253
			if (isset($retour[3])){$types['special'] = $retour[3];}
-
 
254
		}else {
-
 
255
			$types['principal'] = $type;
-
 
256
		}
-
 
257
		return $types;
-
 
258
	}
-
 
Line 259... Line -...
259
	
-
 
260
	public function separerTypes_SousTypes($type_a_traiter, $typePrimOuSec){
-
 
261
		$types = array();
-
 
262
		if(( preg_match("/^([a-zA-Zé]+)\-(.+)$|^([a-zA-Zé]+[^\-])$/", $type_a_traiter, $retour) == 1  )){
-
 
263
    		//recupère le type unique
-
 
264
		
-
 
265
			$type = (isset($retour[3]) && ($typePrimOuSec != 'special') ) ? $retour[3] : $retour[1] ;
-
 
266
			$types['type.'.$typePrimOuSec]= $type;
-
 
267
    		//recupère le ou les sous-types
-
 
268
    		if ((isset($retour[3]) && ($typePrimOuSec == 'special') )){
-
 
269
    			$ss_types[0] = (isset($retour[3]) && ($typePrimOuSec == 'special') ) ? $retour[3] : "" ;
-
 
270
    		}elseif(isset($retour[2]) && ($retour[2] != "")){
-
 
271
    			$ss_types[0]= (isset($retour[2]) && ($retour[2] != "") ) ? $retour[2] : "" ;
-
 
272
    		}
-
 
273
     		if (isset($ss_types[0])){
-
 
274
    			if (( preg_match("/\-/", $ss_types[0])) == 1 ){
-
 
275
    				$ss_types = preg_split("/\-/", $ss_types[0]);
-
 
276
    			}
-
 
277
				for($i = 0 ; $i < count($ss_types); $i++){
-
 
278
    				$types['sous_type.'.$typePrimOuSec][$i] = $ss_types[$i];
-
 
279
        			}
-
 
280
    		}
-
 
281
    	}
-
 
282
    	
-
 
283
 		return $types;
26
			$versionResultat = $info->consulter($ressources, $parametres);
284
	}
27
			if($versionResultat != null) {
285
	
28
				break;
286
	
29
			}
287
//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
-
 
288
	
-
 
289
	public function assemblerLaRequete() {
-
 
290
		$requete = 	' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
-
 
291
		.$this->retournerRequeteCondition().' '
-
 
292
		.$this->delimiterResultatsRequete();
-
 
293
		return $requete;
-
 
294
	}
-
 
295
	
-
 
296
 
30
		}
297
	
31
		
298
	public  function retournerRequeteCondition() {
32
		if ($versionResultat == null) {
Line 299... Line -...
299
		$condition = '';
-
 
Line 300... Line -...
300
		if ($this->requete_condition !== "") {
-
 
301
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
-
 
302
		}
-
 
303
		return $condition;
-
 
304
	}
-
 
305
	
-
 
306
 
-
 
307
	
-
 
308
	public function calculerTotalResultat() {
-
 
309
		//on récupère le nombre total de résultats de la requete
-
 
310
		$requete = 'SELECT count(*) as nombre FROM '.$this->table.' '
-
 
311
		.$this->retournerRequeteCondition();
-
 
312
		$res = $this->getBdd()->recuperer($requete);
-
 
313
		if ($res) {
-
 
314
			$this->total_resultat = $res['nombre'];
-
 
315
		} else {
-
 
316
			$this->total_resultat = 0;
-
 
317
			$e = 'Données introuvables dans la base';
-
 
318
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
-
 
319
			Debug::printr($requete);
-
 
320
		}
-
 
321
	}
-
 
322
	
-
 
323
	public function delimiterResultatsRequete() {
-
 
324
		$this->calculerTotalResultat();
-
 
325
		if ((count($this->ressources)) == 0)  { 
-
 
326
			if (($this->limite_requete['depart'] <  $this->total_resultat) && 
-
 
327
				(($this->limite_requete['depart'] + $this->limite_requete['limite'])
-
 
328
				< $this->total_resultat  )){ 
-
 
329
					$this->requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
-
 
330
					.$this->limite_requete['limite'];
-
 
331
			}else {
-
 
332
				$e = 'Données introuvables dans la base. ';
33
			$message = 'Les données recherchées sont introuvables.';
333
				$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
34
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;