Subversion Repositories eFlore/Applications.cel

Rev

Rev 2688 | Rev 2786 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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