Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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