Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev 288 Rev 289
1
<?php
1
<?php
2
 
2
 
3
/**
3
/**
4
* Description :
4
* Description :
5
* Classe Textes.php est une classe qui contient les méthodes permettant la consultation des textes (clé de détermination
5
* Classe Textes.php est une classe qui contient les méthodes permettant la consultation des textes (clé de détermination
6
*  ou description). Les identifiants des textes correspondent au tag de type CleCoste1937TXXPXXNomSci pour le projet 
6
*  ou description). Les identifiants des textes correspondent au tag de type CleCoste1937TXXPXXNomSci pour le projet 
7
* coste.
7
* coste.
8
* Encodage en entrée : utf8
8
* Encodage en entrée : utf8
9
* Encodage en sortie : utf8
9
* Encodage en sortie : utf8
10
* @package framework-v3
10
* @package framework-v3
11
* @author Jennifer Dhé <jennifer.dhe@tela-botanica.org>
11
* @author Jennifer Dhé <jennifer.dhe@tela-botanica.org>
12
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
12
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
13
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
13
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
14
* @version 1.0
14
* @version 1.0
15
* @copyright 1999-${year} Tela Botanica (accueil@tela-botanica.org)
15
* @copyright 1999-${year} Tela Botanica (accueil@tela-botanica.org)
16
*/
16
*/
17
 
17
 
18
 
18
 
19
class Textes extends Commun {
19
class Textes extends Commun {
20
	
20
	
21
	protected $limite_requete	= array('depart' => 0, 'limite' => 100);
21
	protected $limite_requete	= array('depart' => 0, 'limite' => 100);
22
	protected $requete_champ = ' t1.resource AS tag, t1.value AS num_nom, t2.value AS titre, p.body AS texte';
22
	protected $requete_champ = ' t1.resource AS tag, t1.value AS num_nom, t2.value AS titre, p.body AS texte';
23
	protected $requete_condition = null;
23
	protected $requete_condition = null;
24
	protected $requete_group_by = ' ORDER BY CAST( t1.value AS DECIMAL )';
24
	protected $requete_group_by = ' ORDER BY CAST( t1.value AS DECIMAL )';
25
	protected $table_retour;
25
	protected $table_retour;
26
	protected $retour_format = 'max';
26
	protected $retour_format = 'max';
27
	protected $format_reponse = 'textes';
27
	protected $format_reponse = 'textes';
28
	protected $table_param = array();
28
	protected $table_param = array();
29
	protected $total_resultat;
29
	protected $total_resultat;
30
	protected $table_ressources = array();
30
	protected $table_ressources = array();
31
	protected $html = 'txt';
31
	protected $html = 'txt';
32
	protected $recherche = 'stricte';
32
	protected $recherche = 'stricte';
33
	protected $service = 'textes';
33
	protected $service = 'textes';
34
	protected $masque = null;
34
	protected $masque = null;
35
	
35
	
36
	
36
	
37
	public function consulter($ressources, $parametres) {
37
	public function consulter($ressources, $parametres) {
38
		$resultat_formate = '';
38
		$resultat_formate = '';
39
		$this->table = array(Config::get("bdd_triples")." t1", Config::get("bdd_triples")." t2", Config::get("bdd_pages")." p");
39
		$this->table = array(Config::get("bdd_triples")." t1", Config::get("bdd_triples")." t2", Config::get("bdd_pages")." p");
40
		$this->traiterParametres($parametres);
40
		$this->traiterParametres($parametres);
41
		$this->traiterRessources($ressources);
41
		$this->traiterRessources($ressources);
42
		if ($this->corps_http == '' && $this->entete_http == '') {
42
		if ($this->corps_http == '' && $this->entete_http == '') {
43
			$requete = $this->assemblerLaRequete();
43
			$requete = $this->assemblerLaRequete();
44
			$resultat = $this->getBdd()->recupererTous($requete); 
44
			$resultat = $this->getBdd()->recupererTous($requete); 
45
			$this->testerResultat($resultat, $resultat_formate, $requete);
45
			$this->testerResultat($resultat, $resultat_formate, $requete);
46
		}
46
		}
47
		return $this->formerReponseHTTP($resultat_formate);
47
		return $this->formerReponseHTTP($resultat_formate);
48
	}
48
	}
49
	
49
	
50
	public function formerReponseHTTP($resultat_formate, $mime = 'application/json', $encodage= 'utf-8') {
50
	public function formerReponseHTTP($resultat_formate, $mime = 'application/json', $encodage= 'utf-8') {
51
		// Si aucune erreur n'a été établie (donc un tableau de résultat a bien ete renvoyé...)
51
		// Si aucune erreur n'a été établie (donc un tableau de résultat a bien ete renvoyé...)
52
		if ($this->corps_http == '' && $this->entete_http == '') {
52
		if ($this->corps_http == '' && $this->entete_http == '') {
53
			$this->entete_http = RestServeur::HTTP_CODE_OK;
53
			$this->entete_http = RestServeur::HTTP_CODE_OK;
54
			$this->corps_http  = $resultat_formate;
54
			$this->corps_http  = $resultat_formate;
55
		}
55
		}
56
		if ($this->entete_http != RestServeur::HTTP_CODE_OK) {
56
		if ($this->entete_http != RestServeur::HTTP_CODE_OK) {
57
			$mime = 'text/html';
57
			$mime = 'text/html';
58
		}
58
		}
59
		// Gestion du type de contenu
59
		// Gestion du type de contenu
60
		if (!is_null($mime) && !is_null($encodage)) {
60
		if (!is_null($mime) && !is_null($encodage)) {
61
			header("Content-Type: $mime; charset=$encodage");
61
			header("Content-Type: $mime; charset=$encodage");
62
		} else if (!is_null($mime) && is_null($encodage)) {
62
		} else if (!is_null($mime) && is_null($encodage)) {
63
			header("Content-Type: $mime");
63
			header("Content-Type: $mime");
64
		}
64
		}
65
		// Envoie de l'entête
65
		// Envoie de l'entête
66
		RestServeur::envoyerEnteteStatutHttp($this->entete_http);
66
		RestServeur::envoyerEnteteStatutHttp($this->entete_http);
67
		// Envoie du corps
67
		// Envoie du corps
68
		return $this->corps_http;
68
		return $this->corps_http;
69
	}
69
	}
70
	
70
	
71
	
71
	
72
	public function testerResultat($resultat, &$resultat_formate, $requete) {
72
	public function testerResultat($resultat, &$resultat_formate, $requete) {
73
		if ($resultat == '') { //cas ou coste/services/ ou la requete comporte des erreurs
73
		if ($resultat == '') { //cas ou coste/services/ ou la requete comporte des erreurs
74
			$e = 'La requête SQL formée comporte une erreur!';
74
			$e = 'La requête SQL formée comporte une erreur!';
75
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE,$e);
75
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE,$e);
76
			Debug::printr($requete);
76
			Debug::printr($requete);
77
		} elseif ($resultat) {
77
		} elseif ($resultat) {
78
			$resultat_formate = $this->retournerResultatFormate($resultat);
78
			$resultat_formate = $this->retournerResultatFormate($resultat);
79
		} else {
79
		} else {
80
			if ($this->format_reponse = 'textes/relations') {
80
			if ($this->format_reponse = 'textes/relations') {
81
				$resultat_formate = 'null';
81
				$resultat_formate = 'null';
82
			} else {
82
			} else {
83
				$m = 'Données introuvables dans la base';
83
				$m = 'Données introuvables dans la base';
84
				$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE,$m);
84
				$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE,$m);
85
				Debug::printr($requete);
85
				Debug::printr($requete);
86
			}
86
			}
87
		}
87
		}
88
	}
88
	}
89
	
89
	
90
	
90
	
91
//---------------------------------TRAITER RESSOURCES-----------------------------------------------------------------
91
//---------------------------------TRAITER RESSOURCES-----------------------------------------------------------------
92
	
92
	
93
	public function traiterRessources(&$ressources) {
93
	public function traiterRessources(&$ressources) {
94
		if (isset($ressources) && !empty($ressources)) {
94
		if (isset($ressources) && !empty($ressources)) {
95
			$this->table = Config::get('bdd_triples');
95
			$this->table = Config::get('bdd_triples');
96
			$this->table_ressources = $ressources;
96
			$this->table_ressources = $ressources;
97
			if ($ressources[0] == 'relations') {
97
			if ($ressources[0] == 'relations') {
98
				$this->traiterRelationsProjet($ressources);
98
				$this->traiterRelationsProjet($ressources);
99
				$this->format_reponse .= '/relations';
99
				$this->format_reponse .= '/relations';
100
			} elseif (isset($this->table_ressources[0]) && !empty($this->table_ressources[0])) {
100
			} elseif (isset($this->table_ressources[0]) && !empty($this->table_ressources[0])) {
101
				$this->traiterRessourceId($this->table_ressources[0]);
101
				$this->traiterRessourceId($this->table_ressources[0]);
102
				if (isset($this->table_ressources[1]) && !empty($this->table_ressources[1])) {
102
				if (isset($this->table_ressources[1]) && !empty($this->table_ressources[1])) {
103
					$this->format_reponse .= '/champ';
103
					$this->format_reponse .= '/champ';
104
				}
104
				}
105
			} else {
105
			} else {
106
				$e = "Erreur dans votre requête. <br/> Les ressources disponibles sont : 
106
				$e = "Erreur dans votre requête. <br/> Les ressources disponibles sont : 
107
				<li> /textes/#id (id correspondant au tag de la page </li>
107
				<li> /textes/#id (id correspondant au tag de la page </li>
108
				<li> /textes/#id/#champ+#champ </li>
108
				<li> /textes/#id/#champ+#champ </li>
109
				<li> /textes/relations/bdtfx/#id (id correspondant au num_nom du projet bdtfx </li>";
109
				<li> /textes/relations/bdtfx/#id (id correspondant au num_nom du projet bdtfx </li>";
110
				$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
110
				$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
111
			}
111
			}
112
		} else {
112
		} else {
113
			$this->traiterRessourcesListe();
113
			$this->traiterRessourcesListe();
114
		}
114
		}
115
		$this->requete_condition[] = "p.latest = 'Y'";
115
		$this->requete_condition[] = "p.latest = 'Y'";
116
	}
116
	}
117
	
117
	
118
	public function traiterRessourceId($id) {
118
	public function traiterRessourceId($id) {
119
		$this->format_reponse .= '/id';
119
		$this->format_reponse .= '/id';
120
		if ($this->recherche == 'etendue') {
120
		if ($this->recherche == 'etendue') {
121
			$id = '%'.str_replace(' ','%', $id).'%';
121
			$id = '%'.str_replace(' ','%', $id).'%';
122
		} 
122
		}
-
 
123
		if (is_numeric($id)) {
-
 
124
			$this->requete_condition[] = 'c.id ='.$this->getBdd()->proteger($id);
-
 
125
		} else {
-
 
126
			$id = explode('bdtfx.nn:', $id);
-
 
127
			if (is_numeric($id[1])) {
-
 
128
				$this->requete_condition[] = 'c.num_nom ='.$this->getBdd()->proteger($id[1]);
-
 
129
			}
-
 
130
		}
123
		
131
		
124
		$this->requete_champ = 'c.id AS num_nom, i.nom_sci AS titre, t1.resource AS tag, p.body AS texte';
132
		$this->requete_champ = 'c.id AS num_nom, i.nom_sci AS titre, t1.resource AS tag, p.body AS texte';
125
		$this->table = array(Config::get("bdd_correspondance_bdnff").' c',
133
		$this->table = array(Config::get("bdd_correspondance_bdnff").' c',
126
			Config::get("bdd_triples").' t1',
134
			Config::get("bdd_triples").' t1',
127
			Config::get("bdd_index").' i',
135
			Config::get("bdd_index").' i',
128
			Config::get("bdd_pages").' p');
136
			Config::get("bdd_pages").' p');
129
		$this->requete_condition[] = 'c.num_nom ='.$this->getBdd()->proteger($id);
-
 
-
 
137
		
130
		$this->requete_condition[] = 'c.id = i.num_nom';
138
		$this->requete_condition[] = 'c.id = i.num_nom';
131
		$this->requete_condition[] = 'i.nom_sci = t1.value';
139
		$this->requete_condition[] = 'i.nom_sci = t1.value';
132
		$this->requete_condition[] = 't1.resource = p.tag';
140
		$this->requete_condition[] = 't1.resource = p.tag';
133
		
141
		
134
		/*$this->requete_champ = ' t1.resource AS tag, t1.value AS num_nom, t2.value AS titre, p.body AS texte';
142
		/*$this->requete_champ = ' t1.resource AS tag, t1.value AS num_nom, t2.value AS titre, p.body AS texte';
135
		$this->requete_condition[] = 't1.resource LIKE '.$this->getBdd()->proteger($id);
143
		$this->requete_condition[] = 't1.resource LIKE '.$this->getBdd()->proteger($id);
136
		$this->requete_condition[] = 't1.property = "num_nom"';
144
		$this->requete_condition[] = 't1.property = "num_nom"';
137
		$this->requete_condition[] = '(t1.resource = t2.resource AND t2.property = "titre")';
145
		$this->requete_condition[] = '(t1.resource = t2.resource AND t2.property = "titre")';
138
		$this->requete_condition[] = 't1.resource = p.tag';
146
		$this->requete_condition[] = 't1.resource = p.tag';
139
		$this->table = array(Config::get("bdd_triples").' t1', 
147
		$this->table = array(Config::get("bdd_triples").' t1', 
140
							Config::get("bdd_triples").' t2', 
148
							Config::get("bdd_triples").' t2', 
141
							Config::get("bdd_pages").' p');
149
							Config::get("bdd_pages").' p');
142
		$this->ajouterRechercheFloue('t1.resource', $id);*/
150
		$this->ajouterRechercheFloue('t1.resource', $id);*/
143
	}
151
	}
144
	
152
	
145
	public function traiterRessourcesListe() {
153
	public function traiterRessourcesListe() {
146
		$this->requete_condition[] = "t1.property = 'num_nom'";
154
		$this->requete_condition[] = "t1.property = 'num_nom'";
147
		$this->requete_condition[] = "(t1.resource = t2.resource AND t2.property = 'titre')";
155
		$this->requete_condition[] = "(t1.resource = t2.resource AND t2.property = 'titre')";
148
		$this->requete_condition[] = "t1.resource = p.tag";
156
		$this->requete_condition[] = "t1.resource = p.tag";
149
	}
157
	}
150
	
158
	
151
	public function traiterRelationsProjet($tab_ress) {
159
	public function traiterRelationsProjet($tab_ress) {
152
		$this->table = array(Config::get("bdd_triples")." t1", Config::get("bdd_pages")." p", Config::get("bdd_table").'_v0_1 c');
160
		$this->table = array(Config::get("bdd_triples")." t1", Config::get("bdd_pages")." p", Config::get("bdd_table").'_v0_1 c');
153
		$projet = $tab_ress[1];
161
		$projet = $tab_ress[1];
154
		$num_nom = $tab_ress[2];
162
		$num_nom = $tab_ress[2];
155
		$nom_champ_projet = "flore_$projet"."_num";
163
		$nom_champ_projet = "flore_$projet"."_num";
156
		if (isset($tab_ress[3])) {
164
		if (isset($tab_ress[3])) {
157
			$type = $tab_ress[3];
165
			$type = $tab_ress[3];
158
			if (!in_array($type, array('cle', 'dsc'))) {
166
			if (!in_array($type, array('cle', 'dsc'))) {
159
				$e = "Le type de texte ne peut etre que clé ou description";
167
				$e = "Le type de texte ne peut etre que clé ou description";
160
				$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
168
				$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
161
			}
169
			}
162
		}
170
		}
163
		$this->requete_condition[] = "c.flore_bdnff_num = ".$this->getBdd()->proteger($num_nom);
171
		$this->requete_condition[] = "c.flore_bdnff_num = ".$this->getBdd()->proteger($num_nom);
164
		$this->requete_condition[] = "t1.value = c.num_nom";
172
		$this->requete_condition[] = "t1.value = c.num_nom";
165
		$this->requete_condition[] = "t1.property = 'num_nom'";
173
		$this->requete_condition[] = "t1.property = 'num_nom'";
166
		$this->requete_condition[] = "t1.resource = p.tag";
174
		$this->requete_condition[] = "t1.resource = p.tag";
167
		$this->requete_champ = 't1.resource AS tag, t1.value AS num_nom, p.body AS texte';
175
		$this->requete_champ = 't1.resource AS tag, t1.value AS num_nom, p.body AS texte';
168
		if (isset($type)) $this->requete_condition[] = "t1.resource LIKE ".$this->getBdd()->proteger(ucfirst($type.'%'));		
176
		if (isset($type)) $this->requete_condition[] = "t1.resource LIKE ".$this->getBdd()->proteger(ucfirst($type.'%'));		
169
	}
177
	}
170
	
178
	
171
	
179
	
172
	public function ajouterRechercheFloue($nom_champ, $id) {
180
	public function ajouterRechercheFloue($nom_champ, $id) {
173
		if ($this->recherche == 'floue') {
181
		if ($this->recherche == 'floue') {
174
			$id = $this->getBdd()->proteger($id);
182
			$id = $this->getBdd()->proteger($id);
175
			$this->requete_condition[array_search("$nom_champ LIKE $id", 
183
			$this->requete_condition[array_search("$nom_champ LIKE $id", 
176
				$this->requete_condition)] = 
184
				$this->requete_condition)] = 
177
				"($nom_champ LIKE $id)".
185
				"($nom_champ LIKE $id)".
178
				" OR ( SOUNDEX($nom_champ) = SOUNDEX($id))".
186
				" OR ( SOUNDEX($nom_champ) = SOUNDEX($id))".
179
				" OR SOUNDEX(REVERSE($nom_champ)) = SOUNDEX(REVERSE($id))))";
187
				" OR SOUNDEX(REVERSE($nom_champ)) = SOUNDEX(REVERSE($id))))";
180
		}
188
		}
181
	}
189
	}
182
	
190
	
183
//---------------------------------------TRAITER PARAMETRES-------------------------------------------------------------
191
//---------------------------------------TRAITER PARAMETRES-------------------------------------------------------------
184
	
192
	
185
	public function traiterParametres($parametres) {
193
	public function traiterParametres($parametres) {
186
		if (isset($parametres) && !empty($parametres)) {
194
		if (isset($parametres) && !empty($parametres)) {
187
			$this->table_param = $parametres;
195
			$this->table_param = $parametres;
188
			if (isset($parametres['recherche']) && $parametres['recherche'] != '') {
196
			if (isset($parametres['recherche']) && $parametres['recherche'] != '') {
189
				$this->recherche = $parametres['recherche'];
197
				$this->recherche = $parametres['recherche'];
190
			}
198
			}
191
			foreach ($parametres as $param => $val) {
199
			foreach ($parametres as $param => $val) {
192
				switch ($param) {
200
				switch ($param) {
193
					case 'retour.format'	: $this->retour_format = $val;										break;
201
					case 'retour.format'	: $this->retour_format = $val;										break;
194
					case 'masque.titre' 	: $this->ajouterLeFiltreMasque('titre', $val);						break;
202
					case 'masque.titre' 	: $this->ajouterLeFiltreMasque('titre', $val);						break;
195
					case 'masque.famille'	: $this->ajouterLeFiltreMasque('famille', $val);					break;
203
					case 'masque.famille'	: $this->ajouterLeFiltreMasque('famille', $val);					break;
196
					case 'masque.ns'		: $this->ajouterLeFiltreMasque('nom_sci', $val);					break;
204
					case 'masque.ns'		: $this->ajouterLeFiltreMasque('nom_sci', $val);					break;
197
					case 'masque.type'		: $this->ajouterLeFiltreMasque('type', $val);						break;
205
					case 'masque.type'		: $this->ajouterLeFiltreMasque('type', $val);						break;
198
					case 'masque.txt'		: $this->ajouterLeFiltreMasque('texte', $val);						break;
206
					case 'masque.txt'		: $this->ajouterLeFiltreMasque('texte', $val);						break;
199
					case 'masque.page'		: $this->ajouterLeFiltreMasque('page',$val);						break;
207
					case 'masque.page'		: $this->ajouterLeFiltreMasque('page',$val);						break;
200
					case 'masque.tome'		: $this->ajouterLeFiltreMasque('tome',$val);						break;
208
					case 'masque.tome'		: $this->ajouterLeFiltreMasque('tome',$val);						break;
201
					case 'navigation.depart': $this->limite_requete['depart'] = $val;							break;
209
					case 'navigation.depart': $this->limite_requete['depart'] = $val;							break;
202
					case 'navigation.limite': $this->limite_requete['limite'] = $val;							break;
210
					case 'navigation.limite': $this->limite_requete['limite'] = $val;							break;
203
					case 'recherche'		: 																	break;
211
					case 'recherche'		: 																	break;
204
					default					: 
212
					default					: 
205
						$e = "Erreur dans les paramètres de recherche de votre requête : </br> Le parametre	$param n\'existe pas.";
213
						$e = "Erreur dans les paramètres de recherche de votre requête : </br> Le parametre	$param n\'existe pas.";
206
						$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE,$e);
214
						$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE,$e);
207
						break;
215
						break;
208
				}
216
				}
209
			}
217
			}
210
		}
218
		}
211
	}
219
	}
212
	
220
	
213
	
221
	
214
	public function ajouterLeFiltreMasque($masque, $valeur) {
222
	public function ajouterLeFiltreMasque($masque, $valeur) {
215
		if ($this->recherche == 'etendue') {
223
		if ($this->recherche == 'etendue') {
216
			$valeur = '%'.str_replace(' ','%', $valeur).'%';
224
			$valeur = '%'.str_replace(' ','%', $valeur).'%';
217
		}
225
		}
218
		$valeur = $this->getBdd()->proteger($valeur);
226
		$valeur = $this->getBdd()->proteger($valeur);
219
		$this->masque[] = "$masque=$valeur";
227
		$this->masque[] = "$masque=$valeur";
220
		switch ($masque) {
228
		switch ($masque) {
221
			case 'famille' : 
229
			case 'famille' : 
222
				break;
230
				break;
223
			case 'nom_sci' : 
231
			case 'nom_sci' : 
224
				$this->requete_condition[] = "t1.value = c.num_nom AND nom_sci LIKE $valeur)";
232
				$this->requete_condition[] = "t1.value = c.num_nom AND nom_sci LIKE $valeur)";
225
				$this->ajouterRechercheFloue('nom_sci', $valeur);
233
				$this->ajouterRechercheFloue('nom_sci', $valeur);
226
				$this->table[] = Config::get("bdd_table").'_v0_1 c';
234
				$this->table[] = Config::get("bdd_table").'_v0_1 c';
227
				break;
235
				break;
228
			case 'type'    : 
236
			case 'type'    : 
229
				$type = ($this->getBdd()->proteger($valeur) == '"cle"') ? 'Cle%' : 'Dsc%';
237
				$type = ($this->getBdd()->proteger($valeur) == '"cle"') ? 'Cle%' : 'Dsc%';
230
				$this->requete_condition[] = 't1.resource LIKE '.$this->getBdd()->proteger($type);
238
				$this->requete_condition[] = 't1.resource LIKE '.$this->getBdd()->proteger($type);
231
				break;
239
				break;
232
			case 'texte'   : 
240
			case 'texte'   : 
233
				$this->requete_condition[] = "p.body LIKE $valeur";
241
				$this->requete_condition[] = "p.body LIKE $valeur";
234
				$this->ajouterRechercheFloue('p.body', $valeur);
242
				$this->ajouterRechercheFloue('p.body', $valeur);
235
				break;
243
				break;
236
			case 'titre'   : 
244
			case 'titre'   : 
237
				$this->requete_condition[] = "t2.value LIKE $valeur";
245
				$this->requete_condition[] = "t2.value LIKE $valeur";
238
				$this->ajouterRechercheFloue('t2.value', $valeur);
246
				$this->ajouterRechercheFloue('t2.value', $valeur);
239
				break;
247
				break;
240
			case 'page'	   : 
248
			case 'page'	   : 
241
				$this->requete_condition[] =  "t1.value = c.num_nom AND c.page = $valeur";
249
				$this->requete_condition[] =  "t1.value = c.num_nom AND c.page = $valeur";
242
				$this->table[] = Config::get("bdd_table").'_v0_1 c';
250
				$this->table[] = Config::get("bdd_table").'_v0_1 c';
243
				break;
251
				break;
244
			case 'tome'	   : 
252
			case 'tome'	   : 
245
				$this->requete_condition[] =  "t1.value = c.num_nom AND c.tome = $valeur";
253
				$this->requete_condition[] =  "t1.value = c.num_nom AND c.tome = $valeur";
246
				$this->table[] = Config::get("bdd_table").'_v0_1 c';
254
				$this->table[] = Config::get("bdd_table").'_v0_1 c';
247
				break;
255
				break;
248
			default 		: 	
256
			default 		: 	
249
				break;
257
				break;
250
		}
258
		}
251
	}
259
	}
252
	
260
	
253
	
261
	
254
//-----------------------------FONCTIONS DASSEMBLAGE DE LA REQUETE-----------------------------------------------------
262
//-----------------------------FONCTIONS DASSEMBLAGE DE LA REQUETE-----------------------------------------------------
255
	
263
	
256
	public function assemblerLaRequete() {
264
	public function assemblerLaRequete() {
257
		$requete = 	' SELECT '.$this->requete_champ.
265
		$requete = 	' SELECT '.$this->requete_champ.
258
					' FROM '.implode(', ', $this->table)
266
					' FROM '.implode(', ', $this->table)
259
					.$this->retourneRequeteCondition()
267
					.$this->retourneRequeteCondition()
260
					.$this->requete_group_by
268
					.$this->requete_group_by
261
					.$this->formerRequeteLimite();
269
					.$this->formerRequeteLimite();
262
		return $requete;
270
		return $requete;
263
	}
271
	}
264
 
272
 
265
	
273
	
266
	public  function retourneRequeteCondition() {
274
	public  function retourneRequeteCondition() {
267
		$condition = '';
275
		$condition = '';
268
		if ($this->requete_condition !== null) { 
276
		if ($this->requete_condition !== null) { 
269
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
277
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
270
		} 
278
		} 
271
		return $condition;
279
		return $condition;
272
	}
280
	}
273
	
281
	
274
	public function formerRequeteLimite() {
282
	public function formerRequeteLimite() {
275
		if (in_array($this->format_reponse , array('textes/id', 'textes/id/relations'))) {
283
		if (in_array($this->format_reponse , array('textes/id', 'textes/id/relations'))) {
276
			$this->requete_limite = ''; 
284
			$this->requete_limite = ''; 
277
		} elseif (($depart = $this->limite_requete['depart']) > ($this->total_resultat = $this->recupererTotalResultat())) {
285
		} elseif (($depart = $this->limite_requete['depart']) > ($this->total_resultat = $this->recupererTotalResultat())) {
278
			$this->limite_requete['depart'] = 
286
			$this->limite_requete['depart'] = 
279
				(($this->total_resultat - $this->limite_requete['limite']) < 0) ? 0 : ($this->total_resultat - $this->limite_requete['limite']);
287
				(($this->total_resultat - $this->limite_requete['limite']) < 0) ? 0 : ($this->total_resultat - $this->limite_requete['limite']);
280
			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite']; 
288
			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite']; 
281
		} else {
289
		} else {
282
			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite']; 
290
			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite']; 
283
		}
291
		}
284
		return $this->requete_limite;
292
		return $this->requete_limite;
285
	}
293
	}
286
	
294
	
287
	
295
	
288
	/**
296
	/**
289
	 * Recupere le nombre total de résultat d'une requete lancée. 
297
	 * Recupere le nombre total de résultat d'une requete lancée. 
290
	 * Est utilisée lors de l'affichage du total dans le résultat retourné et pr déterminer les limites et départ
298
	 * Est utilisée lors de l'affichage du total dans le résultat retourné et pr déterminer les limites et départ
291
	 * @return nombre total de résultat
299
	 * @return nombre total de résultat
292
	 */
300
	 */
293
	public function recupererTotalResultat() {
301
	public function recupererTotalResultat() {
294
		$total = null;
302
		$total = null;
295
		$requete = 'SELECT count(*) as nombre FROM '
303
		$requete = 'SELECT count(*) as nombre FROM '
296
					.implode(', ', $this->table).$this->retourneRequeteCondition()
304
					.implode(', ', $this->table).$this->retourneRequeteCondition()
297
					.$this->requete_group_by;
305
					.$this->requete_group_by;
298
		$res = $this->getBdd()->recuperer($requete);
306
		$res = $this->getBdd()->recuperer($requete);
299
		if ($res) {
307
		if ($res) {
300
			$total = $res['nombre'];
308
			$total = $res['nombre'];
301
		} else {
309
		} else {
302
			$e = 'Données introuvables dans la base ou erreur dans la requête SQL';
310
			$e = 'Données introuvables dans la base ou erreur dans la requête SQL';
303
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
311
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
304
		}
312
		}
305
		return $total;
313
		return $total;
306
	}
314
	}
307
	
315
	
308
 
316
 
309
	
317
	
310
//-------------------FONCTIONS POUR LE FORMATAGE EN JSON----------------------------------------------------------------
318
//-------------------FONCTIONS POUR LE FORMATAGE EN JSON----------------------------------------------------------------
311
	
319
	
312
	public function retournerResultatFormate($resultat) {
320
	public function retournerResultatFormate($resultat) {
313
		$reponse = '';
321
		$reponse = '';
314
		switch ($this->format_reponse) {
322
		switch ($this->format_reponse) {
315
			case 'textes' 				: $reponse = $this->formaterTextes($resultat);  		break;
323
			case 'textes' 				: $reponse = $this->formaterTextes($resultat);  		break;
316
			case 'textes/relations'		: $reponse = $this->formaterTextesRelations($resultat); break;
324
			case 'textes/relations'		: $reponse = $this->formaterTextesRelations($resultat); break;
317
			case 'textes/id' 			: $reponse = $this->formaterTextesId($resultat[0]); 	 break;
325
			case 'textes/id' 			: $reponse = $this->formaterTextesId($resultat[0]); 	 break;
318
			case 'textes/id/champ' 		: $reponse = $this->formaterTextesIdChamp($resultat[0]); break;
326
			case 'textes/id/champ' 		: $reponse = $this->formaterTextesIdChamp($resultat[0]); break;
319
			default : break;
327
			default : break;
320
		}
328
		}
321
		return $reponse;
329
		return $reponse;
322
	}
330
	}
323
	
331
	
324
	
332
	
325
	public function formaterTextes($resultat) {
333
	public function formaterTextes($resultat) {
326
		$this->ajouterEnteteResultat($resultat);
334
		$this->ajouterEnteteResultat($resultat);
327
		$reponse['entete'] = $this->table_retour;
335
		$reponse['entete'] = $this->table_retour;
328
		$this->table_retour = array();
336
		$this->table_retour = array();
329
		foreach ($resultat as $description) {
337
		foreach ($resultat as $description) {
330
			$tag = $description['tag'];
338
			$tag = $description['tag'];
331
			$this->afficherDonnees('titre', $description['titre']);
339
			$this->afficherDonnees('titre', $description['titre']);
332
			$type = (preg_match('/^Cle.*$/', $tag)) ? '1' : '2' ;
340
			$type = (preg_match('/^Cle.*$/', $tag)) ? '1' : '2' ;
333
			$this->afficherDonnees('type', $type);
341
			$this->afficherDonnees('type', $type);
334
			$this->table_retour['texte'] = $description['texte']; 
342
			$this->table_retour['texte'] = $description['texte']; 
335
			$reponse['resultat'][$tag] = $this->table_retour;
343
			$reponse['resultat'][$tag] = $this->table_retour;
336
			$this->table_retour = array();
344
			$this->table_retour = array();
337
			$reponse['resultat'][$tag]['href'] = $this->ajouterHref('textes', $tag);
345
			$reponse['resultat'][$tag]['href'] = $this->ajouterHref('textes', $tag);
338
		}
346
		}
339
		return $reponse;
347
		return $reponse;
340
	}
348
	}
341
	
349
	
342
	public function formaterTextesRelations($resultat) {
350
	public function formaterTextesRelations($resultat) {
343
		foreach ($resultat as $description) {
351
		foreach ($resultat as $description) {
344
			$tag = $description['tag'];
352
			$tag = $description['tag'];
345
			$type = (preg_match('/^Cle.*$/', $tag)) ? '1' : '2' ;
353
			$type = (preg_match('/^Cle.*$/', $tag)) ? '1' : '2' ;
346
			$this->afficherDonnees('type', $type);
354
			$this->afficherDonnees('type', $type);
347
			$this->table_retour['texte'] = $description['texte']; 
355
			$this->table_retour['texte'] = $description['texte']; 
348
			$reponse[$tag] = $this->table_retour;
356
			$reponse[$tag] = $this->table_retour;
349
			$this->table_retour = array();
357
			$this->table_retour = array();
350
			$reponse[$tag]['href'] = $this->ajouterHref('textes', $tag);
358
			$reponse[$tag]['href'] = $this->ajouterHref('textes', $tag);
351
		}
359
		}
352
		return $reponse;
360
		return $reponse;
353
	}
361
	}
354
	
362
	
355
	public function ajouterEnteteResultat($resultat) {
363
	public function ajouterEnteteResultat($resultat) {
356
		if ($this->masque) {
364
		if ($this->masque) {
357
			$this->table_retour['masque'] = implode('&', $this->masque);
365
			$this->table_retour['masque'] = implode('&', $this->masque);
358
		}
366
		}
359
		$this->table_retour['depart'] = $this->limite_requete['depart'];
367
		$this->table_retour['depart'] = $this->limite_requete['depart'];
360
		$this->table_retour['limite'] = $this->limite_requete['limite'];
368
		$this->table_retour['limite'] = $this->limite_requete['limite'];
361
		$this->table_retour['total']  = $this->total_resultat;
369
		$this->table_retour['total']  = $this->total_resultat;
362
		//formuler les urls
370
		//formuler les urls
363
		$url = $this->formulerUrl($this->total_resultat, '/'.$this->service);
371
		$url = $this->formulerUrl($this->total_resultat, '/'.$this->service);
364
		if ($url['precedent'] != '') { $this->table_retour['href.precedent'] = $url['precedent']; }
372
		if ($url['precedent'] != '') { $this->table_retour['href.precedent'] = $url['precedent']; }
365
		if ($url['suivant']   != '') { $this->table_retour['href.suivant']   = $url['suivant']; }
373
		if ($url['suivant']   != '') { $this->table_retour['href.suivant']   = $url['suivant']; }
366
	}
374
	}
367
	
375
	
368
	
376
	
369
	public function formaterTextesId($reponse) {
377
	public function formaterTextesId($reponse) {
370
	
378
	
371
		$this->afficherDonnees('titre', $reponse['titre']);
379
		$this->afficherDonnees('titre', $reponse['titre']);
372
		$type = (preg_match('/^Cle.*$/', $reponse['tag'])) ? '2' : '1' ;
380
		$type = (preg_match('/^Cle.*$/', $reponse['tag'])) ? '2' : '1' ;
373
		$this->afficherDonnees('type', $type);
381
		$this->afficherDonnees('type', $type);
374
		$this->table_retour['texte'] = $reponse['texte']; 
382
		$this->table_retour['texte'] = $reponse['texte']; 
375
		$this->afficherDonnees('id', $reponse['num_nom']);
383
		//$this->afficherDonnees('id', $reponse['num_nom']);
376
		
384
		
377
		return $this->table_retour;
385
		return $this->table_retour;
378
	}
386
	}
379
	
387
	
380
	
388
	
381
	public function formaterTextesIdChamp($resultat) {
389
	public function formaterTextesIdChamp($resultat) {
382
		//on recupère tous les resultats possibles
390
		//on recupère tous les resultats possibles
383
		$reponse_id = $this->formaterTextesId($resultat);
391
		$reponse_id = $this->formaterTextesId($resultat);
384
		$this->table_retour = array();
392
		$this->table_retour = array();
385
		//on recupère les résultats demandés à partir du tableau de résultat complet
393
		//on recupère les résultats demandés à partir du tableau de résultat complet
386
		$this->table_retour['id'] = $resultat['tag'];
394
		$this->table_retour['id'] = $resultat['tag'];
387
		$champs = explode(' ', $this->table_ressources[1]);
395
		$champs = explode(' ', $this->table_ressources[1]);
388
		foreach ($champs as $champ) {
396
		foreach ($champs as $champ) {
389
			if ($this->verifierValiditeChamp($champ, $reponse_id)) {
397
			if ($this->verifierValiditeChamp($champ, $reponse_id)) {
390
				if (preg_match('/^[^.]+\.\*$/', $champ)) {
398
				if (preg_match('/^[^.]+\.\*$/', $champ)) {
391
					$this->afficherPointEtoile($champ, $reponse_id);
399
					$this->afficherPointEtoile($champ, $reponse_id);
392
				} else {
400
				} else {
393
					$this->table_retour[$champ] = $reponse_id[$champ];
401
					$this->table_retour[$champ] = $reponse_id[$champ];
394
				}
402
				}
395
			} 
403
			} 
396
		}
404
		}
397
		return $this->table_retour;
405
		return $this->table_retour;
398
	}
406
	}
399
	
407
	
400
	
408
	
401
	public function afficherPointEtoile($champ, $reponse_id) {
409
	public function afficherPointEtoile($champ, $reponse_id) {
402
		preg_match('/^([^.]+\.)\*$/', $champ, $match);
410
		preg_match('/^([^.]+\.)\*$/', $champ, $match);
403
		foreach ($reponse_id as $chp => $valeur) {
411
		foreach ($reponse_id as $chp => $valeur) {
404
			if (strrpos($chp, $match[1]) !== false) {
412
			if (strrpos($chp, $match[1]) !== false) {
405
				$this->table_retour[$chp] = $valeur;
413
				$this->table_retour[$chp] = $valeur;
406
			}
414
			}
407
		}
415
		}
408
	}
416
	}
409
	
417
	
410
	
418
	
411
	public function verifierValiditeChamp($champ, $reponse_id) {
419
	public function verifierValiditeChamp($champ, $reponse_id) {
412
		$validite = false;
420
		$validite = false;
413
		preg_match('/^([^.]+)(:?\.([^.]+))?$/', $champ, $match);
421
		preg_match('/^([^.]+)(:?\.([^.]+))?$/', $champ, $match);
414
		if (array_key_exists($match[1], $reponse_id)) {
422
		if (array_key_exists($match[1], $reponse_id)) {
415
			$validite = true;
423
			$validite = true;
416
			if (isset($match[2]) && !empty($match[2])) {
424
			if (isset($match[2]) && !empty($match[2])) {
417
				if ($match[1] == 'type') {
425
				if ($match[1] == 'type') {
418
					$suffixes = array('.code', '.href', '.*');
426
					$suffixes = array('.code', '.href', '.*');
419
				$validite = (in_array($match[2], $suffixes)) ? true : false;
427
				$validite = (in_array($match[2], $suffixes)) ? true : false;
420
				} else {
428
				} else {
421
					$validite = false;
429
					$validite = false;
422
				}
430
				}
423
			}
431
			}
424
		} 
432
		} 
425
		if (!$validite) {
433
		if (!$validite) {
426
			$champs = implode('</li><li>', array_keys($reponse_id));
434
			$champs = implode('</li><li>', array_keys($reponse_id));
427
			$e = 'Erreur dans votre requête : </br> Le champ "'.$champ.'" n\'existe pas. Les champs disponibles
435
			$e = 'Erreur dans votre requête : </br> Le champ "'.$champ.'" n\'existe pas. Les champs disponibles
428
				 sont : <li>'.$champs.'</li>';
436
				 sont : <li>'.$champs.'</li>';
429
			$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
437
			$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
430
		}
438
		}
431
		return $validite;
439
		return $validite;
432
	}
440
	}
433
	
441
	
434
	
442
	
435
//------------------------------------------------Fonction d'affichage ------------------------------------------------
443
//------------------------------------------------Fonction d'affichage ------------------------------------------------
436
	
444
	
437
	public function afficherDonnees($champ, $valeur) {
445
	public function afficherDonnees($champ, $valeur) {
438
		if ($this->retour_format == 'min') {
446
		if ($this->retour_format == 'min') {
439
			$champ = ($champ == 'type') ? $champ.'.code' : $champ;
447
			$champ = ($champ == 'type') ? $champ.'.code' : $champ;
440
			$this->table_retour[$champ] = $valeur;
448
			$this->table_retour[$champ] = $valeur;
441
		} else {
449
		} else {
442
			$this->afficherDonneesMax($champ, $valeur);
450
			$this->afficherDonneesMax($champ, $valeur);
443
		}
451
		}
444
	}
452
	}
445
	
453
	
446
	
454
	
447
	public function afficherDonneesMax($champ, $valeur) {
455
	public function afficherDonneesMax($champ, $valeur) {
448
		if ($champ == 'id') {
456
		if ($champ == 'id') {
449
			$this->table_retour[$champ] = "coste.".$valeur;
457
			$this->table_retour[$champ] = "coste.".$valeur;
450
			$this->table_retour['nom_sci'] = $this->recupererNomSci($valeur);
458
			$this->table_retour['nom_sci'] = $this->recupererNomSci($valeur);
451
			$this->table_retour['id.href'] = $this->ajouterHref('noms', $valeur);
459
			$this->table_retour['id.href'] = $this->ajouterHref('noms', $valeur);
452
		} elseif (in_array($champ, array('type'))) {
460
		} elseif (in_array($champ, array('type'))) {
453
			$this->table_retour[$champ.'.code'] = $valeur;
461
			$this->table_retour[$champ.'.code'] = $valeur;
454
			$this->table_retour[$champ] = $this->recupererSignificationCode($valeur);
462
			$this->table_retour[$champ] = $this->recupererSignificationCode($valeur);
455
			$this->table_retour[$champ.'.href'] = $this->ajouterHrefAutreProjet('ontologies', 'texteType:', $valeur, 'commun');
463
			$this->table_retour[$champ.'.href'] = $this->ajouterHrefAutreProjet('ontologies', 'texteType:', $valeur, 'eflore');
456
		} else {
464
		} else {
457
			$this->table_retour[$champ] = $valeur;
465
			$this->table_retour[$champ] = $valeur;
458
		}
466
		}
459
	}
467
	}
460
	
468
	
461
	
469
	
462
	public function recupererSignificationCode($code) {
470
	public function recupererSignificationCode($code) {
463
		$url = $this->ajouterHrefAutreProjet('ontologies', 'texteType:', $code.'/nom', 'commun');
471
		$url = $this->ajouterHrefAutreProjet('ontologies', 'texteType:', $code.'/nom', 'eflore');
464
		$res = $this->consulterHref($url);
472
		$res = $this->consulterHref($url);
465
		return $res->nom;
473
		return $res->nom;
466
	}	
474
	}	
467
	
475
	
468
	
476
	
469
	public function recupererNomSci($id) {
477
	public function recupererNomSci($id) {
470
		$req = 'SELECT nom_sci FROM coste_v0_1 WHERE num_nom = '.$id;
478
		$req = 'SELECT nom_sci FROM coste_v0_1 WHERE num_nom = '.$id;
471
		if ($this->html == 'htm') { 
479
		if ($this->html == 'htm') { 
472
			$req = str_replace('nom_sci', 'nom_sci_html as nom_sci', $req);
480
			$req = str_replace('nom_sci', 'nom_sci_html as nom_sci', $req);
473
		}
481
		}
474
		$res = $this->getBdd()->recuperer($req);
482
		$res = $this->getBdd()->recuperer($req);
475
		return $res['nom_sci'];
483
		return $res['nom_sci'];
476
	}
484
	}
477
 
485
 
478
}
486
}
479
 
487
 
480
?>
488
?>