Subversion Repositories eFlore/Applications.cel

Rev

Rev 3163 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3120 delphine 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
 * Service affichant les dernières photo publiques du CEL ouvrable sous forme de diaporama.
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=AideCELWidgetPhoto
10
 *
11
 * Paramètres :
12
 * ===> extra = booléen (1 ou 0)  [par défaut : 1]
13
 * Affiche / Cache la vignette en taille plus importante au bas du widget.
14
 * ===> vignette = [0-9]+,[0-9]+  [par défaut : 4,3]
15
 * Indique le nombre de vignette par ligne et le nombre de ligne.
16
 *
17
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
18
 * @license	GPL v3 <http://www.gnu.org/licenses/gpl.txt>
19
 * @license	CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
20
 * @version	$Id$
21
 * @copyright	Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org)
22
 */
23
class Saisie2 extends WidgetCommun {
24
 
25
	const DS = DIRECTORY_SEPARATOR;
26
	const SERVICE_DEFAUT = 'saisie';
27
	const LANGUE_DEFAUT = 'fr';
28
	const PROJET_DEFAUT = 'base';
29
	const WS_NOM = 'noms';
30
	const EFLORE_API_VERSION = '0.1';
31
	private $cel_url_tpl = null;
32
	/** Si spécifié, on ajoute une barre de navigation inter-applications */
33
	private $bar;
34
	//private $parametres_autorises = array('projet', 'type', 'langue', 'order');
35
	private $parametres_autorises = array(
36
			'projet' => 'projet',
37
			'type' => 'type',
38
			'langue' => 'langue',
39
			'order' => 'order'
40
	);
41
	/**
42
	 * Méthode appelée par défaut pour charger ce widget.
43
	 */
44
	public function executer() {
45
		$retour = null;
46
		// Pour la création de l'id du cache nous ne tenons pas compte du paramètre de l'url callback
47
		unset($this->parametres['callback']);
48
		extract($this->parametres);
49
		$this->bar = (isset($bar)) ? $bar : false;
50
		/* Le fichier Framework.php du Framework de Tela Botanica doit être appelé avant tout autre chose dans l'application.
51
		 Sinon, rien ne sera chargé.
52
		 L'emplacement du Framework peut varier en fonction de l'environnement (test, prod...). Afin de faciliter la configuration
53
		 de l'emplacement du Framework, un fichier framework.defaut.php doit être renommé en framework.php et configuré pour chaque installation de
54
		 l'application.
55
		 Chemin du fichier chargeant le framework requis */
56
		$framework = dirname(__FILE__).'/framework.php';
57
		if (!file_exists($framework)) {
58
			$e = "Veuillez paramêtrer l'emplacement et la version du Framework dans le fichier $framework";
59
			trigger_error($e, E_USER_ERROR);
60
		} else {
61
			// Inclusion du Framework
62
			require_once $framework;
63
			// Ajout d'information concernant cette application
64
			Framework::setCheminAppli(__FILE__);// Obligatoire
65
			Framework::setInfoAppli(Config::get('info'));// Optionnel
66
 
67
		}
68
		$langue = (isset($langue)) ? $langue : self::LANGUE_DEFAUT;
69
		$this->langue = I18n::setLangue($langue);
70
		$this->parametres['langue'] = $langue;
71
 
72
		if (!isset($mode)) {
73
			$mode = self::SERVICE_DEFAUT;
74
		}
75
		if (!isset($projet)) {
76
			$this->parametres['projet'] = self::PROJET_DEFAUT;
77
		}
78
 
79
		$methode = $this->traiterNomMethodeExecuter($mode);
80
		if (method_exists($this, $methode)) {
81
			$retour = $this->$methode();
82
		} else {
83
			$this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
84
		}
85
 
86
		$contenu = '';
87
		if (is_null($retour)) {
88
			$this->messages[] = 'La ressource demandée a retourné une valeur nulle.';
89
		} else {
90
			if (isset($retour['donnees'])) {
91
				$retour['donnees']['prod'] = ($this->config['parametres']['modeServeur'] == "prod");
92
				$retour['donnees']['bar'] = $this->bar;
93
				$retour['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
94
				$retour['donnees']['url_ws_annuaire'] = sprintf($this->config['chemins']['baseURLServicesAnnuaireTpl'], 'utilisateur/identite-par-courriel/');
95
				$retour['donnees']['mode'] = $mode;
96
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$retour['squelette'].'.tpl.html';
97
				$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
98
			} else {
99
				$this->messages[] = 'Les données à transmettre au squelette sont nulles.';
100
			}
101
		}
102
		$this->envoyer($contenu);
103
	}
104
 
105
 
106
	private function executerSaisie() {
107
		$retour = '';
108
		$retour['squelette'] = 'saisie';
109
		$retour['donnees']['general'] = I18n::get('General');
110
		$retour['donnees']['aide'] = I18n::get('Aide');
111
		$retour['donnees']['observateur'] = I18n::get('Observateur');
112
		$retour['donnees']['observation'] = I18n::get('Observation');
113
		$retour['donnees']['widget'] = $this->rechercherProjet();
114
		return $retour;
115
	}
116
 
117
	/* Recherche si le projet existe sinon va chercher les infos de base */
118
	private function rechercherProjet() {
119
		// projet avec un squelette défini (et non juste un mot-clé d'observation)
120
		$estProjetDefini = true; $tab = "";
121
		$url = $this->config['manager']['celUrlTpl'].'?projet='.$this->parametres['projet'].'&langue='.$this->parametres['langue'];
122
		$json = $this->getDao()->consulter($url);
123
		if ($json == false) {
124
			$url = $this->config['manager']['celUrlTpl'].'?projet=base&langue='.$this->parametres['langue'];
125
			$json = $this->getDao()->consulter($url);
126
			$estProjetDefini = false;
127
		} else {
128
			$tab = $this->rechercherChampsSupp();
129
		}
130
		$tableau = json_decode($json, true);
131
		$tableau = $this->traiterParametres($estProjetDefini, $tableau[0]);
132
		$tableau['especes'] = $this->rechercherInfosEspeces($tableau);
133
		if ($tableau['milieux'] != "") {
134
			$tableau['milieux']= explode(";", $tableau['milieux']);
135
		} else {
136
			$tableau['milieux'] = array();
137
		}
138
		$tableau['chpSupp'] = $tab;
139
		return $tableau;
140
	}
141
 
142
	/* Recherche si un projet a des champs de saisie supplémentaire */
143
	private function rechercherChampsSupp() {
144
		$retour = "";
145
		$url = $this->config['manager']['celChpSupTpl'].'groupes?groupe='.$this->parametres['projet'].'&langue='.$this->parametres['langue'];
146
		$json = $this->getDao()->consulter($url);
147
		$tableau = (array) json_decode($json, true);
148
		if ($tableau != false) {
149
			foreach ($tableau[0]['champs'] as $champ) {
150
				switch ($champ['type']) {
151
					case "select" :
152
						if ($champ['valeur'] != "") {
153
							$valeurs = split(";", $champ['valeur']);
154
							$html = '<label for="'.$champ['cle'].'" class="col-sm-3" >'.$champ['label'].'</label>'.
155
								'<div class="col-sm-8 input-group" >'.
156
								'<select class="form-control obs-chp-etendu" id="'.$champ['cle'].'"></div>';
157
							foreach ( $valeurs as $valeur) {
158
								$html .= '<option>'.$valeur.'</option>';
159
							}
160
							$html .= "</select>";
161
							$retour[] = $html;
162
						}
163
						break;
164
					default :
165
						//$this->verifierChampsCachés();
166
						break;
167
				}
168
			}
169
		}
170
		return $retour;
171
	}
172
 
173
	// remplace certains parametres définis en bd par les parametres définis dans l'url
174
	private function traiterParametres($estProjetDefini, $tableau) {
175
		$criteres = array('tag', 'motcle', 'projet', 'titre', 'logo');
176
		$criteresProjetNonDefini = array('commune', 'num_nom', 'referentiel');
177
		foreach($this->parametres as $nom_critere => $valeur_critere) {
178
			if (($estProjetDefini == false || $tableau['projet'] == "base") && in_array($nom_critere, $criteresProjetNonDefini)) {
179
				$tableau[$nom_critere] = $valeur_critere;
180
			} else if (in_array($nom_critere, $criteres)) {
181
				$tableau[$nom_critere] = $valeur_critere;
182
			}
183
		}
184
		return $tableau;
185
	}
186
 
187
 
188
	private function rechercherInfosEspeces($infos_projets) { //print_r($infos_projets);exit;
189
		$retour = ""; $referentiel = $infos_projets['referentiel'];
190
		$urlWsNsTpl = $this->config['chemins']['baseURLServicesEfloreTpl'];
191
		$urlWsNs = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, $referentiel, self::WS_NOM);
192
		$urlWsNsSansRef = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, '{referentiel}', self::WS_NOM);
193
		$retour['url_ws_autocompletion_ns'] = $urlWsNs;
194
		$retour['url_ws_autocompletion_ns_tpl'] = $urlWsNsSansRef;
195
		$retour['ns_referentiel'] = $referentiel;
196
 
197
		if (isset($infos_projets['type_especes'])) {
198
			switch ($infos_projets['type_especes']) {
199
				case "referentiel" : $referentiel = $infos_projets['referentiel']; break;
200
				case "liste" : $referentiel = $infos_projets['referentiel']; break;
201
				case "fixe" :
202
					$retour['especes'] = $this->chargerInfosTaxon($infos_projets['referentiel'], $infos_projets['especes']);
203
					break;
204
			}
205
		} else if (isset($infos_projets['referentiel'])) {
206
			$referentiel = $infos_projets['referentiel'];
207
			if (isset($infos_projets['num_nom'])) {
208
				$retour['especes'] = $this->chargerInfosTaxon($infos_projets['referentiel'], $infos_projets['num_nom']);
209
			}
210
		}
211
 
212
		$projetsAListeDeNoms = $this->transformerEnTableau($this->config['projets']['liste_noms']);
213
		if (in_array($this->projet, $projetsAListeDeNoms) && !$this->especeEstImposee()) {
214
			$projetsAListeDeNomsSciEtVerna = $this->transformerEnTableau($this->config['projets']['liste_noms_sci_et_verna']);
215
			if (in_array($this->projet, $projetsAListeDeNomsSciEtVerna)) {
216
				$retour['taxons'] = $this->recupererListeNoms();
217
			} else {
218
				$retour['taxons'] = $this->recupererListeNomsSci();
219
			}
220
		}
221
		return $retour;
222
	}
223
 
224
	/**
225
	 * Consulte un webservice pour obtenir des informations sur le taxon dont le
226
	 * numéro nomenclatural est $num_nom (ce sont donc plutôt des infos sur le nom
227
	 * et non le taxon?)
228
	 * @param string|int $num_nom
229
	 * @return array
230
	 */
231
	protected function chargerInfosTaxon($referentiel, $num_nom) {
232
		$url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $referentiel, $num_nom);
233
		$infos = json_decode(file_get_contents($url_service_infos));
234
		// trop de champs injectés dans les infos espèces peuvent
235
		// faire planter javascript
236
		$champs_a_garder = array('id', 'nom_sci','nom_sci_complet', 'nom_complet',
237
				'famille','nom_retenu.id', 'nom_retenu_complet', 'num_taxonomique');
238
		$resultat = array();
239
		$retour = array();
240
		if (isset($infos) && !empty($infos)) {
241
			$infos = (array)$infos;
242
			if (isset($infos['nom_sci']) && $infos['nom_sci'] != '') {
243
				$resultat = array_intersect_key($infos, array_flip($champs_a_garder));
244
				$resultat['retenu'] = ($infos['id'] == $infos['nom_retenu.id']) ? "true" : "false";
245
				$retour['espece_imposee'] = true;
246
				$retour['nn_espece_defaut'] = $nnEspeceImposee;
247
				$retour['nom_sci_espece_defaut'] = $resultat['nom_complet'];
248
				$retour['infos_espece'] = $this->array2js($resultat, true);
249
			}
250
		}
251
		return $retour;
252
	}
253
 
254
	protected function getReferentielImpose() {
255
		$referentiel_impose = true;
256
		if (!empty($_GET['referentiel']) && $_GET['referentiel'] != "autre") {
257
			$this->ns_referentiel = $_GET['referentiel'];
258
		} else if (isset($this->configProjet['referentiel'])) {
259
			$this->ns_referentiel = $this->configProjet['referentiel'];
260
		} else if (isset($this->configMission['referentiel'])) {
261
			$this->ns_referentiel = $this->configMission['referentiel'];
262
		} else {
263
			$referentiel_impose = false;
264
		}
265
		return $referentiel_impose;
266
	}
267
 
268
	/**
269
	 * Trie par nom français les taxons lus dans le fichier tsv
270
	 */
271
	protected function recupererListeNomsSci() {
272
		$taxons = $this->recupererListeTaxon();
273
		if (is_array($taxons)) {
274
			$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
275
		}
276
		return $taxons;
277
	}
278
 
279
	/**
280
	 * @TODO documenter
281
	 * @return array
282
	 */
283
	protected function recupererListeNoms() {
284
		$taxons = $this->recupererListeTaxon();
285
		$nomsAAfficher = array();
286
		$nomsSpeciaux = array();
287
		if (is_array($taxons)) {
288
			foreach ($taxons as $taxon) {
289
				$nomSciTitle = $taxon['nom_ret'].
290
				($taxon['nom_fr'] != '' ? ' - '.$taxon['nom_fr'] : '' ).
291
				($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
292
				$nomFrTitle = $taxon['nom_sel'].
293
				($taxon['nom_ret'] != $taxon['nom_sel']? ' - '.$taxon['nom_ret'] : '' ).
294
				($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
295
 
296
				if ($taxon['groupe'] == 'special') {
297
					$nomsSpeciaux[] = array(
298
							'num_nom' => $taxon['num_nom_sel'],
299
							'nom_a_afficher' => $taxon['nom_fr'],
300
							'nom_a_sauver' => $taxon['nom_sel'],
301
							'nom_title' => $nomSciTitle,
302
							'nom_type' => 'nom-special');
303
				} else {
304
					$nomsAAfficher[] = array(
305
							'num_nom' => $taxon['num_nom_sel'],
306
							'nom_a_afficher' => $taxon['nom_sel'],
307
							'nom_a_sauver' => $taxon['nom_sel'],
308
							'nom_title' => $nomSciTitle,
309
							'nom_type' => 'nom-sci');
310
					$nomsAAfficher[] = array(
311
							'num_nom' => $taxon['num_nom_sel'],
312
							'nom_a_afficher' => $taxon['nom_fr'],
313
							'nom_a_sauver' => $taxon['nom_fr'],
314
							'nom_title' => $nomFrTitle,
315
							'nom_type' => 'nom-fr');
316
				}
317
			}
318
			$nomsAAfficher = self::trierTableauMd($nomsAAfficher, array('nom_a_afficher' => SORT_ASC));
319
			$nomsSpeciaux = self::trierTableauMd($nomsSpeciaux, array('nom_a_afficher' => SORT_ASC));
320
		}
321
		return array('speciaux' => $nomsSpeciaux, 'sci-et-fr' => $nomsAAfficher);
322
	}
323
 
324
	/**
325
	 * Lit une liste de taxons depuis un fichier tsv fourni
326
	 */
327
	protected function recupererListeTaxon() {
328
		$taxons = array();
329
		if ($this->projet == 'missions-flore') {
330
			$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_'.$this->mission.'_taxons.tsv';
331
		} else {
332
			$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons.tsv';
333
		}
334
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
335
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
336
		} else {
337
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
338
		}
339
		return $taxons;
340
	}
341
 
342
	/**
343
	 * Découpe un fihcier tsv
344
	 */
345
	protected function decomposerFichierTsv($fichier, $delimiter = "\t"){
346
		$header = null;
347
		$data = array();
348
		if (($handle = fopen($fichier, 'r')) !== FALSE) {
349
			while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
350
				if (!$header) {
351
					$header = $row;
352
				} else {
353
					$data[] = array_combine($header, $row);
354
				}
355
			}
356
			fclose($handle);
357
		}
358
		return $data;
359
	}
360
 
361
	/**
362
	 * Convertit un tableau PHP en Javascript - @WTF pourquoi ne pas faire un json_encode ?
363
	 * @param array $array
364
	 * @param boolean $show_keys
365
	 * @return une portion de JSON représentant le tableau
366
	 */
367
	protected function array2js($array,$show_keys) {
368
		$tableauJs = '{}';
369
		if (!empty($array)) {
370
			$total = count($array) - 1;
371
			$i = 0;
372
			$dimensions = array();
373
			foreach ($array as $key => $value) {
374
				if (is_array($value)) {
375
					$dimensions[$i] = array2js($value,$show_keys);
376
					if ($show_keys) {
377
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
378
					}
379
				} else {
380
					$dimensions[$i] = '"'.addslashes($value).'"';
381
					if ($show_keys) {
382
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
383
					}
384
				}
385
				if ($i == 0) {
386
					$dimensions[$i] = '{'.$dimensions[$i];
387
				}
388
				if ($i == $total) {
389
					$dimensions[$i].= '}';
390
				}
391
				$i++;
392
			}
393
			$tableauJs = implode(',', $dimensions);
394
		}
395
		return $tableauJs;
396
	}
397
 
398
 
399
}
400
?>