Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev Author Line No. Line
379 mathilde 1
<?php
2
 
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
12
*
13
* Encodage en entrée : utf8
14
* Encodage en sortie : utf8
15
* @package eflore-projets
16
* @author Mathilde SALTHUN-LASSALLE <mathilde@tela-botanica.org>
17
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
18
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
19
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
20
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
21
* @version 1.0
22
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
23
*/
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 $limite_requete = array( 'depart' => 0, 'limite' => 10);
33
	private $champs_recherches='*';
34
 
35
 
36
	public function traiterParametres() {
37
		if (isset($this->parametres) && !empty($this->parametres)) {
38
			foreach ($this->parametres as $param => $valeur) {
39
				switch ($param) {
40
					case 'navigation.depart'  :
41
						 $this->limite_requete['depart'] = $valeur;	 break;
42
					case 'navigation.limite'  :  $this->limite_requete['limite'] = $valeur;	  break;
43
					case 'test'  :
44
						if ($valeur == "description") {
45
							$this->champs_recherches="cle, chorologie, inflorescence, sexualite, ordre_maturation, "
46
							."pollinisation, fruit, dissemination, couleur_fleur, macule, floraison, type_bio, "
47
						 	."form_vegetale ";
48
						}elseif ($valeur == "ecologie"){
49
							$this->champs_recherches="cle, ve_lumiere , ve_temperature, ve_continentalite,
50
							ve_humidite_atmos, ve_humidite_edaph, ve_reaction_sol, ve_nutriments_sol, ve_salinite,
51
							ve_texture_sol, ve_mat_org_sol ";
52
						}else {
53
							$e = 'Valeur de paramètre inconnue  pour \'test\'. ';
54
							$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
55
						}
56
						break;
57
					default :
58
						$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
59
							.$param.' " n\'existe pas.';
60
						$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);	break;
61
				}
62
			}
63
		}
64
	}
65
 
66
	public function traiterRessources() {
67
		if (isset($this->ressources) && !empty($this->ressources[0])) {
68
			if(preg_match('/^(.+)\.nn:([0-9]+)$/', $this->ressources[0], $retour)==1){
69
				switch ($retour[1]) {
385 mathilde 70
					case 'bdtfx' :
379 mathilde 71
						$this->requete_condition[] = "num_nomen = ".$retour[2]." AND BDNT = 'BDTFX' ";
72
 
73
						break;
385 mathilde 74
					case  'bdafx' :
379 mathilde 75
						$this->requete_condition[] = "num_nomen = ".$retour[2]." AND BDNT = 'BDAFX' ";
76
						break;
385 mathilde 77
					case  'bdbfx' :
379 mathilde 78
						$this->requete_condition[] = "num_nomen = ".$retour[2]." AND BDNT = 'BDBFX' ";
79
						break;
80
					default :
81
						$e = 'Erreur dans l\'url de votre requête : </br> La ressource " '
82
							.$retour[1].' " n\'existe pas.';
83
						$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
84
						break;
85
				}
86
 
87
			}
88
		} else {
89
			$this->champs_recherches=" cle, catminat_code, BDNT, num_taxon, num_nomen ";
90
		}
91
	}
92
 
93
	public function retournerResultatFormate($resultat) {
94
		if ((count($this->ressources)) == 0){
95
			$table_retour_json=array();
96
			$table_retour_json['entete'] = $this->ajouterEnteteResultat();
390 mathilde 97
			$table_retour_json['resultat'] = $this->formaterResultats($resultat);
98
		} else {
99
			$table_retour_json = $resultat[0];
379 mathilde 100
		}
390 mathilde 101
		return $table_retour_json;
102
	}
103
 
104
	public function formaterResultats($resultat) {
379 mathilde 105
		foreach ($resultat as $tab) {
106
			$num = $tab['cle'];
107
			unset($tab['cle']);
108
			foreach ($tab as $param => $valeur) {
109
				$resultat_json[$num][$param] = $valeur;
110
			}
111
		}
390 mathilde 112
		return $resultat_json;
379 mathilde 113
	}
114
	public function ajouterEnteteResultat() {
115
 
116
		$entete['depart'] = $this->limite_requete['depart'];
117
		$entete['limite'] = $this->limite_requete['limite'];
118
		$entete['total']  =  $this->total_resultat;
119
		$entete['version']  = $this->version_projet;
120
 
121
		return $entete;
122
	}
123
 
124
 
125
 
126
 
127
	//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
128
 
129
	public function assemblerLaRequete() {
130
		$requete = 	' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
131
		.$this->retournerRequeteCondition().' '
132
		.$this->delimiterResultatsRequete();
133
		return $requete;
134
	}
135
 
136
 
137
 
138
	public  function retournerRequeteCondition() {
139
		$condition = '';
140
		if ($this->requete_condition !== "") {
141
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
142
		}
143
		return $condition;
144
	}
145
 
146
 
147
 
148
	public function calculerTotalResultat() {
149
		//on récupère le nombre total de résultats de la requete
150
		$requete = 'SELECT count(*) as nombre FROM '.$this->table.' '
151
		.$this->retournerRequeteCondition();
152
		$res = $this->getBdd()->recuperer($requete);
153
		if ($res) {
154
			$this->total_resultat = $res['nombre'];
155
		} else {
156
			$this->total_resultat = 0;
157
			$e = 'Données introuvables dans la base';
158
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
159
			Debug::printr($requete);
160
		}
161
	}
162
 
163
	public function delimiterResultatsRequete() {
164
		$this->calculerTotalResultat();
165
		if ((count($this->ressources)) == 0)  {
166
			if (($this->limite_requete['depart'] <  $this->total_resultat) &&
167
				(($this->limite_requete['depart'] + $this->limite_requete['limite'])
168
				< $this->total_resultat  )){
169
					$this->requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
170
					.$this->limite_requete['limite'];
171
			}else {
172
				$e = 'Données introuvables dans la base. ';
173
				$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
174
				}
175
		}else {
176
			$this->requete_limite='';
177
		}
178
		return $this->requete_limite;
179
	}
180
}