Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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