Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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