Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 385 | Rev 391 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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