Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 659 | Rev 730 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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