Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 277 | Rev 280 | 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();
277 aurelien 192
 
6 jpm 193
		return $requete;
194
	}
109 jpm 195
 
6 jpm 196
	public  function formerRequeteChamp() {
197
		if (in_array('*', $this->requete_champ)) {
198
			$champ = ' * ';
199
		} else {
200
			$champ = implode(', ', $this->requete_champ);
201
		}
202
		return $champ;
203
	}
109 jpm 204
 
6 jpm 205
	public  function formerRequeteCondition() {
206
		$condition = '';
109 jpm 207
		if ($this->requete_condition != null) {
6 jpm 208
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
109 jpm 209
		}
6 jpm 210
		return $condition;
211
	}
109 jpm 212
 
213
	//ajout d'une limite seulement pour les listes (pas plus de 100 resultats retournés pr les requetes
6 jpm 214
	// suivantes : /noms-vernaculaires et /noms-vernaculaires/#id/relations)
215
	public function formerRequeteLimite() {
216
		if (in_array($this->format_reponse , array($this->service.'/id', $this->service.'/id/champs'))) {
109 jpm 217
			$this->requete_limite = '';
6 jpm 218
		} elseif (($depart = $this->limite_requete['depart']) > ($this->total_resultat = $this->recupererTotalResultat())) {
109 jpm 219
			$this->limite_requete['depart'] =
6 jpm 220
				(($this->total_resultat - $this->limite_requete['limite']) < 0) ? 0 : ($this->total_resultat - $this->limite_requete['limite']);
109 jpm 221
			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
6 jpm 222
		} else {
109 jpm 223
			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
6 jpm 224
		}
225
		return $this->requete_limite;
226
	}
109 jpm 227
 
6 jpm 228
	//on récupère le nombre total de résultats de la requete (ex : le nombre d'id contenu dans la liste /noms-vernaculaires)
229
	public function recupererTotalResultat() {
160 delphine 230
		$distinct = ($this->format_reponse == 'noms-vernaculaires/attributions') ? 'id' : 'distinct(id)';
231
		$requete = 'SELECT count('.$distinct.') as nombre FROM '
6 jpm 232
			.$this->table
233
			.$this->formerRequeteCondition();
234
		$res = $this->getBdd()->recuperer($requete);
109 jpm 235
 
6 jpm 236
		if ($res) {
237
			$total = $res['nombre'];
238
		} else {
160 delphine 239
			$t = 'Fonction recupererTotalResultat() : <br/>Données introuvables dans la base '.$requete;
6 jpm 240
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $t);
241
		}
242
		return $total;
243
	}
109 jpm 244
 
245
	//+------------------------------------------------------------------------------------------------------+
246
	// determine en fct du service appelé (/noms-vernaculaires | /noms-vernaculaires/#id | /noms-vernaculaires/#id/champ |
6 jpm 247
	// /noms-vernaculaires/#id/relations) le format du tableau à retourner.
248
	public function retournerResultatFormate($resultat) {
249
		$this->recupererTableConfig('correspondance_champs');
250
		switch ($this->format_reponse) {
132 aurelien 251
			case 'noms-vernaculaires'				:
252
				$reponse = ($this->retour_format == 'oss') ? $this->formaterEnOss($resultat) : $this->formaterNomsVernaculaires($resultat);			break;
160 delphine 253
			case 'noms-vernaculaires/attributions'	: $reponse = $this->formaterNomsVernaculairesAttributions($resultat);	break;
6 jpm 254
			case 'noms-vernaculaires/id'			: $reponse = $this->formaterNomsVernaculairesId($resultat);			break;
255
			case 'noms-vernaculaires/id/champ'		: $reponse = $this->formaterNomsVernaculairesIdChamp($resultat);	break;
256
			default									:																	break;
257
		}
258
		return $reponse;
259
	}
109 jpm 260
 
6 jpm 261
	public function formaterNomsVernaculaires($resultat) {
262
		//on remplit la table $table_retour_json['entete']
160 delphine 263
		$table_retour_json['entete']['masque'] = $this->recupererMasque();
6 jpm 264
		$table_retour_json['entete']['depart'] = $this->limite_requete['depart'];
265
		$table_retour_json['entete']['limite'] = $this->limite_requete['limite'];
266
		$table_retour_json['entete']['total']  = $this->total_resultat;
267
		$url = $this->formulerUrl($this->total_resultat, '/noms-vernaculaires');
109 jpm 268
		if (isset($url['precedent']) && $url['precedent'] != '') { $table_retour_json['entete']['href.precedent'] = $url['precedent']; }
269
		if (isset($url['suivant']) && $url['suivant']   != '') { $table_retour_json['entete']['href.suivant']   = $url['suivant']; }
160 delphine 270
 
109 jpm 271
		//on remplit la table $table_retour_json['resultat']
152 delphine 272
		if (isset($this->parametres['masque.nv'])) {
273
			$resultat = $this->trierRechercheFloue($this->parametres['masque.nv'], $resultat, 'nom_vernaculaire');
6 jpm 274
		}
152 delphine 275
		if (isset($this->parametres['masque'])) {
276
			$resultat = $this->trierRechercheFloue($this->parametres['masque'], $resultat, 'nom_vernaculaire');
277
		}
6 jpm 278
		foreach ($resultat as $tab) {
279
			foreach ($tab as $key => $valeur) {
280
				if ($valeur != '') {
281
					switch ($key) {
282
						case 'id'				: $num = $valeur;								break;
283
						case 'nom_vernaculaire'	: $this->table_retour['nom'] = $valeur;			break;
284
						default					:												break;
285
					}
286
				}
287
			}
288
			if ($this->retour_format == 'max') $this->table_retour['href'] = $this->ajouterHref('noms-vernaculaires', $num);
289
			$resultat_json[$num] = $this->table_retour;
290
			$this->table_retour = array();
291
		}
109 jpm 292
 
6 jpm 293
		$table_retour_json['resultat'] = $resultat_json;
294
		return $table_retour_json;
295
	}
132 aurelien 296
 
160 delphine 297
	public function recupererMasque() {
298
		$tab_masque = array();
299
		foreach ($this->parametres as $param=>$valeur) {
300
			if (strstr($param, 'masque') != false) {
301
				$tab_masque[] = $param.'='.$valeur;
302
			}
303
		}
304
		$masque = implode('&', $tab_masque);
305
		return $masque;
306
	}
307
 
132 aurelien 308
	public function formaterEnOss($resultat) {
309
		$table_nom = array();
310
		$oss = '';
311
		foreach ($resultat as $tab) {
312
			if (isset($tab['nom_vernaculaire']) ) {
313
				if (!in_array($tab['nom_vernaculaire'], $table_nom)) {
314
					$table_nom[] = $tab['nom_vernaculaire'];
315
					$oss [] = $tab['nom_vernaculaire'];
316
				}
317
			}
318
		}
319
		if (isset($this->masque)) $masque = implode('&', $this->masque);
320
		else $masque = 'Pas de masque';
321
		$table_retour_oss = array($masque, $oss);
322
		return $table_retour_oss;
323
	}
160 delphine 324
 
325
	public function formaterNomsVernaculairesAttributions($resultat) {
326
		//on remplie la table $table_retour_json['entete']
327
		$table_retour_json['entete']['masque'] = $this->recupererMasque();
328
		$table_retour_json['entete']['depart'] = $this->limite_requete['depart'];
329
		$table_retour_json['entete']['limite'] = $this->limite_requete['limite'];
330
		$table_retour_json['entete']['total']  = $this->total_resultat;
331
		$url = $this->formulerUrl($this->total_resultat, '/noms-vernaculaires/attributions');
332
		if (isset($url['precedent']) && $url['precedent'] != '') {
333
			$table_retour_json['entete']['href.precedent'] = $url['precedent'];
334
		}
335
		if (isset($url['suivant']) && $url['suivant']   != '') {
336
			$table_retour_json['entete']['href.suivant']   = $url['suivant'];
337
		}
180 delphine 338
 
277 aurelien 339
		foreach ($resultat as $tab) {
160 delphine 340
			$resultat_json[$tab['num_nom_vernaculaire']]['id'] = $tab['id'];
341
			$resultat_json[$tab['num_nom_vernaculaire']]['nom_vernaculaire'] = $tab['nom_vernaculaire'];
342
			$resultat_json[$tab['num_nom_vernaculaire']]['langue.code'] = $tab['code_langue'];
343
			$resultat_json[$tab['num_nom_vernaculaire']]['taxon.code'] = 'bdtfx.nt:'.$tab['num_taxon'];
344
			if ($this->retour_format == 'max') {
345
				$resultat_json[$tab['num_nom_vernaculaire']]['taxon'] = $tab['num_taxon'];
164 delphine 346
				$resultat_json[$tab['num_nom_vernaculaire']]['nom_retenu.code'] = $tab['num_taxon'];
160 delphine 347
				$this->taxons[] = $tab['num_taxon']; // utiliser pour chercher les noms latins
348
				$resultat_json[$tab['num_nom_vernaculaire']]['href'] = $this->ajouterHref('noms-vernaculaires', $tab['id']);
349
			}
277 aurelien 350
 
351
			if($this->retour_format == 'max') {
352
				//TODO: la fonction afficherTaxonsAttributions itere sur tout le tableau de résultats alors qu'elle ne devrait
353
				// traiter qu'une ligne à la fois (i.e.: la ligne en cours dans $tab), ce qui serait bien plus rapide
354
				$resultat_json = $this->afficherTaxonsAttributions($resultat_json);
355
				$resultat_json[$tab['num_nom_vernaculaire']] = $this->afficherChampsSupplementairesOntologie($tab);
356
			}
160 delphine 357
		}
279 aurelien 358
		uasort($resultat_json, array($this,'trierLigneTableau'));
160 delphine 359
		$table_retour_json['resultat'] = $resultat_json;
360
		return $table_retour_json;
361
	}
362
 
277 aurelien 363
	public function afficherChampsSupplementairesOntologie($ligne_resultat) {
364
		foreach($this->champ_infos as $cle => $champs_supplementaires) {
365
			extract($champs_supplementaires);
366
			$valeur_recherche = '';
367
			switch($cle) {
368
				case 'taxon':
369
					$valeur_recherche = $ligne_resultat['num_taxon'];
370
					break;
371
				case 'genre':
372
					$valeur_recherche = $ligne_resultat['num_genre'];
373
					break;
374
				case 'conseil_emploi':
375
					$valeur_recherche = $ligne_resultat['num_statut'];
376
					break;
377
			}
378
			$code_valeur = '';
379
			if(trim($valeur_recherche) != '') {
380
				$url = $this->ajouterHrefAutreProjet($service, $ressource, $valeur_recherche, $projet);
381
				$code_valeur = $this->chercherSignificationCode($url, $nom);
382
			}
383
			$ligne_resultat[$cle] = $code_valeur;
384
		}
385
		return $ligne_resultat;
386
	}
387
 
160 delphine 388
	public function afficherTaxonsAttributions($resultat) {
389
		$nom_sci = $this->recupererNomTaxons();
390
		foreach ($resultat as $num=>$tab) {
391
			if (isset($nom_sci[$tab['taxon']])) {
164 delphine 392
				$resultat[$num]['nom_retenu.code'] = $nom_sci[$tab['taxon']]['id'];
393
				$resultat[$num]['taxon'] = $nom_sci[$tab['taxon']]['nom_sci'];
160 delphine 394
			}
395
		}
396
		return $resultat;
397
	}
398
 
279 aurelien 399
	private function trierLigneTableau($a, $b) {
400
		if ($a[$this->champ_tri] == $b[$this->champ_tri]) return 0;
401
		if($this->direction_tri == 'asc') {
402
			return ($a[$this->champ_tri] < $b[$this->champ_tri]) ? -1 : 1;
403
		} else {
404
			return ($a[$this->champ_tri] > $b[$this->champ_tri]) ? -1 : 1;
405
		}
406
	}
407
 
6 jpm 408
	// formatage de la reponse /id ss la forme
409
	// id, nom_vernaculaire, attributions
410
	// langue
411
	// num_nom (correspond à un taxon bdtfx)
412
	public function formaterNomsVernaculairesId($resultat) {
413
		foreach ($resultat as $taxon) { // pour chaque attribution à un taxon bdtfx
414
			// on crée les variables qui serviront de clés et on les enléves du tableau
415
			$num_nom = $taxon['num_nom_vernaculaire']; // unique pour un trinôme id, langue, taxon
416
			unset($taxon['num_nom_vernaculaire']);
417
			$langue = $taxon['code_langue'];
418
			unset($taxon['code_langue']);
109 jpm 419
 
6 jpm 420
			foreach ($this->correspondance_champs as $key => $correspondance) { // ordonne les infos pour affichage
421
				if (isset($taxon[$key]) && $taxon[$key] != "") {
422
					$this->afficherDonnees($correspondance, $taxon[$key], $langue, $num_nom);
423
				}
424
			}
425
			foreach ($taxon as $key => $valeur) { // rajoute les champs non prévus dans l'api
426
				if (!isset($this->correspondance_champs[$key]) && $valeur != "") {
427
					$this->afficherDonnees($key, $valeur, $langue, $num_nom);
428
				}
429
			}
430
			if ($this->retour_format == 'max') $this->chargerBiblio($num_nom, $langue);
431
		}
432
		if ($this->retour_format == 'max') $this->afficherTaxons(); // va chercher les noms de tous les taxons
433
		unset($this->table_retour['href']);
434
		return $this->table_retour;
435
	}
109 jpm 436
 
6 jpm 437
	public function afficherDonnees($champ, $valeur, $langue = '', $num_nom = '') {
438
		if ($champ == 'id' || $champ == 'nom_vernaculaire') {
439
			$this->table_retour[$champ] = $valeur;
440
		} elseif (preg_match('/^(.*)\.code$/', $champ, $match)) {
441
				switch ($match[1]) {
442
					case 'taxon'	: if ($this->retour_format == 'max') {$this->taxons[$num_nom] = $valeur;}
443
						$this->afficherPointCode($match[1], $langue, $num_nom, $valeur);	break;
109 jpm 444
					case 'langue'	: //$this->afficherPointCode($match[1], 'iso-639-3', 'langues', $valeur);
6 jpm 445
						break;
446
					case 'genre'	: $this->afficherPointCode($match[1], $langue, $num_nom, $valeur);	break;
447
					case 'conseil_emploi'	: $this->afficherPointCode($match[1], $langue, $num_nom, $valeur);	break;
448
					default : break;
449
				}
109 jpm 450
 
6 jpm 451
		} elseif ($langue != '') {
452
			$this->table_retour['attributions'][$langue][$num_nom][$champ] = $valeur;
453
		} else {
454
			$this->table_retour[$champ] = $valeur;
455
		}
456
	}
109 jpm 457
 
6 jpm 458
	public function afficherPointCode($nomChamp, $langue, $num_nom, $valeur) {
459
		if (isset($this->champ_infos[$nomChamp])) {
460
			extract($this->champ_infos[$nomChamp]);
461
		}
109 jpm 462
 
6 jpm 463
		if ($this->retour_format == 'max') {
464
			$url = $this->ajouterHrefAutreProjet($service, $ressource, $valeur, $projet);
465
			if ($service == 'taxons') {
466
				$code_valeur = '';
164 delphine 467
				$this->table_retour['attributions'][$langue][$num_nom]['nom_retenu.code'] = $code_valeur;
109 jpm 468
			} else {
6 jpm 469
				$code_valeur = $this->chercherSignificationCode($url, $nom);
470
			}
471
			if ($projet != '') $projet .= '.';
472
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp] = $code_valeur;
473
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp.'.code'] = $projet.$ressource.$valeur;
474
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp.'.href'] = $url;
475
		} else {
476
			if ($projet != '') $projet .= '.';
477
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp.'.code'] = $projet.$ressource.$valeur;
478
		}
479
	}
109 jpm 480
 
6 jpm 481
	public function chercherSignificationCode($url, $nom) {
482
		if (isset($this->signification_code[$url])) {
483
			$valeur = $this->signification_code[$url];
484
		} else {
485
			$res = $this->consulterHref($url);
486
			$valeur = $res->$nom;
487
			$this->signification_code[$url] = $valeur;
488
		}
489
		return $valeur;
490
	}
109 jpm 491
 
6 jpm 492
	public function afficherTaxons() {
160 delphine 493
		$resultat = $this->recupererNomTaxons();
6 jpm 494
		foreach ($this->table_retour['attributions'] as $code_langue=>$langue) {
495
			foreach ($langue as $num_nom=>$taxon) {
496
				$num_tax = ltrim($taxon['taxon.code'], 'bdtfx.nt:');
497
				if (isset($resultat[$num_tax])) {
164 delphine 498
					$this->table_retour['attributions'][$code_langue][$num_nom]['nom_retenu.code'] = $resultat[$num_tax]['id'];
499
					$this->table_retour['attributions'][$code_langue][$num_nom]['taxon'] = $resultat[$num_tax]['nom_sci'];
6 jpm 500
				}
501
			}
502
		}
503
	}
160 delphine 504
 
505
	public function recupererNomTaxons() {
506
		$url = Config::get('url_service_base').'bdtfx/taxons?navigation.limite=500&masque.nt='.
507
			$this->getBdd()->proteger(implode(',', $this->taxons));
508
		$res = $this->consulterHref($url);
509
		foreach ($res->resultat as $id=>$taxon) {
164 delphine 510
			$resultat[$taxon->num_taxonomique]['id'] = 'bdtfx.nn:'.$id;
511
			$resultat[$taxon->num_taxonomique]['nom_sci'] = $taxon->nom_sci;
160 delphine 512
		}
513
		return $resultat;
514
	}
6 jpm 515
 
516
	public function formaterNomsVernaculairesIdChamp($resultat) {
109 jpm 517
		$this->table_retour['id'] = $this->ressources[0];
518
		$champs = explode(' ', $this->ressources[1]);
6 jpm 519
		if (in_array('attributions', $champs) != false) {
520
			$this->formaterNomsVernaculairesId($resultat);
521
			unset($this->table_retour['nom_vernaculaire']);
160 delphine 522
		} else {
6 jpm 523
			$champ_attributions = array('num_taxon', 'zone_usage', 'num_statut', 'num_genre', 'notes');
524
			foreach ($resultat as $taxon) {
525
				foreach ($taxon as $key=>$valeur) {
526
					if ($key == 'code_langue' && in_array('langue', $champs) != false) {
527
						$this->table_retour['attributions']['langue'][] = $valeur;
528
					} elseif (in_array($key, $champ_attributions) != false) {
529
						$this->afficherPoint($this->correspondance_champs[$key] , $valeur, $taxon['code_langue'], $taxon['num_nom_vernaculaire']);
530
					} elseif (in_array($key, $champs) != false) {
531
						$this->table_retour[$key] = $valeur;
532
					}
533
				}
534
				if (in_array('biblio', $champs) != false) $this->chargerBiblio($taxon['num_nom_vernaculaire'], $taxon['code_langue']);
535
			}
536
			if (in_array('biblio', $champs) != false && array_search('biblio.num_ref', $this->table_retour) != false) $this->table_retour['biblio'] = null;
537
		}
538
		return $this->table_retour;
539
	}
109 jpm 540
 
6 jpm 541
	public function afficherPoint($champ, $valeur, $langue, $num_nom) {
542
		preg_match('/^(.*)\.code$/', $champ, $match);
543
		$champ = $match[1];
544
		if (isset($this->champ_infos[$champ])) {
545
			extract($this->champ_infos[$champ]);
546
			$url = $this->ajouterHrefAutreProjet($service, $ressource, $valeur, $projet);
547
			$projet .= '.';
548
		}
109 jpm 549
 
550
		$champs = explode(' ', $this->ressources[1]);
6 jpm 551
		if (in_array($champ.'.*', $champs) !== false && isset($projet)) {
552
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.code'] = $projet.$ressource.$valeur;
553
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.href'] = $url;
109 jpm 554
		}
6 jpm 555
		if (in_array($champ.'.code', $champs) !== false && isset($projet)) {
556
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.code'] = $projet.$ressource.$valeur;
109 jpm 557
		}
6 jpm 558
		if (in_array($champ.'.href', $champs) !== false && isset($projet)) {
559
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.href'] = $url;
109 jpm 560
		}
6 jpm 561
		if (in_array($champ, $champs) !== false) {
562
			if (isset($url)) {
563
				$this->table_retour['attributions'][$langue][$num_nom][$champ] = $this->chercherSignificationCode($url, $nom);
564
			} else {
565
				$this->table_retour['attributions'][$langue][$champ] = $valeur;
566
			}
109 jpm 567
		}
6 jpm 568
	}
569
 
570
	public function afficherLangue($nomChamp, $projet, $service, $valeur, $ressource = '', $nom = 'nom') {
571
		if ($this->retour_format == 'max') {
572
				$this->table_retour['attributions'][$nomChamp] = $nom;
573
				$this->table_retour['attributions'][$nomChamp.'.code'] = $projet.$ressource.$valeur;
574
				$this->table_retour['attributions'][$nomChamp.'.href'] = $url;
575
		} else {
576
			$this->table_retour['attributions'][$nomChamp.'.code'] = $projet.$ressource.$valeur;
577
		}
578
	}
109 jpm 579
 
6 jpm 580
	public function chargerBiblio($num_nom, $langue) {
581
		list($table, $version) = explode('_v',$this->table);
582
		$requete = "SELECT b.*, lb.notes FROM nvjfl_lien_biblio_v$version lb, nvjfl_biblio_v$version b ".
583
					"WHERE b.num_ref = lb.num_ref AND lb.num_nom = '$num_nom' ;";
277 aurelien 584
		$resultat = $this->getBdd()->recupererTous($requete);
109 jpm 585
 
6 jpm 586
		 if ($resultat == '') { //cas ou la requete comporte des erreurs
587
		 	$r = 'La requête SQL formée comporte une erreur !!';
588
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $r);
589
			Debug::printr($requete);
590
		 } elseif ($resultat) {
591
			foreach ($resultat as $res) {
592
			   	foreach ($res as $cle => $valeur) {
593
			   		if ($valeur !== "") {
594
			   			$this->table_retour['attributions'][$langue][$num_nom]['biblio.'.$cle] = $valeur;
595
			   		}
596
			    }
597
			}
598
		}
599
	}
109 jpm 600
 
6 jpm 601
}
602
?>