Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 285 | Rev 374 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 jpm 1
<?php
2
/**
3
* Description :
4
* Classe NomsVernaculaires.php fournit une liste de noms vernaculaires et leur liaison à la bdtfx
109 jpm 5
* Le but étant de fournir un ensemble minimal d'information comprenant :
6
* un identifiant (numérique ou alphanumérique sous forme de ChatMot si possible), un nom, une langue et
6 jpm 7
* une relation avec un taxon de la bdtfx.
8
* Si l'url finit par /noms-vernaculaires on retourne une liste de noms (seulement les 100 premières par défaut).
9
* L'url peut contenir des paramètres optionnels passés après le ? : /observations?param1=val1&param2=val2&...
109 jpm 10
*
11
* Les paramètres de requête disponibles sont : masque, masque.code, masque.nom, masque.region , recherche,
6 jpm 12
* distinct, retour.format, navigation.depart et navigation.limite.
109 jpm 13
*
6 jpm 14
* Encodage en entrée : utf8
15
* Encodage en sortie : utf8
16
* @package framework-v3
109 jpm 17
* @author Delphine Cauquil <delphine@tela-botanica.org>
6 jpm 18
* @author Jennifer Dhé <jennifer.dhe@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-${year} Tela Botanica (accueil@tela-botanica.org)
23
*/
24
class NomsVernaculaires extends Commun {
25
	protected $champ_infos = array(
26
			'taxon' => array('service' => 'taxons', 'ressource' => 'nt:', 'projet' => 'bdtfx', 'nom' => 'nom_sci'),
27
			'conseil_emploi' => array('service' => 'ontologies', 'ressource' => 'numStatut:', 'projet' => 'nvjfl', 'nom' => 'nom'),
28
			'genre' => array('service' => 'ontologies', 'ressource' => 'genreNombre:', 'projet' => 'nvjfl', 'nom' => 'nom'));
109 jpm 29
 
6 jpm 30
	protected $service = 'noms-vernaculaires';
109 jpm 31
 
6 jpm 32
	/**
33
	 * Permet de stocker la requete formulée : /noms-vernaculaires | /noms-vernaculaires/#id |
34
	 *  /noms-vernaculaires/#id/champ | /noms-vernaculaires/#id/relations
109 jpm 35
	 * Est remplit au cours de l'analyse des ressources (traiterRessources()), par défaut, a la valeur du service.
6 jpm 36
	 * Est utilisée principalement pr déterminer le format du tableau à retourner.	 */
109 jpm 37
	protected $format_reponse = 'noms-vernaculaires';
38
 
39
	/** Variables constituant les parametres de la requete SQL (champ, condition, limit) remplie
6 jpm 40
	 * selon ressources et paramètres */
41
	protected $requete_champ = array(' * ');
42
	protected $requete_condition = '';
43
	protected $limite_requete = array(
109 jpm 44
		'depart' => 0,
6 jpm 45
		'limite' => 100
46
	);
284 jpm 47
 
48
	protected $champ_tri = 'code_langue';
49
	protected $direction_tri = 'asc';
286 aurelien 50
 
51
	/**
52
	 * Indique les champs supplémentaires à retourner
53
	 *  - conseil_emploi = conseil d'emploi du nom vernaculaire
54
	 *  - genre = genre et nombre du nom
55
	 *  - taxon = nom retenu associé à ce nom
56
	 */
57
	protected $champs_supp = array();
109 jpm 58
 
6 jpm 59
	/**
109 jpm 60
	 * Precise la contenance plus ou moins précise du tableau à retourner :
61
	 *  - min = les données présentes dans la table
6 jpm 62
	 *  - max = les données de la table + les informations complémentaires (pour les identifiants et les codes)
63
	 *  - oss = la liste des nom_sci (uniquement pour noms et taxons) */
64
	protected $retour_format = 'max';
109 jpm 65
	/** Valeur du paramètre de requete recherche :
66
	 *  - stricte : le masque est passé tel quel à l'opérateur LIKE.
67
	 *  - etendue : ajout automatique du signe % à la place des espaces et en fin de masque avec utilisation de LIKE.
6 jpm 68
	 *  - floue : recherche tolérante vis-à-vis d'approximations ou d'erreurs (fautes d'orthographe par exemple) */
69
	protected $recherche;
284 jpm 70
 
6 jpm 71
	/** Permet de stocker le tableau de résultat (non encodé en json) */
72
	protected $table_retour = array();
73
	/** Stocke le nombre total de résultats de la requete principale. Est calculée lors de l'assemblage de la requete */
74
	protected $total_resultat;
109 jpm 75
 
76
	//+------------------------------------------------------------------------------------------------------+
6 jpm 77
	// créer une condition en fonction du paramétre
109 jpm 78
	public function traiterParametres() {
79
		if (isset($this->parametres) && !empty($this->parametres)) {
80
 
81
			if (isset($this->parametres['recherche']) && $this->parametres['recherche'] != '') {
82
				$this->recherche = $this->parametres['recherche'];
6 jpm 83
			}
109 jpm 84
			foreach ($this->parametres as $param => $valeur) {
6 jpm 85
				switch ($param) {
109 jpm 86
					case 'masque' :
284 jpm 87
						$this->ajouterFiltreMasque('nom_vernaculaire', $valeur);
109 jpm 88
						break;
89
					case 'masque.nt' :
284 jpm 90
						$this->ajouterFiltreMasque('num_taxon', $valeur);
109 jpm 91
						break;
92
					case 'masque.nv' :
284 jpm 93
						$this->ajouterFiltreMasque('nom_vernaculaire', $valeur);
109 jpm 94
						break;
95
					case 'masque.lg' :
284 jpm 96
						$this->ajouterFiltreMasque('code_langue', $valeur);
109 jpm 97
						break;
98
					case 'masque.cce' :
284 jpm 99
						$this->ajouterFiltreMasque('num_statut', $valeur);
109 jpm 100
						break;
101
					case 'retour.format' :
102
						$this->retour_format = $valeur;
103
						break;
104
					case 'navigation.depart' :
105
						$this->limite_requete['depart'] = $valeur;
106
						break;
107
					case 'navigation.limite' :
108
						$this->limite_requete['limite'] = $valeur;
109
						break;
286 aurelien 110
					case 'retour.champs' :
111
						$this->champs_supp = explode(',',$valeur);
112
					break;
109 jpm 113
					case 'recherche' :
114
						break;
152 delphine 115
					case 'version.projet' :
116
						break;
109 jpm 117
					default :
118
						$p = 'Erreur dans les paramètres de recherche de votre requête : '.
119
							'</br> Le paramètre " '.$param.' " n\'existe pas.';
120
							$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $p);
6 jpm 121
				}
122
			}
123
		}
124
	}
109 jpm 125
 
284 jpm 126
	public function ajouterFiltreMasque($nom_champ, $valeur) {
6 jpm 127
		if ($nom_champ == 'num_taxon') { // si il s'agit d'un chiffre
128
			$this->requete_condition[] = $nom_champ.' = '.$this->getBdd()->proteger($valeur);
129
		} else {
130
			if ($this->recherche == 'floue') {
131
				$this->requete_condition[] = '(SOUNDEX('.$nom_champ.') = SOUNDEX(\''.$valeur.'\')'
132
					.' OR SOUNDEX(REVERSE('.$nom_champ.')) = SOUNDEX(REVERSE(\''.$valeur.'\'))) ';
133
			} else {
134
				if ($this->recherche == 'etendue') {
180 delphine 135
					$valeur = '%'.str_replace(' ','% ', $valeur);
6 jpm 136
					$valeur .= '%';
137
				}
138
				$this->requete_condition[] = $nom_champ.' LIKE '.$this->getBdd()->proteger($valeur);
139
			}
140
		}
141
	}
142
 
109 jpm 143
	//+------------------------------------------------------------------------------------------------------+
6 jpm 144
	// en fonction de la présence des ressources modifie requete_champ et requete_condition
109 jpm 145
	public function traiterRessources() {
146
		if (isset($this->ressources) && !empty($this->ressources)) {
147
			if (isset($this->ressources[0]) && !empty($this->ressources[0])) {
6 jpm 148
				$this->traiterRessourceId(); // ajoute condition id=#valeur
109 jpm 149
				if (isset($this->ressources[1]) && !empty($this->ressources[1])) {
160 delphine 150
					$this->traiterRessourceChamp(); //modifie requete_champ ou requete_condition
6 jpm 151
				}
152
			}
153
		} else { //rajoute distinct pour ne pas avoir plusieurs fois le même nom
154
			$this->requete_champ = array('distinct(id)', 'nom_vernaculaire ');
155
		}
156
	}
109 jpm 157
 
6 jpm 158
	//requete : /noms-vernaculaires/#id (ex : /noms-vernaculaires/7)
159
	public function traiterRessourceId() {
109 jpm 160
		if (is_numeric($this->ressources[0])) {
161
			$this->requete_condition[] = ' id = '.$this->getBdd()->proteger($this->ressources[0]);
6 jpm 162
			$this->format_reponse .= '/id';
160 delphine 163
		} elseif ($this->ressources[0] == 'attributions') {
164
			$this->format_reponse .= '/attributions';
6 jpm 165
		} else {
109 jpm 166
			$r = 'Erreur dans les ressources de votre requête : </br> La ressource " '.$this->ressources[0].
6 jpm 167
				' " n\'existe pas.';
109 jpm 168
			$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $r);
6 jpm 169
		}
170
	}
109 jpm 171
 
172
 
160 delphine 173
	public function traiterRessourceChamp() {
174
		$this->format_reponse .= '/champ';
175
		$this->analyserChamp();
6 jpm 176
	}
109 jpm 177
 
6 jpm 178
	public function analyserChamp() {
179
		$this->requete_champ = array();
180
		$this->recupererTableConfig('champs_possibles');// s'il y a plusieurs champs correspondant au champ demandé ils sont séparé par des |
109 jpm 181
		$champs = explode(' ', $this->ressources[1]);
6 jpm 182
		foreach ($champs as $champ) {
183
			preg_match('/^([^.]+)(\.([^.]+))?$/', $champ, $match);
184
			if (isset($this->champs_possibles[$match[1]])) {
185
				$this->requete_champ[] = str_replace('|', ', ', $this->champs_possibles[$match[1]]);
186
			} elseif (isset($this->champs_possibles[$match[0]])) {
187
				$this->requete_champ[] = str_replace('|', ', ', $this->champs_possibles[$match[0]]);
188
			} else {
189
				$champs_possibles = implode('</li><li>', array_keys($this->champs_possibles));
190
				$c = 'Erreur dans votre requête : </br> Le champ "'.$champ_possibles.'" n\'existe pas. '.
191
					'Les champs disponibles sont : <li>'.$champs_possibles.'</li> et leurs déclinaisons (ex. ".code").';
192
				$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $c);
193
			}
194
		}
195
	}
109 jpm 196
 
197
	//+------------------------------------------------------------------------------------------------------+
6 jpm 198
	public function assemblerLaRequete() {
199
		$requete = ' SELECT '.$this->formerRequeteChamp().
200
					' FROM '.$this->table
201
					.$this->formerRequeteCondition()
202
					.$this->formerRequeteLimite();
203
		return $requete;
204
	}
109 jpm 205
 
6 jpm 206
	public  function formerRequeteChamp() {
207
		if (in_array('*', $this->requete_champ)) {
208
			$champ = ' * ';
209
		} else {
210
			$champ = implode(', ', $this->requete_champ);
211
		}
212
		return $champ;
213
	}
109 jpm 214
 
6 jpm 215
	public  function formerRequeteCondition() {
216
		$condition = '';
109 jpm 217
		if ($this->requete_condition != null) {
6 jpm 218
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
109 jpm 219
		}
6 jpm 220
		return $condition;
221
	}
109 jpm 222
 
223
	//ajout d'une limite seulement pour les listes (pas plus de 100 resultats retournés pr les requetes
6 jpm 224
	// suivantes : /noms-vernaculaires et /noms-vernaculaires/#id/relations)
225
	public function formerRequeteLimite() {
226
		if (in_array($this->format_reponse , array($this->service.'/id', $this->service.'/id/champs'))) {
109 jpm 227
			$this->requete_limite = '';
6 jpm 228
		} elseif (($depart = $this->limite_requete['depart']) > ($this->total_resultat = $this->recupererTotalResultat())) {
109 jpm 229
			$this->limite_requete['depart'] =
6 jpm 230
				(($this->total_resultat - $this->limite_requete['limite']) < 0) ? 0 : ($this->total_resultat - $this->limite_requete['limite']);
109 jpm 231
			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
6 jpm 232
		} else {
109 jpm 233
			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
6 jpm 234
		}
235
		return $this->requete_limite;
236
	}
109 jpm 237
 
6 jpm 238
	//on récupère le nombre total de résultats de la requete (ex : le nombre d'id contenu dans la liste /noms-vernaculaires)
239
	public function recupererTotalResultat() {
160 delphine 240
		$distinct = ($this->format_reponse == 'noms-vernaculaires/attributions') ? 'id' : 'distinct(id)';
241
		$requete = 'SELECT count('.$distinct.') as nombre FROM '
6 jpm 242
			.$this->table
243
			.$this->formerRequeteCondition();
244
		$res = $this->getBdd()->recuperer($requete);
109 jpm 245
 
6 jpm 246
		if ($res) {
247
			$total = $res['nombre'];
248
		} else {
160 delphine 249
			$t = 'Fonction recupererTotalResultat() : <br/>Données introuvables dans la base '.$requete;
6 jpm 250
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $t);
251
		}
252
		return $total;
253
	}
109 jpm 254
 
255
	//+------------------------------------------------------------------------------------------------------+
256
	// determine en fct du service appelé (/noms-vernaculaires | /noms-vernaculaires/#id | /noms-vernaculaires/#id/champ |
6 jpm 257
	// /noms-vernaculaires/#id/relations) le format du tableau à retourner.
258
	public function retournerResultatFormate($resultat) {
259
		$this->recupererTableConfig('correspondance_champs');
260
		switch ($this->format_reponse) {
132 aurelien 261
			case 'noms-vernaculaires'				:
262
				$reponse = ($this->retour_format == 'oss') ? $this->formaterEnOss($resultat) : $this->formaterNomsVernaculaires($resultat);			break;
160 delphine 263
			case 'noms-vernaculaires/attributions'	: $reponse = $this->formaterNomsVernaculairesAttributions($resultat);	break;
6 jpm 264
			case 'noms-vernaculaires/id'			: $reponse = $this->formaterNomsVernaculairesId($resultat);			break;
265
			case 'noms-vernaculaires/id/champ'		: $reponse = $this->formaterNomsVernaculairesIdChamp($resultat);	break;
266
			default									:																	break;
267
		}
268
		return $reponse;
269
	}
109 jpm 270
 
6 jpm 271
	public function formaterNomsVernaculaires($resultat) {
272
		//on remplit la table $table_retour_json['entete']
160 delphine 273
		$table_retour_json['entete']['masque'] = $this->recupererMasque();
6 jpm 274
		$table_retour_json['entete']['depart'] = $this->limite_requete['depart'];
275
		$table_retour_json['entete']['limite'] = $this->limite_requete['limite'];
276
		$table_retour_json['entete']['total']  = $this->total_resultat;
277
		$url = $this->formulerUrl($this->total_resultat, '/noms-vernaculaires');
109 jpm 278
		if (isset($url['precedent']) && $url['precedent'] != '') { $table_retour_json['entete']['href.precedent'] = $url['precedent']; }
279
		if (isset($url['suivant']) && $url['suivant']   != '') { $table_retour_json['entete']['href.suivant']   = $url['suivant']; }
160 delphine 280
 
109 jpm 281
		//on remplit la table $table_retour_json['resultat']
152 delphine 282
		if (isset($this->parametres['masque.nv'])) {
283
			$resultat = $this->trierRechercheFloue($this->parametres['masque.nv'], $resultat, 'nom_vernaculaire');
6 jpm 284
		}
152 delphine 285
		if (isset($this->parametres['masque'])) {
286
			$resultat = $this->trierRechercheFloue($this->parametres['masque'], $resultat, 'nom_vernaculaire');
287
		}
6 jpm 288
		foreach ($resultat as $tab) {
289
			foreach ($tab as $key => $valeur) {
290
				if ($valeur != '') {
291
					switch ($key) {
292
						case 'id'				: $num = $valeur;								break;
293
						case 'nom_vernaculaire'	: $this->table_retour['nom'] = $valeur;			break;
294
						default					:												break;
295
					}
296
				}
297
			}
298
			if ($this->retour_format == 'max') $this->table_retour['href'] = $this->ajouterHref('noms-vernaculaires', $num);
299
			$resultat_json[$num] = $this->table_retour;
300
			$this->table_retour = array();
301
		}
109 jpm 302
 
6 jpm 303
		$table_retour_json['resultat'] = $resultat_json;
304
		return $table_retour_json;
305
	}
132 aurelien 306
 
160 delphine 307
	public function recupererMasque() {
308
		$tab_masque = array();
309
		foreach ($this->parametres as $param=>$valeur) {
310
			if (strstr($param, 'masque') != false) {
311
				$tab_masque[] = $param.'='.$valeur;
312
			}
313
		}
314
		$masque = implode('&', $tab_masque);
315
		return $masque;
316
	}
317
 
132 aurelien 318
	public function formaterEnOss($resultat) {
319
		$table_nom = array();
320
		$oss = '';
321
		foreach ($resultat as $tab) {
322
			if (isset($tab['nom_vernaculaire']) ) {
323
				if (!in_array($tab['nom_vernaculaire'], $table_nom)) {
324
					$table_nom[] = $tab['nom_vernaculaire'];
325
					$oss [] = $tab['nom_vernaculaire'];
326
				}
327
			}
328
		}
329
		if (isset($this->masque)) $masque = implode('&', $this->masque);
330
		else $masque = 'Pas de masque';
331
		$table_retour_oss = array($masque, $oss);
332
		return $table_retour_oss;
333
	}
160 delphine 334
 
335
	public function formaterNomsVernaculairesAttributions($resultat) {
336
		//on remplie la table $table_retour_json['entete']
337
		$table_retour_json['entete']['masque'] = $this->recupererMasque();
338
		$table_retour_json['entete']['depart'] = $this->limite_requete['depart'];
339
		$table_retour_json['entete']['limite'] = $this->limite_requete['limite'];
340
		$table_retour_json['entete']['total']  = $this->total_resultat;
341
		$url = $this->formulerUrl($this->total_resultat, '/noms-vernaculaires/attributions');
342
		if (isset($url['precedent']) && $url['precedent'] != '') {
343
			$table_retour_json['entete']['href.precedent'] = $url['precedent'];
344
		}
345
		if (isset($url['suivant']) && $url['suivant']   != '') {
346
			$table_retour_json['entete']['href.suivant']   = $url['suivant'];
347
		}
284 jpm 348
 
349
		foreach ($resultat as &$tab) {
160 delphine 350
			$resultat_json[$tab['num_nom_vernaculaire']]['id'] = $tab['id'];
351
			$resultat_json[$tab['num_nom_vernaculaire']]['nom_vernaculaire'] = $tab['nom_vernaculaire'];
284 jpm 352
			$resultat_json[$tab['num_nom_vernaculaire']]['code_langue'] = $tab['code_langue'];
160 delphine 353
			$resultat_json[$tab['num_nom_vernaculaire']]['taxon.code'] = 'bdtfx.nt:'.$tab['num_taxon'];
354
			if ($this->retour_format == 'max') {
286 aurelien 355
				$resultat_json[$tab['num_nom_vernaculaire']]['num_taxon'] = $tab['num_taxon'];
356
				$resultat_json[$tab['num_nom_vernaculaire']]['nom_retenu.code'] = $tab['num_taxon'];
160 delphine 357
				$resultat_json[$tab['num_nom_vernaculaire']]['taxon'] = $tab['num_taxon'];
284 jpm 358
				$this->taxons[] = $tab['num_taxon']; // utilisé pour chercher les noms latins plus bas
160 delphine 359
				$resultat_json[$tab['num_nom_vernaculaire']]['href'] = $this->ajouterHref('noms-vernaculaires', $tab['id']);
286 aurelien 360
 
361
				if($this->champs_supp != array()) {
362
					$resultat_json[$tab['num_nom_vernaculaire']] = $this->ajouterChampsOntologieLigneResultat($tab);
363
				}
160 delphine 364
			}
280 aurelien 365
		}
366
 
367
		if ($this->retour_format == 'max') {
284 jpm 368
			// On est obligé de faire un deuxième boucle pour demander tous les taxons présents en une
369
			// fois et les attribuer aux noms car c'est beaucoup plus rapide
370
			$noms_sci = $this->recupererNomTaxons();
286 aurelien 371
			foreach ($resultat_json as $num_nom => &$tab) {
372
				$tab = $this->ajouterTaxonsAttributionsLigneResultat($tab, $noms_sci);
284 jpm 373
			}
160 delphine 374
		}
280 aurelien 375
 
284 jpm 376
		uasort($resultat_json, array($this,'trierLigneTableau'));
160 delphine 377
		$table_retour_json['resultat'] = $resultat_json;
378
		return $table_retour_json;
379
	}
380
 
286 aurelien 381
	/**
382
	 * Ajoute les champs d'ontologie supplémentaires si necéssaire
383
	 * en faisant appels aux web services associés
384
	 * @param array $ligne_resultat
385
	 *
386
	 * @return array la ligne modifiée
387
	 */
284 jpm 388
	public function ajouterChampsOntologieLigneResultat($ligne_resultat) {
389
 
390
		$intitule = '';
391
		foreach($this->champ_infos as $cle => $champs_supplementaires) {
286 aurelien 392
			if(in_array($cle, $this->champs_supp)) {
393
				extract($champs_supplementaires);
394
				$valeur_recherche = '';
395
				switch($cle) {
396
					case 'taxon':
397
						$valeur_recherche = $ligne_resultat['num_taxon'];
398
						$intitule = 'taxon.code';
399
						break;
400
					case 'genre':
401
						$valeur_recherche = $ligne_resultat['num_genre'];
402
						$intitule = 'genre';
403
						break;
404
					case 'conseil_emploi':
405
						$valeur_recherche = $ligne_resultat['num_statut'];
406
						$intitule = 'conseil_emploi';
407
						break;
408
				}
409
				$code_valeur = '';
410
				if(trim($valeur_recherche) != '') {
411
					$url = $this->ajouterHrefAutreProjet($service, $ressource, $valeur_recherche, $projet);
412
					$code_valeur = $this->chercherSignificationCode($url, $nom);
413
				}
414
				$ligne_resultat[$intitule] = $code_valeur;
277 aurelien 415
			}
416
		}
284 jpm 417
		return $ligne_resultat;
277 aurelien 418
	}
419
 
284 jpm 420
	/**
421
	 * Fonction qui ajoute les attributions à une ligne de résultats
422
	 *
423
	 * @param array $ligne_tableau_resultat
424
	 * @param array $nom_sci
425
	 */
426
	public function ajouterTaxonsAttributionsLigneResultat(&$ligne_tableau_resultat, &$noms_sci) {
285 aurelien 427
		if (isset($noms_sci[$ligne_tableau_resultat['num_taxon']])) {
428
			$ligne_tableau_resultat['nom_retenu.code'] = $noms_sci[$ligne_tableau_resultat['num_taxon']]['id'];
429
			$ligne_tableau_resultat['taxon'] = $noms_sci[$ligne_tableau_resultat['num_taxon']]['nom_sci'];
284 jpm 430
		}
431
		return $ligne_tableau_resultat;
432
	}
433
 
434
	private function trierLigneTableau($a, $b) {
435
		$retour = 0;
436
 
437
		if ($a[$this->champ_tri] == $b[$this->champ_tri]) {
438
			$retour = 0;
439
		}
440
 
441
		if($this->champ_tri == 'code_langue') {
442
			if ($a[$this->champ_tri] == 'fra' && $b[$this->champ_tri] != 'fra') {
443
				$retour = ($this->direction_tri == 'asc') ? -1 : 1;
444
			} else if ($a[$this->champ_tri] != 'fra' && $b[$this->champ_tri] == 'fra') {
445
				$retour = ($this->direction_tri == 'asc') ? 1 : -1;
446
			} else {
447
				$retour = $this->comparerChaineSelonDirectionTri($a[$this->champ_tri], $b[$this->champ_tri]);
448
			}
449
		} else {
450
			$retour = $this->comparerChaineSelonDirectionTri($a[$this->champ_tri], $b[$this->champ_tri]);
451
		}
452
		return $retour;
453
	}
454
 
455
	private function comparerChaineSelonDirectionTri($a, $b) {
456
		if($this->direction_tri == 'asc') {
457
			return ($a < $b) ? -1 : 1;
458
		} else {
459
			return ($a > $b) ? -1 : 1;
460
		}
461
	}
462
 
6 jpm 463
	// formatage de la reponse /id ss la forme
464
	// id, nom_vernaculaire, attributions
465
	// langue
466
	// num_nom (correspond à un taxon bdtfx)
467
	public function formaterNomsVernaculairesId($resultat) {
468
		foreach ($resultat as $taxon) { // pour chaque attribution à un taxon bdtfx
469
			// on crée les variables qui serviront de clés et on les enléves du tableau
470
			$num_nom = $taxon['num_nom_vernaculaire']; // unique pour un trinôme id, langue, taxon
471
			unset($taxon['num_nom_vernaculaire']);
472
			$langue = $taxon['code_langue'];
473
			unset($taxon['code_langue']);
109 jpm 474
 
6 jpm 475
			foreach ($this->correspondance_champs as $key => $correspondance) { // ordonne les infos pour affichage
476
				if (isset($taxon[$key]) && $taxon[$key] != "") {
477
					$this->afficherDonnees($correspondance, $taxon[$key], $langue, $num_nom);
478
				}
479
			}
480
			foreach ($taxon as $key => $valeur) { // rajoute les champs non prévus dans l'api
481
				if (!isset($this->correspondance_champs[$key]) && $valeur != "") {
482
					$this->afficherDonnees($key, $valeur, $langue, $num_nom);
483
				}
484
			}
485
			if ($this->retour_format == 'max') $this->chargerBiblio($num_nom, $langue);
486
		}
487
		if ($this->retour_format == 'max') $this->afficherTaxons(); // va chercher les noms de tous les taxons
488
		unset($this->table_retour['href']);
489
		return $this->table_retour;
490
	}
109 jpm 491
 
6 jpm 492
	public function afficherDonnees($champ, $valeur, $langue = '', $num_nom = '') {
493
		if ($champ == 'id' || $champ == 'nom_vernaculaire') {
494
			$this->table_retour[$champ] = $valeur;
495
		} elseif (preg_match('/^(.*)\.code$/', $champ, $match)) {
496
				switch ($match[1]) {
497
					case 'taxon'	: if ($this->retour_format == 'max') {$this->taxons[$num_nom] = $valeur;}
498
						$this->afficherPointCode($match[1], $langue, $num_nom, $valeur);	break;
109 jpm 499
					case 'langue'	: //$this->afficherPointCode($match[1], 'iso-639-3', 'langues', $valeur);
6 jpm 500
						break;
501
					case 'genre'	: $this->afficherPointCode($match[1], $langue, $num_nom, $valeur);	break;
502
					case 'conseil_emploi'	: $this->afficherPointCode($match[1], $langue, $num_nom, $valeur);	break;
503
					default : break;
504
				}
109 jpm 505
 
6 jpm 506
		} elseif ($langue != '') {
507
			$this->table_retour['attributions'][$langue][$num_nom][$champ] = $valeur;
508
		} else {
509
			$this->table_retour[$champ] = $valeur;
510
		}
511
	}
109 jpm 512
 
6 jpm 513
	public function afficherPointCode($nomChamp, $langue, $num_nom, $valeur) {
514
		if (isset($this->champ_infos[$nomChamp])) {
515
			extract($this->champ_infos[$nomChamp]);
516
		}
109 jpm 517
 
6 jpm 518
		if ($this->retour_format == 'max') {
519
			$url = $this->ajouterHrefAutreProjet($service, $ressource, $valeur, $projet);
520
			if ($service == 'taxons') {
521
				$code_valeur = '';
164 delphine 522
				$this->table_retour['attributions'][$langue][$num_nom]['nom_retenu.code'] = $code_valeur;
109 jpm 523
			} else {
6 jpm 524
				$code_valeur = $this->chercherSignificationCode($url, $nom);
525
			}
526
			if ($projet != '') $projet .= '.';
527
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp] = $code_valeur;
528
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp.'.code'] = $projet.$ressource.$valeur;
529
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp.'.href'] = $url;
530
		} else {
531
			if ($projet != '') $projet .= '.';
532
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp.'.code'] = $projet.$ressource.$valeur;
533
		}
534
	}
109 jpm 535
 
6 jpm 536
	public function chercherSignificationCode($url, $nom) {
537
		if (isset($this->signification_code[$url])) {
538
			$valeur = $this->signification_code[$url];
539
		} else {
540
			$res = $this->consulterHref($url);
541
			$valeur = $res->$nom;
542
			$this->signification_code[$url] = $valeur;
543
		}
544
		return $valeur;
545
	}
109 jpm 546
 
6 jpm 547
	public function afficherTaxons() {
160 delphine 548
		$resultat = $this->recupererNomTaxons();
6 jpm 549
		foreach ($this->table_retour['attributions'] as $code_langue=>$langue) {
550
			foreach ($langue as $num_nom=>$taxon) {
551
				$num_tax = ltrim($taxon['taxon.code'], 'bdtfx.nt:');
552
				if (isset($resultat[$num_tax])) {
164 delphine 553
					$this->table_retour['attributions'][$code_langue][$num_nom]['nom_retenu.code'] = $resultat[$num_tax]['id'];
554
					$this->table_retour['attributions'][$code_langue][$num_nom]['taxon'] = $resultat[$num_tax]['nom_sci'];
6 jpm 555
				}
556
			}
557
		}
558
	}
160 delphine 559
 
560
	public function recupererNomTaxons() {
561
		$url = Config::get('url_service_base').'bdtfx/taxons?navigation.limite=500&masque.nt='.
562
			$this->getBdd()->proteger(implode(',', $this->taxons));
563
		$res = $this->consulterHref($url);
564
		foreach ($res->resultat as $id=>$taxon) {
164 delphine 565
			$resultat[$taxon->num_taxonomique]['id'] = 'bdtfx.nn:'.$id;
566
			$resultat[$taxon->num_taxonomique]['nom_sci'] = $taxon->nom_sci;
160 delphine 567
		}
568
		return $resultat;
569
	}
6 jpm 570
 
571
	public function formaterNomsVernaculairesIdChamp($resultat) {
109 jpm 572
		$this->table_retour['id'] = $this->ressources[0];
573
		$champs = explode(' ', $this->ressources[1]);
6 jpm 574
		if (in_array('attributions', $champs) != false) {
575
			$this->formaterNomsVernaculairesId($resultat);
576
			unset($this->table_retour['nom_vernaculaire']);
160 delphine 577
		} else {
6 jpm 578
			$champ_attributions = array('num_taxon', 'zone_usage', 'num_statut', 'num_genre', 'notes');
579
			foreach ($resultat as $taxon) {
580
				foreach ($taxon as $key=>$valeur) {
581
					if ($key == 'code_langue' && in_array('langue', $champs) != false) {
582
						$this->table_retour['attributions']['langue'][] = $valeur;
583
					} elseif (in_array($key, $champ_attributions) != false) {
584
						$this->afficherPoint($this->correspondance_champs[$key] , $valeur, $taxon['code_langue'], $taxon['num_nom_vernaculaire']);
585
					} elseif (in_array($key, $champs) != false) {
586
						$this->table_retour[$key] = $valeur;
587
					}
588
				}
589
				if (in_array('biblio', $champs) != false) $this->chargerBiblio($taxon['num_nom_vernaculaire'], $taxon['code_langue']);
590
			}
591
			if (in_array('biblio', $champs) != false && array_search('biblio.num_ref', $this->table_retour) != false) $this->table_retour['biblio'] = null;
592
		}
593
		return $this->table_retour;
594
	}
109 jpm 595
 
6 jpm 596
	public function afficherPoint($champ, $valeur, $langue, $num_nom) {
597
		preg_match('/^(.*)\.code$/', $champ, $match);
598
		$champ = $match[1];
599
		if (isset($this->champ_infos[$champ])) {
600
			extract($this->champ_infos[$champ]);
601
			$url = $this->ajouterHrefAutreProjet($service, $ressource, $valeur, $projet);
602
			$projet .= '.';
603
		}
109 jpm 604
 
605
		$champs = explode(' ', $this->ressources[1]);
6 jpm 606
		if (in_array($champ.'.*', $champs) !== false && isset($projet)) {
607
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.code'] = $projet.$ressource.$valeur;
608
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.href'] = $url;
109 jpm 609
		}
6 jpm 610
		if (in_array($champ.'.code', $champs) !== false && isset($projet)) {
611
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.code'] = $projet.$ressource.$valeur;
109 jpm 612
		}
6 jpm 613
		if (in_array($champ.'.href', $champs) !== false && isset($projet)) {
614
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.href'] = $url;
109 jpm 615
		}
6 jpm 616
		if (in_array($champ, $champs) !== false) {
617
			if (isset($url)) {
618
				$this->table_retour['attributions'][$langue][$num_nom][$champ] = $this->chercherSignificationCode($url, $nom);
619
			} else {
620
				$this->table_retour['attributions'][$langue][$champ] = $valeur;
621
			}
109 jpm 622
		}
6 jpm 623
	}
624
 
625
	public function afficherLangue($nomChamp, $projet, $service, $valeur, $ressource = '', $nom = 'nom') {
626
		if ($this->retour_format == 'max') {
627
				$this->table_retour['attributions'][$nomChamp] = $nom;
628
				$this->table_retour['attributions'][$nomChamp.'.code'] = $projet.$ressource.$valeur;
629
				$this->table_retour['attributions'][$nomChamp.'.href'] = $url;
630
		} else {
631
			$this->table_retour['attributions'][$nomChamp.'.code'] = $projet.$ressource.$valeur;
632
		}
633
	}
109 jpm 634
 
6 jpm 635
	public function chargerBiblio($num_nom, $langue) {
636
		list($table, $version) = explode('_v',$this->table);
637
		$requete = "SELECT b.*, lb.notes FROM nvjfl_lien_biblio_v$version lb, nvjfl_biblio_v$version b ".
638
					"WHERE b.num_ref = lb.num_ref AND lb.num_nom = '$num_nom' ;";
284 jpm 639
		$resultat = $this->getBdd()->recupererTous($requete);
109 jpm 640
 
6 jpm 641
		 if ($resultat == '') { //cas ou la requete comporte des erreurs
642
		 	$r = 'La requête SQL formée comporte une erreur !!';
643
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $r);
644
			Debug::printr($requete);
645
		 } elseif ($resultat) {
646
			foreach ($resultat as $res) {
647
			   	foreach ($res as $cle => $valeur) {
648
			   		if ($valeur !== "") {
649
			   			$this->table_retour['attributions'][$langue][$num_nom]['biblio.'.$cle] = $valeur;
650
			   		}
651
			    }
652
			}
653
		}
654
	}
109 jpm 655
 
6 jpm 656
}
657
?>