Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 81 | Rev 91 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 jpm 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
* Description :
5
* Classe CommunNomsTaxons.php
6
* Encodage en entrée : utf8
7
* Encodage en sortie : utf8
8
* @package framework-v3
9
* @author Jennifer Dhé <jennifer.dhe@tela-botanica.org>
10
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
11
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
12
* @version 1.0
13
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
14
*/
15
 
16
abstract class CommunNomsTaxons extends Commun {
17
 
18
	/** Tableau de correspondance entre les noms des champs et les codes de l'ontologie.*/
19
	private $relationsChampsCodesOntologie = null;
20
	protected $table_retour; //Permet de stocker le tableau de résultat (non encodé en json)
21
	protected $resultat_req; // Permet de stocker le résultat de la requete principale.
22
	protected $compo_nom = null; //Stocke sous forme de tableau les composant du nom à ajouter au nom scientifique
23
	protected $table;// Nom de la table dans laquelle on récupèrera les données dans les requetes SQL
24
	protected $total_resultat = null;
25
	 /** Stocke le service appelé correspondant. Est utilisé principalement lors de l'affichage du href d'un synonyme
26
	  (ex id=12, basionyme num 25 est un synonyme) dans le service taxon */
27
	protected $service_href = null;
28
	protected $erreursParametres = null;
29
 
30
//+------------------------------- PARAMÈTRES ---------------------------------------------------------------+
31
 
32
	public function traiterParametres() {
33
		$this->definirParametresParDefaut();
34
		$this->verifierParametres();
35
 
36
		if (isset($this->parametres) && count($this->parametres) > 0) {
37
			foreach ($this->parametres as $param => $val) {
38
				switch ($param) {
39
					case 'ns.structure' :
40
						$this->remplirTableCompositionNom($val);
41
						$this->requete_champ .= ' ,'.implode(', ',$this->compo_nom);
42
						break;
43
					case 'navigation.depart' :
44
							$this->limite_requete['depart'] = $val;
45
						break;
46
					case 'navigation.limite' :
47
							$this->limite_requete['limite'] = $val;
48
						break;
49
				}
50
			}
51
			$this->traiterParametresSpecifiques();
52
		}
53
	}
54
 
55
	protected function definirParametresParDefaut() {
56
		if (empty($this->parametres['recherche'])) {
57
			$this->parametres['recherche'] = 'stricte';
58
		}
59
		if (empty($this->parametres['ns.format'])) {
60
			$this->parametres['ns.format'] =  'txt';
61
		}
62
		if (empty($this->parametres['retour.format'])) {
63
			$this->parametres['retour.format'] = 'max';
64
		}
65
	}
66
 
67
	public function verifierParametres() {
68
		$this->verifierParametresAPI();
69
 
70
		$this->verifierParametre('recherche', 'stricte|floue|etendue');
71
		$this->verifierParametre('ns.format', 'htm|txt');
72
		$this->verifierParametre('retour.format', 'min|max|oss|perso');
73
		$this->verifierParametreAvecValeurMultipe('ns.structure', 'an|au|bib|ad');
74
 
75
		if (count($this->erreursParametres) > 0) {
76
			$m = 'Erreur dans votre requête : '.implode('<br/>', $this->erreursParametres);
77
			$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $m);
78
		}
79
	}
80
 
81
	public function verifierParametresAPI() {
82
		$parametresApi = $this->recupererTableauConfig('parametresAPI');
83
		while (!is_null($parametre = key($this->parametres))) {
84
			if (!in_array($parametre, $parametresApi)) {
85
				$this->erreursParametres[] = "Le paramètre '$parametre' n'est pas pris en compte par cette version de l'API.";
86
			}
87
			next($this->parametres);
88
		}
89
	}
90
 
91
	public function verifierParametre($parametre, $valeursPermises) {
92
		if (isset($this->parametres[$parametre]) && !empty($this->parametres[$parametre])) {
93
			$valeur = $this->parametres[$parametre];
94
			$this->verifierValeursPermises($parametre, $valeur, $valeursPermises);
95
		}
96
	}
97
 
98
	public function verifierParametreAvecValeurMultipe($parametre, $valeursPermises) {
99
		if (isset($this->parametres[$parametre]) && !empty($this->parametres[$parametre])) {
100
			$valeursConcatenees = $this->parametres[$parametre];
101
			$valeurs = explode(',', $valeursConcatenees);
102
			foreach ($valeurs as $valeur) {
103
				$this->verifierValeursPermises($parametre, $valeur, $valeursPermises);
104
			}
105
		}
106
	}
107
 
108
	private function verifierValeursPermises($parametre, $valeur, $valeursPermises) {
109
		if (!in_array($valeur, explode('|', $valeursPermises))) {
110
			$this->erreursParametres[] = "Le paramètre '$parametre' ne peut pas prendre la valeur '$valeur'. Valeurs permises : $valeursPermises";
111
		}
112
	}
113
 
114
	public function traiterParametresCommuns() {
115
 
116
	}
117
 
118
	public function ajouterFiltreMasque($nom_champ, $valeur) {
119
		if ($nom_champ == 'annee' || $nom_champ == 'rang') {
120
			$this->requete_condition []= "$nom_champ = ".$this->getBdd()->proteger($valeur);
121
		} else {
122
			if ($this->parametres['recherche'] == 'etendue') {
123
				$valeur = $this->modifierValeur($valeur);
124
				$this->requete_condition[] = "$nom_champ LIKE ".$this->getBdd()->proteger($valeur);
125
			} elseif ($this->parametres['recherche'] == 'floue') {
126
				$valeur = $this->getBdd()->proteger($valeur);
127
				$this->requete_condition[] = "( SOUNDEX($nom_champ) = SOUNDEX($valeur))".
128
						" OR ( SOUNDEX(REVERSE($nom_champ)) = SOUNDEX(REVERSE($valeur)))";
129
			} else {
130
				$this->requete_condition[] = "$nom_champ LIKE ".$this->getBdd()->proteger($valeur);
131
			}
132
		}
133
		$this->masque[$nom_champ] = "$nom_champ=$valeur";
134
	}
135
 
136
	private function modifierValeur($valeur) {
137
		$valeur = $this->remplacerCaractereHybrideEtChimere($valeur);
138
		$valeur = $this->preparerChainePourRechercheEtendue($valeur);
139
		return $valeur;
140
	}
141
 
142
	private function remplacerCaractereHybrideEtChimere($valeur) {
143
		$caracteres = array('×', '%D7', '+', '%2B');
144
		$remplacements = array('x ','x ', '+', '+');
145
		$valeur = str_replace($caracteres, $remplacements, $valeur);
146
		return $valeur;
147
	}
148
 
149
	private function preparerChainePourRechercheEtendue($valeur) {
150
		$valeur = str_replace(' ', '%', trim($valeur));
151
		$valeur = $valeur.'%';
152
		return $valeur;
153
	}
154
 
155
//+-------------------------------Fonctions d'analyse des ressources-----------------------------------------+
156
 
81 delphine 157
 
3 jpm 158
 
159
	private function etreRessourceId() {
160
		$ok = false;
161
		if ($this->estUnIdentifiant() && count($this->ressources) == 1) {
162
			$ok = true;
163
		}
164
		return $ok;
165
	}
166
 
167
	public function traiterRessources() {
168
		if (isset($this->ressources) && count($this->ressources) > 0) {
169
			if ($this->ressources[0] == 'relations') {
170
				$this->traiterRessourceRelations();
171
			} elseif ($this->estUnIdentifiant()) { //l'identifiant peut etre de type /#id ou /nt:#id
172
				$this->traiterRessourcesIdentifiant(); // dans le service noms ou taxons
173
			} elseif ($this->ressources[0] == 'stats') { //ressource = noms/stats
174
				$this->traiterRessourcesStats();
175
			} else {
176
				$e = 'Erreur dans votre requete </br> Ressources disponibles : <br/>
177
					 <li> /'.$this->service.'/#id (id : L\'identifiant du nom rechercher)</li>
178
					 <li> /'.$this->service.'/nt:#id (id : Numero du taxon recherche)</li>
179
					 <li> /'.$this->service.'/stats </li>';
180
				$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
181
			}
182
		}
183
	}
184
 
185
	public function traiterRessourcesStats() {
186
		$this->format_reponse = $this->service.'/stats';
187
 
188
		$e = "Erreur dans votre requête </br> Ressources disponibles : $this->service/stats/[annees|rangs|initiales]";
189
		if (isset($this->ressources[1]) && !empty($this->ressources[1])) {
190
			switch ($this->ressources[1]) {
191
				case 'annees' :
192
					$this->traiterRessourceStatsAnnees();
193
					break;
194
				case 'rangs' :
195
					$this->traiterRessourceStatsRangs();
196
					break;
197
				case 'initiales' :
198
					$this->traiterRessourceStatsInitiales();
199
					break;
200
				default :
201
					$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
202
					break;
203
			}
204
		} else {
205
			$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
206
		}
207
	}
208
 
209
	/** Vérifie si la première valeur de la table de ressource est un identifiant :
210
	 * un numerique ou un numéro taxonomique sous la forme nt:xx */
211
	public function estUnIdentifiant() {
212
		return (is_numeric($this->ressources[0]) || (strrpos($this->ressources[0],'nt:') !== false
213
				&& is_numeric(str_replace('nt:','',$this->ressources[0]))));
214
	}
215
 
216
//+--------------------------------------------Fonction d'analyse des parametres-----------------------------+
217
 
218
	/** Permet de remplir le tableau compo_nom. Il comprendra en fct du paramètre ns.structure les éléments à rajouter
219
	 * au nom_sci (annee, auteur, biblio ou addendum). */
220
	public function remplirTableCompositionNom($valeur) {
221
		$structure_nom = explode(',', $valeur);
222
		foreach ($structure_nom as $structure) {
223
			$this->compo_nom[$structure] = str_replace(array('an', 'au', 'bib', 'ad'),
224
				array('annee', 'auteur', 'biblio_origine', 'nom_addendum'), $structure);
225
		}
226
	}
227
 
228
	public function mettreAuFormat() {
229
		if ($this->parametres['ns.format'] == 'htm') {
230
			if (strrpos($this->requete_champ, 'nom_sci_html as nom_sci') === false) {
231
				$this->requete_champ = str_replace('nom_sci', 'nom_sci_html as nom_sci', $this->requete_champ);
232
			}
233
		}
234
	}
235
 
236
//+-------------------------------fonctions de formatage-----------------------------------------------------+
237
 
238
	/** Fonction permettant de creer la table dont le nom est passé en paramètre (champs_api, champs_bdtfx,
239
	 * correspondance_champs...). Les données de chaque table sont présentes dans le fichier de configuration config.ini
240
	 * @param String $table : Peut contenir plusieurs nom de table dont on souhaite récupérer les données : table,table,table. */
241
	public function recupererTableSignification($table) {
242
		$tables = explode(',', $table);
243
		foreach ($tables as $tab) {
244
			if ($tab == 'champs_comp') {
245
				$champ_bdnff_api = array_keys($this->champs_api); //on recupère le nom des champ ds la bdd
246
				$this->champs_comp = array_diff($this->champs_table, $champ_bdnff_api);
247
			} elseif ($tab == 'champs_api') {
248
				foreach ($this->correspondance_champs as $key => $val) {
249
					preg_match('/(hybride[.]parent_0[12](?:[.]notes)?|nom_sci[.][^.]+|[^.]+)(?:[.](id|code))?/', $val, $match);
250
					$val = $match[1];
251
					$this->champs_api[$key] = $val;
252
				}
253
			} else {
254
				$this->$tab = $this->recupererTableauConfig($tab);
255
			}
256
		}
257
	}
258
 
259
	public function formaterEnOss($resultat) {
260
		$table_nom = array();
261
		$oss = '';
262
		foreach ($resultat as $tab) {
263
			if (isset($tab['nom_sci']) ) {
264
				if (!in_array($tab['nom_sci'], $table_nom)) {
265
					$table_nom[] = $tab['nom_sci'];
266
					$oss [] = $tab['nom_sci'].$this->ajouterCompositionNom($tab);
267
				}
268
			}
269
		}
270
		if (isset($this->masque)) $masque = implode('&', $this->masque);
271
		else $masque = 'Pas de masque';
272
		$table_retour_oss = array($masque, $oss);
273
		return $table_retour_oss;
274
	}
275
 
276
	public function afficherEnteteResultat($url_service) {
277
		$this->table_retour['depart'] = $this->limite_requete['depart'];
278
		$this->table_retour['limite'] = $this->limite_requete['limite'];
279
		$this->table_retour['total']  = $this->total_resultat;
280
		$url = $this->formulerUrl($this->total_resultat, $url_service);
281
		if (isset($url['precedent']) && $url['precedent'] != '') {
282
			$this->table_retour['href.precedent'] = $url['precedent'];
283
		}
284
		if (isset($url['suivant']) && $url['suivant']   != '') {
285
			$this->table_retour['href.suivant']   = $url['suivant'];
286
		}
287
	}
288
 
289
	public function afficherNomHrefRetenu($tab, $num) {
290
		$this->resultat_req = $tab;
291
		$this->afficherDonnees('num_nom', $num);
292
		if ($this->parametres['retour.format'] == 'min') { // sinon est affiché ds afficherDonnees(num_nom, $val) ci-dessus
293
			 $this->table_retour['nom_sci'] = $tab['nom_sci'].$this->ajouterCompositionNom($tab);
294
		}
295
		$this->table_retour['retenu'] = ($tab['num_nom_retenu'] == $num) ? 'true' : 'false';
296
		unset($this->table_retour['id']);
297
	}
298
 
299
 
300
//+---------------------------Fonction de formatage pour les services /#id/----------------------------------+
301
 
302
	public function formaterId($resultat) {
303
		$this->recupererTableSignification('correspondance_champs,champs_api,champs_comp');
304
		$this->resultat_req = $resultat;
305
		foreach ($resultat as $cle => $valeur) {
306
			if ($valeur != '') {
307
				$this->afficherDonnees($cle, $valeur);
308
			}
309
		}
310
		unset($this->table_retour['href']);
311
		return $this->table_retour;
312
	}
313
 
314
	public function formaterIdChamp($resultat) {
315
		$this->recupererTableSignification('correspondance_champs,champs_api,champs_comp');
316
		$reponse_id = $this->formaterId($resultat);
317
		$this->table_retour = array();
318
		$champs = explode(' ', $this->ressources[1]);
319
		$this->ajouterChampsPersonnalises($champs, $reponse_id);
320
		return $this->table_retour;
321
	}
322
 
323
	protected function ajouterChampsPersonnalises($champs, $reponse_id) {
324
		if (! is_null($champs) && is_array($champs) && count($champs) > 0) {
325
			foreach ($champs as $champ) {
326
				if ($this->verifierValiditeChamp($champ)) {
327
					if (strrpos($champ, '.*') !== false) {
328
						$this->afficherPointEtoile($champ, $reponse_id);
329
					} else {
330
						$champ = $this->trouverChampBddCorrespondant($champ);
331
						$this->table_retour[$champ] = (isset($reponse_id[$champ])) ? $reponse_id[$champ] : null;
332
					}
333
				}
334
			}
335
		}
336
	}
337
 
338
	public function afficherPointEtoile($champ, $reponse) {
339
		preg_match('/^([^.]+\.)\*$/', $champ, $match);
340
		if ($match[1] == 'nom_sci') {
341
			$this->afficherNomSciPointEpithete($this->resultat_req);
342
		} else {
343
			foreach ($reponse as $chp => $valeur) {
344
				if (strrpos($chp, $match[1]) !== false) {
345
					if ($valeur != '') {
346
						$this->table_retour[$chp] = $valeur;
347
					} else {
348
						$this->table_retour[$chp] = null;
349
					}
350
				}
351
			}
352
		}
353
	}
354
 
355
	public function decomposerNomChamp($champ) {
356
		$decomposition = false;
357
		if (preg_match('/^(?:([^.]+\.parent_0[12]|[^.]+))(?:\.(.+))?$/', $champ, $match)) {
358
			$radical_champ = $match[1];
90 delphine 359
			$suffixe = (isset($match[2])) ? $match[2] : "";
3 jpm 360
			$decomposition = array($radical_champ, $suffixe);
361
		}
362
		return $decomposition;
363
	}
364
 
365
	public function verifierValiditeChamp($champ) {
366
		$decomposition = $this->decomposerNomChamp($champ);
367
		$validite_ressource = true;
368
		if ($decomposition) {
369
			list($radical, $suffixe) = $decomposition;
370
			// on verifie si le nom du champ existe bien
371
			if (!$this->estChampApi($radical) && !$this->estChampComplementaire($radical)) {
372
				$validite_ressource = false;
373
				$e = 'Le champ "'.$radical.'" n\'existe pas dans la base. <br/><br/>';
374
				$this->renvoyerErreur( RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
375
			} elseif ($this->estUnPoint($champ)) {
376
				$validite_ressource = $this->verifierValiditeSuffixe($suffixe, $radical);
377
			}
378
		}
379
		return $validite_ressource;
380
	}
381
 
382
	public function estChampApi($radical_champ) {
383
		$champ_api_ok = false;
384
		if (in_array($radical_champ, $this->champs_api) || in_array($radical_champ, $this->correspondance_champs)) {
385
			$champ_api_ok = true;
386
		}
387
		return $champ_api_ok;
388
	}
389
 
390
	public function estChampComplementaire($radical_champ) {
391
		$champ_complementaire_ok = in_array($radical_champ, $this->champs_comp) ? true : false;
392
		return	$champ_complementaire_ok;
393
	}
394
 
395
	public function verifierValiditeSuffixe($suffixe, $radical_champ) {
396
		$validite_ressource = true;
397
		if ($this->correspondAUnId($radical_champ) || $radical_champ == 'id') {
398
			$this->verificationSuffixesIdentifiant($suffixe, $radical_champ, $validite_ressource);
399
		} elseif ($this->correspondAUnCode($radical_champ)) {
400
			$this->verificationSuffixesCodes($suffixe, $radical_champ, $validite_ressource);
401
		} elseif ($radical_champ == 'nom_sci') {
402
			if ($suffixe != '*') {
403
				$validite_ressource = false;
404
				$m = 'Erreur : Le suffixe demandé n\'existe pas pour le champ "'.$radical_champ.'".<br/>
405
					Les suffixes possibles sont les suivants : <li> * </li>';
406
				$this->renvoyerErreur( RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $m);
407
			}
408
		} else {
409
			$validite_ressource = false;
410
			$m = 'Erreur : Le paramètre "'.$radical_champ.'" ne peut pas présenter de suffixe. <br/><br/>';
411
			$this->renvoyerErreur( RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $m);
412
		}
413
		return $validite_ressource;
414
	}
415
 
416
	public function verificationSuffixesCodes(&$suffixe, &$radical_champ, &$validite_ressource ) {
417
		if (!in_array($suffixe, array('*', 'code', 'href', 'details'))) {
418
			$validite_ressource = false;
419
			$e = 'Erreur : Le suffixe demandé n\'existe pas pour le champ "'.$radical_champ.'.<br/> Les suffixes '
420
				.'possibles sont les suivants : <li> .* </li><li> .code </li><li> .href </li><li> .details </li>';
421
			$this->renvoyerErreur( RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
422
		}
423
	}
424
 
425
	public function verificationSuffixesIdentifiant(&$suffixe, &$radical_champ, &$validite_ressource) {
426
		if ((strrpos($radical_champ, 'parent') !== false && !in_array($suffixe, array('*', 'id', 'href', 'details', 'notes')))
427
			|| !in_array($suffixe, array('*', 'id', 'href', 'details')) && strrpos($radical_champ, 'parent') === false) {
428
			$validite_ressource = false;
429
			$e = 'Erreur : Le suffixe demandé n\'existe pas pour le champ "'.$radical_champ.'".<br/> Les suffixes '
430
				.'possibles sont les suivants : <li> .* </li><li> .id </li><li> .href </li><li> .details </li>'
431
				.'<li> .notes (seulement pour les hybride.parent)';
432
			$this->renvoyerErreur( RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
433
		}
434
	}
435
 
436
 
437
//------------------------------fonction de formatage pour les services /stats/-----------------------------------------
438
 
439
	public function formaterStatsAnnee($resultat) {
440
		foreach ($resultat as $cle_annee) {
441
			$annee = ($cle_annee['annee'] != '') ? $cle_annee['annee'] : 'ND';
442
			$nb = $cle_annee['nombre'];
443
			$retour_stats_annee[$annee] = $nb;
444
		}
445
		return $retour_stats_annee;
446
	}
447
 
448
	public function formaterStatsRang($resultat) {
449
		foreach ($resultat as $rangs) {
450
			if ($rangs['rang'] != 0) {
451
				$rang = $rangs['rang'];
452
				if ($this->parametres['retour.format'] == 'max') {
453
					$retour_rang[$rang]['rang'] = $this->ajouterSignificationCode('rang',$rang);
454
				}
455
				$nombre = $rangs['nombre'];
456
				$retour_rang[$rang]['nombre'] = $nombre;
457
			}
458
		}
459
		return $retour_rang;
460
	}
461
 
462
	public function formaterStatsInitiales($resultat) {
463
		$rang = null;
464
		$table_rang = array();
465
		foreach ($resultat as $tuple) {
466
			if ($tuple['rang'] != 0) {
467
				$this->memoriserRang($table_rang, $tuple, $rang);
468
				if ($tuple['lettre'] == 'x ') {
469
					$this->ajouterHybrideChimere('hybride', $rang, $tuple);
470
				} elseif ($tuple['lettre'] == '+ ') {
471
					$this->ajouterHybrideChimere('chimere', $rang, $tuple);
472
				} else {
473
					$l = substr($tuple['lettre'], 0, 1);
474
					if (isset($this->table_retour[$rang][$l])) {
475
						$this->table_retour[$rang][substr($tuple['lettre'], 0, 1)] += floatval($tuple['nb']);
476
					} else {
477
						$this->table_retour[$rang][substr($tuple['lettre'], 0, 1)] = floatval($tuple['nb']);
478
					}
479
				}
480
			}
481
		}
482
		return $this->table_retour;
483
	}
484
 
485
	public function memoriserRang(&$table_rang, $tuple, &$rang) {
486
		if (is_array($table_rang)) {
487
			if (!in_array($tuple['rang'], $table_rang)) {
488
				$rang = $tuple['rang'];
489
				$table_rang[] = $rang;
490
				if ($this->parametres['retour.format'] == 'max') {
491
					$rang = $this->ajouterSignificationCode('rang', $rang);
492
				}
493
			}
494
		}
495
	}
496
 
497
	public function ajouterHybrideChimere($groupe, &$rang, &$tuple) {
498
		if (isset($this->table_retour[$rang][str_replace('hybride', 'hyb', $groupe)])) {
499
			$this->table_retour[$rang][$groupe] += floatval($tuple['nb']);
500
		} else {
501
			$this->table_retour[$rang][$groupe] = floatval($tuple['nb']);
502
		}
503
	}
504
 
505
	//-----------------------------Fonctions d'affichage utiliser dans les fonctions de formatage---------------------------
506
 
507
	public function afficherDonnees($champApi, $valeur) {
508
		$champBdd = $this->trouverChampBddCorrespondant($champApi);
509
		if ($this->parametres['retour.format'] == 'min') {
510
			if ($cle == 'nom_sci') {
511
				$valeur = $valeur.$this->ajouterCompositionNom($this->resultat_req);
512
			}
513
			$this->table_retour[$champBdd] = $valeur;
514
		} else {
515
			$this->afficherToutesLesInfos($champBdd, $valeur);
516
		}
517
	}
518
 
519
	public function trouverChampBddCorrespondant($champApi) {
520
		if (array_key_exists($champApi, $this->champs_api)) {
521
			$champBdd = $this->correspondance_champs[$champApi];
522
		} else {
523
			$champBdd = $champApi;
524
		}
525
		return $champBdd;
526
	}
527
 
528
	public function afficherToutesLesInfos($nom_champ_api, $valeur) {
529
		if ($this->presentePlusieursId($nom_champ_api, $valeur)) {
530
			preg_match('/^([^.]+\.parent_0[12]|[^.]+)(?:\.id)?$/', $nom_champ_api, $match);
531
			$this->afficherInfosPrecises($match[1], 'details', $valeur);
532
			$this->table_retour[$nom_champ_api] = $valeur;
533
 
534
		} elseif (strrpos($nom_champ_api, 'parent') !== false && strrpos($nom_champ_api, 'notes') !== false) {
535
			$this->table_retour[$nom_champ_api] = $valeur;
536
 
537
		} elseif (($this->correspondAUnId($nom_champ_api) || $nom_champ_api == 'id' && $valeur != '0')) {
538
			preg_match('/^([^.]+\.parent_0[12]|[^.]+)(?:\.id)?$/', $nom_champ_api, $match);
539
			$this->afficherInfosPrecises($match[1], 'id,signification,href', $valeur);
540
 
541
		} elseif ($this->correspondAUnCode($nom_champ_api)) {
542
			preg_match('/^([^.]+)(?:\.code)?$/', $nom_champ_api, $match);
543
			$this->afficherInfosPrecises($match[1], 'code,signification,href', $valeur);
544
 
545
		} elseif ($nom_champ_api != 'nom_sci') {
546
			$this->table_retour[$nom_champ_api] = $valeur;
547
		}
548
	}
549
 
550
	public function presentePlusieursId($ressource, $valeur = null) {
551
		if ($valeur) {
552
			$presente = strrpos($ressource, 'proparte') !== false && strrpos($valeur, ',') !== false;
553
		} else { //pour la vérification du champ, on ignore alors la valeur de la ressource
554
			$presente = strrpos($ressource, 'proparte') !== false;
555
		}
556
		return $presente;
557
	}
558
 
559
	public function afficherInfosPrecises($champ, $suffixe, $valeur) {
560
		$suffixes = explode(',', $suffixe);
561
		//on initialise au service appelé. Sera potentiellement modifié dans la fonction afficherSignification()
562
		$this->service_href = $this->service;
563
		foreach ($suffixes  as $suffixe) {
564
			switch ($suffixe) {
565
				case 'id' 			 :
566
					$this->table_retour[str_replace('id.id', 'id', $champ.'.id')] = $valeur;
567
					break;
568
				case 'details' 		 :
569
					$this->afficherTableDetails($champ, $valeur);
570
					break;
571
				case 'signification' :
572
					$this->afficherSignification($champ, $valeur);
573
					break;
574
				case 'href' 		 :
575
					$url = $this->creerUrl($champ, $valeur);
576
					$this->table_retour[str_replace('id.href', 'href', $champ.'.href')] = $url;
577
					break;
578
				case 'code' 		 :
579
					$this->table_retour[$champ.'.code'] = $this->obtenirCode($champ, $valeur);
580
					break;
581
				case 'notes' 		 :
582
					$this->table_retour[$champ.'.notes'] = $this->resultat_req[str_replace('.', '_', $champ).'_notes'];
583
					break;
584
				default : break;
585
			}
586
		}
587
	}
588
 
589
	public function afficherTableDetails($nom_champ_api, $valeur) {
590
		$tab_id = explode(',', $valeur);
591
		$tab_res = $this->table_retour;
592
		$this->table_retour = array();
593
		foreach ($tab_id as $id) {
594
			$this->afficherInfosPrecises($nom_champ_api, 'id,signification,href', $id);
595
			$tab_res[$nom_champ_api.'.details'][] = $this->table_retour;
596
			$this->table_retour = array();
597
		}
598
		$this->table_retour = $tab_res;
599
	}
600
 
601
	private function obtenirCode($champ, $valeur) {
602
		$code = $this->transformerChampEnCode($champ);
603
		return "bdnt.$code:$valeur";
604
	}
605
 
606
	private function transformerChampEnCode($champ) {
607
		if (is_null($this->relationsChampsCodesOntologie)) {
608
			$this->relationsChampsCodesOntologie = Outils::recupererTableauConfig('ChampsCodesOntologie');
609
		}
610
 
611
		$code = $champ;
612
		if (array_key_exists($champ, $this->relationsChampsCodesOntologie)) {
613
			$code = $this->relationsChampsCodesOntologie[$champ];
614
		}
615
		return $code;
616
	}
617
 
618
	public function creerUrl($champ, $valeur) {
619
		if ($this->correspondAUnId($champ) || $champ == 'id') {
620
			$service = $this->service_href;
621
			$url = $this->ajouterHref($service, $valeur);
622
		} else {
623
			$code = $this->transformerChampEnCode($champ);
624
			$url = $this->ajouterHrefAutreProjet('ontologies', "$code:", $valeur, 'bdnt');
625
		}
626
		return $url;
627
	}
628
 
629
	public function afficherSignification($champ, $valeur) {
630
		if ($champ == 'id' && isset($this->resultat_req['nom_sci']) && $this->resultat_req['num_nom'] == $valeur) {
631
			//si le nom_sci du num_nom que l'on veut afficher est déjà dans la table de résultat :
632
			$this->table_retour['nom_sci'] = $this->resultat_req['nom_sci'].
633
				$this->ajouterCompositionNom($this->resultat_req);
634
		} elseif ($this->correspondAUnId($champ) || $champ == 'id') {
635
			$nom = $this->recupererNomSci($valeur);
636
			if ($nom != array()) {
637
				$this->table_retour[$champ] = $nom['nom_sci'];
638
				$this->service_href = $nom['service'];
639
			}
640
		} elseif ($this->correspondAUnCode($champ)) {
641
			$this->table_retour[$champ] = $this->ajouterSignificationCode($champ, $valeur);
642
		}
643
	}
644
 
645
	/** Permet d'afficher les élements nomenclatural du nom_sci lors de l'appel dans le service noms/id/champ du champ^nom_sci.*/
646
	public function afficherNomSciPointEpithete($resultat) {
647
		$tab_nom_sci   = array('nom_supra_generique', 'genre', 'epithete_infra_generique', 'epithete_sp',
648
		'type_epithete', 'epithete_infra_sp', 'cultivar_groupe', 'cultivar', 'nom_commercial');
649
		foreach ($tab_nom_sci as $compo_nom) {
650
			if (isset($resultat[$compo_nom]) && !empty($resultat[$compo_nom])) {
651
				$this->table_retour['nom_sci.'.$compo_nom] = $resultat[$compo_nom];
652
			}
653
		}
654
	}
655
 
656
	public function ajouterSignificationCode($champ, $valeur) {
657
		$champ = ($champ == 'rang') ? 'rangTaxo' : rtrim($champ, '_Co,_Ga');
658
		if (preg_match('/^([^_-]+)(?:_|-)([^_-]+)$/', $champ, $match)) {
659
			$champ = $match[1].ucfirst($match[2]);
660
		}
661
		$url = Config::get('url_ontologie').$champ.':'.$valeur.'/nom';
662
		$res = $this->consulterHref($url); //dans commun.php
663
		$nom_code = $valeur;
664
		if (is_object($res)) {
665
			$nom_code = $res->nom;
666
		}
667
		return $nom_code;
668
	}
669
 
670
	public function recupererNomSci($id) {
671
		$nom = array();
672
		if ($id != 0) {
673
			if ($this->compo_nom == null) {
674
				$req = 'SELECT nom_sci, num_nom_retenu FROM '.$this->table.' WHERE num_nom = '.$id;
675
			} else { //on ajoute à la requete sql, les champs de ns.structure
676
				//print_r($this->compo_nom);
677
				$req = 'SELECT nom_sci, num_nom_retenu, '.implode(', ', $this->compo_nom)
678
						.' FROM '.$this->table
679
						.' WHERE num_nom = '.$id;
680
			}
681
			if ($this->parametres['ns.format'] == 'htm') {
682
				$req = str_replace('nom_sci', 'nom_sci_html as nom_sci', $req);
683
			}
684
			$res = $this->getBdd()->recuperer($req);
685
			if ($res) {
686
				$nom['nom_sci']	= $res['nom_sci'].$this->ajouterCompositionNom($res);
687
				$nom['service'] = ($res['num_nom_retenu'] == $id && $this->service == 'taxons') ? 'taxons' : 'noms';
688
			}
689
		}
690
		return $nom;
691
	}
692
 
693
	/** Permet de retourner une chaine de caractère composée des parametres du nom (ns.structure : annnée, auteur,
694
	 * bibilio et addendum). A ajouter au nom scientifique */
695
	public function ajouterCompositionNom($tab_res) {
696
		$nom = '';
697
		if (isset($this->compo_nom)) {
698
			$compo_nom = null;
699
			$nom_complet = null;
700
			if ($this->parametres['ns.format'] == 'htm') {
701
				$format = array(
702
					'au' => '<span class="auteur">%s</span>',
703
					'an' => '[<span class="annee">%s</span>]',
704
					'an_bib' => '[<span class="annee">%s</span>, <span class="biblio">%s</span>]',
705
					'bib' => '[<span class="biblio">%s</span>]',
706
					'ad' => '[<span class="adendum">%s</span>]');
707
			} else {
708
				$format = array(
709
					'au' => '%s',
710
					'an' => '[%s]',
711
					'an_bib' => '[%s, %s]',
712
					'bib' => '[%s]',
713
					'ad' => '[%s]');
714
			}
715
			foreach ($this->compo_nom as $key => $champ) {
716
				if (isset($tab_res[$champ]) && !empty($tab_res[$champ])) {
717
					$compo_nom[$key] = $tab_res[$champ];
718
				}
719
			}
720
			$this->formerNomComplet($compo_nom, $nom_complet, $format);
721
			$nom = ' '.implode(' ', $nom_complet);
722
		}
723
		return rtrim($nom, ' ');
724
	}
725
 
726
 
727
	public function formerNomComplet(&$compo_nom, &$nom_complet, $format) {
728
		extract($compo_nom);
729
		if (isset($au)) $nom_complet[] = sprintf($format['au'], $au);
730
		if (isset($an)) {
731
			if (isset($bib)) {
732
				$nom_complet[] = sprintf($format['an_bib'], $an, $bib);
733
			} else {
734
				$nom_complet[] = sprintf($format['an'], $an);
735
			}
736
		} elseif (isset($bib)) {
737
			$nom_complet[] = sprintf($format['bib'], $bib);
738
		}
739
		if (isset($ad)) $nom_complet[] = sprintf($format['ad'], $ad);
740
		return $nom_complet;
741
	}
742
 
743
 
744
	public function correspondAUnCode($key) {
745
		return (strrpos($key, '.code') !== false) || (in_array($key.'.code', $this->correspondance_champs));
746
	}
747
 
748
	public function correspondAUnId($key) {
749
		return (strrpos($key, '.id') !== false) || (in_array($key.'.id', $this->correspondance_champs));
750
	}
751
 
752
	public function estUnPoint($key) {
753
		if (strrpos($key, 'hybride.parent') !== false) {
754
			$key = str_replace('hybride.parent', 'hybride_parent', $key);
755
		}
756
		return (strrpos($key, '.') !== false);
757
	}
758
 
759
	public function recupererMasquePrincipal() {
760
		$masque = null;
761
		$tab_masque   = array(
762
			'masque' => 'nom_sci',
763
			'masque_sg' => 'nom_supra_generique',
764
			'masque_gen' => 'genre',
765
			'masque_sp' => 'epithete_sp',
766
			'masque_ssp' => 'epithete_infra_sp',
767
			'masque_au' => 'auteur',
768
			'masque_an' => 'annee',
769
			'masque_bib' => 'biblio_origine',
770
			'masque_ad' => 'addendum',
771
			'masque_rg' => 'rang');
772
		$liste_masque = array();
773
 
774
		if (isset($this->masque['num_nom'])) {
775
			$liste_masque[] = $this->masque['num_nom'];
776
		}
777
 
778
		foreach ($tab_masque as $key => $filtre) {
779
            if (isset($this->masque[$filtre])) {
780
            	if (!isset($masque) && !in_array($filtre, array('rang', 'annee'))) {
781
            		$masque = array($key, $filtre);
782
            	}
783
                $liste_masque[] = $this->masque[$filtre];
784
            }
785
        }
786
        $this->masque = $liste_masque;
787
        return $masque;
788
	}
789
}
790
?>