Subversion Repositories eFlore/Applications.cel

Rev

Rev 2577 | Rev 2744 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2577 Rev 2688
Line 1... Line 1...
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
-
 
3
/**
2
/**
4
 * Widget fournissant des interfaces de saisies simplifiée pour différent projets.
3
 * Widget fournissant des interfaces de saisie simplifiée pour différent projets
5
 * Encodage en entrée : utf8
-
 
6
 * Encodage en sortie : utf8
-
 
7
 *
4
 *
8
 * Cas d'utilisation et documentation :
5
 * Cas d'utilisation et documentation :
9
 * @link http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=AideCELWidgetSaisie
6
 * @link http://www.tela-botanica.org/wikini/AideCarnetEnLigne/wakka.php?wiki=AideCELWidgetSaisie
10
 *
7
 *
11
 * Paramètres :
8
 * Paramètres :
12
 * ===> projet = chaine  [par défaut : defaut] : indique le widgetde saisie à charger.
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é
13
 * ===> mission = chaine  [par défaut : vide] : permet de charger un "sous-widget" vis à vis du projet.
11
 * - mission [par défaut : vide] : permet de charger un "sous-widget", dans le cas où un widget personnalisé
14
 * Indique quel projet nous voulons charger
12
 * 		est associé au projet, et ce widget accepte des sous-widgets (ex: "missions-flore")
15
 *
13
 *
16
 * @author Mathias CHOUET <mathias@tela-botanica.org>
14
 * @author Mathias CHOUET <mathias@tela-botanica.org>
17
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
15
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
18
 * @author Aurelien PERONNET <aurelien@tela-botanica.org>
16
 * @author Aurelien PERONNET <aurelien@tela-botanica.org>
19
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
17
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
20
 * @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>
21
 * @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
19
 * @copyright 1999-2015 Tela Botanica <accueil@tela-botanica.org>
22
 */
20
 */
23
class Saisie extends WidgetCommun {
21
class Saisie extends WidgetCommun {
Line 24... Line 22...
24
 
22
 
25
	const DS = DIRECTORY_SEPARATOR;
23
	const DS = DIRECTORY_SEPARATOR;
26
	const PROJET_DEFAUT = 'defaut';
24
	const PROJET_DEFAUT = 'defaut';
27
	const WS_SAISIE = 'CelWidgetSaisie';
25
	const WS_SAISIE = 'CelWidgetSaisie';
28
	const WS_UPLOAD = 'CelWidgetUploadImageTemp';
26
	const WS_UPLOAD = 'CelWidgetUploadImageTemp';
29
	const WS_OBS = 'CelObs';
27
	const WS_OBS = 'CelObs';
30
	const WS_NOM = 'noms';
28
	const WS_NOM = 'noms';
-
 
29
	const EFLORE_API_VERSION = '0.1';
Line -... Line 30...
-
 
30
	const REFERENTIEL_DEFAUT = 'bdtfx';
31
	const EFLORE_API_VERSION = '0.1';
31
 
-
 
32
	/** référentiel utilisé pour al complétion des noms scientifiques */
32
 
33
	protected $ns_referentiel;
33
	private $ns_referentiel = 'bdtfx';
34
	/** mot-clé associé aux saisies, et template personnalisé si appliquable */
34
	private $projet = null;
35
	protected $projet = null;
Line 35... Line 36...
35
	private $configProjet = null;
36
	protected $configProjet = null;
36
	private $configMission = null;
37
	protected $configMission = null;
37
 
38
 
38
	/**
39
	/**
39
	 * Méthode appelée par défaut pour charger ce widget.
40
	 * Amorçage du widget
40
	 */
41
	 */
41
	public function executer() {
-
 
42
		$retour = null;
42
	public function executer() {
-
 
43
		// paramètres par défaut
-
 
44
		$this->ns_referentiel = self::REFERENTIEL_DEFAUT;
43
		extract($this->parametres);
45
		$this->projet = self::PROJET_DEFAUT;
44
 
46
 
45
		$this->projet = self::PROJET_DEFAUT;
47
		// définition du projet / mission
46
		if (isset($projet) && trim($projet) != "") {
48
		if (isset($this->parametres['projet']) && trim($this->parametres['projet']) != "") {
47
			$projets = explode(',', $projet);
49
			$projets = explode(',', $this->parametres['projet']);
Line -... Line 50...
-
 
50
			$this->projet = strtolower($projets[0]);
-
 
51
		}
48
			$this->projet = strtolower($projets[0]);
52
		$this->chargerConfigProjet();
49
		}
53
 
50
		$this->chargerConfigProjet();
54
		// exécution du service (le widget entier ou une sous-partie, par ex "Taxons")
51
 
55
		$retour = null;
52
		$service = isset($service) ? $service : 'widget';
56
		$service = isset($this->parametres['service']) ? $this->parametres['service'] : 'widget';
53
		$methode = $this->traiterNomMethodeExecuter($service);
57
		$methode = $this->traiterNomMethodeExecuter($service);
54
		if (method_exists($this, $methode)) {
58
		if (method_exists($this, $methode)) {
Line -... Line 59...
-
 
59
			$retour = $this->$methode();
55
			$retour = $this->$methode();
60
		} else {
56
		} else {
61
			$this->messages[] = "Le service '$methode' n'est pas disponible.";
57
			$this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
62
		}
58
		}
63
 
59
 
64
		// injection des données dans le squelette
Line 68... Line 73...
68
			}
73
			}
69
			$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
74
			$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
70
			$mime = isset($retour['mime']) ? $retour['mime'] : null;
75
			$mime = isset($retour['mime']) ? $retour['mime'] : null;
71
		} else {
76
		} else {
72
			if (count($this->messages) == 0) {
77
			if (count($this->messages) == 0) {
73
				$this->messages[] = "La méthode du sous-service ne renvoie pas les données dans le bon format.";
78
				$this->messages[] = "La méthode du sous-service ne renvoie pas les données dans le bon format";
74
			}
79
			}
75
			$contenu = 'Un problème est survenu : '.print_r($this->messages, true);
80
			$contenu = 'Un problème est survenu : ' . print_r($this->messages, true);
76
		}
81
		}
Line -... Line 82...
-
 
82
 
77
 
83
		// envoi de la page
78
		$this->envoyer($contenu, $mime);
84
		$this->envoyer($contenu, $mime);
Line -... Line 85...
-
 
85
	}
-
 
86
 
-
 
87
	/**
-
 
88
	 * Charge le fichier de configuration associé au projet : configurations/nomduprojet.ini
-
 
89
	 * Si une mission est définie, charge séparément la section de la configuration concernant
79
	}
90
	 * cette mission : [nommission]
80
 
91
	 */
81
	private function chargerConfigProjet() {
92
	protected function chargerConfigProjet() {
82
		$fichier_config = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'.ini';
93
		$fichier_config = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'.ini';
83
		if (file_exists($fichier_config)) {
94
		if (file_exists($fichier_config)) {
84
			if ($this->configProjet = parse_ini_file($fichier_config, true)) {
95
			if ($this->configProjet = parse_ini_file($fichier_config, true)) {
85
				if (isset($_GET['mission'])) {
96
				if (isset($_GET['mission'])) {
86
					$mission = strtolower(trim($_GET['mission']));
97
					$mission = strtolower(trim($_GET['mission']));
87
					if (isset($this->configProjet[$mission])) {
98
					if (isset($this->configProjet[$mission])) {
88
						$this->configMission = $this->configProjet[$mission];
99
						$this->configMission = $this->configProjet[$mission];
89
					}
100
					}
90
			}
101
				}
91
			} else {
102
			} else {
92
				$this->messages[] = "Le fichier ini '$fichier_config' du projet n'a pu être chargé.";
103
				$this->messages[] = "Le fichier de configuration '$fichier_config' n'a pu être chargé.";
93
			}
104
			}
94
		} else {
105
		} else {
95
			$this->debug[] = "Le fichier ini '$fichier_config' du projet n'existe pas.";
106
			$this->debug[] = "Le fichier de configuration '$fichier_config' n'existe pas.";
Line -... Line 107...
-
 
107
		}
96
		}
108
	}
97
	}
109
 
-
 
110
	/**
98
 
111
	 * Retourne true si le dossier du projet courant existe, false sinon
99
	private function projetASquelette() {
112
	 * @return boolean
100
		// fonction très simple qui ne teste que si le dossier du projet courant
113
	 */
Line -... Line 114...
-
 
114
	protected function projetASquelette() {
-
 
115
		return file_exists(dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet);
-
 
116
	}
-
 
117
 
101
		// existe, mais elle suffit pour le moment.
118
	/**
102
		return file_exists(dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet);
119
	 * Exécution du widget complet 
103
	}
120
	 * @return Ambigous <string, unknown, multitype:string unknown >
104
 
121
	 */
105
	public function executerWidget() {
122
	public function executerWidget() {
106
		$referentiel_impose = false;
123
		$referentiel_impose = false;
Line 107... Line 124...
107
		if (isset($_GET['referentiel']) && $_GET['referentiel'] != '' && $_GET['referentiel'] != "autre") {
124
		if (isset($_GET['referentiel']) && $_GET['referentiel'] != '' && $_GET['referentiel'] != "autre") {
108
			$this->ns_referentiel = isset($_GET['referentiel']) && $_GET['referentiel'] != '' ? $_GET['referentiel'] : $this->ns_referentiel;
125
			$this->ns_referentiel = $_GET['referentiel'];
Line 136... Line 153...
136
		$widget['donnees']['url_ws_autocompletion_ns_tpl'] = $urlWsNsSansRef;
153
		$widget['donnees']['url_ws_autocompletion_ns_tpl'] = $urlWsNsSansRef;
137
		$widget['donnees']['ns_referentiel'] = $this->ns_referentiel;
154
		$widget['donnees']['ns_referentiel'] = $this->ns_referentiel;
Line 138... Line 155...
138
		
155
		
139
		if ($this->especeEstImposee()) {
156
		if ($this->especeEstImposee()) {
140
			$nnEspeceImposee = $this->getNnEspeceImposee();
157
			$nnEspeceImposee = $this->getNnEspeceImposee();
141
			$nom = $this->executerChargementInfosTaxon($nnEspeceImposee);
158
			$nom = $this->chargerInfosTaxon($nnEspeceImposee);
142
			$widget['donnees']['espece_imposee'] = true;
159
			$widget['donnees']['espece_imposee'] = true;
143
			$widget['donnees']['nn_espece_defaut'] = $nnEspeceImposee;
160
			$widget['donnees']['nn_espece_defaut'] = $nnEspeceImposee;
144
			$widget['donnees']['nom_sci_espece_defaut'] = $nom['nom_sci'];
161
			$widget['donnees']['nom_sci_espece_defaut'] = $nom['nom_sci'];
145
			$widget['donnees']['infos_espece'] = $this->array2js($nom, true);
162
			$widget['donnees']['infos_espece'] = $this->array2js($nom, true);
Line 162... Line 179...
162
		}
179
		}
Line 163... Line 180...
163
 
180
 
164
		return $widget;
181
		return $widget;
Line 165... Line 182...
165
	}
182
	}
166
 
183
 
167
	private function getTitrePage() {
184
	protected function getTitrePage() {
168
		$titre = 'defaut';
185
		$titre = 'defaut';
169
		if (isset($this->configProjet['titre_page'])) {
186
		if (isset($this->configProjet['titre_page'])) {
170
			$titre = $this->configProjet['titre_page'];
187
			$titre = $this->configProjet['titre_page'];
Line 179... Line 196...
179
			$titre = '';
196
			$titre = '';
180
		}
197
		}
181
		return $titre;
198
		return $titre;
182
	}
199
	}
Line -... Line 200...
-
 
200
 
-
 
201
	/**
-
 
202
	 * Remplit un fichier JS avec une variable contenant une liste restreinte de taxons,
-
 
203
	 * pour certains projets
-
 
204
	 * @return string
183
 
205
	 */
184
	public function executerTaxons() {
206
	public function executerTaxons() {
185
		$widget['squelette'] = $this->projet.'_taxons';
207
		$widget['squelette'] = $this->projet.'_taxons';
186
		$widget['squelette_ext'] = '.tpl.js';
208
		$widget['squelette_ext'] = '.tpl.js';
187
		$widget['donnees'] = array();
209
		$widget['donnees'] = array();
Line 192... Line 214...
192
		}
214
		}
193
		$widget['donnees']['taxons'] = json_encode($taxons_tries);
215
		$widget['donnees']['taxons'] = json_encode($taxons_tries);
194
		return $widget;
216
		return $widget;
195
	}
217
	}
Line -... Line 218...
-
 
218
 
-
 
219
	/**
-
 
220
	 * Trie par nom français les taxons lus dans le fichier tsv
196
 
221
	 */
197
	private function recupererListeNomsSci() {
222
	protected function recupererListeNomsSci() {
198
		$taxons = $this->recupererListeTaxon();
223
		$taxons = $this->recupererListeTaxon();
199
		if (is_array($taxons)) {
224
		if (is_array($taxons)) {
200
			$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
225
			$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
201
		}
226
		}
202
		return $taxons;
227
		return $taxons;
Line -... Line 228...
-
 
228
	}
-
 
229
 
-
 
230
	/**
-
 
231
	 * @TODO documenter
203
	}
232
	 * @return array
204
 
233
	 */
205
	private function recupererListeNoms() {
234
	protected function recupererListeNoms() {
206
		$taxons = $this->recupererListeTaxon();
235
		$taxons = $this->recupererListeTaxon();
207
		$nomsAAfficher = array();
236
		$nomsAAfficher = array();
208
		$nomsSpeciaux = array();
237
		$nomsSpeciaux = array();
Line 241... Line 270...
241
			$nomsSpeciaux = self::trierTableauMd($nomsSpeciaux, array('nom_a_afficher' => SORT_ASC));
270
			$nomsSpeciaux = self::trierTableauMd($nomsSpeciaux, array('nom_a_afficher' => SORT_ASC));
242
		}
271
		}
243
		return array('speciaux' => $nomsSpeciaux, 'sci-et-fr' => $nomsAAfficher);
272
		return array('speciaux' => $nomsSpeciaux, 'sci-et-fr' => $nomsAAfficher);
244
	}
273
	}
Line -... Line 274...
-
 
274
 
-
 
275
	/**
-
 
276
	 * Lit une liste de taxons depuis un fichier tsv fourni
245
 
277
	 */
246
	private function recupererListeTaxon() {
278
	protected function recupererListeTaxon() {
247
		$taxons = null;
279
		$taxons = array();
248
		$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons.tsv';
280
		$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons.tsv';
249
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
281
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
250
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
282
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
251
		} else {
283
		} else {
252
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
284
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
253
		}
285
		}
254
		return $taxons;
286
		return $taxons;
Line -... Line 287...
-
 
287
	}
-
 
288
 
-
 
289
	/**
255
	}
290
	 * Découpe un fihcier tsv
256
 
291
	 */
257
	private function decomposerFichierTsv($fichier, $delimiter = "\t"){
292
	protected function decomposerFichierTsv($fichier, $delimiter = "\t"){
258
		$header = null;
293
		$header = null;
259
		$data = array();
294
		$data = array();
260
		if (($handle = fopen($fichier, 'r')) !== FALSE) {
295
		if (($handle = fopen($fichier, 'r')) !== FALSE) {
Line 268... Line 303...
268
			fclose($handle);
303
			fclose($handle);
269
		}
304
		}
270
		return $data;
305
		return $data;
271
	}
306
	}
Line -... Line 307...
-
 
307
 
-
 
308
	/**
-
 
309
	 * Récupère la liste des milieux depuis la section [milieux] de la configuration
-
 
310
	 * du projet, si elle existe
-
 
311
	 * @return array
272
 
312
	 */
273
	private function parserMilieux() {
313
	protected function parserMilieux() {
274
		$infosMilieux = array();
314
		$infosMilieux = array();
275
		if (isset($this->configProjet['milieux'])) {
315
		if (isset($this->configProjet['milieux'])) {
276
			$milieux = explode('|', $this->configProjet['milieux']);
316
			$milieux = explode('|', $this->configProjet['milieux']);
277
			foreach ($milieux as $milieu) {
317
			foreach ($milieux as $milieu) {
Line 286... Line 326...
286
			ksort($infosMilieux);
326
			ksort($infosMilieux);
287
		}
327
		}
288
		return $infosMilieux;
328
		return $infosMilieux;
289
	}
329
	}
Line -... Line 330...
-
 
330
 
-
 
331
	/**
-
 
332
	 * Retourne true si le widget est restreint à une espèce, false sinon
-
 
333
	 * @return boolean
290
 
334
	 */
291
	private function especeEstImposee() {
335
	protected function especeEstImposee() {
292
		return (isset($_GET['num_nom']) && $_GET['num_nom'] != ''
336
		return ((isset($_GET['num_nom']) && $_GET['num_nom'] != '')
293
			|| isset($this->configProjet['sp_imposee']) || isset($this->configMission['sp_imposee']));
337
			|| isset($this->configProjet['sp_imposee']) || isset($this->configMission['sp_imposee']));
Line -... Line 338...
-
 
338
	}
-
 
339
 
-
 
340
	/**
-
 
341
	 * Retourne le numéro nomenclatural (nn) de l'espèce imposée si tel est
-
 
342
	 * le cas, null sinon
294
	}
343
	 * @return string
295
 
344
	 */
296
	private function getNnEspeceImposee() {
345
	protected function getNnEspeceImposee() {
297
		$nn = null;
346
		$nn = null;
298
		if (isset($_GET['num_nom']) && is_numeric($_GET['num_nom'])) {
347
		if (isset($_GET['num_nom']) && is_numeric($_GET['num_nom'])) {
299
			$nn = $_GET['num_nom'];
348
			$nn = $_GET['num_nom'];
Line 303... Line 352...
303
			$nn = $this->configMission['sp_imposee'];
352
			$nn = $this->configMission['sp_imposee'];
304
		}
353
		}
305
		return $nn;
354
		return $nn;
306
	}
355
	}
Line -... Line 356...
-
 
356
 
-
 
357
	/**
-
 
358
	 * Consulte un webservice pour obtenir des informations sur le taxon dont le
-
 
359
	 * numéro nomenclatural est $num_nom (ce sont donc plutôt des infos sur le nom
-
 
360
	 * et non le taxon?)
-
 
361
	 * @param string|int $num_nom
-
 
362
	 * @return array
307
 
363
	 */
308
	private function executerChargementInfosTaxon($num_nom) {
364
	protected function chargerInfosTaxon($num_nom) {
309
		$url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $this->ns_referentiel, $num_nom);
365
		$url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $this->ns_referentiel, $num_nom);
310
		$infos = json_decode(file_get_contents($url_service_infos));
366
		$infos = json_decode(file_get_contents($url_service_infos));
311
		// trop de champs injectés dans les infos espèces peut
367
		// trop de champs injectés dans les infos espèces peuvent
312
		// faire planter javascript
368
		// faire planter javascript
313
		$champs_a_garder = array('id', 'nom_sci','nom_sci_complet',
369
		$champs_a_garder = array('id', 'nom_sci','nom_sci_complet',
314
			'famille','nom_retenu.id', 'nom_retenu.libelle', 'num_taxonomique');
370
			'famille','nom_retenu.id', 'nom_retenu.libelle', 'num_taxonomique');
315
		$resultat = array();
371
		$resultat = array();
Line 321... Line 377...
321
			}
377
			}
322
		}
378
		}
323
		return $resultat;
379
		return $resultat;
324
	}
380
	}
Line -... Line 381...
-
 
381
 
-
 
382
	/**
-
 
383
	 * Convertit un tableau PHP en Javascript - @WTF pourquoi ne pas faire un json_encode ?
-
 
384
	 * @param array $array
-
 
385
	 * @param boolean $show_keys
-
 
386
	 * @return une portion de JSON représentant le tableau
325
 
387
	 */
326
	private function array2js($array,$show_keys) {
388
	protected function array2js($array,$show_keys) {
327
		$tableauJs = '{}';
389
		$tableauJs = '{}';
328
		if (!empty($array)) {
390
		if (!empty($array)) {
329
			$total = count($array) - 1;
391
			$total = count($array) - 1;
330
			$i = 0;
392
			$i = 0;