Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 385 | Go to most recent revision | Details | 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]) {
70
					case 'BDTFX' :
71
						$this->requete_condition[] = "num_nomen = ".$retour[2]." AND BDNT = 'BDTFX' ";
72
 
73
						break;
74
					case  'BDAFX' :
75
						$this->requete_condition[] = "num_nomen = ".$retour[2]." AND BDNT = 'BDAFX' ";
76
						break;
77
					case  'BDBFX' :
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();
97
		}
98
		foreach ($resultat as $tab) {
99
			$num = $tab['cle'];
100
			unset($tab['cle']);
101
			foreach ($tab as $param => $valeur) {
102
				$resultat_json[$num][$param] = $valeur;
103
			}
104
		}
105
		$table_retour_json['resultat'] = $resultat_json;
106
		return $table_retour_json;
107
	}
108
	public function ajouterEnteteResultat() {
109
 
110
		$entete['depart'] = $this->limite_requete['depart'];
111
		$entete['limite'] = $this->limite_requete['limite'];
112
		$entete['total']  =  $this->total_resultat;
113
		$entete['version']  = $this->version_projet;
114
 
115
		return $entete;
116
	}
117
 
118
 
119
 
120
 
121
	//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
122
 
123
	public function assemblerLaRequete() {
124
		$requete = 	' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
125
		.$this->retournerRequeteCondition().' '
126
		.$this->delimiterResultatsRequete();
127
		return $requete;
128
	}
129
 
130
 
131
 
132
	public  function retournerRequeteCondition() {
133
		$condition = '';
134
		if ($this->requete_condition !== "") {
135
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
136
		}
137
		return $condition;
138
	}
139
 
140
 
141
 
142
	public function calculerTotalResultat() {
143
		//on récupère le nombre total de résultats de la requete
144
		$requete = 'SELECT count(*) as nombre FROM '.$this->table.' '
145
		.$this->retournerRequeteCondition();
146
		$res = $this->getBdd()->recuperer($requete);
147
		if ($res) {
148
			$this->total_resultat = $res['nombre'];
149
		} else {
150
			$this->total_resultat = 0;
151
			$e = 'Données introuvables dans la base';
152
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
153
			Debug::printr($requete);
154
		}
155
	}
156
 
157
	public function delimiterResultatsRequete() {
158
		$this->calculerTotalResultat();
159
		if ((count($this->ressources)) == 0)  {
160
			if (($this->limite_requete['depart'] <  $this->total_resultat) &&
161
				(($this->limite_requete['depart'] + $this->limite_requete['limite'])
162
				< $this->total_resultat  )){
163
					$this->requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
164
					.$this->limite_requete['limite'];
165
			}else {
166
				$e = 'Données introuvables dans la base. ';
167
				$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
168
				}
169
		}else {
170
			$this->requete_limite='';
171
		}
172
		return $this->requete_limite;
173
	}
174
}