Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 909 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
452 mathilde 1
<?php
2
 
3
/**
4
* Classe InformationsBaseflor.php permet de faire des requetes de baseflor en fonction d'un référentiel
499 jpm 5
*( BDTFX, BDAFX, BDBFX ) et d'un numéro nomenclatural ( différent de 0 ).
452 mathilde 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?champs=description --> retourne champs description pour un BDNT et un num_nomen
9
*  /informations/#bdnt.nn:#num_nomen?champs=ecologie --> retourne champs ecologiques pour un BDNT et un num_nomen
580 mathilde 10
*   /informations/#bdnt.nn:#num_nomen/relations/catminat --> donne les num nomen et les bdnt pour tous
11
*   les taxons qui ont le même code catminat ( code milieu ) que celui entré
452 mathilde 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
585 mathilde 22
* @copyright 1999-2012 Tela Botanica (accueil@tela-botanica.org)
452 mathilde 23
*/
24
 
25
class InformationsBaseflor extends Commun{
499 jpm 26
 
452 mathilde 27
	protected $table = "";
28
	private $format_reponse = 'informations';
29
	private $retour_format = 'max';
30
	private $champs_ontologiques = array();
31
	protected $serviceNom = 'informations';
525 mathilde 32
	private $Bdd;
548 mathilde 33
	private $requete_condition = "";
34
	private $champs_recherches = '*';
35
	private $resultat_json;
579 mathilde 36
	private $nb_ressources;
37
	protected $limite_requete = array( 'depart' => 0, 'limite' => 10);
525 mathilde 38
 
499 jpm 39
 
452 mathilde 40
	public function consulter($ressources, $parametres) {
41
		$this->ressources = $ressources;
42
		$this->parametres = $parametres;
579 mathilde 43
		$this->nb_ressources = count($ressources);
585 mathilde 44
 
452 mathilde 45
		$this->traiterParametres();
548 mathilde 46
		$this->definirTables();
518 mathilde 47
		$this->traiterRessources();
452 mathilde 48
		$resultats = null;
499 jpm 49
		foreach ($this->table_version as $version) {
548 mathilde 50
			$this->table = $version;
585 mathilde 51
			$requete = $this->assemblerLaRequete();
525 mathilde 52
			$resultat =  $this->Bdd->recupererTous($requete);
53
			$versionResultat = $this->analyserResultat($resultat);
452 mathilde 54
			if (count($this->table_version) > 1) {
55
				$resultats[$version] = $versionResultat;
56
			} else {
57
				$resultats = $versionResultat;
58
			}
59
		}
499 jpm 60
		return $resultats;
452 mathilde 61
	}
499 jpm 62
 
525 mathilde 63
	public function __construct(Conteneur $Conteneur) {
64
		$this->Bdd = $Conteneur->getBdd();
65
	}
66
 
548 mathilde 67
 
525 mathilde 68
	public function analyserResultat($resultat) {
452 mathilde 69
		$versionResultat = null;
70
		if ($resultat == '') {
71
			$message = 'La requête SQL formée comporte une erreur!';
72
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
73
			throw new Exception($message, $code);
74
		} elseif ($resultat) {
525 mathilde 75
			$versionResultat = $this->retournerResultatFormate($resultat);
499 jpm 76
		}
452 mathilde 77
		return $versionResultat;
78
	}
499 jpm 79
 
80
//+--------------------------traitement ressources ou paramètres  -------------------------------------------+
452 mathilde 81
	public function traiterParametres() {
82
		if (isset($this->parametres) && !empty($this->parametres)) {
83
			foreach ($this->parametres as $param => $valeur) {
84
				switch ($param) {
516 mathilde 85
					case 'categorie'  :
761 raphael 86
						if($this->nb_ressources != 1 ) {
87
							$e = "Nombre de ressource != 1 pour informations/#id/relations/catminat";
88
							throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
89
						}
90
 
91
						if ($valeur == "description") {
548 mathilde 92
							$this->champs_recherches = "num_nomen, BDNT, chorologie, inflorescence,"
499 jpm 93
								." sexualite, ordre_maturation, "
94
								."pollinisation, fruit, dissemination, couleur_fleur, macule, floraison, type_bio, "
95
						 		."form_vegetale ";
548 mathilde 96
							$this->requete_condition[] = "( chorologie != '' OR inflorescence != '' ".
499 jpm 97
								"OR  sexualite != '' OR ordre_maturation != '' OR pollinisation != '' ".
98
								"OR fruit != '' OR dissemination != '' OR couleur_fleur != '' OR macule != '' ".
99
								"OR floraison != '' OR type_bio != '' OR form_vegetale != '' )";
761 raphael 100
						} elseif ($valeur == "ecologie") {
548 mathilde 101
							$this->champs_recherches = " num_nomen, BDNT, ve_lumiere , ve_temperature,"
499 jpm 102
								." ve_continentalite, ve_humidite_atmos, ve_humidite_edaph, ve_reaction_sol,"
517 mathilde 103
								." ve_nutriments_sol, ve_salinite, ve_texture_sol, ve_mat_org_sol, "
104
								." vi_lumiere, vi_temperature, vi_continentalite, vi_humidite_edaph,"
105
								." vi_reaction_sol, vi_nutriments_sol, vi_salinite ";
548 mathilde 106
							$this->requete_condition[] = "( ve_lumiere  != '' OR ve_temperature != '' ".
466 mathilde 107
								"OR ve_continentalite != '' OR ve_humidite_atmos != '' ".
108
								"OR ve_humidite_edaph != '' OR ve_reaction_sol != '' ".
109
								"OR ve_nutriments_sol != '' OR ve_salinite != '' ".
499 jpm 110
								"OR ve_texture_sol != '' OR ve_mat_org_sol != '' )";
111
						} else {
579 mathilde 112
							$e = "Valeur de paramètre inconnue pour 'categorie'. Ce paramètre n'est pas autorisé pour informations/#id/relations/catminat";
525 mathilde 113
							throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
452 mathilde 114
						}
115
						break;
116
					case 'retour.format'  :
579 mathilde 117
						if ($valeur == 'min' || $valeur == 'max' && $this->nb_ressources == 1) {
517 mathilde 118
							$this->retour_format = $valeur;
579 mathilde 119
 
517 mathilde 120
						} else {
579 mathilde 121
							$e = "Valeur de paramètre inconnue pour 'retour.format'. Ce paramètre n'est pas autorisé pour informations/#id/relations/catminat";
525 mathilde 122
							throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
517 mathilde 123
						}
579 mathilde 124
						break;
125
					case 'navigation.depart'  :
126
						$this->definirNavigationDepart($valeur);
127
						break;
128
					case 'navigation.limite'  :
129
						$this->definirNavigationLimite($valeur);
130
						break;
518 mathilde 131
					case 'version.projet' :
132
						$this->traiterVersion($valeur);
133
						break;
452 mathilde 134
					default :
135
						$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
136
							.$param.' " n\'existe pas.';
525 mathilde 137
						throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
138
						break;
452 mathilde 139
				}
140
			}
141
		}
142
	}
517 mathilde 143
 
144
 
452 mathilde 145
 
146
	public function traiterRessources() {
525 mathilde 147
			if(preg_match('/^(.+)\.nn:([0-9]+)$/', $this->ressources[0], $retour)){
579 mathilde 148
				$id = $retour[2];
149
				$ref = $retour[1];
150
				if ($ref == 'bdtfx' || $ref == 'bdafx' || $ref == 'bdbfx') {
151
					if($this->nb_ressources == 1) {
152
						$this->requete_condition[] = "num_nomen = ".$id." AND BDNT = '".strtoupper($ref)."' ";
153
					} else {
154
						$this->definirParametresRequeteRelations($ref, $id);
155
					}
156
				} else {
157
					$e = 'Erreur dans l\'url de votre requête : </br> Le référentiel " '
158
					.$ref.' " n\'existe pas.';
159
					throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
499 jpm 160
				}
161
 
516 mathilde 162
			} else {
163
				$e = 'Erreur dans l\'url de votre requête : </br> La ressource demandée n\'existe pas.';
525 mathilde 164
				throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
585 mathilde 165
 
452 mathilde 166
			}
579 mathilde 167
 
452 mathilde 168
	}
518 mathilde 169
 
579 mathilde 170
	function definirParametresRequeteRelations($ref, $id){
171
		if ($this->ressources[1] == 'relations'){
172
			switch ($this->ressources[2]) {
173
				case 'catminat' :
174
					$this->requete_condition[] = "catminat_code = ( SELECT catminat_code from ".$this->table_version[0]." WHERE "
592 mathilde 175
					." num_nomen = ".$id." AND BDNT = '".$ref. "' ) AND num_nomen != ".$id." ";
944 raphael 176
					$this->champs_recherches = " catminat_code, num_nomen, BDNT, nom_sci ";
579 mathilde 177
					break;
178
				default :
179
					$e = 'Erreur dans l\'url de votre requête : </br> Le type de relation " '
180
				.$this->ressources[2].' " n\'existe pas.';
181
				throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
182
				break;
183
			}
184
		} else {
185
			$e = 'Erreur dans l\'url de votre requête : </br> Le type de ressources " '
186
			.$this->ressources[1].' " n\'existe pas.';
187
			throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
188
		}
189
	}
190
 
191
 
518 mathilde 192
	//+++------------------------------traitement des versions----------------------------------------++
193
 
194
	public function traiterVersion($valeur) {
579 mathilde 195
		if (preg_match('/^[0-9]+(?:[._][0-9]+)*$/', $valeur) || $valeur == '+') {
518 mathilde 196
			$this->version_projet = $valeur;
579 mathilde 197
		} elseif ($valeur == '*') {
198
			$this->version_projet = ($this->nb_ressources == 1) ? $valeur : '+' ;
518 mathilde 199
		} else {
200
			$e = "Erreur : La version est inconnue.";
525 mathilde 201
			throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
518 mathilde 202
		}
525 mathilde 203
 
518 mathilde 204
	}
205
 
548 mathilde 206
	public function definirTables() {
518 mathilde 207
		$table_num_version = $this->recupererVersionDisponible();
208
		$prefixe_table = config::get('bdd_table');
209
		if ( in_array($this->version_projet,$table_num_version) ) {
210
			$this->table_version[] = $prefixe_table.'_v'.$this->version_projet;
211
		} elseif ($this->version_projet == '+') {
212
					$derniere_version = $table_num_version[count($table_num_version) - 1];
213
					$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $derniere_version);
214
		} elseif ($this->version_projet == '*') {
215
			foreach ($table_num_version as $num_version) {
216
				$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $num_version);
217
			}
218
		} else {
219
			$e = "Erreur : La version est inconnue.";
525 mathilde 220
			throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
518 mathilde 221
		}
222
	}
223
 
452 mathilde 224
//+--------------------------formatages de resultats  -------------------------------------------+
525 mathilde 225
 
452 mathilde 226
	public function retournerResultatFormate($resultat) {
499 jpm 227
		$resultat_json = $resultat[0];
228
		if ($this->retour_format == 'max'){
579 mathilde 229
			if ($this->nb_ressources == 1) {
230
				if ($this->parametres['categorie'] == 'ecologie') {
231
					$this->formaterResultatsChampsEcologie($resultat_json);
232
				} elseif ($this->parametres['categorie'] == 'description') {
233
					$this->formaterResultatsChampsDescription($resultat_json);
234
				}else {
235
					$this->formaterResultatsTousChamps($resultat_json);
236
				}
237
			} else {
238
				$this->formaterResultatsRelations($resultat);
499 jpm 239
			}
553 mathilde 240
		} else {
241
			$this->resultat_json = $resultat_json;
452 mathilde 242
		}
548 mathilde 243
		return $this->resultat_json;
452 mathilde 244
	}
579 mathilde 245
 
246
	public function formaterResultatsRelations($resultat) {
247
		$this->resultat_json['entete'] = $this->ajouterEnteteResultat();
248
		$resultat_json = array();
249
		foreach ($resultat as $cle => $res){
250
			$resultat_json[$cle] = $res ;
251
			$resultat_json[$cle]['href'] = $this->ajouterHref('informations',strtolower($res['BDNT']).".nn:".$res['num_nomen']);
252
		}
253
		$this->resultat_json['resultat'] = $resultat_json;
254
	}
255
 
647 aurelien 256
	public function ajouterEnteteResultat() {
579 mathilde 257
		$entete['depart'] = $this->limite_requete['depart'];
258
		$entete['limite'] = $this->limite_requete['limite'];
259
		$entete['total']  =  $this->total_resultat;
260
		$entete['version']  = $this->version_projet;
592 mathilde 261
		if ($this->limite_requete['limite'] != '*') {
262
			$url = $this->formulerUrl($this->total_resultat, '/informations/'.$this->ressources[0].'/relations/catminat');
263
			if (isset($url['precedent']) && $url['precedent'] != '') {
264
				$entete['href.precedent'] = $url['precedent'];
265
			}
266
			if (isset($url['suivant']) && $url['suivant'] != '') {
267
				$entete['href.suivant']   = $url['suivant'];
268
			}
579 mathilde 269
		}
270
		return $entete;
271
	}
499 jpm 272
 
452 mathilde 273
	public function formaterResultatsTousChamps($resultat) {
548 mathilde 274
		$this->resultat_json = $resultat;
275
		$this->traiterTypeBio();
617 mathilde 276
		$graphique_presence = $this->traiterEcologie() ;
548 mathilde 277
		if ($graphique_presence) {
617 mathilde 278
			$graphique = $this->ajouterLiensGraphique($graphique_presence);
279
			}
548 mathilde 280
	}
281
 
282
	public function formaterResultatsChampsEcologie($resultat) {
283
		$this->resultat_json = $resultat;
617 mathilde 284
		$graphique_presence = $this->traiterEcologie() ;
548 mathilde 285
		if ($graphique_presence) {
617 mathilde 286
			$graphique = $this->ajouterLiensGraphique($graphique_presence);
452 mathilde 287
		}
548 mathilde 288
	}
289
 
290
 
291
	public function formaterResultatsChampsDescription($resultat) {
292
	$this->resultat_json = $resultat;
293
	$this->traiterTypeBio();
294
	}
295
 
617 mathilde 296
	//vérifie la présence de données écologiques pour les graphiques et récupère leur ontologie
297
	public function traiterEcologie() {
548 mathilde 298
		$donnees_presence =  false;
452 mathilde 299
		$this->champs_ontologiques = $this->recupererTableauConfig('champs_ontologiques');
300
		foreach ($this->champs_ontologiques as $cle => $valeur){
548 mathilde 301
			if ($this->resultat_json[$cle] != ""){
617 mathilde 302
				$donnees_presence[$this->getNomGraphique($valeur)] =  true;
548 mathilde 303
				$this->recupererOntologies($this->resultat_json[$cle], $cle);
304
				unset($this->resultat_json[$cle]);
452 mathilde 305
			}
306
		}
548 mathilde 307
		return $donnees_presence;
452 mathilde 308
	}
548 mathilde 309
 
617 mathilde 310
	//donne le nom du graphique correspondant à un champ écologique
311
	public function getNomGraphique($code_ecolo) {
312
		$graphique = null;
313
		if (in_array($code_ecolo, explode(',',Config::get('Paramètres.climat')))) {
314
			$graphique = 'climat';
315
		} elseif (in_array($code_ecolo, explode(',', Config::get('Paramètres.sol')) )) {
316
			$graphique = 'sol';
317
		}
318
		return $graphique;
319
	}
320
 
548 mathilde 321
	public function traiterTypeBio() {
322
		if ($this->resultat_json['type_bio'] != ""){
323
			$tab_ontologie = $this->recupererOntologiePrTypeBio($this->resultat_json['type_bio']);
324
			unset($this->resultat_json['type_bio']);
325
			$this->resultat_json= array_merge($this->resultat_json,$tab_ontologie);
452 mathilde 326
		}
327
	}
499 jpm 328
 
617 mathilde 329
	public function ajouterLiensGraphique($graphique_presence) {
330
		if ($graphique_presence['climat']) {
331
			$this->resultat_json['graphique_climat']['libelle'] = 'climat';
332
			$this->resultat_json['graphique_climat']['href'] =
548 mathilde 333
			$this->ajouterHref('graphiques/climat', strtolower($this->resultat_json['BDNT']).'.nn:'.$this->resultat_json['num_nomen']);
617 mathilde 334
 
335
		}
336
		if ($graphique_presence['sol']) {
337
			$this->resultat_json['graphique_sol']['libelle'] = 'sol';
338
			$this->resultat_json['graphique_sol']['href'] =
548 mathilde 339
			$this->ajouterHref('graphiques/sol', strtolower($this->resultat_json['BDNT']).'.nn:'.$this->resultat_json['num_nomen']);
617 mathilde 340
		}
452 mathilde 341
	}
517 mathilde 342
 
343
 
452 mathilde 344
//+--------------------------traitement ontologies -------------------------------------------+
518 mathilde 345
 
452 mathilde 346
	public function recupererOntologies($valeur, $champs){
518 mathilde 347
		$url = Config::get('url_service_base').Config::get('nom_projet').
348
			'/ontologies/'.$this->champs_ontologiques[$champs].':'.urlencode(urlencode($valeur));
764 raphael 349
		$val = $this->getBdd()->recuperer(sprintf(
350
			"SELECT a.nom FROM baseflor_ontologies a LEFT JOIN baseflor_ontologies b ON a.id = b.id LEFT JOIN baseflor_ontologies c ON b.classe_id = c.id WHERE".
351
			" b.code = BINARY '%s' AND c.code = BINARY '%s' LIMIT 0, 100",
352
			$valeur,
353
				$this->champs_ontologiques[$champs]),
354
										  Bdd::MODE_OBJET);
355
 
356
		$this->resultat_json[$champs.'.libelle'] = isset($val->nom) ? $val->nom : NULL; // eg: VEMO:0
548 mathilde 357
		$this->resultat_json[$champs.'.code'] = $valeur;
358
		$this->resultat_json[$champs.'.href'] = $url;
452 mathilde 359
	}
548 mathilde 360
 
361
	//+-------------ontologie pour le champs type biologique (lire le .txt)---+
499 jpm 362
 
452 mathilde 363
	public function recupererOntologiePrTypeBio($type_a_traiter){
364
		$resultat_typeBio = "";
499 jpm 365
		if ($type_a_traiter != "") {
452 mathilde 366
			$types_prim_sec = $this->separerTypesPrimairesSecondaires($type_a_traiter);
548 mathilde 367
			$types_sstypes = $this->classerPrimSecEnTypesSsTypes($types_prim_sec);
368
			$resultat_typeBio = $this->formaterJsonTypeBio($types_sstypes, $type_a_traiter );
369
		}
370
		return $resultat_typeBio;
371
	}
372
 
373
	public function classerPrimSecEnTypesSsTypes($types_prim_sec) {
374
		$types_sstypes = '';
375
		foreach ($types_prim_sec as $cle => $tb) {
376
			$types_sstypes[$cle]= $this->separerTypes_SousTypes($tb, $cle);
377
		}
378
		switch (count($types_sstypes)) {
379
			case 1 :
380
				$types_sstypes = $types_sstypes['principal'];
381
				break;
382
			case 2 :
383
				$types_sstypes = array_merge($types_sstypes['principal'],$types_sstypes['secondaire']);
384
				break;
385
		}
386
		return $types_sstypes;
387
	}
388
 
499 jpm 389
 
548 mathilde 390
	public function formaterJsonTypeBio( $types_sstypes, $type_a_traiter) {
391
		$resultat_typeBio['type_bio.code'] = $type_a_traiter;
392
		foreach ($types_sstypes as $cle => $tb) {
393
			if (($cle == 'type.principal') || ($cle == 'type.secondaire')) {
394
				$i = 0;
395
				foreach ($tb as $types) {
518 mathilde 396
					$url = Config::get('url_service_base').Config::get('nom_projet').
548 mathilde 397
						'/ontologies/TB:'.urlencode(urlencode($types));
452 mathilde 398
					$val = $this->consulterHref($url);
550 mathilde 399
					$libelle[$cle][$i]= $val->nom;
549 mathilde 400
					$detail[$cle][$i]['libelle'] = $val->nom;
401
					$detail[$cle][$i]['code'] = $val->code;
402
					$detail[$cle][$i]['href'] = $url;
403
					$i++;
452 mathilde 404
				}
548 mathilde 405
			}
406
			if ($cle == 'sous_type.principal' || $cle == 'sous_type.secondaire' ) {
407
				$i = 0;
408
				foreach ($tb as $sstypes) {
409
					$url = Config::get('url_service_base').Config::get('nom_projet').
410
								'/ontologies/ssTT:'.urlencode(urlencode($sstypes));
411
					$val = $this->consulterHref($url);
550 mathilde 412
					$libelle[$cle][$i]=$val->nom;
548 mathilde 413
					$detail[$cle][$i]['libelle'] = $val->nom;
414
					$detail[$cle][$i]['code'] = $val->code;
415
					$detail[$cle][$i]['href'] = $url;
416
					$i++;
452 mathilde 417
				}
418
			}
419
		}
550 mathilde 420
		//var_dump($libelle); exit;
548 mathilde 421
		$resultat_typeBio['type_bio.libelle']= $this->formaterTypeBioPrLibelle($libelle);
422
		$resultat_typeBio['type_bio.detail'] = $detail;
452 mathilde 423
		return $resultat_typeBio;
424
	}
548 mathilde 425
 
426
 
549 mathilde 427
 
499 jpm 428
	public function formaterTypeBioPrLibelle($info) {
552 mathilde 429
		$libelle = implode(', ',$info['type.principal'])." ";
430
			$libelle .= isset($info['sous_type.principal']) ? " (".implode(', ',$info['sous_type.principal']).") " : "";
431
			$libelle .= isset($info['type.secondaire']) ? ", ".implode(', ',$info['type.secondaire'])." " : "";
432
			$libelle .= isset($info['sous_type.secondaire']) ? "(".implode(', ',$info['sous_type.secondaire']).") " : "";
452 mathilde 433
		return $libelle;
434
	}
435
 
548 mathilde 436
	//séparation des principaux et des secondaires
499 jpm 437
	public function separerTypesPrimairesSecondaires($type) {
548 mathilde 438
		$types = array();
439
		if (preg_match("/(.+)\((.+)\)\-{0,1}(.+){0,1}/", $type,$retour)) {
440
			$types['principal'] = isset($retour[3]) ? $retour[1].'-'. $retour[3] : $retour[1] ;
452 mathilde 441
			$types['secondaire'] = $retour[2];
499 jpm 442
		} else {
452 mathilde 443
			$types['principal'] = $type;
444
		}
445
		return $types;
446
	}
499 jpm 447
 
548 mathilde 448
 
499 jpm 449
	public function separerTypes_SousTypes($type_a_traiter, $typePrimOuSec) {
452 mathilde 450
		$types = array();
548 mathilde 451
		if (preg_match("/^([a-zA-Zé]+)\-(.+)$|^([a-zA-Zé,]+[^\-])$/", $type_a_traiter, $retour)) {
452
			//type
453
			$type = array();
454
			$type[0] = (isset($retour[3])) ? $retour[3] : $retour[1] ;
549 mathilde 455
			$type = explode(",", $type[0]);
548 mathilde 456
			for ($i = 0 ; $i < count($type); $i++) {
457
				$types['type.'.$typePrimOuSec][$i] = $type[$i];
458
			}
459
			//sous types
460
			$ss_types = array();
461
			if (isset($retour[2]) && $retour[2] != '') {
462
 				$ss_types[0] =  $retour[2] ;
549 mathilde 463
 				$ss_types = explode("-", $ss_types[0]);
499 jpm 464
				for ($i = 0 ; $i < count($ss_types); $i++) {
548 mathilde 465
					$types['sous_type.'.$typePrimOuSec][$i] = $ss_types[$i];
466
				}
467
			}
452 mathilde 468
    	}
469
 		return $types;
548 mathilde 470
 	}
471
 
472
 
473
 
474
 
475
 
476
	//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
477
 
478
	public function assemblerLaRequete() {
479
		$requete = 	' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
579 mathilde 480
		.$this->retournerRequeteCondition().' ';
592 mathilde 481
			if ($this->nb_ressources != 1 ) {
579 mathilde 482
				$requete .= $this->delimiterResultatsRequete();
592 mathilde 483
		}
548 mathilde 484
		return $requete;
452 mathilde 485
	}
548 mathilde 486
 
579 mathilde 487
	public function calculerTotalResultat() {
488
		$requete = 'SELECT count(*) as nombre FROM '.$this->table.' '.$this->retournerRequeteCondition();
489
		$res = $this->getBdd()->recuperer($requete);
490
		if ($res) {
491
			$this->total_resultat = $res['nombre'];
492
		} else {
493
			$this->total_resultat = 0;
494
			$e = 'Données introuvables dans la base';
495
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
496
		}
497
	}
548 mathilde 498
 
579 mathilde 499
	public function delimiterResultatsRequete() {
500
		$this->calculerTotalResultat();
501
		$requete_limite = '';
592 mathilde 502
		if ($this->limite_requete['limite'] != '*') {
503
			if ( ($this->limite_requete['depart'] <=  $this->total_resultat)){
504
				if (($this->limite_requete['limite'] + $this->limite_requete['depart'] )
505
				< $this->total_resultat  ){
506
					$requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
507
					.$this->limite_requete['limite'];
508
				}
509
			} else {
510
				$e = "Erreur : la valeur pour le paramètre navigation.départ est supérieure".
511
									" au nombre total de résultats.";
512
				throw new Exception($e, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
579 mathilde 513
			}
514
		}
515
		return $requete_limite;
516
	}
548 mathilde 517
 
579 mathilde 518
 
519
 
548 mathilde 520
	public  function retournerRequeteCondition() {
521
		$condition = '';
522
		if (empty($this->requete_condition) == false) {
523
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
524
		}
525
		return $condition;
526
	}
499 jpm 527
 
528
 
525 mathilde 529
 
452 mathilde 530
}
531
?>