Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 1175 | Rev 1259 | 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
1206 aurelien 129
			$valeur = implode(',', array_map(array($this->getBdd(), 'proteger'), explode(',',$valeur)));
130
			$this->requete_condition[] = $nom_champ.' IN ('.$valeur.')';
721 aurelien 131
		} else {
132
			if ($this->recherche == 'floue') {
133
				$this->requete_condition[] = '(SOUNDEX('.$nom_champ.') = SOUNDEX(\''.$valeur.'\')'
134
					.' OR SOUNDEX(REVERSE('.$nom_champ.')) = SOUNDEX(REVERSE(\''.$valeur.'\'))) ';
135
			} else {
136
				if ($this->recherche == 'etendue') {
137
					$valeur = '%'.str_replace(' ','% ', $valeur);
138
					$valeur .= '%';
139
				}
932 aurelien 140
				$this->requete_condition[] = 'nva.'.$nom_champ.' LIKE '.$this->getBdd()->proteger($valeur);
721 aurelien 141
			}
142
		}
143
	}
144
 
145
	//+------------------------------------------------------------------------------------------------------+
146
	// en fonction de la présence des ressources modifie requete_champ et requete_condition
147
	public function traiterRessources() {
148
		if (isset($this->ressources) && !empty($this->ressources)) {
149
			if (isset($this->ressources[0]) && !empty($this->ressources[0])) {
150
				$this->traiterRessourceId(); // ajoute condition id=#valeur
151
				if (isset($this->ressources[1]) && !empty($this->ressources[1])) {
152
					$this->traiterRessourceChamp(); //modifie requete_champ ou requete_condition
153
				}
154
			}
155
		}
156
	}
157
 
158
	//requete : /noms-vernaculaires/#id (ex : /noms-vernaculaires/7)
159
	public function traiterRessourceId() {
160
		if (is_numeric($this->ressources[0])) {
932 aurelien 161
			$this->requete_condition[] = ' nva.num_nom_vernaculaire = '.$this->getBdd()->proteger($this->ressources[0]);
721 aurelien 162
			$this->format_reponse .= '/id';
163
		} elseif ($this->ressources[0] == 'attributions') {
164
			$this->format_reponse .= '/attributions';
165
		} else {
166
			$r = 'Erreur dans les ressources de votre requête : </br> La ressource " '.$this->ressources[0].
167
				' " n\'existe pas.';
168
			$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $r);
169
		}
170
	}
171
 
172
 
173
	public function traiterRessourceChamp() {
174
		$this->format_reponse .= '/champ';
175
		$this->analyserChamp();
176
	}
177
 
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 |
181
		$champs = explode(' ', $this->ressources[1]);
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
	}
196
 
197
	//+------------------------------------------------------------------------------------------------------+
198
	public function assemblerLaRequete() {
199
		$requete = ' SELECT '.$this->formerRequeteChamp().
932 aurelien 200
		', CASE nva.code_langue WHEN "fra" THEN 1 ELSE 0 END AS tri '.
201
			' FROM '.str_replace('nva', 'nva_index', $this->table).' inva'.
202
			' LEFT JOIN '.$this->table.' nva ON nva.num_nom_vernaculaire = inva.num_nom_vernaculaire '
203
			.$this->formerRequeteCondition().
1206 aurelien 204
			' GROUP BY nva.num_nom_vernaculaire '.
932 aurelien 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) {
1206 aurelien 298
						case 'num_taxon'				: $this->table_retour['num_taxon'] = $valeur;								break;
932 aurelien 299
						case 'num_nom_vernaculaire'				: $num = $valeur;								break;
721 aurelien 300
						case 'nom_vernaculaire'	: $this->table_retour['nom'] = $valeur;			break;
301
						default					:												break;
302
					}
303
				}
304
			}
305
		    if ($this->retour_format == 'max') $this->table_retour['href'] = $this->ajouterHref('noms-vernaculaires', $num);
306
			$resultat_json[$num] = $this->table_retour;
307
			$this->table_retour = array();
308
		}
309
		return  $resultat_json;
310
	}
311
 
312
 
313
	public function formaterNomsVernaculaires($resultat) {
314
		$table_retour_json['entete'] = $this->ajouterJsonEnTeteNV();
315
		$resultat = $this->hierarchiserResultat($resultat);
316
		$table_retour_json['resultat'] = $this->ajouterJsonResultatNV($resultat);
317
		return $table_retour_json;
318
	}
319
 
320
	public function hierarchiserResultat($resultat) {
321
		//tri recherche floue
322
		if (isset($this->parametres['masque.nv'])) {
323
			$resultat = $this->trierRechercheFloue($this->parametres['masque.nv'], $resultat, 'nom_vernaculaire');
324
		}
325
		if (isset($this->parametres['masque'])) {
326
			$resultat = $this->trierRechercheFloue($this->parametres['masque'], $resultat, 'nom_vernaculaire');
327
		}
328
		return $resultat;
329
	}
330
 
331
	public function recupererMasque() {
332
		$tab_masque = array();
333
		foreach ($this->parametres as $param=>$valeur) {
334
			if (strstr($param, 'masque') != false) {
335
				$tab_masque[] = $param.'='.$valeur;
336
			}
337
		}
338
		$masque = implode('&', $tab_masque);
339
		return $masque;
340
	}
341
 
342
	public function formaterEnOss($resultat) {
343
		$table_nom = array();
344
		$oss = '';
345
		foreach ($resultat as $tab) {
346
			if (isset($tab['nom_vernaculaire']) ) {
347
				if (!in_array($tab['nom_vernaculaire'], $table_nom)) {
348
					$table_nom[] = $tab['nom_vernaculaire'];
349
					$oss [] = $tab['nom_vernaculaire'];
350
				}
351
			}
352
		}
932 aurelien 353
		$masque = $this->recupererMasque();
354
		if($masque == "") {
355
			$masque = "Pas de masque";
356
		}
721 aurelien 357
		$table_retour_oss = array($masque, $oss);
358
		return $table_retour_oss;
359
	}
360
 
361
	public function formaterNomsVernaculairesAttributions($resultat) {
362
		$table_retour_json['entete']['masque'] = $this->recupererMasque();
363
		$table_retour_json['entete']['depart'] = $this->limite_requete['depart'];
364
		$table_retour_json['entete']['limite'] = $this->limite_requete['limite'];
365
		$table_retour_json['entete']['total']  = $this->total_resultat;
366
		$url = $this->formulerUrl($this->total_resultat, '/noms-vernaculaires/attributions');
367
		if (isset($url['precedent']) && $url['precedent'] != '') {
368
			$table_retour_json['entete']['href.precedent'] = $url['precedent'];
369
		}
370
		if (isset($url['suivant']) && $url['suivant']   != '') {
371
			$table_retour_json['entete']['href.suivant']   = $url['suivant'];
372
		}
373
		foreach ($resultat as &$tab) {
374
			unset($tab['tri']);
932 aurelien 375
			$id_nom = $tab['num_nom_vernaculaire'];
376
			$resultat_json[$id_nom]['id'] = $id_nom;
377
			$resultat_json[$id_nom]['nom_vernaculaire'] = $tab['nom_vernaculaire'];
378
			$resultat_json[$id_nom]['code_langue'] = $tab['code_langue'];
379
			$resultat_json[$id_nom]['taxon.code'] = 'bdtxa.nt:'.$tab['num_taxon'];
721 aurelien 380
			if ($this->retour_format == 'max') {
932 aurelien 381
				$resultat_json[$id_nom]['num_taxon'] = $tab['num_taxon'];
382
				$resultat_json[$id_nom]['nom_retenu.code'] = $tab['num_taxon'];
383
				$resultat_json[$id_nom]['taxon'] = $tab['num_taxon'];
721 aurelien 384
				$this->taxons[] = $tab['num_taxon']; // utilisé pour chercher les noms latins plus bas
932 aurelien 385
				$resultat_json[$id_nom]['href'] = $this->ajouterHref('noms-vernaculaires', $id_nom);
721 aurelien 386
 
387
				if($this->champs_supp != array()) {
932 aurelien 388
					$resultat_json[$id_nom] = $this->ajouterChampsOntologieLigneResultat($tab);
721 aurelien 389
				}
390
			}
391
		}
392
 
393
		if ($this->retour_format == 'max') {
394
			// On est obligé de faire un deuxième boucle pour demander tous les taxons présents en une
395
			// fois et les attribuer aux noms car c'est beaucoup plus rapide
396
			$noms_sci = $this->recupererNomTaxons();
397
			foreach ($resultat_json as $num_nom => &$tab) {
398
				$tab = $this->ajouterTaxonsAttributionsLigneResultat($tab, $noms_sci);
399
				if($tab == null) {
400
					unset($resultat_json[$num_nom]);
401
				}
402
			}
403
		}
404
 
405
		$table_retour_json['resultat'] = $resultat_json;
406
		return $table_retour_json;
407
	}
408
 
409
	/**
410
	 * Ajoute les champs d'ontologie supplémentaires si necéssaire
411
	 * en faisant appels aux web services associés
412
	 * @param array $ligne_resultat
413
	 *
414
	 * @return array la ligne modifiée
415
	 */
416
	public function ajouterChampsOntologieLigneResultat($ligne_resultat) {
417
 
418
		$intitule = '';
419
		foreach($this->champ_infos as $cle => $champs_supplementaires) {
420
			if(in_array($cle, $this->champs_supp)) {
421
				extract($champs_supplementaires);
422
				$valeur_recherche = '';
423
				switch($cle) {
424
					case 'taxon':
425
						$valeur_recherche = $ligne_resultat['num_taxon'];
426
						$intitule = 'taxon.code';
427
						break;
428
				}
429
				$code_valeur = '';
430
				if(trim($valeur_recherche) != '') {
431
					$url = $this->ajouterHrefAutreProjet($service, $ressource, $valeur_recherche, $projet);
432
					$code_valeur = $this->chercherSignificationCode($url, $nom);
433
				}
434
				$ligne_resultat[$intitule] = $code_valeur;
435
			}
436
		}
437
		return $ligne_resultat;
438
	}
439
 
440
	/**
441
	 * Fonction qui ajoute les attributions à une ligne de résultats
442
	 *
443
	 * @param array $ligne_tableau_resultat
444
	 * @param array $nom_sci
445
	 */
446
	public function ajouterTaxonsAttributionsLigneResultat(&$ligne_tableau_resultat, &$noms_sci) {
447
		if (isset($noms_sci[$ligne_tableau_resultat['num_taxon']])) {
448
			$ligne_tableau_resultat['nom_retenu.code'] = $noms_sci[$ligne_tableau_resultat['num_taxon']]['id'];
449
			$ligne_tableau_resultat['taxon'] = $noms_sci[$ligne_tableau_resultat['num_taxon']]['nom_sci'];
450
		} else {
451
			$ligne_tableau_resultat = null;
452
		}
453
		return $ligne_tableau_resultat;
454
	}
455
 
456
	private function trierLigneTableau($a, $b) {
457
		$retour = 0;
458
 
459
		if ($a[$this->champ_tri] == $b[$this->champ_tri]) {
460
			$retour = 0;
461
		}
462
 
463
		if($this->champ_tri == 'code_langue') {
464
			if ($a[$this->champ_tri] == 'fra' && $b[$this->champ_tri] != 'fra') {
465
				$retour = ($this->direction_tri == 'asc') ? -1 : 1;
466
			} else if ($a[$this->champ_tri] != 'fra' && $b[$this->champ_tri] == 'fra') {
467
				$retour = ($this->direction_tri == 'asc') ? 1 : -1;
468
			} else {
469
				$retour = $this->comparerChaineSelonDirectionTri($a[$this->champ_tri], $b[$this->champ_tri]);
470
			}
471
		} else {
472
			$retour = $this->comparerChaineSelonDirectionTri($a[$this->champ_tri], $b[$this->champ_tri]);
473
		}
474
		return $retour;
475
	}
476
 
477
	private function comparerChaineSelonDirectionTri($a, $b) {
478
		if($this->direction_tri == 'asc') {
479
			return ($a < $b) ? -1 : 1;
480
		} else {
481
			return ($a > $b) ? -1 : 1;
482
		}
483
	}
484
 
485
	// formatage de la reponse /id ss la forme
486
	// id, nom_vernaculaire, attributions
487
	// langue
488
	// num_nom (correspond à un taxon bdtxa)
489
	public function formaterNomsVernaculairesId($resultat) {
490
		foreach ($resultat as $taxon) { // pour chaque attribution à un taxon bdtxa
491
			// on crée les variables qui serviront de clés et on les enléves du tableau
932 aurelien 492
			$num_nom = $taxon['num_nom_vernaculaire']; // unique pour un trinôme id, langue, taxon
493
			unset($taxon['num_nom_vernaculaire']);
721 aurelien 494
			$langue = $taxon['code_langue'];
495
			unset($taxon['code_langue']);
496
 
497
			foreach ($this->correspondance_champs as $key => $correspondance) { // ordonne les infos pour affichage
498
				if (isset($taxon[$key]) && $taxon[$key] != "") {
499
					$this->afficherDonnees($correspondance, $taxon[$key], $langue, $num_nom);
500
				}
501
			}
502
			foreach ($taxon as $key => $valeur) { // rajoute les champs non prévus dans l'api
503
				if (!isset($this->correspondance_champs[$key]) && $valeur != "") {
504
					$this->afficherDonnees($key, $valeur, $langue, $num_nom);
505
				}
506
			}
507
		}
508
		if ($this->retour_format == 'max') $this->afficherTaxons(); // va chercher les noms de tous les taxons
509
		unset($this->table_retour['href']);
510
		return $this->table_retour;
511
	}
512
 
513
	public function afficherDonnees($champ, $valeur, $langue = '', $num_nom = '') {
932 aurelien 514
		if ($champ == 'num_nom_vernaculaire' || $champ == 'nom_vernaculaire') {
721 aurelien 515
			$this->table_retour[$champ] = $valeur;
516
		} elseif (preg_match('/^(.*)\.code$/', $champ, $match)) {
517
				switch ($match[1]) {
518
					case 'taxon'	: if ($this->retour_format == 'max') {$this->taxons[$num_nom] = $valeur;}
519
						$this->afficherPointCode($match[1], $langue, $num_nom, $valeur);	break;
520
					case 'langue'	: //$this->afficherPointCode($match[1], 'iso-639-3', 'langues', $valeur);
521
						break;
522
					default : break;
523
				}
524
 
525
		} elseif ($langue != '') {
526
			$this->table_retour['attributions'][$langue][$num_nom][$champ] = $valeur;
527
		} else {
528
			$this->table_retour[$champ] = $valeur;
529
		}
530
	}
531
 
532
	public function afficherPointCode($nomChamp, $langue, $num_nom, $valeur) {
533
		if (isset($this->champ_infos[$nomChamp])) {
534
			extract($this->champ_infos[$nomChamp]);
535
		}
536
 
537
		if ($this->retour_format == 'max') {
538
			$url = $this->ajouterHrefAutreProjet($service, $ressource, $valeur, $projet);
539
			if ($service == 'taxons') {
540
				$code_valeur = '';
541
				$this->table_retour['attributions'][$langue][$num_nom]['nom_retenu.code'] = $code_valeur;
542
			} else {
543
				$code_valeur = $this->chercherSignificationCode($url, $nom);
544
			}
545
			if ($projet != '') $projet .= '.';
546
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp] = $code_valeur;
547
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp.'.code'] = $projet.$ressource.$valeur;
548
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp.'.href'] = $url;
549
		} else {
550
			if ($projet != '') $projet .= '.';
551
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp.'.code'] = $projet.$ressource.$valeur;
552
		}
553
	}
554
 
555
	public function chercherSignificationCode($url, $nom) {
556
		if (isset($this->signification_code[$url])) {
557
			$valeur = $this->signification_code[$url];
558
		} else {
559
			$res = $this->consulterHref($url);
560
			$valeur = $res->$nom;
561
			$this->signification_code[$url] = $valeur;
562
		}
563
		return $valeur;
564
	}
565
 
566
	public function afficherTaxons() {
567
		$resultat = $this->recupererNomTaxons();
568
		foreach ($this->table_retour['attributions'] as $code_langue=>$langue) {
569
			foreach ($langue as $num_nom=>$taxon) {
570
				$num_tax = ltrim($taxon['taxon.code'], 'bdtxa.nt:');
571
				if (isset($resultat[$num_tax])) {
572
					$this->table_retour['attributions'][$code_langue][$num_nom]['nom_retenu.code'] = $resultat[$num_tax]['id'];
573
					$this->table_retour['attributions'][$code_langue][$num_nom]['taxon'] = $resultat[$num_tax]['nom_sci'];
574
				}
575
			}
576
		}
577
	}
578
 
579
	public function recupererNomTaxons() {
580
		$taxons = array_unique($this->taxons);
581
		$url = Config::get('url_service_base').'bdtxa/taxons?navigation.limite=500&ns.structure=au&masque.nt='.implode(',', $taxons);
582
		$res = $this->consulterHref($url);
583
		foreach ($res->resultat as $id=>$taxon) {
584
			$resultat[$taxon->num_taxonomique]['id'] = 'bdtxa.nn:'.$id;
585
			$resultat[$taxon->num_taxonomique]['nom_sci'] = $taxon->nom_sci_complet;
586
		}
587
		return $resultat;
588
	}
589
 
590
	public function formaterNomsVernaculairesIdChamp($resultat) {
591
		$this->table_retour['id'] = $this->ressources[0];
592
		$champs = explode(' ', $this->ressources[1]);
593
		if (in_array('attributions', $champs) != false) {
594
			$this->formaterNomsVernaculairesId($resultat);
595
			unset($this->table_retour['nom_vernaculaire']);
596
		} else {
597
			$champ_attributions = array('num_taxon', 'zone_usage', 'num_statut', 'num_genre', 'notes');
598
			foreach ($resultat as $taxon) {
599
				foreach ($taxon as $key=>$valeur) {
600
					if ($key == 'code_langue' && in_array('langue', $champs) != false) {
601
						$this->table_retour['attributions']['langue'][] = $valeur;
602
					} elseif (in_array($key, $champ_attributions) != false) {
603
						$this->afficherPoint($this->correspondance_champs[$key] , $valeur, $taxon['code_langue'], $taxon['num_nom_vernaculaire']);
604
					} elseif (in_array($key, $champs) != false) {
605
						$this->table_retour[$key] = $valeur;
606
					}
607
				}
608
				if (in_array('biblio', $champs) != false) $this->chargerBiblio($taxon['num_nom_vernaculaire'], $taxon['code_langue']);
609
			}
610
			if (in_array('biblio', $champs) != false && array_search('biblio.num_ref', $this->table_retour) != false) $this->table_retour['biblio'] = null;
611
		}
612
		return $this->table_retour;
613
	}
614
 
615
	public function afficherPoint($champ, $valeur, $langue, $num_nom) {
616
		preg_match('/^(.*)\.code$/', $champ, $match);
617
		$champ = $match[1];
618
		if (isset($this->champ_infos[$champ])) {
619
			extract($this->champ_infos[$champ]);
620
			$url = $this->ajouterHrefAutreProjet($service, $ressource, $valeur, $projet);
621
			$projet .= '.';
622
		}
623
 
624
		$champs = explode(' ', $this->ressources[1]);
625
		if (in_array($champ.'.*', $champs) !== false && isset($projet)) {
626
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.code'] = $projet.$ressource.$valeur;
627
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.href'] = $url;
628
		}
629
		if (in_array($champ.'.code', $champs) !== false && isset($projet)) {
630
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.code'] = $projet.$ressource.$valeur;
631
		}
632
		if (in_array($champ.'.href', $champs) !== false && isset($projet)) {
633
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.href'] = $url;
634
		}
635
		if (in_array($champ, $champs) !== false) {
636
			if (isset($url)) {
637
				$this->table_retour['attributions'][$langue][$num_nom][$champ] = $this->chercherSignificationCode($url, $nom);
638
			} else {
639
				$this->table_retour['attributions'][$langue][$champ] = $valeur;
640
			}
641
		}
642
	}
643
 
644
	public function afficherLangue($nomChamp, $projet, $service, $valeur, $ressource = '', $nom = 'nom') {
645
		if ($this->retour_format == 'max') {
646
				$this->table_retour['attributions'][$nomChamp] = $nom;
647
				$this->table_retour['attributions'][$nomChamp.'.code'] = $projet.$ressource.$valeur;
648
				$this->table_retour['attributions'][$nomChamp.'.href'] = $url;
649
		} else {
650
			$this->table_retour['attributions'][$nomChamp.'.code'] = $projet.$ressource.$valeur;
651
		}
652
	}
653
}
654
?>