Subversion Repositories eFlore/Applications.cel

Rev

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

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