Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 627 | Rev 729 | 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
 
563 aurelien 78
	public function __construct($config) {
536 gduche 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().
659 delphine 206
					', CASE num_statut WHEN "" THEN 1 ELSE 0 END AS is_null '.
6 jpm 207
					' FROM '.$this->table
659 delphine 208
					.$this->formerRequeteCondition().
209
					' ORDER BY is_null ASC, num_statut ASC '
6 jpm 210
					.$this->formerRequeteLimite();
211
		return $requete;
212
	}
109 jpm 213
 
6 jpm 214
	public  function formerRequeteChamp() {
215
		if (in_array('*', $this->requete_champ)) {
216
			$champ = ' * ';
217
		} else {
218
			$champ = implode(', ', $this->requete_champ);
219
		}
220
		return $champ;
221
	}
109 jpm 222
 
6 jpm 223
	public  function formerRequeteCondition() {
224
		$condition = '';
109 jpm 225
		if ($this->requete_condition != null) {
6 jpm 226
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
109 jpm 227
		}
6 jpm 228
		return $condition;
229
	}
109 jpm 230
 
231
	//ajout d'une limite seulement pour les listes (pas plus de 100 resultats retournés pr les requetes
6 jpm 232
	// suivantes : /noms-vernaculaires et /noms-vernaculaires/#id/relations)
233
	public function formerRequeteLimite() {
234
		if (in_array($this->format_reponse , array($this->service.'/id', $this->service.'/id/champs'))) {
109 jpm 235
			$this->requete_limite = '';
6 jpm 236
		} elseif (($depart = $this->limite_requete['depart']) > ($this->total_resultat = $this->recupererTotalResultat())) {
109 jpm 237
			$this->limite_requete['depart'] =
6 jpm 238
				(($this->total_resultat - $this->limite_requete['limite']) < 0) ? 0 : ($this->total_resultat - $this->limite_requete['limite']);
109 jpm 239
			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
6 jpm 240
		} else {
109 jpm 241
			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
6 jpm 242
		}
243
		return $this->requete_limite;
244
	}
109 jpm 245
 
6 jpm 246
	//on récupère le nombre total de résultats de la requete (ex : le nombre d'id contenu dans la liste /noms-vernaculaires)
247
	public function recupererTotalResultat() {
160 delphine 248
		$distinct = ($this->format_reponse == 'noms-vernaculaires/attributions') ? 'id' : 'distinct(id)';
249
		$requete = 'SELECT count('.$distinct.') as nombre FROM '
6 jpm 250
			.$this->table
251
			.$this->formerRequeteCondition();
252
		$res = $this->getBdd()->recuperer($requete);
109 jpm 253
 
6 jpm 254
		if ($res) {
255
			$total = $res['nombre'];
256
		} else {
160 delphine 257
			$t = 'Fonction recupererTotalResultat() : <br/>Données introuvables dans la base '.$requete;
6 jpm 258
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $t);
259
		}
260
		return $total;
261
	}
109 jpm 262
 
263
	//+------------------------------------------------------------------------------------------------------+
264
	// determine en fct du service appelé (/noms-vernaculaires | /noms-vernaculaires/#id | /noms-vernaculaires/#id/champ |
6 jpm 265
	// /noms-vernaculaires/#id/relations) le format du tableau à retourner.
266
	public function retournerResultatFormate($resultat) {
267
		$this->recupererTableConfig('correspondance_champs');
268
		switch ($this->format_reponse) {
132 aurelien 269
			case 'noms-vernaculaires'				:
270
				$reponse = ($this->retour_format == 'oss') ? $this->formaterEnOss($resultat) : $this->formaterNomsVernaculaires($resultat);			break;
160 delphine 271
			case 'noms-vernaculaires/attributions'	: $reponse = $this->formaterNomsVernaculairesAttributions($resultat);	break;
6 jpm 272
			case 'noms-vernaculaires/id'			: $reponse = $this->formaterNomsVernaculairesId($resultat);			break;
273
			case 'noms-vernaculaires/id/champ'		: $reponse = $this->formaterNomsVernaculairesIdChamp($resultat);	break;
274
			default									:																	break;
275
		}
276
		return $reponse;
277
	}
608 mathilde 278
 
279
	public function ajouterJsonEnTeteNV() {
280
		$table_retour_json['masque'] = $this->recupererMasque();
281
		$table_retour_json['depart'] = $this->limite_requete['depart'];
282
		$table_retour_json['limite'] = $this->limite_requete['limite'];
283
		$table_retour_json['total']  = $this->total_resultat;
6 jpm 284
		$url = $this->formulerUrl($this->total_resultat, '/noms-vernaculaires');
608 mathilde 285
		if (isset($url['precedent']) && $url['precedent'] != '') {
286
			$table_retour_json['href.precedent'] = $url['precedent'];
6 jpm 287
		}
608 mathilde 288
		if (isset($url['suivant']) && $url['suivant']   != '') {
289
			$table_retour_json['href.suivant']   = $url['suivant'];
152 delphine 290
		}
608 mathilde 291
		return $table_retour_json;
292
	}
293
 
294
	public function ajouterJsonResultatNV($resultat) {
6 jpm 295
		foreach ($resultat as $tab) {
296
			foreach ($tab as $key => $valeur) {
297
				if ($valeur != '') {
298
					switch ($key) {
299
						case 'id'				: $num = $valeur;								break;
300
						case 'nom_vernaculaire'	: $this->table_retour['nom'] = $valeur;			break;
301
						default					:												break;
302
					}
303
				}
304
			}
608 mathilde 305
		    if ($this->retour_format == 'max') $this->table_retour['href'] = $this->ajouterHref('noms-vernaculaires', $num);
6 jpm 306
			$resultat_json[$num] = $this->table_retour;
307
			$this->table_retour = array();
308
		}
608 mathilde 309
		return  $resultat_json;
310
	}
109 jpm 311
 
608 mathilde 312
 
313
	public function formaterNomsVernaculaires($resultat) {
314
		$table_retour_json['entete'] = $this->ajouterJsonEnTeteNV();
315
		$resultat = $this->hierarchiserResultat($resultat);
316
		$table_retour_json['resultat'] = $this->ajouterJsonResultatNV($resultat);
6 jpm 317
		return $table_retour_json;
318
	}
132 aurelien 319
 
608 mathilde 320
	public function hierarchiserResultat($resultat) {
321
		//tri recherche floue
322
		if (isset($this->parametres['masque.nv'])) {
323
			$resultat = $this->trierRechercheFloue($this->parametres['masque.nv'], $resultat, 'nom_vernaculaire');
324
		}
325
		if (isset($this->parametres['masque'])) {
326
			$resultat = $this->trierRechercheFloue($this->parametres['masque'], $resultat, 'nom_vernaculaire');
327
		}
328
		return $resultat;
329
	}
330
 
160 delphine 331
	public function recupererMasque() {
332
		$tab_masque = array();
333
		foreach ($this->parametres as $param=>$valeur) {
334
			if (strstr($param, 'masque') != false) {
335
				$tab_masque[] = $param.'='.$valeur;
336
			}
337
		}
338
		$masque = implode('&', $tab_masque);
339
		return $masque;
340
	}
341
 
132 aurelien 342
	public function formaterEnOss($resultat) {
343
		$table_nom = array();
344
		$oss = '';
345
		foreach ($resultat as $tab) {
346
			if (isset($tab['nom_vernaculaire']) ) {
347
				if (!in_array($tab['nom_vernaculaire'], $table_nom)) {
348
					$table_nom[] = $tab['nom_vernaculaire'];
349
					$oss [] = $tab['nom_vernaculaire'];
350
				}
351
			}
352
		}
353
		if (isset($this->masque)) $masque = implode('&', $this->masque);
354
		else $masque = 'Pas de masque';
355
		$table_retour_oss = array($masque, $oss);
356
		return $table_retour_oss;
357
	}
160 delphine 358
 
359
	public function formaterNomsVernaculairesAttributions($resultat) {
360
		//on remplie la table $table_retour_json['entete']
361
		$table_retour_json['entete']['masque'] = $this->recupererMasque();
362
		$table_retour_json['entete']['depart'] = $this->limite_requete['depart'];
363
		$table_retour_json['entete']['limite'] = $this->limite_requete['limite'];
364
		$table_retour_json['entete']['total']  = $this->total_resultat;
365
		$url = $this->formulerUrl($this->total_resultat, '/noms-vernaculaires/attributions');
366
		if (isset($url['precedent']) && $url['precedent'] != '') {
367
			$table_retour_json['entete']['href.precedent'] = $url['precedent'];
368
		}
369
		if (isset($url['suivant']) && $url['suivant']   != '') {
370
			$table_retour_json['entete']['href.suivant']   = $url['suivant'];
371
		}
284 jpm 372
		foreach ($resultat as &$tab) {
160 delphine 373
			$resultat_json[$tab['num_nom_vernaculaire']]['id'] = $tab['id'];
374
			$resultat_json[$tab['num_nom_vernaculaire']]['nom_vernaculaire'] = $tab['nom_vernaculaire'];
284 jpm 375
			$resultat_json[$tab['num_nom_vernaculaire']]['code_langue'] = $tab['code_langue'];
160 delphine 376
			$resultat_json[$tab['num_nom_vernaculaire']]['taxon.code'] = 'bdtfx.nt:'.$tab['num_taxon'];
377
			if ($this->retour_format == 'max') {
286 aurelien 378
				$resultat_json[$tab['num_nom_vernaculaire']]['num_taxon'] = $tab['num_taxon'];
379
				$resultat_json[$tab['num_nom_vernaculaire']]['nom_retenu.code'] = $tab['num_taxon'];
160 delphine 380
				$resultat_json[$tab['num_nom_vernaculaire']]['taxon'] = $tab['num_taxon'];
284 jpm 381
				$this->taxons[] = $tab['num_taxon']; // utilisé pour chercher les noms latins plus bas
160 delphine 382
				$resultat_json[$tab['num_nom_vernaculaire']]['href'] = $this->ajouterHref('noms-vernaculaires', $tab['id']);
286 aurelien 383
 
384
				if($this->champs_supp != array()) {
385
					$resultat_json[$tab['num_nom_vernaculaire']] = $this->ajouterChampsOntologieLigneResultat($tab);
386
				}
160 delphine 387
			}
280 aurelien 388
		}
659 delphine 389
 
280 aurelien 390
		if ($this->retour_format == 'max') {
284 jpm 391
			// On est obligé de faire un deuxième boucle pour demander tous les taxons présents en une
392
			// fois et les attribuer aux noms car c'est beaucoup plus rapide
393
			$noms_sci = $this->recupererNomTaxons();
286 aurelien 394
			foreach ($resultat_json as $num_nom => &$tab) {
395
				$tab = $this->ajouterTaxonsAttributionsLigneResultat($tab, $noms_sci);
627 aurelien 396
				if($tab == null) {
397
					unset($resultat_json[$num_nom]);
398
				}
284 jpm 399
			}
160 delphine 400
		}
280 aurelien 401
 
160 delphine 402
		$table_retour_json['resultat'] = $resultat_json;
403
		return $table_retour_json;
404
	}
405
 
286 aurelien 406
	/**
407
	 * Ajoute les champs d'ontologie supplémentaires si necéssaire
408
	 * en faisant appels aux web services associés
409
	 * @param array $ligne_resultat
410
	 *
411
	 * @return array la ligne modifiée
412
	 */
284 jpm 413
	public function ajouterChampsOntologieLigneResultat($ligne_resultat) {
414
 
415
		$intitule = '';
416
		foreach($this->champ_infos as $cle => $champs_supplementaires) {
286 aurelien 417
			if(in_array($cle, $this->champs_supp)) {
418
				extract($champs_supplementaires);
419
				$valeur_recherche = '';
420
				switch($cle) {
421
					case 'taxon':
422
						$valeur_recherche = $ligne_resultat['num_taxon'];
423
						$intitule = 'taxon.code';
424
						break;
425
					case 'genre':
426
						$valeur_recherche = $ligne_resultat['num_genre'];
427
						$intitule = 'genre';
428
						break;
429
					case 'conseil_emploi':
430
						$valeur_recherche = $ligne_resultat['num_statut'];
431
						$intitule = 'conseil_emploi';
432
						break;
433
				}
434
				$code_valeur = '';
435
				if(trim($valeur_recherche) != '') {
436
					$url = $this->ajouterHrefAutreProjet($service, $ressource, $valeur_recherche, $projet);
437
					$code_valeur = $this->chercherSignificationCode($url, $nom);
438
				}
439
				$ligne_resultat[$intitule] = $code_valeur;
277 aurelien 440
			}
441
		}
284 jpm 442
		return $ligne_resultat;
277 aurelien 443
	}
444
 
284 jpm 445
	/**
446
	 * Fonction qui ajoute les attributions à une ligne de résultats
447
	 *
448
	 * @param array $ligne_tableau_resultat
449
	 * @param array $nom_sci
450
	 */
451
	public function ajouterTaxonsAttributionsLigneResultat(&$ligne_tableau_resultat, &$noms_sci) {
285 aurelien 452
		if (isset($noms_sci[$ligne_tableau_resultat['num_taxon']])) {
453
			$ligne_tableau_resultat['nom_retenu.code'] = $noms_sci[$ligne_tableau_resultat['num_taxon']]['id'];
454
			$ligne_tableau_resultat['taxon'] = $noms_sci[$ligne_tableau_resultat['num_taxon']]['nom_sci'];
627 aurelien 455
		} else {
456
			$ligne_tableau_resultat = null;
284 jpm 457
		}
458
		return $ligne_tableau_resultat;
459
	}
460
 
461
	private function trierLigneTableau($a, $b) {
462
		$retour = 0;
463
 
464
		if ($a[$this->champ_tri] == $b[$this->champ_tri]) {
465
			$retour = 0;
466
		}
467
 
468
		if($this->champ_tri == 'code_langue') {
469
			if ($a[$this->champ_tri] == 'fra' && $b[$this->champ_tri] != 'fra') {
470
				$retour = ($this->direction_tri == 'asc') ? -1 : 1;
471
			} else if ($a[$this->champ_tri] != 'fra' && $b[$this->champ_tri] == 'fra') {
472
				$retour = ($this->direction_tri == 'asc') ? 1 : -1;
473
			} else {
474
				$retour = $this->comparerChaineSelonDirectionTri($a[$this->champ_tri], $b[$this->champ_tri]);
475
			}
476
		} else {
477
			$retour = $this->comparerChaineSelonDirectionTri($a[$this->champ_tri], $b[$this->champ_tri]);
478
		}
479
		return $retour;
480
	}
481
 
482
	private function comparerChaineSelonDirectionTri($a, $b) {
483
		if($this->direction_tri == 'asc') {
484
			return ($a < $b) ? -1 : 1;
485
		} else {
486
			return ($a > $b) ? -1 : 1;
487
		}
488
	}
489
 
6 jpm 490
	// formatage de la reponse /id ss la forme
491
	// id, nom_vernaculaire, attributions
492
	// langue
493
	// num_nom (correspond à un taxon bdtfx)
494
	public function formaterNomsVernaculairesId($resultat) {
495
		foreach ($resultat as $taxon) { // pour chaque attribution à un taxon bdtfx
496
			// on crée les variables qui serviront de clés et on les enléves du tableau
497
			$num_nom = $taxon['num_nom_vernaculaire']; // unique pour un trinôme id, langue, taxon
498
			unset($taxon['num_nom_vernaculaire']);
499
			$langue = $taxon['code_langue'];
500
			unset($taxon['code_langue']);
109 jpm 501
 
6 jpm 502
			foreach ($this->correspondance_champs as $key => $correspondance) { // ordonne les infos pour affichage
503
				if (isset($taxon[$key]) && $taxon[$key] != "") {
504
					$this->afficherDonnees($correspondance, $taxon[$key], $langue, $num_nom);
505
				}
506
			}
507
			foreach ($taxon as $key => $valeur) { // rajoute les champs non prévus dans l'api
508
				if (!isset($this->correspondance_champs[$key]) && $valeur != "") {
509
					$this->afficherDonnees($key, $valeur, $langue, $num_nom);
510
				}
511
			}
512
			if ($this->retour_format == 'max') $this->chargerBiblio($num_nom, $langue);
513
		}
514
		if ($this->retour_format == 'max') $this->afficherTaxons(); // va chercher les noms de tous les taxons
515
		unset($this->table_retour['href']);
516
		return $this->table_retour;
517
	}
109 jpm 518
 
6 jpm 519
	public function afficherDonnees($champ, $valeur, $langue = '', $num_nom = '') {
520
		if ($champ == 'id' || $champ == 'nom_vernaculaire') {
521
			$this->table_retour[$champ] = $valeur;
522
		} elseif (preg_match('/^(.*)\.code$/', $champ, $match)) {
523
				switch ($match[1]) {
524
					case 'taxon'	: if ($this->retour_format == 'max') {$this->taxons[$num_nom] = $valeur;}
525
						$this->afficherPointCode($match[1], $langue, $num_nom, $valeur);	break;
109 jpm 526
					case 'langue'	: //$this->afficherPointCode($match[1], 'iso-639-3', 'langues', $valeur);
6 jpm 527
						break;
528
					case 'genre'	: $this->afficherPointCode($match[1], $langue, $num_nom, $valeur);	break;
529
					case 'conseil_emploi'	: $this->afficherPointCode($match[1], $langue, $num_nom, $valeur);	break;
530
					default : break;
531
				}
109 jpm 532
 
6 jpm 533
		} elseif ($langue != '') {
534
			$this->table_retour['attributions'][$langue][$num_nom][$champ] = $valeur;
535
		} else {
536
			$this->table_retour[$champ] = $valeur;
537
		}
538
	}
109 jpm 539
 
6 jpm 540
	public function afficherPointCode($nomChamp, $langue, $num_nom, $valeur) {
541
		if (isset($this->champ_infos[$nomChamp])) {
542
			extract($this->champ_infos[$nomChamp]);
543
		}
109 jpm 544
 
6 jpm 545
		if ($this->retour_format == 'max') {
546
			$url = $this->ajouterHrefAutreProjet($service, $ressource, $valeur, $projet);
547
			if ($service == 'taxons') {
548
				$code_valeur = '';
164 delphine 549
				$this->table_retour['attributions'][$langue][$num_nom]['nom_retenu.code'] = $code_valeur;
109 jpm 550
			} else {
6 jpm 551
				$code_valeur = $this->chercherSignificationCode($url, $nom);
552
			}
553
			if ($projet != '') $projet .= '.';
554
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp] = $code_valeur;
555
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp.'.code'] = $projet.$ressource.$valeur;
556
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp.'.href'] = $url;
557
		} else {
558
			if ($projet != '') $projet .= '.';
559
			$this->table_retour['attributions'][$langue][$num_nom][$nomChamp.'.code'] = $projet.$ressource.$valeur;
560
		}
561
	}
109 jpm 562
 
6 jpm 563
	public function chercherSignificationCode($url, $nom) {
564
		if (isset($this->signification_code[$url])) {
565
			$valeur = $this->signification_code[$url];
566
		} else {
567
			$res = $this->consulterHref($url);
568
			$valeur = $res->$nom;
569
			$this->signification_code[$url] = $valeur;
570
		}
571
		return $valeur;
572
	}
109 jpm 573
 
6 jpm 574
	public function afficherTaxons() {
160 delphine 575
		$resultat = $this->recupererNomTaxons();
6 jpm 576
		foreach ($this->table_retour['attributions'] as $code_langue=>$langue) {
577
			foreach ($langue as $num_nom=>$taxon) {
578
				$num_tax = ltrim($taxon['taxon.code'], 'bdtfx.nt:');
579
				if (isset($resultat[$num_tax])) {
164 delphine 580
					$this->table_retour['attributions'][$code_langue][$num_nom]['nom_retenu.code'] = $resultat[$num_tax]['id'];
581
					$this->table_retour['attributions'][$code_langue][$num_nom]['taxon'] = $resultat[$num_tax]['nom_sci'];
6 jpm 582
				}
583
			}
584
		}
585
	}
160 delphine 586
 
587
	public function recupererNomTaxons() {
627 aurelien 588
		$taxons = array_unique($this->taxons);
589
		$url = Config::get('url_service_base').'bdtfx/taxons?navigation.limite=500&ns.structure=au&masque.nt='.implode(',', $taxons);
160 delphine 590
		$res = $this->consulterHref($url);
591
		foreach ($res->resultat as $id=>$taxon) {
164 delphine 592
			$resultat[$taxon->num_taxonomique]['id'] = 'bdtfx.nn:'.$id;
627 aurelien 593
			$resultat[$taxon->num_taxonomique]['nom_sci'] = $taxon->nom_sci_complet;
160 delphine 594
		}
595
		return $resultat;
596
	}
6 jpm 597
 
598
	public function formaterNomsVernaculairesIdChamp($resultat) {
109 jpm 599
		$this->table_retour['id'] = $this->ressources[0];
600
		$champs = explode(' ', $this->ressources[1]);
6 jpm 601
		if (in_array('attributions', $champs) != false) {
602
			$this->formaterNomsVernaculairesId($resultat);
603
			unset($this->table_retour['nom_vernaculaire']);
160 delphine 604
		} else {
6 jpm 605
			$champ_attributions = array('num_taxon', 'zone_usage', 'num_statut', 'num_genre', 'notes');
606
			foreach ($resultat as $taxon) {
607
				foreach ($taxon as $key=>$valeur) {
608
					if ($key == 'code_langue' && in_array('langue', $champs) != false) {
609
						$this->table_retour['attributions']['langue'][] = $valeur;
610
					} elseif (in_array($key, $champ_attributions) != false) {
611
						$this->afficherPoint($this->correspondance_champs[$key] , $valeur, $taxon['code_langue'], $taxon['num_nom_vernaculaire']);
612
					} elseif (in_array($key, $champs) != false) {
613
						$this->table_retour[$key] = $valeur;
614
					}
615
				}
616
				if (in_array('biblio', $champs) != false) $this->chargerBiblio($taxon['num_nom_vernaculaire'], $taxon['code_langue']);
617
			}
618
			if (in_array('biblio', $champs) != false && array_search('biblio.num_ref', $this->table_retour) != false) $this->table_retour['biblio'] = null;
619
		}
620
		return $this->table_retour;
621
	}
109 jpm 622
 
6 jpm 623
	public function afficherPoint($champ, $valeur, $langue, $num_nom) {
624
		preg_match('/^(.*)\.code$/', $champ, $match);
625
		$champ = $match[1];
626
		if (isset($this->champ_infos[$champ])) {
627
			extract($this->champ_infos[$champ]);
628
			$url = $this->ajouterHrefAutreProjet($service, $ressource, $valeur, $projet);
629
			$projet .= '.';
630
		}
109 jpm 631
 
632
		$champs = explode(' ', $this->ressources[1]);
6 jpm 633
		if (in_array($champ.'.*', $champs) !== false && isset($projet)) {
634
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.code'] = $projet.$ressource.$valeur;
635
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.href'] = $url;
109 jpm 636
		}
6 jpm 637
		if (in_array($champ.'.code', $champs) !== false && isset($projet)) {
638
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.code'] = $projet.$ressource.$valeur;
109 jpm 639
		}
6 jpm 640
		if (in_array($champ.'.href', $champs) !== false && isset($projet)) {
641
			$this->table_retour['attributions'][$langue][$num_nom][$champ.'.href'] = $url;
109 jpm 642
		}
6 jpm 643
		if (in_array($champ, $champs) !== false) {
644
			if (isset($url)) {
645
				$this->table_retour['attributions'][$langue][$num_nom][$champ] = $this->chercherSignificationCode($url, $nom);
646
			} else {
647
				$this->table_retour['attributions'][$langue][$champ] = $valeur;
648
			}
109 jpm 649
		}
6 jpm 650
	}
651
 
652
	public function afficherLangue($nomChamp, $projet, $service, $valeur, $ressource = '', $nom = 'nom') {
653
		if ($this->retour_format == 'max') {
654
				$this->table_retour['attributions'][$nomChamp] = $nom;
655
				$this->table_retour['attributions'][$nomChamp.'.code'] = $projet.$ressource.$valeur;
656
				$this->table_retour['attributions'][$nomChamp.'.href'] = $url;
657
		} else {
658
			$this->table_retour['attributions'][$nomChamp.'.code'] = $projet.$ressource.$valeur;
659
		}
660
	}
109 jpm 661
 
6 jpm 662
	public function chargerBiblio($num_nom, $langue) {
663
		list($table, $version) = explode('_v',$this->table);
664
		$requete = "SELECT b.*, lb.notes FROM nvjfl_lien_biblio_v$version lb, nvjfl_biblio_v$version b ".
665
					"WHERE b.num_ref = lb.num_ref AND lb.num_nom = '$num_nom' ;";
284 jpm 666
		$resultat = $this->getBdd()->recupererTous($requete);
109 jpm 667
 
6 jpm 668
		 if ($resultat == '') { //cas ou la requete comporte des erreurs
669
		 	$r = 'La requête SQL formée comporte une erreur !!';
670
			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $r);
671
			Debug::printr($requete);
672
		 } elseif ($resultat) {
673
			foreach ($resultat as $res) {
674
			   	foreach ($res as $cle => $valeur) {
675
			   		if ($valeur !== "") {
676
			   			$this->table_retour['attributions'][$langue][$num_nom]['biblio.'.$cle] = $valeur;
677
			   		}
678
			    }
679
			}
680
		}
681
	}
109 jpm 682
 
6 jpm 683
}
684
?>