Subversion Repositories eFlore/Applications.cel

Rev

Rev 2577 | Rev 2744 | 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;
155
 
156
		if ($this->especeEstImposee()) {
157
			$nnEspeceImposee = $this->getNnEspeceImposee();
2688 mathias 158
			$nom = $this->chargerInfosTaxon($nnEspeceImposee);
2498 aurelien 159
			$widget['donnees']['espece_imposee'] = true;
160
			$widget['donnees']['nn_espece_defaut'] = $nnEspeceImposee;
161
			$widget['donnees']['nom_sci_espece_defaut'] = $nom['nom_sci'];
162
			$widget['donnees']['infos_espece'] = $this->array2js($nom, true);
1536 jpm 163
		}
2406 jpm 164
 
165
		$projetsAListeDeNoms = $this->transformerEnTableau($this->config['projets']['liste_noms']);
166
		if (in_array($this->projet, $projetsAListeDeNoms)) {
167
			$projetsAListeDeNomsSciEtVerna = $this->transformerEnTableau($this->config['projets']['liste_noms_sci_et_verna']);
168
			if (in_array($this->projet, $projetsAListeDeNomsSciEtVerna)) {
1613 jpm 169
				$widget['donnees']['taxons'] = $this->recupererListeNoms();
170
			} else {
171
				$widget['donnees']['taxons'] = $this->recupererListeNomsSci();
2328 jpm 172
			}
2406 jpm 173
		}
174
 
175
		// Chargement de la liste des milieux issues du fichier .ini du projet
176
		$projetsAListeDeMilieux = $this->transformerEnTableau($this->config['projets']['liste_milieux']);
177
		if (in_array($this->projet, $projetsAListeDeMilieux)) {
1345 aurelien 178
			$widget['donnees']['milieux'] = $this->parserMilieux();
719 jpm 179
		}
2406 jpm 180
 
181
		return $widget;
719 jpm 182
	}
1526 jpm 183
 
2688 mathias 184
	protected function getTitrePage() {
2408 jpm 185
		$titre = 'defaut';
186
		if (isset($this->configProjet['titre_page'])) {
187
			$titre = $this->configProjet['titre_page'];
188
		}
189
		if (isset($this->configMission['titre_page'])) {
190
			$titre = $this->configMission['titre_page'];
191
		}
192
		if (isset($_GET['titre'])) {
193
			$titre = $_GET['titre'];
194
		}
195
		if ($titre === 0) {
196
			$titre = '';
197
		}
198
		return $titre;
1475 aurelien 199
	}
1050 jpm 200
 
2688 mathias 201
	/**
202
	 * Remplit un fichier JS avec une variable contenant une liste restreinte de taxons,
203
	 * pour certains projets
204
	 * @return string
205
	 */
2328 jpm 206
	public function executerTaxons() {
207
		$widget['squelette'] = $this->projet.'_taxons';
208
		$widget['squelette_ext'] = '.tpl.js';
1613 jpm 209
		$widget['donnees'] = array();
1629 jpm 210
		$nomsAAfficher = $this->recupererListeNomsSci();
211
		$taxons_tries = array();
1691 raphael 212
		foreach ($nomsAAfficher as $taxon) {
1629 jpm 213
			$taxons_tries[$taxon['num_nom_sel']] = $taxon;
2328 jpm 214
		}
215
		$widget['donnees']['taxons'] = json_encode($taxons_tries);
216
		return $widget;
217
	}
1613 jpm 218
 
2688 mathias 219
	/**
220
	 * Trie par nom français les taxons lus dans le fichier tsv
221
	 */
222
	protected function recupererListeNomsSci() {
1613 jpm 223
		$taxons = $this->recupererListeTaxon();
224
		if (is_array($taxons)) {
225
			$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
226
		}
1629 jpm 227
		return $taxons;
1613 jpm 228
	}
229
 
2688 mathias 230
	/**
231
	 * @TODO documenter
232
	 * @return array
233
	 */
234
	protected function recupererListeNoms() {
1613 jpm 235
		$taxons = $this->recupererListeTaxon();
236
		$nomsAAfficher = array();
237
		$nomsSpeciaux = array();
238
		if (is_array($taxons)) {
239
			foreach ($taxons as $taxon) {
240
				$nomSciTitle = $taxon['nom_ret'].
241
					($taxon['nom_fr'] != '' ? ' - '.$taxon['nom_fr'] : '' ).
242
					($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
243
				$nomFrTitle = $taxon['nom_sel'].
244
					($taxon['nom_ret'] != $taxon['nom_sel']? ' - '.$taxon['nom_ret'] : '' ).
245
					($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
246
 
247
				if ($taxon['groupe'] == 'special') {
248
					$nomsSpeciaux[] = array(
249
						'num_nom' => $taxon['num_nom_sel'],
250
						'nom_a_afficher' => $taxon['nom_fr'],
251
						'nom_a_sauver' => $taxon['nom_sel'],
252
						'nom_title' => $nomSciTitle,
253
						'nom_type' => 'nom-special');
254
				} else {
255
					$nomsAAfficher[] = array(
256
						'num_nom' => $taxon['num_nom_sel'],
257
						'nom_a_afficher' => $taxon['nom_sel'],
258
						'nom_a_sauver' => $taxon['nom_sel'],
259
						'nom_title' => $nomSciTitle,
260
						'nom_type' => 'nom-sci');
261
					$nomsAAfficher[] = array(
262
						'num_nom' => $taxon['num_nom_sel'],
263
						'nom_a_afficher' => $taxon['nom_fr'],
264
						'nom_a_sauver' => $taxon['nom_fr'],
265
						'nom_title' => $nomFrTitle,
266
						'nom_type' => 'nom-fr');
267
				}
268
			}
269
			$nomsAAfficher = self::trierTableauMd($nomsAAfficher, array('nom_a_afficher' => SORT_ASC));
270
			$nomsSpeciaux = self::trierTableauMd($nomsSpeciaux, array('nom_a_afficher' => SORT_ASC));
271
		}
272
		return array('speciaux' => $nomsSpeciaux, 'sci-et-fr' => $nomsAAfficher);
273
	}
2328 jpm 274
 
2688 mathias 275
	/**
276
	 * Lit une liste de taxons depuis un fichier tsv fourni
277
	 */
278
	protected function recupererListeTaxon() {
279
		$taxons = array();
2328 jpm 280
		$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons.tsv';
281
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
712 jpm 282
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
2328 jpm 283
		} else {
284
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
285
		}
286
		return $taxons;
712 jpm 287
	}
1050 jpm 288
 
2688 mathias 289
	/**
290
	 * Découpe un fihcier tsv
291
	 */
292
	protected function decomposerFichierTsv($fichier, $delimiter = "\t"){
2328 jpm 293
		$header = null;
294
		$data = array();
295
		if (($handle = fopen($fichier, 'r')) !== FALSE) {
296
			while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
297
				if (!$header) {
298
					$header = $row;
299
				} else {
300
					$data[] = array_combine($header, $row);
301
				}
302
			}
303
			fclose($handle);
304
		}
305
		return $data;
306
	}
307
 
2688 mathias 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
312
	 */
313
	protected function parserMilieux() {
1050 jpm 314
		$infosMilieux = array();
1536 jpm 315
		if (isset($this->configProjet['milieux'])) {
316
			$milieux = explode('|', $this->configProjet['milieux']);
317
			foreach ($milieux as $milieu) {
2328 jpm 318
				$milieu = trim($milieu);
1536 jpm 319
				$details = explode(';', $milieu);
320
				if (isset($details[1])) {
321
					$infosMilieux[$details[0]] = $details[1];
322
				} else {
323
					$infosMilieux[$details[0]] = '';
324
				}
1050 jpm 325
			}
1536 jpm 326
			ksort($infosMilieux);
1050 jpm 327
		}
328
		return $infosMilieux;
329
	}
1526 jpm 330
 
2688 mathias 331
	/**
332
	 * Retourne true si le widget est restreint à une espèce, false sinon
333
	 * @return boolean
334
	 */
335
	protected function especeEstImposee() {
336
		return ((isset($_GET['num_nom']) && $_GET['num_nom'] != '')
2408 jpm 337
			|| isset($this->configProjet['sp_imposee']) || isset($this->configMission['sp_imposee']));
1418 aurelien 338
	}
1526 jpm 339
 
2688 mathias 340
	/**
341
	 * Retourne le numéro nomenclatural (nn) de l'espèce imposée si tel est
342
	 * le cas, null sinon
343
	 * @return string
344
	 */
345
	protected function getNnEspeceImposee() {
2328 jpm 346
		$nn = null;
347
		if (isset($_GET['num_nom']) && is_numeric($_GET['num_nom'])) {
348
			$nn = $_GET['num_nom'];
349
		} else if (isset($this->configProjet['sp_imposee'])) {
350
			$nn = $this->configProjet['sp_imposee'];
2408 jpm 351
		} else if (isset($this->configMission['sp_imposee'])) {
352
			$nn = $this->configMission['sp_imposee'];
2328 jpm 353
		}
354
		return $nn;
355
	}
356
 
2688 mathias 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
363
	 */
364
	protected function chargerInfosTaxon($num_nom) {
2408 jpm 365
		$url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $this->ns_referentiel, $num_nom);
1418 aurelien 366
		$infos = json_decode(file_get_contents($url_service_infos));
2688 mathias 367
		// trop de champs injectés dans les infos espèces peuvent
1909 raphael 368
		// faire planter javascript
1916 jpm 369
		$champs_a_garder = array('id', 'nom_sci','nom_sci_complet',
2367 jpm 370
			'famille','nom_retenu.id', 'nom_retenu.libelle', 'num_taxonomique');
1419 aurelien 371
		$resultat = array();
1539 jpm 372
		if (isset($infos) && !empty($infos)) {
1419 aurelien 373
			$infos = (array)$infos;
2367 jpm 374
			if (isset($infos['nom_sci']) && $infos['nom_sci'] != '') {
1909 raphael 375
				$resultat = array_intersect_key($infos, array_flip($champs_a_garder));
376
				$resultat['retenu'] = ($infos['id'] == $infos['nom_retenu.id']) ? "true" : "false";
1916 jpm 377
			}
1419 aurelien 378
		}
1418 aurelien 379
		return $resultat;
380
	}
1050 jpm 381
 
2688 mathias 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
387
	 */
388
	protected function array2js($array,$show_keys) {
1536 jpm 389
		$tableauJs = '{}';
390
		if (!empty($array)) {
391
			$total = count($array) - 1;
392
			$i = 0;
393
			$dimensions = array();
394
			foreach ($array as $key => $value) {
395
				if (is_array($value)) {
396
					$dimensions[$i] = array2js($value,$show_keys);
397
					if ($show_keys) {
398
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
399
					}
400
				} else {
401
					$dimensions[$i] = '"'.addslashes($value).'"';
402
					if ($show_keys) {
403
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
404
					}
1526 jpm 405
				}
1536 jpm 406
				if ($i == 0) {
407
					$dimensions[$i] = '{'.$dimensions[$i];
1526 jpm 408
				}
1536 jpm 409
				if ($i == $total) {
410
					$dimensions[$i].= '}';
411
				}
412
				$i++;
1526 jpm 413
			}
1536 jpm 414
			$tableauJs = implode(',', $dimensions);
1526 jpm 415
		}
1536 jpm 416
		return $tableauJs;
1526 jpm 417
	}
2360 jpm 418
}