Subversion Repositories eFlore/Projets.eflore-projets

Rev

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