Subversion Repositories eFlore/Projets.eflore-projets

Rev

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