Subversion Repositories eFlore/Applications.cel

Rev

Rev 1946 | Rev 2328 | 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 :
12
 * ===> projet = chaine  [par défaut : Biodiversite34]
13
 * Indique quel projet nous voulons charger
14
 *
15
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
16
 * @license		GPL v3 <http://www.gnu.org/licenses/gpl.txt>
17
 * @license		CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
18
 * @version		$Id$
19
 * @copyright	Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org)
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
	private $NS_PROJET_VERSION = '1.01';
30
	const EFLORE_API_VERSION = '0.1';
31
	private $NS_PROJET = 'bdtfx';
1050 jpm 32
 
1476 aurelien 33
	private $projetsVersions = array();
719 jpm 34
	private $projet = null;
35
	private $configProjet = null;
1050 jpm 36
 
712 jpm 37
	/**
38
	 * Méthode appelée par défaut pour charger ce widget.
39
	 */
40
	public function executer() {
41
		$retour = null;
42
		extract($this->parametres);
43
 
1345 aurelien 44
		$this->projet = isset($projet) ? $projet : self::PROJET_DEFAUT;
719 jpm 45
		$this->chargerConfigProjet();
1476 aurelien 46
		$this->chargerProjetsVersion();
1050 jpm 47
 
1345 aurelien 48
		$service = isset($service) ? $service : 'widget';
719 jpm 49
		$methode = $this->traiterNomMethodeExecuter($service);
712 jpm 50
		if (method_exists($this, $methode)) {
51
			$retour = $this->$methode();
52
		} else {
53
			$this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
54
		}
55
 
1050 jpm 56
		$contenu = null;
57
		$mime = null;
58
		if (is_array($retour) && array_key_exists('squelette', $retour)) {
719 jpm 59
			$ext = (isset($retour['squelette_ext'])) ? $retour['squelette_ext'] : '.tpl.html';
1475 aurelien 60
			if($this->projetASquelette()) {
61
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet.self::DS.$retour['squelette'].$ext;
62
			} else {
63
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.'defaut'.self::DS.'defaut'.$ext;
64
			}
712 jpm 65
			$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
1050 jpm 66
			$mime = isset($retour['mime']) ? $retour['mime'] : null;
67
		} else {
68
			if (count($this->messages) == 0) {
69
				$this->messages[] = "La méthode du sous-service ne renvoie pas les données dans le bon format.";
70
			}
71
			$contenu = 'Un problème est survenu : '.print_r($this->messages, true);
712 jpm 72
		}
1050 jpm 73
 
74
		$this->envoyer($contenu, $mime);
712 jpm 75
	}
1050 jpm 76
 
719 jpm 77
	private function chargerConfigProjet() {
78
		$fichier_config = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'.ini';
79
		if (file_exists($fichier_config)) {
1526 jpm 80
			if (!$this->configProjet = parse_ini_file($fichier_config)) {
719 jpm 81
				$this->messages[] = "Le fichier ini '$fichier_config' du projet n'a pu être chargé.";
1050 jpm 82
			}
719 jpm 83
		} else {
1345 aurelien 84
			$this->debug[] = "Le fichier ini '$fichier_config' du projet n'existe pas.";
719 jpm 85
		}
86
	}
1526 jpm 87
 
1476 aurelien 88
	private function chargerProjetsVersion() {
1526 jpm 89
		if (isset($this->configProjet)) {
90
			foreach ($this->configProjet as $config => $valeur) {
91
				if(strstr($config,'.version')) {
92
					$this->projetsVersions[str_replace('.version', '', $config)] = $valeur;
93
				}
1476 aurelien 94
			}
95
		}
96
	}
1050 jpm 97
 
1345 aurelien 98
	public function executerWidget() {
1476 aurelien 99
		$referentiel_impose = false;
1526 jpm 100
		if (isset($_GET['referentiel']) && $_GET['referentiel'] != '') {
1476 aurelien 101
			$this->NS_PROJET = isset($_GET['referentiel']) && $_GET['referentiel'] != '' ? $_GET['referentiel'] : $this->NS_PROJET;
102
			$this->NS_PROJET_VERSION = $this->projetsVersions[$this->NS_PROJET];
103
			$referentiel_impose = true;
104
		}
1526 jpm 105
 
1050 jpm 106
		$widget['squelette'] = $this->projet;
107
		$widget['donnees'] = array();
108
		$widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
109
		$widget['donnees']['url_ws_saisie'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_SAISIE);
1580 jpm 110
		$widget['donnees']['url_ws_obs'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_OBS);
1888 mathias 111
		$widget['donnees']['url_ws_upload'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_UPLOAD);
1536 jpm 112
		$widget['donnees']['url_ws_annuaire'] = sprintf($this->config['chemins']['baseURLServicesAnnuaireTpl'], 'utilisateur/identite-par-courriel/');
2082 mathias 113
		$widget['donnees']['url_remarques'] = $this->config['chemins']['widgetRemarquesUrl'];
1526 jpm 114
 
1536 jpm 115
 
1516 aurelien 116
		$widget['donnees']['logo'] = isset($_GET['logo']) ? $_GET['logo'] : 'defaut';
117
		$widget['donnees']['titre'] = isset($_GET['titre']) ? $_GET['titre'] : 'defaut';
118
		$widget['donnees']['titre'] = ($widget['donnees']['titre'] == '0') ? '' : $widget['donnees']['titre'];
1526 jpm 119
 
1475 aurelien 120
		// cas du projet par défaut ou bien d'un projet n'ayant pas de squelette spécifique
1916 jpm 121
		if ($this->projet == 'defaut' || $this->projet == 'florileges' || $this->projet == 'sauvages' || !$this->projetASquelette()) {
1345 aurelien 122
			$urlWsNsTpl = $this->config['chemins']['baseURLServicesEfloreTpl'];
1476 aurelien 123
			$urlWsNs = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, $this->NS_PROJET, self::WS_NOM);
124
			$urlWsNsSansRef = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, '{referentiel}', self::WS_NOM);
1345 aurelien 125
			$widget['donnees']['url_ws_autocompletion_ns'] = $urlWsNs;
1476 aurelien 126
			$widget['donnees']['url_ws_autocompletion_ns_tpl'] = $urlWsNsSansRef;
127
			$widget['donnees']['ns_referentiel'] = $this->NS_PROJET.':'.$this->NS_PROJET_VERSION;
128
			$widget['donnees']['ns_projet'] = $this->NS_PROJET;
129
			$widget['donnees']['ns_version'] = $this->NS_PROJET_VERSION;
130
			$widget['donnees']['referentiel_impose'] = $referentiel_impose;
131
			$widget['donnees']['projets_versions'] = $this->projetsVersions;
1526 jpm 132
			$widget['donnees']['espece_imposee'] = false;
133
			$widget['donnees']['nn_espece_defaut'] = '';
134
			$widget['donnees']['nom_sci_espece_defaut'] = '';
1536 jpm 135
			$widget['donnees']['infos_espece'] = '{}';
1526 jpm 136
 
137
			if ($this->especeEstImposee()) {
138
				$nom = $this->executerChargementInfosTaxon($_GET['num_nom']);
1418 aurelien 139
				$widget['donnees']['espece_imposee'] = true;
140
				$widget['donnees']['nn_espece_defaut'] = $_GET['num_nom'];
1419 aurelien 141
				$widget['donnees']['nom_sci_espece_defaut'] = $nom['nom_sci'];
142
				$widget['donnees']['infos_espece'] = $this->array2js($nom, true);
1418 aurelien 143
			}
1536 jpm 144
		}
145
		if ($this->projet != 'defaut' || $this->projetASquelette()) {
1946 jpm 146
			if ($this->projet == 'florileges' || $this->projet == 'sauvages') {
1613 jpm 147
				$widget['donnees']['taxons'] = $this->recupererListeNoms();
148
			} else {
149
				$widget['donnees']['taxons'] = $this->recupererListeNomsSci();
150
			}
1345 aurelien 151
			$widget['donnees']['milieux'] = $this->parserMilieux();
719 jpm 152
		}
1345 aurelien 153
		return  $widget;
719 jpm 154
	}
1526 jpm 155
 
1475 aurelien 156
	private function projetASquelette() {
1526 jpm 157
		// fonction très simple qui ne teste que si le dossier du projet courant
1475 aurelien 158
		// existe, mais elle suffit pour le moment.
159
		return file_exists(dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet);
160
	}
1050 jpm 161
 
1345 aurelien 162
	public function executerTaxons() {
163
		$widget['squelette'] = $this->projet.'_taxons';
164
		$widget['squelette_ext'] = '.tpl.js';
1613 jpm 165
		$widget['donnees'] = array();
1629 jpm 166
		$nomsAAfficher = $this->recupererListeNomsSci();
167
		$taxons_tries = array();
1691 raphael 168
		foreach ($nomsAAfficher as $taxon) {
1629 jpm 169
			$taxons_tries[$taxon['num_nom_sel']] = $taxon;
170
		}
171
		$widget['donnees']['taxons'] = json_encode($taxons_tries);
172
		return $widget;
1345 aurelien 173
	}
1613 jpm 174
 
175
	private function recupererListeNomsSci() {
176
		$taxons = $this->recupererListeTaxon();
177
		if (is_array($taxons)) {
178
			$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
179
		}
1629 jpm 180
		return $taxons;
1613 jpm 181
	}
182
 
183
	private function recupererListeNoms() {
184
		$taxons = $this->recupererListeTaxon();
185
		$nomsAAfficher = array();
186
		$nomsSpeciaux = array();
187
		if (is_array($taxons)) {
188
			foreach ($taxons as $taxon) {
189
				$nomSciTitle = $taxon['nom_ret'].
190
					($taxon['nom_fr'] != '' ? ' - '.$taxon['nom_fr'] : '' ).
191
					($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
192
				$nomFrTitle = $taxon['nom_sel'].
193
					($taxon['nom_ret'] != $taxon['nom_sel']? ' - '.$taxon['nom_ret'] : '' ).
194
					($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
195
 
196
				if ($taxon['groupe'] == 'special') {
197
					$nomsSpeciaux[] = array(
198
						'num_nom' => $taxon['num_nom_sel'],
199
						'nom_a_afficher' => $taxon['nom_fr'],
200
						'nom_a_sauver' => $taxon['nom_sel'],
201
						'nom_title' => $nomSciTitle,
202
						'nom_type' => 'nom-special');
203
				} else {
204
					$nomsAAfficher[] = array(
205
						'num_nom' => $taxon['num_nom_sel'],
206
						'nom_a_afficher' => $taxon['nom_sel'],
207
						'nom_a_sauver' => $taxon['nom_sel'],
208
						'nom_title' => $nomSciTitle,
209
						'nom_type' => 'nom-sci');
210
					$nomsAAfficher[] = array(
211
						'num_nom' => $taxon['num_nom_sel'],
212
						'nom_a_afficher' => $taxon['nom_fr'],
213
						'nom_a_sauver' => $taxon['nom_fr'],
214
						'nom_title' => $nomFrTitle,
215
						'nom_type' => 'nom-fr');
216
				}
217
			}
218
			$nomsAAfficher = self::trierTableauMd($nomsAAfficher, array('nom_a_afficher' => SORT_ASC));
219
			$nomsSpeciaux = self::trierTableauMd($nomsSpeciaux, array('nom_a_afficher' => SORT_ASC));
220
		}
221
		return array('speciaux' => $nomsSpeciaux, 'sci-et-fr' => $nomsAAfficher);
222
	}
1345 aurelien 223
 
224
	private function recupererListeTaxon() {
225
		$taxons = null;
226
		$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons.tsv';
227
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
712 jpm 228
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
1345 aurelien 229
		} else {
230
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
231
		}
232
		return $taxons;
233
	}
234
 
235
	private function decomposerFichierTsv($fichier, $delimiter = "\t"){
1536 jpm 236
		$header = null;
1345 aurelien 237
		$data = array();
238
		if (($handle = fopen($fichier, 'r')) !== FALSE) {
239
			while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
240
				if (!$header) {
241
					$header = $row;
242
				} else {
243
					$data[] = array_combine($header, $row);
244
				}
245
			}
246
			fclose($handle);
247
		}
248
		return $data;
712 jpm 249
	}
1050 jpm 250
 
1345 aurelien 251
	private function parserMilieux() {
1050 jpm 252
		$infosMilieux = array();
1536 jpm 253
		if (isset($this->configProjet['milieux'])) {
254
			$milieux = explode('|', $this->configProjet['milieux']);
255
			foreach ($milieux as $milieu) {
256
				$details = explode(';', $milieu);
257
				if (isset($details[1])) {
258
					$infosMilieux[$details[0]] = $details[1];
259
				} else {
260
					$infosMilieux[$details[0]] = '';
261
				}
1050 jpm 262
			}
1536 jpm 263
			ksort($infosMilieux);
1050 jpm 264
		}
265
		return $infosMilieux;
266
	}
1526 jpm 267
 
1418 aurelien 268
	private function especeEstImposee() {
1536 jpm 269
		return (isset($_GET['num_nom']) && $_GET['num_nom'] != '');
1418 aurelien 270
	}
1526 jpm 271
 
1418 aurelien 272
	private function executerChargementInfosTaxon($num_nom) {
1476 aurelien 273
		$url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $this->NS_PROJET, $num_nom);
1418 aurelien 274
		$infos = json_decode(file_get_contents($url_service_infos));
1909 raphael 275
		// trop de champs injectés dans les infos espèces peut
276
		// faire planter javascript
1916 jpm 277
		$champs_a_garder = array('id', 'nom_sci','nom_sci_complet',
1909 raphael 278
		'famille','nom_retenu.id', 'nom_retenu.libelle', 'num_taxonomique');
1419 aurelien 279
		$resultat = array();
1539 jpm 280
		if (isset($infos) && !empty($infos)) {
1419 aurelien 281
			$infos = (array)$infos;
1909 raphael 282
			if(isset($infos['nom_sci']) && $infos['nom_sci'] != '') {
283
				$resultat = array_intersect_key($infos, array_flip($champs_a_garder));
284
				$resultat['retenu'] = ($infos['id'] == $infos['nom_retenu.id']) ? "true" : "false";
1916 jpm 285
			}
1419 aurelien 286
		}
1418 aurelien 287
		return $resultat;
288
	}
1050 jpm 289
 
1526 jpm 290
	private function array2js($array,$show_keys) {
1536 jpm 291
		$tableauJs = '{}';
292
		if (!empty($array)) {
293
			$total = count($array) - 1;
294
			$i = 0;
295
			$dimensions = array();
296
			foreach ($array as $key => $value) {
297
				if (is_array($value)) {
298
					$dimensions[$i] = array2js($value,$show_keys);
299
					if ($show_keys) {
300
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
301
					}
302
				} else {
303
					$dimensions[$i] = '"'.addslashes($value).'"';
304
					if ($show_keys) {
305
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
306
					}
1526 jpm 307
				}
1536 jpm 308
				if ($i == 0) {
309
					$dimensions[$i] = '{'.$dimensions[$i];
1526 jpm 310
				}
1536 jpm 311
				if ($i == $total) {
312
					$dimensions[$i].= '}';
313
				}
314
				$i++;
1526 jpm 315
			}
1536 jpm 316
			$tableauJs = implode(',', $dimensions);
1526 jpm 317
		}
1536 jpm 318
		return $tableauJs;
1526 jpm 319
	}
1345 aurelien 320
}
321
?>