Subversion Repositories eFlore/Applications.cel

Rev

Rev 712 | Rev 917 | 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 {
22
 
23
	const DS = DIRECTORY_SEPARATOR;
24
	const PROJET_DEFAUT = 'biodiversite34';
719 jpm 25
	private $projet = null;
26
	private $configProjet = null;
27
 
712 jpm 28
	/**
29
	 * Méthode appelée par défaut pour charger ce widget.
30
	 */
31
	public function executer() {
32
		$retour = null;
33
		extract($this->parametres);
34
 
35
		if (!isset($projet)) {
719 jpm 36
			$this->projet = self::PROJET_DEFAUT;
37
		} else {
38
			$this->projet = $projet;
712 jpm 39
		}
719 jpm 40
		$this->chargerConfigProjet();
712 jpm 41
 
719 jpm 42
		$service = (isset($service)) ? $service : $this->projet;
43
		$methode = $this->traiterNomMethodeExecuter($service);
712 jpm 44
		if (method_exists($this, $methode)) {
45
			$retour = $this->$methode();
46
		} else {
47
			$this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
48
		}
49
 
50
		if (is_null($retour)) {
51
			$contenu = 'Un problème est survenu : '.print_r($this->messages, true);
52
		} else {
719 jpm 53
			$ext = (isset($retour['squelette_ext'])) ? $retour['squelette_ext'] : '.tpl.html';
54
			$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet.self::DS.$retour['squelette'].$ext;
712 jpm 55
			$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
56
		}
57
		$this->envoyer($contenu);
58
	}
59
 
719 jpm 60
	private function chargerConfigProjet() {
61
		$fichier_config = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'.ini';
62
		if (file_exists($fichier_config)) {
63
			if (!$this->configProjet	= parse_ini_file($fichier_config)) {
64
				$this->messages[] = "Le fichier ini '$fichier_config' du projet n'a pu être chargé.";
65
			}
66
		} else {
67
			$this->messages[] = "Le fichier ini '$fichier_config' du projet n'existe pas.";
68
		}
69
	}
70
 
712 jpm 71
	public function executerBiodiversite34() {
719 jpm 72
		$widget['squelette'] = $this->projet;
712 jpm 73
		$widget['donnees'] = array();
74
		$widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
75
		$widget['donnees']['taxons'] = $this->recupererListeTaxonBiodiversite34();
719 jpm 76
		$widget['donnees']['milieux'] = $this->parserMilieuxBiodiversite34();
712 jpm 77
		return  $widget;
78
	}
719 jpm 79
	public function executerTaxons() {
80
		$widget['squelette'] = $this->projet.'_taxons';
81
		$widget['squelette_ext'] = '.tpl.js';
82
		$widget['donnees'] = array();
83
		$methode = 'recupererListeTaxon'.str_replace(' ', '', ucwords(str_replace('-', ' ', strtolower($this->projet))));
84
		$taxons = $this->$methode();
85
		$taxons_tries = array();
86
		foreach ($taxons as $taxon) {
87
			$taxons_tries[$taxon['num_nom_sel']] = $taxon;
88
		}
89
		$widget['donnees']['taxons'] = json_encode($taxons_tries);
90
		return  $widget;
91
	}
712 jpm 92
 
719 jpm 93
	private function parserMilieuxBiodiversite34() {
94
		$infosMilieux = array();
95
		$milieux = explode('|', $this->configProjet['milieux']);
96
		foreach ($milieux as $milieu) {
97
			$details = explode(';', $milieu);
98
			if (isset($details[1])) {
99
				$infosMilieux[$details[0]] = $details[1];
100
			} else {
101
				$infosMilieux[$details[0]] = '';
102
			}
103
		}
104
		return $infosMilieux;
105
	}
106
 
712 jpm 107
	private function recupererListeTaxonBiodiversite34() {
108
		$taxons = null;
109
		$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.'biodiversite34_taxons.tsv';
110
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
111
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
112
		} else {
113
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
114
		}
115
		return $taxons;
116
	}
117
 
118
	private function decomposerFichierTsv($fichier, $delimiter = "\t"){
119
		$header = NULL;
120
	    $data = array();
121
	    if (($handle = fopen($fichier, 'r')) !== FALSE) {
122
	        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
123
	            if (!$header) {
124
	                $header = $row;
125
	            } else {
126
	                $data[] = array_combine($header, $row);
127
	            }
128
	        }
129
	        fclose($handle);
130
	    }
131
	    return $data;
132
	}
133
}