Subversion Repositories eFlore/Projets.eflore-projets

Rev

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