Subversion Repositories eFlore/Applications.cel

Rev

Rev 2498 | Rev 2577 | 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
// declare(encoding='UTF-8');
3
/**
4
 * Widget fournissant des interfaces de saisies simplifiée pour différent projets.
5
 * Encodage en entrée : utf8
6
 * Encodage en sortie : utf8
7
 *
8
 * Cas d'utilisation et documentation :
9
 * @link http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=AideCELWidgetSaisie
10
 *
11
 * Paramètres :
2408 jpm 12
 * ===> projet = chaine  [par défaut : defaut] : indique le widgetde saisie à charger.
13
 * ===> mission = chaine  [par défaut : vide] : permet de charger un "sous-widget" vis à vis du projet.
712 jpm 14
 * Indique quel projet nous voulons charger
15
 *
2408 jpm 16
 * @author Mathias CHOUET <mathias@tela-botanica.org>
17
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
18
 * @author Aurelien PERONNET <aurelien@tela-botanica.org>
19
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
20
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
21
 * @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
712 jpm 22
 */
23
class Saisie extends WidgetCommun {
1050 jpm 24
 
712 jpm 25
	const DS = DIRECTORY_SEPARATOR;
1345 aurelien 26
	const PROJET_DEFAUT = 'defaut';
1580 jpm 27
	const WS_SAISIE = 'CelWidgetSaisie';
1888 mathias 28
	const WS_UPLOAD = 'CelWidgetUploadImageTemp';
1580 jpm 29
	const WS_OBS = 'CelObs';
30
	const WS_NOM = 'noms';
31
	const EFLORE_API_VERSION = '0.1';
1050 jpm 32
 
2408 jpm 33
	private $ns_referentiel = 'bdtfx';
719 jpm 34
	private $projet = null;
35
	private $configProjet = null;
2408 jpm 36
	private $configMission = null;
1050 jpm 37
 
712 jpm 38
	/**
39
	 * Méthode appelée par défaut pour charger ce widget.
40
	 */
41
	public function executer() {
42
		$retour = null;
43
		extract($this->parametres);
44
 
2500 mathias 45
		$this->projet = self::PROJET_DEFAUT;
46
		if (isset($projet) && trim($projet) != "") {
47
			$projets = explode(',', $projet);
48
			$this->projet = $projets[0];
49
		}
719 jpm 50
		$this->chargerConfigProjet();
1050 jpm 51
 
1345 aurelien 52
		$service = isset($service) ? $service : 'widget';
719 jpm 53
		$methode = $this->traiterNomMethodeExecuter($service);
712 jpm 54
		if (method_exists($this, $methode)) {
55
			$retour = $this->$methode();
56
		} else {
57
			$this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
58
		}
59
 
1050 jpm 60
		$contenu = null;
61
		$mime = null;
62
		if (is_array($retour) && array_key_exists('squelette', $retour)) {
719 jpm 63
			$ext = (isset($retour['squelette_ext'])) ? $retour['squelette_ext'] : '.tpl.html';
2406 jpm 64
			if ($this->projetASquelette()) {
1475 aurelien 65
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet.self::DS.$retour['squelette'].$ext;
66
			} else {
67
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.'defaut'.self::DS.'defaut'.$ext;
68
			}
712 jpm 69
			$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
1050 jpm 70
			$mime = isset($retour['mime']) ? $retour['mime'] : null;
71
		} else {
72
			if (count($this->messages) == 0) {
73
				$this->messages[] = "La méthode du sous-service ne renvoie pas les données dans le bon format.";
74
			}
75
			$contenu = 'Un problème est survenu : '.print_r($this->messages, true);
712 jpm 76
		}
1050 jpm 77
 
78
		$this->envoyer($contenu, $mime);
712 jpm 79
	}
1050 jpm 80
 
719 jpm 81
	private function chargerConfigProjet() {
82
		$fichier_config = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'.ini';
83
		if (file_exists($fichier_config)) {
2408 jpm 84
			if ($this->configProjet = parse_ini_file($fichier_config, true)) {
85
				if (isset($_GET['mission'])) {
86
					$mission = strtolower(trim($_GET['mission']));
87
					if (isset($this->configProjet[$mission])) {
88
						$this->configMission = $this->configProjet[$mission];
89
					}
90
			}
91
			} else {
719 jpm 92
				$this->messages[] = "Le fichier ini '$fichier_config' du projet n'a pu être chargé.";
1050 jpm 93
			}
719 jpm 94
		} else {
1345 aurelien 95
			$this->debug[] = "Le fichier ini '$fichier_config' du projet n'existe pas.";
719 jpm 96
		}
97
	}
1526 jpm 98
 
2408 jpm 99
	private function projetASquelette() {
100
		// fonction très simple qui ne teste que si le dossier du projet courant
101
		// existe, mais elle suffit pour le moment.
102
		return file_exists(dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet);
1476 aurelien 103
	}
1050 jpm 104
 
1345 aurelien 105
	public function executerWidget() {
1476 aurelien 106
		$referentiel_impose = false;
2343 aurelien 107
		if (isset($_GET['referentiel']) && $_GET['referentiel'] != '' && $_GET['referentiel'] != "autre") {
2408 jpm 108
			$this->ns_referentiel = isset($_GET['referentiel']) && $_GET['referentiel'] != '' ? $_GET['referentiel'] : $this->ns_referentiel;
1476 aurelien 109
			$referentiel_impose = true;
110
		}
1526 jpm 111
 
1050 jpm 112
		$widget['squelette'] = $this->projet;
113
		$widget['donnees'] = array();
114
		$widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
115
		$widget['donnees']['url_ws_saisie'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_SAISIE);
1580 jpm 116
		$widget['donnees']['url_ws_obs'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_OBS);
1888 mathias 117
		$widget['donnees']['url_ws_upload'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_UPLOAD);
1536 jpm 118
		$widget['donnees']['url_ws_annuaire'] = sprintf($this->config['chemins']['baseURLServicesAnnuaireTpl'], 'utilisateur/identite-par-courriel/');
2082 mathias 119
		$widget['donnees']['url_remarques'] = $this->config['chemins']['widgetRemarquesUrl'];
1526 jpm 120
 
1516 aurelien 121
		$widget['donnees']['logo'] = isset($_GET['logo']) ? $_GET['logo'] : 'defaut';
2408 jpm 122
		$widget['donnees']['titre'] = $this->getTitrePage();
1526 jpm 123
 
2498 aurelien 124
		$widget['donnees']['referentiel_impose'] = $referentiel_impose;
125
		$widget['donnees']['espece_imposee'] = false;
126
		$widget['donnees']['nn_espece_defaut'] = '';
127
		$widget['donnees']['nom_sci_espece_defaut'] = '';
128
		$widget['donnees']['infos_espece'] = '{}';
129
 
2406 jpm 130
		$projetsAutorises = $this->transformerEnTableau($this->config['projets']['autorises']);
1526 jpm 131
 
2498 aurelien 132
		$urlWsNsTpl = $this->config['chemins']['baseURLServicesEfloreTpl'];
133
		$urlWsNs = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, $this->ns_referentiel, self::WS_NOM);
134
		$urlWsNsSansRef = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, '{referentiel}', self::WS_NOM);
135
		$widget['donnees']['url_ws_autocompletion_ns'] = $urlWsNs;
136
		$widget['donnees']['url_ws_autocompletion_ns_tpl'] = $urlWsNsSansRef;
137
		$widget['donnees']['ns_referentiel'] = $this->ns_referentiel;
138
 
139
		if ($this->especeEstImposee()) {
140
			$nnEspeceImposee = $this->getNnEspeceImposee();
141
			$nom = $this->executerChargementInfosTaxon($nnEspeceImposee);
142
			$widget['donnees']['espece_imposee'] = true;
143
			$widget['donnees']['nn_espece_defaut'] = $nnEspeceImposee;
144
			$widget['donnees']['nom_sci_espece_defaut'] = $nom['nom_sci'];
145
			$widget['donnees']['infos_espece'] = $this->array2js($nom, true);
1536 jpm 146
		}
2406 jpm 147
 
148
		$projetsAListeDeNoms = $this->transformerEnTableau($this->config['projets']['liste_noms']);
149
		if (in_array($this->projet, $projetsAListeDeNoms)) {
150
			$projetsAListeDeNomsSciEtVerna = $this->transformerEnTableau($this->config['projets']['liste_noms_sci_et_verna']);
151
			if (in_array($this->projet, $projetsAListeDeNomsSciEtVerna)) {
1613 jpm 152
				$widget['donnees']['taxons'] = $this->recupererListeNoms();
153
			} else {
154
				$widget['donnees']['taxons'] = $this->recupererListeNomsSci();
2328 jpm 155
			}
2406 jpm 156
		}
157
 
158
		// Chargement de la liste des milieux issues du fichier .ini du projet
159
		$projetsAListeDeMilieux = $this->transformerEnTableau($this->config['projets']['liste_milieux']);
160
		if (in_array($this->projet, $projetsAListeDeMilieux)) {
1345 aurelien 161
			$widget['donnees']['milieux'] = $this->parserMilieux();
719 jpm 162
		}
2406 jpm 163
 
164
		return $widget;
719 jpm 165
	}
1526 jpm 166
 
2408 jpm 167
	private function getTitrePage() {
168
		$titre = 'defaut';
169
		if (isset($this->configProjet['titre_page'])) {
170
			$titre = $this->configProjet['titre_page'];
171
		}
172
		if (isset($this->configMission['titre_page'])) {
173
			$titre = $this->configMission['titre_page'];
174
		}
175
		if (isset($_GET['titre'])) {
176
			$titre = $_GET['titre'];
177
		}
178
		if ($titre === 0) {
179
			$titre = '';
180
		}
181
		return $titre;
1475 aurelien 182
	}
1050 jpm 183
 
2328 jpm 184
	public function executerTaxons() {
185
		$widget['squelette'] = $this->projet.'_taxons';
186
		$widget['squelette_ext'] = '.tpl.js';
1613 jpm 187
		$widget['donnees'] = array();
1629 jpm 188
		$nomsAAfficher = $this->recupererListeNomsSci();
189
		$taxons_tries = array();
1691 raphael 190
		foreach ($nomsAAfficher as $taxon) {
1629 jpm 191
			$taxons_tries[$taxon['num_nom_sel']] = $taxon;
2328 jpm 192
		}
193
		$widget['donnees']['taxons'] = json_encode($taxons_tries);
194
		return $widget;
195
	}
1613 jpm 196
 
197
	private function recupererListeNomsSci() {
198
		$taxons = $this->recupererListeTaxon();
199
		if (is_array($taxons)) {
200
			$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
201
		}
1629 jpm 202
		return $taxons;
1613 jpm 203
	}
204
 
205
	private function recupererListeNoms() {
206
		$taxons = $this->recupererListeTaxon();
207
		$nomsAAfficher = array();
208
		$nomsSpeciaux = array();
209
		if (is_array($taxons)) {
210
			foreach ($taxons as $taxon) {
211
				$nomSciTitle = $taxon['nom_ret'].
212
					($taxon['nom_fr'] != '' ? ' - '.$taxon['nom_fr'] : '' ).
213
					($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
214
				$nomFrTitle = $taxon['nom_sel'].
215
					($taxon['nom_ret'] != $taxon['nom_sel']? ' - '.$taxon['nom_ret'] : '' ).
216
					($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
217
 
218
				if ($taxon['groupe'] == 'special') {
219
					$nomsSpeciaux[] = array(
220
						'num_nom' => $taxon['num_nom_sel'],
221
						'nom_a_afficher' => $taxon['nom_fr'],
222
						'nom_a_sauver' => $taxon['nom_sel'],
223
						'nom_title' => $nomSciTitle,
224
						'nom_type' => 'nom-special');
225
				} else {
226
					$nomsAAfficher[] = array(
227
						'num_nom' => $taxon['num_nom_sel'],
228
						'nom_a_afficher' => $taxon['nom_sel'],
229
						'nom_a_sauver' => $taxon['nom_sel'],
230
						'nom_title' => $nomSciTitle,
231
						'nom_type' => 'nom-sci');
232
					$nomsAAfficher[] = array(
233
						'num_nom' => $taxon['num_nom_sel'],
234
						'nom_a_afficher' => $taxon['nom_fr'],
235
						'nom_a_sauver' => $taxon['nom_fr'],
236
						'nom_title' => $nomFrTitle,
237
						'nom_type' => 'nom-fr');
238
				}
239
			}
240
			$nomsAAfficher = self::trierTableauMd($nomsAAfficher, array('nom_a_afficher' => SORT_ASC));
241
			$nomsSpeciaux = self::trierTableauMd($nomsSpeciaux, array('nom_a_afficher' => SORT_ASC));
242
		}
243
		return array('speciaux' => $nomsSpeciaux, 'sci-et-fr' => $nomsAAfficher);
244
	}
2328 jpm 245
 
246
	private function recupererListeTaxon() {
247
		$taxons = null;
248
		$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons.tsv';
249
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
712 jpm 250
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
2328 jpm 251
		} else {
252
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
253
		}
254
		return $taxons;
712 jpm 255
	}
1050 jpm 256
 
2328 jpm 257
	private function decomposerFichierTsv($fichier, $delimiter = "\t"){
258
		$header = null;
259
		$data = array();
260
		if (($handle = fopen($fichier, 'r')) !== FALSE) {
261
			while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
262
				if (!$header) {
263
					$header = $row;
264
				} else {
265
					$data[] = array_combine($header, $row);
266
				}
267
			}
268
			fclose($handle);
269
		}
270
		return $data;
271
	}
272
 
1345 aurelien 273
	private function parserMilieux() {
1050 jpm 274
		$infosMilieux = array();
1536 jpm 275
		if (isset($this->configProjet['milieux'])) {
276
			$milieux = explode('|', $this->configProjet['milieux']);
277
			foreach ($milieux as $milieu) {
2328 jpm 278
				$milieu = trim($milieu);
1536 jpm 279
				$details = explode(';', $milieu);
280
				if (isset($details[1])) {
281
					$infosMilieux[$details[0]] = $details[1];
282
				} else {
283
					$infosMilieux[$details[0]] = '';
284
				}
1050 jpm 285
			}
1536 jpm 286
			ksort($infosMilieux);
1050 jpm 287
		}
288
		return $infosMilieux;
289
	}
1526 jpm 290
 
1418 aurelien 291
	private function especeEstImposee() {
2328 jpm 292
		return (isset($_GET['num_nom']) && $_GET['num_nom'] != ''
2408 jpm 293
			|| isset($this->configProjet['sp_imposee']) || isset($this->configMission['sp_imposee']));
1418 aurelien 294
	}
1526 jpm 295
 
2328 jpm 296
	private function getNnEspeceImposee() {
297
		$nn = null;
298
		if (isset($_GET['num_nom']) && is_numeric($_GET['num_nom'])) {
299
			$nn = $_GET['num_nom'];
300
		} else if (isset($this->configProjet['sp_imposee'])) {
301
			$nn = $this->configProjet['sp_imposee'];
2408 jpm 302
		} else if (isset($this->configMission['sp_imposee'])) {
303
			$nn = $this->configMission['sp_imposee'];
2328 jpm 304
		}
305
		return $nn;
306
	}
307
 
1418 aurelien 308
	private function executerChargementInfosTaxon($num_nom) {
2408 jpm 309
		$url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $this->ns_referentiel, $num_nom);
1418 aurelien 310
		$infos = json_decode(file_get_contents($url_service_infos));
1909 raphael 311
		// trop de champs injectés dans les infos espèces peut
312
		// faire planter javascript
1916 jpm 313
		$champs_a_garder = array('id', 'nom_sci','nom_sci_complet',
2367 jpm 314
			'famille','nom_retenu.id', 'nom_retenu.libelle', 'num_taxonomique');
1419 aurelien 315
		$resultat = array();
1539 jpm 316
		if (isset($infos) && !empty($infos)) {
1419 aurelien 317
			$infos = (array)$infos;
2367 jpm 318
			if (isset($infos['nom_sci']) && $infos['nom_sci'] != '') {
1909 raphael 319
				$resultat = array_intersect_key($infos, array_flip($champs_a_garder));
320
				$resultat['retenu'] = ($infos['id'] == $infos['nom_retenu.id']) ? "true" : "false";
1916 jpm 321
			}
1419 aurelien 322
		}
1418 aurelien 323
		return $resultat;
324
	}
1050 jpm 325
 
1526 jpm 326
	private function array2js($array,$show_keys) {
1536 jpm 327
		$tableauJs = '{}';
328
		if (!empty($array)) {
329
			$total = count($array) - 1;
330
			$i = 0;
331
			$dimensions = array();
332
			foreach ($array as $key => $value) {
333
				if (is_array($value)) {
334
					$dimensions[$i] = array2js($value,$show_keys);
335
					if ($show_keys) {
336
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
337
					}
338
				} else {
339
					$dimensions[$i] = '"'.addslashes($value).'"';
340
					if ($show_keys) {
341
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
342
					}
1526 jpm 343
				}
1536 jpm 344
				if ($i == 0) {
345
					$dimensions[$i] = '{'.$dimensions[$i];
1526 jpm 346
				}
1536 jpm 347
				if ($i == $total) {
348
					$dimensions[$i].= '}';
349
				}
350
				$i++;
1526 jpm 351
			}
1536 jpm 352
			$tableauJs = implode(',', $dimensions);
1526 jpm 353
		}
1536 jpm 354
		return $tableauJs;
1526 jpm 355
	}
2360 jpm 356
}