Subversion Repositories eFlore/Projets.eflore-projets

Rev

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