Subversion Repositories eFlore/Projets.eflore-projets

Rev

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