Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 2805 Rev 2806
1
<?php
1
<?php
2
/**
2
/**
3
 * Widget fournissant des interfaces de saisie simplifiée pour différent projets
3
 * Widget fournissant des interfaces de saisie simplifiée pour différent projets
4
 *
4
 *
5
 * Cas d'utilisation et documentation :
5
 * Cas d'utilisation et documentation :
6
 * @link http://www.tela-botanica.org/wikini/AideCarnetEnLigne/wakka.php?wiki=AideCELWidgetSaisie
6
 * @link http://www.tela-botanica.org/wikini/AideCarnetEnLigne/wakka.php?wiki=AideCELWidgetSaisie
7
 *
7
 *
8
 * Paramètres :
8
 * Paramètres :
9
 * - projet [par défaut : defaut] : indique le mot-clé à associer aux obs saisies; si un widget de saisie personnalisé
9
 * - projet [par défaut : defaut] : indique le mot-clé à associer aux obs saisies; si un widget de saisie personnalisé
10
 * 		portant ce nom existe, il sera chargé
10
 * 		portant ce nom existe, il sera chargé
11
 * - mission [par défaut : vide] : permet de charger un "sous-widget", dans le cas où un widget personnalisé
11
 * - mission [par défaut : vide] : permet de charger un "sous-widget", dans le cas où un widget personnalisé
12
 * 		est associé au projet, et ce widget accepte des sous-widgets (ex: "missions-flore")
12
 * 		est associé au projet, et ce widget accepte des sous-widgets (ex: "missions-flore")
13
 *
13
 *
14
 * @author Mathias CHOUET <mathias@tela-botanica.org>
14
 * @author Mathias CHOUET <mathias@tela-botanica.org>
15
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
15
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
16
 * @author Aurelien PERONNET <aurelien@tela-botanica.org>
16
 * @author Aurelien PERONNET <aurelien@tela-botanica.org>
17
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
17
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
18
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
18
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
19
 * @copyright 1999-2015 Tela Botanica <accueil@tela-botanica.org>
19
 * @copyright 1999-2015 Tela Botanica <accueil@tela-botanica.org>
20
 */
20
 */
21
class Saisie extends WidgetCommun {
21
class Saisie extends WidgetCommun {
22
 
22
 
23
	const DS = DIRECTORY_SEPARATOR;
23
	const DS = DIRECTORY_SEPARATOR;
24
	const PROJET_DEFAUT = 'defaut';
24
	const PROJET_DEFAUT = 'defaut';
25
	const WS_SAISIE = 'CelWidgetSaisie';
25
	const WS_SAISIE = 'CelWidgetSaisie';
26
	const WS_UPLOAD = 'CelWidgetUploadImageTemp';
26
	const WS_UPLOAD = 'CelWidgetUploadImageTemp';
27
	const WS_OBS = 'CelObs';
27
	const WS_OBS = 'CelObs';
28
	const WS_NOM = 'noms';
28
	const WS_NOM = 'noms';
29
	const EFLORE_API_VERSION = '0.1';
29
	const EFLORE_API_VERSION = '0.1';
30
	const REFERENTIEL_DEFAUT = 'bdtfx';
30
	const REFERENTIEL_DEFAUT = 'bdtfx';
31
 
31
 
32
	/** référentiel utilisé pour al complétion des noms scientifiques */
32
	/** référentiel utilisé pour al complétion des noms scientifiques */
33
	protected $ns_referentiel;
33
	protected $ns_referentiel;
34
	/** mot-clé associé aux saisies, et template personnalisé si appliquable */
34
	/** mot-clé associé aux saisies, et template personnalisé si appliquable */
35
	protected $projet = null;
35
	protected $projet = null;
36
	protected $configProjet = null;
36
	protected $configProjet = null;
37
	protected $configMission = null;
37
	protected $configMission = null;
38
	protected $mission = null;
38
	protected $mission = null;
39
	/** langue (traduction), charge un template de la forme "defaut_en.tpl.html" */
39
	/** langue (traduction), charge un template de la forme "defaut_en.tpl.html" */
40
	protected $langue = null;
40
	protected $langue = null;
41
 
41
 
42
	/**
42
	/**
43
	 * Amorçage du widget
43
	 * Amorçage du widget
44
	 */
44
	 */
45
	public function executer() {
45
	public function executer() {
46
		// paramètres par défaut
46
		// paramètres par défaut
47
		$this->ns_referentiel = self::REFERENTIEL_DEFAUT;
47
		$this->ns_referentiel = self::REFERENTIEL_DEFAUT;
48
		$this->projet = self::PROJET_DEFAUT;
48
		$this->projet = self::PROJET_DEFAUT;
49
 
49
 
50
		// définition du projet / mission
50
		// définition du projet / mission
51
		if (isset($this->parametres['projet']) && trim($this->parametres['projet']) != "") {
51
		if (isset($this->parametres['projet']) && trim($this->parametres['projet']) != "") {
52
			$projets = explode(',', $this->parametres['projet']);
52
			$projets = explode(',', $this->parametres['projet']);
53
			$this->projet = strtolower($projets[0]);
53
			$this->projet = strtolower($projets[0]);
54
		}
54
		}
55
		$this->chargerConfigProjet();
55
		$this->chargerConfigProjet();
56
 
56
 
57
		// exécution du service (le widget entier ou une sous-partie, par ex "Taxons")
57
		// exécution du service (le widget entier ou une sous-partie, par ex "Taxons")
58
		$retour = null;
58
		$retour = null;
59
		$service = isset($this->parametres['service']) ? $this->parametres['service'] : 'widget';
59
		$service = isset($this->parametres['service']) ? $this->parametres['service'] : 'widget';
60
		$methode = $this->traiterNomMethodeExecuter($service);
60
		$methode = $this->traiterNomMethodeExecuter($service);
61
		if (method_exists($this, $methode)) {
61
		if (method_exists($this, $methode)) {
62
			$retour = $this->$methode();
62
			$retour = $this->$methode();
63
		} else {
63
		} else {
64
			$this->messages[] = "Le service '$methode' n'est pas disponible.";
64
			$this->messages[] = "Le service '$methode' n'est pas disponible.";
65
		}
65
		}
66
 
66
 
67
		// définition de la langue, en mode souple
67
		// définition de la langue, en mode souple
68
		if (isset($this->parametres['lang'])) {
68
		if (isset($this->parametres['lang'])) {
69
			$this->langue = $this->parametres['lang'];
69
			$this->langue = $this->parametres['lang'];
70
		}
70
		}
71
 
71
 
72
		// injection des données dans le squelette
72
		// injection des données dans le squelette
73
		$contenu = null;
73
		$contenu = null;
74
		$mime = null;
74
		$mime = null;
75
		if (is_array($retour) && array_key_exists('squelette', $retour)) {
75
		if (is_array($retour) && array_key_exists('squelette', $retour)) {
76
			$ext = (isset($retour['squelette_ext'])) ? $retour['squelette_ext'] : '.tpl.html';
76
			$ext = (isset($retour['squelette_ext'])) ? $retour['squelette_ext'] : '.tpl.html';
77
			// Suffixe de template pour la langue - fr par défaut @TODO configurer ça un jour
77
			// Suffixe de template pour la langue - fr par défaut @TODO configurer ça un jour
78
			$suffixeLangue = "";
78
			$suffixeLangue = "";
79
			if ($this->langue != null && $this->langue != "fr") {
79
			if ($this->langue != null && $this->langue != "fr") {
80
				$suffixeLangue = "_" . $this->langue;
80
				$suffixeLangue = "_" . $this->langue;
81
			}
81
			}
82
			// Template par défaut ou spécifique
82
			// Template par défaut ou spécifique
83
			if ($this->projetASquelette()) {
83
			if ($this->projetASquelette()) {
84
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet.self::DS.$retour['squelette'].$suffixeLangue.$ext;
84
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet.self::DS.$retour['squelette'].$suffixeLangue.$ext;
85
			} else {
85
			} else {
86
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.'defaut'.self::DS.'defaut'.$suffixeLangue.$ext;
86
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.'defaut'.self::DS.'defaut'.$suffixeLangue.$ext;
87
			}
87
			}
88
			$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
88
			$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
89
			$mime = isset($retour['mime']) ? $retour['mime'] : null;
89
			$mime = isset($retour['mime']) ? $retour['mime'] : null;
90
		} else {
90
		} else {
91
			if (count($this->messages) == 0) {
91
			if (count($this->messages) == 0) {
92
				$this->messages[] = "La méthode du sous-service ne renvoie pas les données dans le bon format";
92
				$this->messages[] = "La méthode du sous-service ne renvoie pas les données dans le bon format";
93
			}
93
			}
94
			$contenu = 'Un problème est survenu : ' . print_r($this->messages, true);
94
			$contenu = 'Un problème est survenu : ' . print_r($this->messages, true);
95
		}
95
		}
96
 
96
 
97
		// envoi de la page
97
		// envoi de la page
98
		$this->envoyer($contenu, $mime);
98
		$this->envoyer($contenu, $mime);
99
	}
99
	}
100
 
100
 
101
	/**
101
	/**
102
	 * Charge le fichier de configuration associé au projet : configurations/nomduprojet.ini
102
	 * Charge le fichier de configuration associé au projet : configurations/nomduprojet.ini
103
	 * Si une mission est définie, charge séparément la section de la configuration concernant
103
	 * Si une mission est définie, charge séparément la section de la configuration concernant
104
	 * cette mission : [nommission]
104
	 * cette mission : [nommission]
105
	 */
105
	 */
106
	protected function chargerConfigProjet() {
106
	protected function chargerConfigProjet() {
107
		$fichier_config = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'.ini';
107
		$fichier_config = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'.ini';
108
		if (file_exists($fichier_config)) {
108
		if (file_exists($fichier_config)) {
109
			if ($this->configProjet = parse_ini_file($fichier_config, true)) {
109
			if ($this->configProjet = parse_ini_file($fichier_config, true)) {
110
				if (isset($_GET['mission'])) {
110
				if (isset($_GET['mission'])) {
111
					$this->mission = strtolower(trim($_GET['mission']));
111
					$this->mission = strtolower(trim($_GET['mission']));
112
					if (isset($this->configProjet[$this->mission])) {
112
					if (isset($this->configProjet[$this->mission])) {
113
						$this->configMission = $this->configProjet[$this->mission];
113
						$this->configMission = $this->configProjet[$this->mission];
114
					}
114
					}
115
				}
115
				}
116
			} else {
116
			} else {
117
				$this->messages[] = "Le fichier de configuration '$fichier_config' n'a pu être chargé.";
117
				$this->messages[] = "Le fichier de configuration '$fichier_config' n'a pu être chargé.";
118
			}
118
			}
119
		} else {
119
		} else {
120
			$this->debug[] = "Le fichier de configuration '$fichier_config' n'existe pas.";
120
			$this->debug[] = "Le fichier de configuration '$fichier_config' n'existe pas.";
121
		}
121
		}
122
	}
122
	}
123
 
123
 
124
	/**
124
	/**
125
	 * Retourne true si le dossier du projet courant existe, false sinon
125
	 * Retourne true si le dossier du projet courant existe, false sinon
126
	 * @return boolean
126
	 * @return boolean
127
	 */
127
	 */
128
	protected function projetASquelette() {
128
	protected function projetASquelette() {
129
		return file_exists(dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet);
129
		return file_exists(dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet);
130
	}
130
	}
131
 
131
 
132
	/**
132
	/**
133
	 * Exécution du widget complet 
133
	 * Exécution du widget complet 
134
	 * @return Ambigous <string, unknown, multitype:string unknown >
134
	 * @return Ambigous <string, unknown, multitype:string unknown >
135
	 */
135
	 */
136
	public function executerWidget() {
136
	public function executerWidget() {
137
		$referentiel_impose = false;
137
		$referentiel_impose = false;
138
		if (isset($_GET['referentiel']) && $_GET['referentiel'] != '' && $_GET['referentiel'] != "autre") {
138
		if (isset($_GET['referentiel']) && $_GET['referentiel'] != '' && $_GET['referentiel'] != "autre") {
139
			$this->ns_referentiel = $_GET['referentiel'];
139
			$this->ns_referentiel = $_GET['referentiel'];
140
			$referentiel_impose = true;
140
			$referentiel_impose = true;
141
		}
141
		}
142
 
142
 
143
		$widget['squelette'] = $this->projet;
143
		$widget['squelette'] = $this->projet;
144
		$widget['donnees'] = array();
144
		$widget['donnees'] = array();
145
		$widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
145
		$widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
146
		$widget['donnees']['url_ws_saisie'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_SAISIE);
146
		$widget['donnees']['url_ws_saisie'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_SAISIE);
147
		$widget['donnees']['url_ws_obs'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_OBS);
147
		$widget['donnees']['url_ws_obs'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_OBS);
148
		$widget['donnees']['url_ws_upload'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_UPLOAD);
148
		$widget['donnees']['url_ws_upload'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_UPLOAD);
149
		$widget['donnees']['url_ws_annuaire'] = sprintf($this->config['chemins']['baseURLServicesAnnuaireTpl'], 'utilisateur/identite-par-courriel/');
149
		$widget['donnees']['url_ws_annuaire'] = sprintf($this->config['chemins']['baseURLServicesAnnuaireTpl'], 'utilisateur/identite-par-courriel/');
150
		$widget['donnees']['url_remarques'] = $this->config['chemins']['widgetRemarquesUrl'];
150
		$widget['donnees']['url_remarques'] = $this->config['chemins']['widgetRemarquesUrl'];
151
 
151
 
152
		$widget['donnees']['logo'] = isset($_GET['logo']) ? $_GET['logo'] : 'defaut';
152
		$widget['donnees']['logo'] = isset($_GET['logo']) ? $_GET['logo'] : 'defaut';
153
		$widget['donnees']['titre'] = $this->getTitrePage();
153
		$widget['donnees']['titre'] = $this->getTitrePage();
154
		$widget['donnees']['nom_mission'] = $this->getNomMissionFlore();
154
		$widget['donnees']['nom_mission'] = $this->getNomMissionFlore();
155
 
155
 
156
		$widget['donnees']['referentiel_impose'] = $referentiel_impose;
156
		$widget['donnees']['referentiel_impose'] = $referentiel_impose;
157
		$widget['donnees']['espece_imposee'] = false;
157
		$widget['donnees']['espece_imposee'] = false;
158
		$widget['donnees']['nn_espece_defaut'] = '';
158
		$widget['donnees']['nn_espece_defaut'] = '';
159
		$widget['donnees']['nom_sci_espece_defaut'] = '';
159
		$widget['donnees']['nom_sci_espece_defaut'] = '';
160
		$widget['donnees']['infos_espece'] = '{}';
160
		$widget['donnees']['infos_espece'] = '{}';
161
		
161
		
162
		$projetsAutorises = $this->transformerEnTableau($this->config['projets']['autorises']);
162
		$projetsAutorises = $this->transformerEnTableau($this->config['projets']['autorises']);
163
 
163
 
164
		$urlWsNsTpl = $this->config['chemins']['baseURLServicesEfloreTpl'];
164
		$urlWsNsTpl = $this->config['chemins']['baseURLServicesEfloreTpl'];
165
		$urlWsNs = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, $this->ns_referentiel, self::WS_NOM);
165
		$urlWsNs = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, $this->ns_referentiel, self::WS_NOM);
166
		$urlWsNsSansRef = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, '{referentiel}', self::WS_NOM);
166
		$urlWsNsSansRef = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, '{referentiel}', self::WS_NOM);
167
		$widget['donnees']['url_ws_autocompletion_ns'] = $urlWsNs;
167
		$widget['donnees']['url_ws_autocompletion_ns'] = $urlWsNs;
168
		$widget['donnees']['url_ws_autocompletion_ns_tpl'] = $urlWsNsSansRef;
168
		$widget['donnees']['url_ws_autocompletion_ns_tpl'] = $urlWsNsSansRef;
169
		$widget['donnees']['ns_referentiel'] = $this->ns_referentiel;
169
		$widget['donnees']['ns_referentiel'] = $this->ns_referentiel;
170
		
170
		
171
		$widget['donnees']['url_ws_trace_rue_tpl'] = $this->config['chemins']['serviceTraceRueUrl'];
171
		$widget['donnees']['url_ws_trace_rue_tpl'] = $this->config['chemins']['serviceTraceRueUrl'];
172
		
172
		
173
		if ($this->especeEstImposee()) {
173
		if ($this->especeEstImposee()) {
174
			$nnEspeceImposee = $this->getNnEspeceImposee();
174
			$nnEspeceImposee = $this->getNnEspeceImposee();
175
			$nom = $this->chargerInfosTaxon($nnEspeceImposee);
175
			$nom = $this->chargerInfosTaxon($nnEspeceImposee);
-
 
176
			var_dump($nom);exit;
176
			$widget['donnees']['espece_imposee'] = true;
177
			$widget['donnees']['espece_imposee'] = true;
177
			$widget['donnees']['nn_espece_defaut'] = $nnEspeceImposee;
178
			$widget['donnees']['nn_espece_defaut'] = $nnEspeceImposee;
178
			$widget['donnees']['nom_sci_espece_defaut'] = $nom['nom_complet'];
179
			$widget['donnees']['nom_sci_espece_defaut'] = $nom['nom_complet'];
179
			$widget['donnees']['infos_espece'] = $this->array2js($nom, true);
180
			$widget['donnees']['infos_espece'] = $this->array2js($nom, true);
180
		}
181
		}
181
 
182
 
182
		$projetsAListeDeNoms = $this->transformerEnTableau($this->config['projets']['liste_noms']);
183
		$projetsAListeDeNoms = $this->transformerEnTableau($this->config['projets']['liste_noms']);
183
		if (in_array($this->projet, $projetsAListeDeNoms)) {
184
		if (in_array($this->projet, $projetsAListeDeNoms)) {
184
			$projetsAListeDeNomsSciEtVerna = $this->transformerEnTableau($this->config['projets']['liste_noms_sci_et_verna']);
185
			$projetsAListeDeNomsSciEtVerna = $this->transformerEnTableau($this->config['projets']['liste_noms_sci_et_verna']);
185
			if (in_array($this->projet, $projetsAListeDeNomsSciEtVerna)) {
186
			if (in_array($this->projet, $projetsAListeDeNomsSciEtVerna)) {
186
				$widget['donnees']['taxons'] = $this->recupererListeNoms();
187
				$widget['donnees']['taxons'] = $this->recupererListeNoms();
187
			} else {
188
			} else {
188
				$widget['donnees']['taxons'] = $this->recupererListeNomsSci();
189
				$widget['donnees']['taxons'] = $this->recupererListeNomsSci();
189
			}
190
			}
190
		}
191
		}
191
 
192
 
192
		// Chargement de la liste des milieux issues du fichier .ini du projet
193
		// Chargement de la liste des milieux issues du fichier .ini du projet
193
		$projetsAListeDeMilieux = $this->transformerEnTableau($this->config['projets']['liste_milieux']);
194
		$projetsAListeDeMilieux = $this->transformerEnTableau($this->config['projets']['liste_milieux']);
194
		if (in_array($this->projet, $projetsAListeDeMilieux)) {
195
		if (in_array($this->projet, $projetsAListeDeMilieux)) {
195
			$widget['donnees']['milieux'] = $this->parserMilieux();
196
			$widget['donnees']['milieux'] = $this->parserMilieux();
196
		}
197
		}
197
 
198
 
198
		return $widget;
199
		return $widget;
199
	}
200
	}
200
 
201
 
201
	protected function getTitrePage() {
202
	protected function getTitrePage() {
202
		$titre = 'defaut';
203
		$titre = 'defaut';
203
		if (isset($this->configProjet['titre_page'])) {
204
		if (isset($this->configProjet['titre_page'])) {
204
			$titre = $this->configProjet['titre_page'];
205
			$titre = $this->configProjet['titre_page'];
205
		}
206
		}
206
		if (isset($this->configMission['titre_page'])) {
207
		if (isset($this->configMission['titre_page'])) {
207
			$titre = $this->configMission['titre_page'];
208
			$titre = $this->configMission['titre_page'];
208
		}
209
		}
209
		if (isset($_GET['titre'])) {
210
		if (isset($_GET['titre'])) {
210
			$titre = $_GET['titre'];
211
			$titre = $_GET['titre'];
211
		}
212
		}
212
		if ($titre === 0) {
213
		if ($titre === 0) {
213
			$titre = '';
214
			$titre = '';
214
		}
215
		}
215
		return $titre;
216
		return $titre;
216
	}
217
	}
217
 
218
 
218
	/**
219
	/**
219
	 * Un nom un peu plus sympatoche à afficher que juste le mot-clef associé; s'il
220
	 * Un nom un peu plus sympatoche à afficher que juste le mot-clef associé; s'il
220
	 * n'est pas défini dans la config, on prend le mot-clef tout de même
221
	 * n'est pas défini dans la config, on prend le mot-clef tout de même
221
	 */
222
	 */
222
	protected function getNomMissionFlore() {
223
	protected function getNomMissionFlore() {
223
		$nom = $this->mission;
224
		$nom = $this->mission;
224
		if (isset($this->configMission['nom_mission'])) {
225
		if (isset($this->configMission['nom_mission'])) {
225
			$nom = $this->configMission['nom_mission'];
226
			$nom = $this->configMission['nom_mission'];
226
		}
227
		}
227
		return $nom;
228
		return $nom;
228
	}
229
	}
229
 
230
 
230
	/**
231
	/**
231
	 * Remplit un fichier JS avec une variable contenant une liste restreinte de taxons,
232
	 * Remplit un fichier JS avec une variable contenant une liste restreinte de taxons,
232
	 * pour certains projets
233
	 * pour certains projets
233
	 * @return string
234
	 * @return string
234
	 */
235
	 */
235
	public function executerTaxons() {
236
	public function executerTaxons() {
236
		$widget['squelette'] = $this->projet.'_taxons';
237
		$widget['squelette'] = $this->projet.'_taxons';
237
		$widget['squelette_ext'] = '.tpl.js';
238
		$widget['squelette_ext'] = '.tpl.js';
238
		$widget['donnees'] = array();
239
		$widget['donnees'] = array();
239
		$nomsAAfficher = $this->recupererListeNomsSci();
240
		$nomsAAfficher = $this->recupererListeNomsSci();
240
		$taxons_tries = array();
241
		$taxons_tries = array();
241
		foreach ($nomsAAfficher as $taxon) {
242
		foreach ($nomsAAfficher as $taxon) {
242
			$taxons_tries[$taxon['num_nom_sel']] = $taxon;
243
			$taxons_tries[$taxon['num_nom_sel']] = $taxon;
243
		}
244
		}
244
		$widget['donnees']['taxons'] = json_encode($taxons_tries);
245
		$widget['donnees']['taxons'] = json_encode($taxons_tries);
245
		return $widget;
246
		return $widget;
246
	}
247
	}
247
 
248
 
248
	/**
249
	/**
249
	 * Trie par nom français les taxons lus dans le fichier tsv
250
	 * Trie par nom français les taxons lus dans le fichier tsv
250
	 */
251
	 */
251
	protected function recupererListeNomsSci() {
252
	protected function recupererListeNomsSci() {
252
		$taxons = $this->recupererListeTaxon();
253
		$taxons = $this->recupererListeTaxon();
253
		if (is_array($taxons)) {
254
		if (is_array($taxons)) {
254
			$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
255
			$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
255
		}
256
		}
256
		return $taxons;
257
		return $taxons;
257
	}
258
	}
258
 
259
 
259
	/**
260
	/**
260
	 * @TODO documenter
261
	 * @TODO documenter
261
	 * @return array
262
	 * @return array
262
	 */
263
	 */
263
	protected function recupererListeNoms() {
264
	protected function recupererListeNoms() {
264
		$taxons = $this->recupererListeTaxon();
265
		$taxons = $this->recupererListeTaxon();
265
		$nomsAAfficher = array();
266
		$nomsAAfficher = array();
266
		$nomsSpeciaux = array();
267
		$nomsSpeciaux = array();
267
		if (is_array($taxons)) {
268
		if (is_array($taxons)) {
268
			foreach ($taxons as $taxon) {
269
			foreach ($taxons as $taxon) {
269
				$nomSciTitle = $taxon['nom_ret'].
270
				$nomSciTitle = $taxon['nom_ret'].
270
					($taxon['nom_fr'] != '' ? ' - '.$taxon['nom_fr'] : '' ).
271
					($taxon['nom_fr'] != '' ? ' - '.$taxon['nom_fr'] : '' ).
271
					($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
272
					($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
272
				$nomFrTitle = $taxon['nom_sel'].
273
				$nomFrTitle = $taxon['nom_sel'].
273
					($taxon['nom_ret'] != $taxon['nom_sel']? ' - '.$taxon['nom_ret'] : '' ).
274
					($taxon['nom_ret'] != $taxon['nom_sel']? ' - '.$taxon['nom_ret'] : '' ).
274
					($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
275
					($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
275
 
276
 
276
				if ($taxon['groupe'] == 'special') {
277
				if ($taxon['groupe'] == 'special') {
277
					$nomsSpeciaux[] = array(
278
					$nomsSpeciaux[] = array(
278
						'num_nom' => $taxon['num_nom_sel'],
279
						'num_nom' => $taxon['num_nom_sel'],
279
						'nom_a_afficher' => $taxon['nom_fr'],
280
						'nom_a_afficher' => $taxon['nom_fr'],
280
						'nom_a_sauver' => $taxon['nom_sel'],
281
						'nom_a_sauver' => $taxon['nom_sel'],
281
						'nom_title' => $nomSciTitle,
282
						'nom_title' => $nomSciTitle,
282
						'nom_type' => 'nom-special');
283
						'nom_type' => 'nom-special');
283
				} else {
284
				} else {
284
					$nomsAAfficher[] = array(
285
					$nomsAAfficher[] = array(
285
						'num_nom' => $taxon['num_nom_sel'],
286
						'num_nom' => $taxon['num_nom_sel'],
286
						'nom_a_afficher' => $taxon['nom_sel'],
287
						'nom_a_afficher' => $taxon['nom_sel'],
287
						'nom_a_sauver' => $taxon['nom_sel'],
288
						'nom_a_sauver' => $taxon['nom_sel'],
288
						'nom_title' => $nomSciTitle,
289
						'nom_title' => $nomSciTitle,
289
						'nom_type' => 'nom-sci');
290
						'nom_type' => 'nom-sci');
290
					$nomsAAfficher[] = array(
291
					$nomsAAfficher[] = array(
291
						'num_nom' => $taxon['num_nom_sel'],
292
						'num_nom' => $taxon['num_nom_sel'],
292
						'nom_a_afficher' => $taxon['nom_fr'],
293
						'nom_a_afficher' => $taxon['nom_fr'],
293
						'nom_a_sauver' => $taxon['nom_fr'],
294
						'nom_a_sauver' => $taxon['nom_fr'],
294
						'nom_title' => $nomFrTitle,
295
						'nom_title' => $nomFrTitle,
295
						'nom_type' => 'nom-fr');
296
						'nom_type' => 'nom-fr');
296
				}
297
				}
297
			}
298
			}
298
			$nomsAAfficher = self::trierTableauMd($nomsAAfficher, array('nom_a_afficher' => SORT_ASC));
299
			$nomsAAfficher = self::trierTableauMd($nomsAAfficher, array('nom_a_afficher' => SORT_ASC));
299
			$nomsSpeciaux = self::trierTableauMd($nomsSpeciaux, array('nom_a_afficher' => SORT_ASC));
300
			$nomsSpeciaux = self::trierTableauMd($nomsSpeciaux, array('nom_a_afficher' => SORT_ASC));
300
		}
301
		}
301
		return array('speciaux' => $nomsSpeciaux, 'sci-et-fr' => $nomsAAfficher);
302
		return array('speciaux' => $nomsSpeciaux, 'sci-et-fr' => $nomsAAfficher);
302
	}
303
	}
303
 
304
 
304
	/**
305
	/**
305
	 * Lit une liste de taxons depuis un fichier tsv fourni
306
	 * Lit une liste de taxons depuis un fichier tsv fourni
306
	 */
307
	 */
307
	protected function recupererListeTaxon() {
308
	protected function recupererListeTaxon() {
308
		$taxons = array();
309
		$taxons = array();
309
		$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons.tsv';
310
		$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons.tsv';
310
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
311
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
311
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
312
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
312
		} else {
313
		} else {
313
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
314
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
314
		}
315
		}
315
		return $taxons;
316
		return $taxons;
316
	}
317
	}
317
 
318
 
318
	/**
319
	/**
319
	 * Découpe un fihcier tsv
320
	 * Découpe un fihcier tsv
320
	 */
321
	 */
321
	protected function decomposerFichierTsv($fichier, $delimiter = "\t"){
322
	protected function decomposerFichierTsv($fichier, $delimiter = "\t"){
322
		$header = null;
323
		$header = null;
323
		$data = array();
324
		$data = array();
324
		if (($handle = fopen($fichier, 'r')) !== FALSE) {
325
		if (($handle = fopen($fichier, 'r')) !== FALSE) {
325
			while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
326
			while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
326
				if (!$header) {
327
				if (!$header) {
327
					$header = $row;
328
					$header = $row;
328
				} else {
329
				} else {
329
					$data[] = array_combine($header, $row);
330
					$data[] = array_combine($header, $row);
330
				}
331
				}
331
			}
332
			}
332
			fclose($handle);
333
			fclose($handle);
333
		}
334
		}
334
		return $data;
335
		return $data;
335
	}
336
	}
336
 
337
 
337
	/**
338
	/**
338
	 * Récupère la liste des milieux depuis la section [milieux] de la configuration
339
	 * Récupère la liste des milieux depuis la section [milieux] de la configuration
339
	 * du projet, si elle existe
340
	 * du projet, si elle existe
340
	 * @return array
341
	 * @return array
341
	 */
342
	 */
342
	protected function parserMilieux() {
343
	protected function parserMilieux() {
343
		$infosMilieux = array();
344
		$infosMilieux = array();
344
		if (isset($this->configProjet['milieux'])) {
345
		if (isset($this->configProjet['milieux'])) {
345
			$milieux = explode('|', $this->configProjet['milieux']);
346
			$milieux = explode('|', $this->configProjet['milieux']);
346
			foreach ($milieux as $milieu) {
347
			foreach ($milieux as $milieu) {
347
				$milieu = trim($milieu);
348
				$milieu = trim($milieu);
348
				$details = explode(';', $milieu);
349
				$details = explode(';', $milieu);
349
				if (isset($details[1])) {
350
				if (isset($details[1])) {
350
					$infosMilieux[$details[0]] = $details[1];
351
					$infosMilieux[$details[0]] = $details[1];
351
				} else {
352
				} else {
352
					$infosMilieux[$details[0]] = '';
353
					$infosMilieux[$details[0]] = '';
353
				}
354
				}
354
			}
355
			}
355
			ksort($infosMilieux);
356
			ksort($infosMilieux);
356
		}
357
		}
357
		return $infosMilieux;
358
		return $infosMilieux;
358
	}
359
	}
359
 
360
 
360
	/**
361
	/**
361
	 * Retourne true si le widget est restreint à une espèce, false sinon
362
	 * Retourne true si le widget est restreint à une espèce, false sinon
362
	 * @return boolean
363
	 * @return boolean
363
	 */
364
	 */
364
	protected function especeEstImposee() {
365
	protected function especeEstImposee() {
365
		return ((isset($_GET['num_nom']) && $_GET['num_nom'] != '')
366
		return ((isset($_GET['num_nom']) && $_GET['num_nom'] != '')
366
			|| isset($this->configProjet['sp_imposee']) || isset($this->configMission['sp_imposee']));
367
			|| isset($this->configProjet['sp_imposee']) || isset($this->configMission['sp_imposee']));
367
	}
368
	}
368
 
369
 
369
	/**
370
	/**
370
	 * Retourne le numéro nomenclatural (nn) de l'espèce imposée si tel est
371
	 * Retourne le numéro nomenclatural (nn) de l'espèce imposée si tel est
371
	 * le cas, null sinon
372
	 * le cas, null sinon
372
	 * @return string
373
	 * @return string
373
	 */
374
	 */
374
	protected function getNnEspeceImposee() {
375
	protected function getNnEspeceImposee() {
375
		$nn = null;
376
		$nn = null;
376
		if (isset($_GET['num_nom']) && is_numeric($_GET['num_nom'])) {
377
		if (isset($_GET['num_nom']) && is_numeric($_GET['num_nom'])) {
377
			$nn = $_GET['num_nom'];
378
			$nn = $_GET['num_nom'];
378
		} else if (isset($this->configProjet['sp_imposee'])) {
379
		} else if (isset($this->configProjet['sp_imposee'])) {
379
			$nn = $this->configProjet['sp_imposee'];
380
			$nn = $this->configProjet['sp_imposee'];
380
		} else if (isset($this->configMission['sp_imposee'])) {
381
		} else if (isset($this->configMission['sp_imposee'])) {
381
			$nn = $this->configMission['sp_imposee'];
382
			$nn = $this->configMission['sp_imposee'];
382
		}
383
		}
383
		return $nn;
384
		return $nn;
384
	}
385
	}
385
 
386
 
386
	/**
387
	/**
387
	 * Consulte un webservice pour obtenir des informations sur le taxon dont le
388
	 * Consulte un webservice pour obtenir des informations sur le taxon dont le
388
	 * numéro nomenclatural est $num_nom (ce sont donc plutôt des infos sur le nom
389
	 * numéro nomenclatural est $num_nom (ce sont donc plutôt des infos sur le nom
389
	 * et non le taxon?)
390
	 * et non le taxon?)
390
	 * @param string|int $num_nom
391
	 * @param string|int $num_nom
391
	 * @return array
392
	 * @return array
392
	 */
393
	 */
393
	protected function chargerInfosTaxon($num_nom) {
394
	protected function chargerInfosTaxon($num_nom) {
394
		$url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $this->ns_referentiel, $num_nom);
395
		$url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $this->ns_referentiel, $num_nom);
395
		$infos = json_decode(file_get_contents($url_service_infos));
396
		$infos = json_decode(file_get_contents($url_service_infos));
396
		// trop de champs injectés dans les infos espèces peuvent
397
		// trop de champs injectés dans les infos espèces peuvent
397
		// faire planter javascript
398
		// faire planter javascript
398
		$champs_a_garder = array('id', 'nom_sci','nom_sci_complet', 'nom_complet',
399
		$champs_a_garder = array('id', 'nom_sci','nom_sci_complet', 'nom_complet',
399
			'famille','nom_retenu.id', 'nom_retenu.libelle', 'num_taxonomique');
400
			'famille','nom_retenu.id', 'nom_retenu.libelle', 'num_taxonomique');
400
		$resultat = array();
401
		$resultat = array();
401
		if (isset($infos) && !empty($infos)) {
402
		if (isset($infos) && !empty($infos)) {
402
			$infos = (array)$infos;
403
			$infos = (array)$infos;
403
			if (isset($infos['nom_sci']) && $infos['nom_sci'] != '') {
404
			if (isset($infos['nom_sci']) && $infos['nom_sci'] != '') {
404
				$resultat = array_intersect_key($infos, array_flip($champs_a_garder));
405
				$resultat = array_intersect_key($infos, array_flip($champs_a_garder));
405
				$resultat['retenu'] = ($infos['id'] == $infos['nom_retenu.id']) ? "true" : "false";
406
				$resultat['retenu'] = ($infos['id'] == $infos['nom_retenu.id']) ? "true" : "false";
406
			}
407
			}
407
		}
408
		}
408
		return $resultat;
409
		return $resultat;
409
	}
410
	}
410
 
411
 
411
	/**
412
	/**
412
	 * Convertit un tableau PHP en Javascript - @WTF pourquoi ne pas faire un json_encode ?
413
	 * Convertit un tableau PHP en Javascript - @WTF pourquoi ne pas faire un json_encode ?
413
	 * @param array $array
414
	 * @param array $array
414
	 * @param boolean $show_keys
415
	 * @param boolean $show_keys
415
	 * @return une portion de JSON représentant le tableau
416
	 * @return une portion de JSON représentant le tableau
416
	 */
417
	 */
417
	protected function array2js($array,$show_keys) {
418
	protected function array2js($array,$show_keys) {
418
		$tableauJs = '{}';
419
		$tableauJs = '{}';
419
		if (!empty($array)) {
420
		if (!empty($array)) {
420
			$total = count($array) - 1;
421
			$total = count($array) - 1;
421
			$i = 0;
422
			$i = 0;
422
			$dimensions = array();
423
			$dimensions = array();
423
			foreach ($array as $key => $value) {
424
			foreach ($array as $key => $value) {
424
				if (is_array($value)) {
425
				if (is_array($value)) {
425
					$dimensions[$i] = array2js($value,$show_keys);
426
					$dimensions[$i] = array2js($value,$show_keys);
426
					if ($show_keys) {
427
					if ($show_keys) {
427
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
428
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
428
					}
429
					}
429
				} else {
430
				} else {
430
					$dimensions[$i] = '"'.addslashes($value).'"';
431
					$dimensions[$i] = '"'.addslashes($value).'"';
431
					if ($show_keys) {
432
					if ($show_keys) {
432
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
433
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
433
					}
434
					}
434
				}
435
				}
435
				if ($i == 0) {
436
				if ($i == 0) {
436
					$dimensions[$i] = '{'.$dimensions[$i];
437
					$dimensions[$i] = '{'.$dimensions[$i];
437
				}
438
				}
438
				if ($i == $total) {
439
				if ($i == $total) {
439
					$dimensions[$i].= '}';
440
					$dimensions[$i].= '}';
440
				}
441
				}
441
				$i++;
442
				$i++;
442
			}
443
			}
443
			$tableauJs = implode(',', $dimensions);
444
			$tableauJs = implode(',', $dimensions);
444
		}
445
		}
445
		return $tableauJs;
446
		return $tableauJs;
446
	}
447
	}
447
}
448
}