Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 390 | Rev 395 | 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;
391 mathilde 32
	protected $limite_requete = array( 'depart' => 0, 'limite' => 10);
379 mathilde 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);
391 mathilde 98
 
390 mathilde 99
		} else {
100
			$table_retour_json = $resultat[0];
379 mathilde 101
		}
390 mathilde 102
		return $table_retour_json;
103
	}
104
 
105
	public function formaterResultats($resultat) {
379 mathilde 106
		foreach ($resultat as $tab) {
107
			$num = $tab['cle'];
108
			unset($tab['cle']);
109
			foreach ($tab as $param => $valeur) {
110
				$resultat_json[$num][$param] = $valeur;
111
			}
391 mathilde 112
			$resultat_json[$num]['href'] = $this->ajouterHref('ontologies', $num);
379 mathilde 113
		}
390 mathilde 114
		return $resultat_json;
379 mathilde 115
	}
116
	public function ajouterEnteteResultat() {
117
 
118
		$entete['depart'] = $this->limite_requete['depart'];
119
		$entete['limite'] = $this->limite_requete['limite'];
120
		$entete['total']  =  $this->total_resultat;
121
		$entete['version']  = $this->version_projet;
391 mathilde 122
		$url = $this->formulerUrl($this->total_resultat, '/informations');
123
		if (isset($url['precedent']) && $url['precedent'] != '') {
124
			$entete['href.precedent'] = $url['precedent'];
125
		}
126
		if (isset($url['suivant']) && $url['suivant'] != '') {
127
			$entete['href.suivant']   = $url['suivant'];
128
		}
379 mathilde 129
 
130
		return $entete;
131
	}
132
 
133
 
134
 
135
 
136
	//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
137
 
138
	public function assemblerLaRequete() {
139
		$requete = 	' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
140
		.$this->retournerRequeteCondition().' '
141
		.$this->delimiterResultatsRequete();
142
		return $requete;
143
	}
144
 
145
 
146
 
147
	public  function retournerRequeteCondition() {
148
		$condition = '';
149
		if ($this->requete_condition !== "") {
150
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
151
		}
152
		return $condition;
153
	}
154
 
155
 
156
 
157
	public function calculerTotalResultat() {
158
		//on récupère le nombre total de résultats de la requete
159
		$requete = 'SELECT count(*) as nombre FROM '.$this->table.' '
160
		.$this->retournerRequeteCondition();
161
		$res = $this->getBdd()->recuperer($requete);
162
		if ($res) {
163
			$this->total_resultat = $res['nombre'];
164
		} else {
165
			$this->total_resultat = 0;
166
			$e = 'Données introuvables dans la base';
167
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
168
			Debug::printr($requete);
169
		}
170
	}
171
 
172
	public function delimiterResultatsRequete() {
173
		$this->calculerTotalResultat();
174
		if ((count($this->ressources)) == 0)  {
175
			if (($this->limite_requete['depart'] <  $this->total_resultat) &&
176
				(($this->limite_requete['depart'] + $this->limite_requete['limite'])
177
				< $this->total_resultat  )){
178
					$this->requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
179
					.$this->limite_requete['limite'];
180
			}else {
181
				$e = 'Données introuvables dans la base. ';
182
				$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
183
				}
184
		}else {
185
			$this->requete_limite='';
186
		}
187
		return $this->requete_limite;
188
	}
189
}