Subversion Repositories eFlore/Applications.cel

Rev

Rev 1580 | Rev 1629 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1580 Rev 1613
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
2
// declare(encoding='UTF-8');
3
/**
3
/**
4
 * Widget fournissant des interfaces de saisies simplifiée pour différent projets.
4
 * Widget fournissant des interfaces de saisies simplifiée pour différent projets.
5
 * Encodage en entrée : utf8
5
 * Encodage en entrée : utf8
6
 * Encodage en sortie : utf8
6
 * Encodage en sortie : utf8
7
 *
7
 *
8
 * Cas d'utilisation et documentation :
8
 * Cas d'utilisation et documentation :
9
 * @link http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=AideCELWidgetSaisie
9
 * @link http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=AideCELWidgetSaisie
10
 *
10
 *
11
 * Paramètres :
11
 * Paramètres :
12
 * ===> projet = chaine  [par défaut : Biodiversite34]
12
 * ===> projet = chaine  [par défaut : Biodiversite34]
13
 * Indique quel projet nous voulons charger
13
 * Indique quel projet nous voulons charger
14
 *
14
 *
15
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
15
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
16
 * @license		GPL v3 <http://www.gnu.org/licenses/gpl.txt>
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>
17
 * @license		CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
18
 * @version		$Id$
18
 * @version		$Id$
19
 * @copyright	Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org)
19
 * @copyright	Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org)
20
 */
20
 */
21
class Saisie extends WidgetCommun {
21
class Saisie extends WidgetCommun {
22
 
22
 
23
	const DS = DIRECTORY_SEPARATOR;
23
	const DS = DIRECTORY_SEPARATOR;
24
	const PROJET_DEFAUT = 'defaut';
24
	const PROJET_DEFAUT = 'defaut';
25
	const WS_SAISIE = 'CelWidgetSaisie';
25
	const WS_SAISIE = 'CelWidgetSaisie';
26
	const WS_OBS = 'CelObs';
26
	const WS_OBS = 'CelObs';
27
	const WS_NOM = 'noms';
27
	const WS_NOM = 'noms';
28
	private $NS_PROJET_VERSION = '1.01';
28
	private $NS_PROJET_VERSION = '1.01';
29
	const EFLORE_API_VERSION = '0.1';
29
	const EFLORE_API_VERSION = '0.1';
30
	private $NS_PROJET = 'bdtfx';
30
	private $NS_PROJET = 'bdtfx';
31
 
31
 
32
	private $projetsVersions = array();
32
	private $projetsVersions = array();
33
	private $projet = null;
33
	private $projet = null;
34
	private $configProjet = null;
34
	private $configProjet = null;
35
 
35
 
36
	/**
36
	/**
37
	 * Méthode appelée par défaut pour charger ce widget.
37
	 * Méthode appelée par défaut pour charger ce widget.
38
	 */
38
	 */
39
	public function executer() {
39
	public function executer() {
40
		$retour = null;
40
		$retour = null;
41
		extract($this->parametres);
41
		extract($this->parametres);
42
 
42
 
43
		$this->projet = isset($projet) ? $projet : self::PROJET_DEFAUT;
43
		$this->projet = isset($projet) ? $projet : self::PROJET_DEFAUT;
44
		$this->chargerConfigProjet();
44
		$this->chargerConfigProjet();
45
		$this->chargerProjetsVersion();
45
		$this->chargerProjetsVersion();
46
 
46
 
47
		$service = isset($service) ? $service : 'widget';
47
		$service = isset($service) ? $service : 'widget';
48
		$methode = $this->traiterNomMethodeExecuter($service);
48
		$methode = $this->traiterNomMethodeExecuter($service);
49
		if (method_exists($this, $methode)) {
49
		if (method_exists($this, $methode)) {
50
			$retour = $this->$methode();
50
			$retour = $this->$methode();
51
		} else {
51
		} else {
52
			$this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
52
			$this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
53
		}
53
		}
54
 
54
 
55
		$contenu = null;
55
		$contenu = null;
56
		$mime = null;
56
		$mime = null;
57
		if (is_array($retour) && array_key_exists('squelette', $retour)) {
57
		if (is_array($retour) && array_key_exists('squelette', $retour)) {
58
			$ext = (isset($retour['squelette_ext'])) ? $retour['squelette_ext'] : '.tpl.html';
58
			$ext = (isset($retour['squelette_ext'])) ? $retour['squelette_ext'] : '.tpl.html';
59
			if($this->projetASquelette()) {
59
			if($this->projetASquelette()) {
60
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet.self::DS.$retour['squelette'].$ext;
60
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet.self::DS.$retour['squelette'].$ext;
61
			} else {
61
			} else {
62
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.'defaut'.self::DS.'defaut'.$ext;
62
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.'defaut'.self::DS.'defaut'.$ext;
63
			}
63
			}
64
			$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
64
			$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
65
			$mime = isset($retour['mime']) ? $retour['mime'] : null;
65
			$mime = isset($retour['mime']) ? $retour['mime'] : null;
66
		} else {
66
		} else {
67
			if (count($this->messages) == 0) {
67
			if (count($this->messages) == 0) {
68
				$this->messages[] = "La méthode du sous-service ne renvoie pas les données dans le bon format.";
68
				$this->messages[] = "La méthode du sous-service ne renvoie pas les données dans le bon format.";
69
			}
69
			}
70
			$contenu = 'Un problème est survenu : '.print_r($this->messages, true);
70
			$contenu = 'Un problème est survenu : '.print_r($this->messages, true);
71
		}
71
		}
72
 
72
 
73
		$this->envoyer($contenu, $mime);
73
		$this->envoyer($contenu, $mime);
74
	}
74
	}
75
 
75
 
76
	private function chargerConfigProjet() {
76
	private function chargerConfigProjet() {
77
		$fichier_config = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'.ini';
77
		$fichier_config = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'.ini';
78
		if (file_exists($fichier_config)) {
78
		if (file_exists($fichier_config)) {
79
			if (!$this->configProjet = parse_ini_file($fichier_config)) {
79
			if (!$this->configProjet = parse_ini_file($fichier_config)) {
80
				$this->messages[] = "Le fichier ini '$fichier_config' du projet n'a pu être chargé.";
80
				$this->messages[] = "Le fichier ini '$fichier_config' du projet n'a pu être chargé.";
81
			}
81
			}
82
		} else {
82
		} else {
83
			$this->debug[] = "Le fichier ini '$fichier_config' du projet n'existe pas.";
83
			$this->debug[] = "Le fichier ini '$fichier_config' du projet n'existe pas.";
84
		}
84
		}
85
	}
85
	}
86
 
86
 
87
	private function chargerProjetsVersion() {
87
	private function chargerProjetsVersion() {
88
		if (isset($this->configProjet)) {
88
		if (isset($this->configProjet)) {
89
			foreach ($this->configProjet as $config => $valeur) {
89
			foreach ($this->configProjet as $config => $valeur) {
90
				if(strstr($config,'.version')) {
90
				if(strstr($config,'.version')) {
91
					$this->projetsVersions[str_replace('.version', '', $config)] = $valeur;
91
					$this->projetsVersions[str_replace('.version', '', $config)] = $valeur;
92
				}
92
				}
93
			}
93
			}
94
		}
94
		}
95
	}
95
	}
96
 
96
 
97
	public function executerWidget() {
97
	public function executerWidget() {
98
		$referentiel_impose = false;
98
		$referentiel_impose = false;
99
		if (isset($_GET['referentiel']) && $_GET['referentiel'] != '') {
99
		if (isset($_GET['referentiel']) && $_GET['referentiel'] != '') {
100
			$this->NS_PROJET = isset($_GET['referentiel']) && $_GET['referentiel'] != '' ? $_GET['referentiel'] : $this->NS_PROJET;
100
			$this->NS_PROJET = isset($_GET['referentiel']) && $_GET['referentiel'] != '' ? $_GET['referentiel'] : $this->NS_PROJET;
101
			$this->NS_PROJET_VERSION = $this->projetsVersions[$this->NS_PROJET];
101
			$this->NS_PROJET_VERSION = $this->projetsVersions[$this->NS_PROJET];
102
			$referentiel_impose = true;
102
			$referentiel_impose = true;
103
		}
103
		}
104
 
104
 
105
		$widget['squelette'] = $this->projet;
105
		$widget['squelette'] = $this->projet;
106
		$widget['donnees'] = array();
106
		$widget['donnees'] = array();
107
		$widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
107
		$widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
108
		$widget['donnees']['url_ws_saisie'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_SAISIE);
108
		$widget['donnees']['url_ws_saisie'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_SAISIE);
109
		$widget['donnees']['url_ws_obs'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_OBS);
109
		$widget['donnees']['url_ws_obs'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_OBS);
110
		$widget['donnees']['url_ws_annuaire'] = sprintf($this->config['chemins']['baseURLServicesAnnuaireTpl'], 'utilisateur/identite-par-courriel/');
110
		$widget['donnees']['url_ws_annuaire'] = sprintf($this->config['chemins']['baseURLServicesAnnuaireTpl'], 'utilisateur/identite-par-courriel/');
111
 
111
 
112
 
112
 
113
		$widget['donnees']['logo'] = isset($_GET['logo']) ? $_GET['logo'] : 'defaut';
113
		$widget['donnees']['logo'] = isset($_GET['logo']) ? $_GET['logo'] : 'defaut';
114
		$widget['donnees']['titre'] = isset($_GET['titre']) ? $_GET['titre'] : 'defaut';
114
		$widget['donnees']['titre'] = isset($_GET['titre']) ? $_GET['titre'] : 'defaut';
115
		$widget['donnees']['titre'] = ($widget['donnees']['titre'] == '0') ? '' : $widget['donnees']['titre'];
115
		$widget['donnees']['titre'] = ($widget['donnees']['titre'] == '0') ? '' : $widget['donnees']['titre'];
116
 
116
 
117
		// cas du projet par défaut ou bien d'un projet n'ayant pas de squelette spécifique
117
		// cas du projet par défaut ou bien d'un projet n'ayant pas de squelette spécifique
118
		if ($this->projet == 'defaut' || $this->projet == 'florileges' || !$this->projetASquelette()) {
118
		if ($this->projet == 'defaut' || $this->projet == 'florileges' || !$this->projetASquelette()) {
119
			$urlWsNsTpl = $this->config['chemins']['baseURLServicesEfloreTpl'];
119
			$urlWsNsTpl = $this->config['chemins']['baseURLServicesEfloreTpl'];
120
			$urlWsNs = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, $this->NS_PROJET, self::WS_NOM);
120
			$urlWsNs = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, $this->NS_PROJET, self::WS_NOM);
121
			$urlWsNsSansRef = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, '{referentiel}', self::WS_NOM);
121
			$urlWsNsSansRef = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, '{referentiel}', self::WS_NOM);
122
			$widget['donnees']['url_ws_autocompletion_ns'] = $urlWsNs;
122
			$widget['donnees']['url_ws_autocompletion_ns'] = $urlWsNs;
123
			$widget['donnees']['url_ws_autocompletion_ns_tpl'] = $urlWsNsSansRef;
123
			$widget['donnees']['url_ws_autocompletion_ns_tpl'] = $urlWsNsSansRef;
124
			$widget['donnees']['ns_referentiel'] = $this->NS_PROJET.':'.$this->NS_PROJET_VERSION;
124
			$widget['donnees']['ns_referentiel'] = $this->NS_PROJET.':'.$this->NS_PROJET_VERSION;
125
			$widget['donnees']['ns_projet'] = $this->NS_PROJET;
125
			$widget['donnees']['ns_projet'] = $this->NS_PROJET;
126
			$widget['donnees']['ns_version'] = $this->NS_PROJET_VERSION;
126
			$widget['donnees']['ns_version'] = $this->NS_PROJET_VERSION;
127
			$widget['donnees']['referentiel_impose'] = $referentiel_impose;
127
			$widget['donnees']['referentiel_impose'] = $referentiel_impose;
128
			$widget['donnees']['projets_versions'] = $this->projetsVersions;
128
			$widget['donnees']['projets_versions'] = $this->projetsVersions;
129
			$widget['donnees']['espece_imposee'] = false;
129
			$widget['donnees']['espece_imposee'] = false;
130
			$widget['donnees']['nn_espece_defaut'] = '';
130
			$widget['donnees']['nn_espece_defaut'] = '';
131
			$widget['donnees']['nom_sci_espece_defaut'] = '';
131
			$widget['donnees']['nom_sci_espece_defaut'] = '';
132
			$widget['donnees']['infos_espece'] = '{}';
132
			$widget['donnees']['infos_espece'] = '{}';
133
 
133
 
134
			if ($this->especeEstImposee()) {
134
			if ($this->especeEstImposee()) {
135
				$nom = $this->executerChargementInfosTaxon($_GET['num_nom']);
135
				$nom = $this->executerChargementInfosTaxon($_GET['num_nom']);
136
				$widget['donnees']['espece_imposee'] = true;
136
				$widget['donnees']['espece_imposee'] = true;
137
				$widget['donnees']['nn_espece_defaut'] = $_GET['num_nom'];
137
				$widget['donnees']['nn_espece_defaut'] = $_GET['num_nom'];
138
				$widget['donnees']['nom_sci_espece_defaut'] = $nom['nom_sci'];
138
				$widget['donnees']['nom_sci_espece_defaut'] = $nom['nom_sci'];
139
				$widget['donnees']['infos_espece'] = $this->array2js($nom, true);
139
				$widget['donnees']['infos_espece'] = $this->array2js($nom, true);
140
			}
140
			}
141
		}
141
		}
142
		if ($this->projet != 'defaut' || $this->projetASquelette()) {
142
		if ($this->projet != 'defaut' || $this->projetASquelette()) {
-
 
143
			if ($this->projet == 'florileges') {
143
			$widget['donnees']['taxons'] = $this->recupererListeTaxon();
144
				$widget['donnees']['taxons'] = $this->recupererListeNoms();
-
 
145
			} else {
-
 
146
				$widget['donnees']['taxons'] = $this->recupererListeNomsSci();
-
 
147
			}
144
			$widget['donnees']['milieux'] = $this->parserMilieux();
148
			$widget['donnees']['milieux'] = $this->parserMilieux();
145
		}
149
		}
146
		return  $widget;
150
		return  $widget;
147
	}
151
	}
148
 
152
 
149
	private function projetASquelette() {
153
	private function projetASquelette() {
150
		// fonction très simple qui ne teste que si le dossier du projet courant
154
		// fonction très simple qui ne teste que si le dossier du projet courant
151
		// existe, mais elle suffit pour le moment.
155
		// existe, mais elle suffit pour le moment.
152
		return file_exists(dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet);
156
		return file_exists(dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet);
153
	}
157
	}
154
 
158
 
155
	public function executerTaxons() {
159
	public function executerTaxons() {
156
		$widget['squelette'] = $this->projet.'_taxons';
160
		$widget['squelette'] = $this->projet.'_taxons';
157
		$widget['squelette_ext'] = '.tpl.js';
161
		$widget['squelette_ext'] = '.tpl.js';
158
		$widget['donnees'] = array();
162
		$widget['donnees'] = array();
159
		$taxons = $this->recupererListeTaxon();
163
		$nomsAAfficher = $this->recupererListeNomsSci();
160
		$taxons_tries = array();
-
 
161
		foreach ($taxons as $taxon) {
-
 
162
			$taxons_tries[$taxon['num_nom_sel']] = $taxon;
-
 
163
		}
-
 
164
		$widget['donnees']['taxons'] = json_encode($taxons_tries);
164
		$widget['donnees']['taxons'] = json_encode($nomsAAfficher);
165
		return  $widget;
165
		return  $widget;
166
	}
166
	}
-
 
167
 
-
 
168
	private function recupererListeNomsSci() {
-
 
169
		$taxons = $this->recupererListeTaxon();
-
 
170
		$nomsAAfficher = array();
-
 
171
		if (is_array($taxons)) {
-
 
172
			$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
-
 
173
			foreach ($taxons as $taxon) {
-
 
174
				$nomsAAfficher[$taxon['num_nom_sel']] = $taxon;
-
 
175
			}
-
 
176
		}
-
 
177
		return $nomsAAfficher;
-
 
178
	}
-
 
179
 
-
 
180
	private function recupererListeNoms() {
-
 
181
		$taxons = $this->recupererListeTaxon();
-
 
182
		$nomsAAfficher = array();
-
 
183
		$nomsSpeciaux = array();
-
 
184
		if (is_array($taxons)) {
-
 
185
			foreach ($taxons as $taxon) {
-
 
186
				$nomSciTitle = $taxon['nom_ret'].
-
 
187
					($taxon['nom_fr'] != '' ? ' - '.$taxon['nom_fr'] : '' ).
-
 
188
					($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
-
 
189
				$nomFrTitle = $taxon['nom_sel'].
-
 
190
					($taxon['nom_ret'] != $taxon['nom_sel']? ' - '.$taxon['nom_ret'] : '' ).
-
 
191
					($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
-
 
192
 
-
 
193
				if ($taxon['groupe'] == 'special') {
-
 
194
					$nomsSpeciaux[] = array(
-
 
195
						'num_nom' => $taxon['num_nom_sel'],
-
 
196
						'nom_a_afficher' => $taxon['nom_fr'],
-
 
197
						'nom_a_sauver' => $taxon['nom_sel'],
-
 
198
						'nom_title' => $nomSciTitle,
-
 
199
						'nom_type' => 'nom-special');
-
 
200
				} else {
-
 
201
					$nomsAAfficher[] = array(
-
 
202
						'num_nom' => $taxon['num_nom_sel'],
-
 
203
						'nom_a_afficher' => $taxon['nom_sel'],
-
 
204
						'nom_a_sauver' => $taxon['nom_sel'],
-
 
205
						'nom_title' => $nomSciTitle,
-
 
206
						'nom_type' => 'nom-sci');
-
 
207
					$nomsAAfficher[] = array(
-
 
208
						'num_nom' => $taxon['num_nom_sel'],
-
 
209
						'nom_a_afficher' => $taxon['nom_fr'],
-
 
210
						'nom_a_sauver' => $taxon['nom_fr'],
-
 
211
						'nom_title' => $nomFrTitle,
-
 
212
						'nom_type' => 'nom-fr');
-
 
213
				}
-
 
214
			}
-
 
215
			$nomsAAfficher = self::trierTableauMd($nomsAAfficher, array('nom_a_afficher' => SORT_ASC));
-
 
216
			$nomsSpeciaux = self::trierTableauMd($nomsSpeciaux, array('nom_a_afficher' => SORT_ASC));
-
 
217
			$nomsAAfficher = array_merge($nomsAAfficher, $nomsSpeciaux);
-
 
218
		}
-
 
219
		return array('speciaux' => $nomsSpeciaux, 'sci-et-fr' => $nomsAAfficher);
-
 
220
	}
167
 
221
 
168
	private function recupererListeTaxon() {
222
	private function recupererListeTaxon() {
169
		$taxons = null;
223
		$taxons = null;
170
		$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons.tsv';
224
		$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons.tsv';
171
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
225
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
172
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
226
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
173
			$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
-
 
174
		} else {
227
		} else {
175
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
228
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
176
		}
229
		}
177
		return $taxons;
230
		return $taxons;
178
	}
231
	}
179
 
232
 
180
	private function decomposerFichierTsv($fichier, $delimiter = "\t"){
233
	private function decomposerFichierTsv($fichier, $delimiter = "\t"){
181
		$header = null;
234
		$header = null;
182
		$data = array();
235
		$data = array();
183
		if (($handle = fopen($fichier, 'r')) !== FALSE) {
236
		if (($handle = fopen($fichier, 'r')) !== FALSE) {
184
			while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
237
			while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
185
				if (!$header) {
238
				if (!$header) {
186
					$header = $row;
239
					$header = $row;
187
				} else {
240
				} else {
188
					$data[] = array_combine($header, $row);
241
					$data[] = array_combine($header, $row);
189
				}
242
				}
190
			}
243
			}
191
			fclose($handle);
244
			fclose($handle);
192
		}
245
		}
193
		return $data;
246
		return $data;
194
	}
247
	}
195
 
248
 
196
	private function parserMilieux() {
249
	private function parserMilieux() {
197
		$infosMilieux = array();
250
		$infosMilieux = array();
198
		if (isset($this->configProjet['milieux'])) {
251
		if (isset($this->configProjet['milieux'])) {
199
			$milieux = explode('|', $this->configProjet['milieux']);
252
			$milieux = explode('|', $this->configProjet['milieux']);
200
			foreach ($milieux as $milieu) {
253
			foreach ($milieux as $milieu) {
201
				$details = explode(';', $milieu);
254
				$details = explode(';', $milieu);
202
				if (isset($details[1])) {
255
				if (isset($details[1])) {
203
					$infosMilieux[$details[0]] = $details[1];
256
					$infosMilieux[$details[0]] = $details[1];
204
				} else {
257
				} else {
205
					$infosMilieux[$details[0]] = '';
258
					$infosMilieux[$details[0]] = '';
206
				}
259
				}
207
			}
260
			}
208
			ksort($infosMilieux);
261
			ksort($infosMilieux);
209
		}
262
		}
210
		return $infosMilieux;
263
		return $infosMilieux;
211
	}
264
	}
212
 
265
 
213
	private function especeEstImposee() {
266
	private function especeEstImposee() {
214
		return (isset($_GET['num_nom']) && $_GET['num_nom'] != '');
267
		return (isset($_GET['num_nom']) && $_GET['num_nom'] != '');
215
	}
268
	}
216
 
269
 
217
	private function executerChargementInfosTaxon($num_nom) {
270
	private function executerChargementInfosTaxon($num_nom) {
218
		$url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $this->NS_PROJET, $num_nom);
271
		$url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $this->NS_PROJET, $num_nom);
219
		$infos = json_decode(file_get_contents($url_service_infos));
272
		$infos = json_decode(file_get_contents($url_service_infos));
220
		$resultat = array();
273
		$resultat = array();
221
		if (isset($infos) && !empty($infos)) {
274
		if (isset($infos) && !empty($infos)) {
222
			$infos = (array)$infos;
275
			$infos = (array)$infos;
223
			$resultat = (isset($infos['nom_sci']) && $infos['nom_sci'] != '') ? $infos : array();
276
			$resultat = (isset($infos['nom_sci']) && $infos['nom_sci'] != '') ? $infos : array();
224
		}
277
		}
225
		return $resultat;
278
		return $resultat;
226
	}
279
	}
227
 
280
 
228
	public function executerUploadImage() {
281
	public function executerUploadImage() {
229
		$retour = array(
282
		$retour = array(
230
				'squelette' => $this->projet.'_image',
283
				'squelette' => $this->projet.'_image',
231
				'squelette_ext' => '.tpl.xml',
284
				'squelette_ext' => '.tpl.xml',
232
				'mime' => 'text/xml',
285
				'mime' => 'text/xml',
233
				'donnees' => array(
286
				'donnees' => array(
234
						'urlMiniature' => '',
287
						'urlMiniature' => '',
235
						'imageNom' => '',
288
						'imageNom' => '',
236
						'message' => '',
289
						'message' => '',
237
						'debogage' => ''));
290
						'debogage' => ''));
238
		$message = '';
291
		$message = '';
239
		$debogage = '';
292
		$debogage = '';
240
		if ($_FILES['fichier']['error'] == UPLOAD_ERR_OK) {
293
		if ($_FILES['fichier']['error'] == UPLOAD_ERR_OK) {
241
			if (is_uploaded_file($_FILES['fichier']['tmp_name'])) {
294
			if (is_uploaded_file($_FILES['fichier']['tmp_name'])) {
242
				if ($this->verifierFormatJpeg($_FILES['fichier']['tmp_name'])) {
295
				if ($this->verifierFormatJpeg($_FILES['fichier']['tmp_name'])) {
243
					$dossierStockage = $this->config['chemins']['imagesTempDossier'];
296
					$dossierStockage = $this->config['chemins']['imagesTempDossier'];
244
 
297
 
245
					$nomFichierOriginal = preg_replace('/[.](jpeg|jpg)$/i', '.jpg', strtolower($_FILES['fichier']['name']));
298
					$nomFichierOriginal = preg_replace('/[.](jpeg|jpg)$/i', '.jpg', strtolower($_FILES['fichier']['name']));
246
					$originalChemin = $dossierStockage.$nomFichierOriginal;
299
					$originalChemin = $dossierStockage.$nomFichierOriginal;
247
					$deplacementOk = move_uploaded_file($_FILES['fichier']['tmp_name'], $originalChemin);
300
					$deplacementOk = move_uploaded_file($_FILES['fichier']['tmp_name'], $originalChemin);
248
 
301
 
249
					if ($deplacementOk === true) {
302
					if ($deplacementOk === true) {
250
						$miniatureFichier = str_replace('.jpg', '_min.jpg', $nomFichierOriginal);
303
						$miniatureFichier = str_replace('.jpg', '_min.jpg', $nomFichierOriginal);
251
						$miniatureChemin = $dossierStockage.$miniatureFichier;
304
						$miniatureChemin = $dossierStockage.$miniatureFichier;
252
 
305
 
253
						// Parametres
306
						// Parametres
254
						$largeurIdeale = 100;
307
						$largeurIdeale = 100;
255
						$hauteurIdeale = 100;
308
						$hauteurIdeale = 100;
256
						$qualite = 85;
309
						$qualite = 85;
257
 
310
 
258
						// Calcul de la hauteur et de la largeur optimale de la miniature
311
						// Calcul de la hauteur et de la largeur optimale de la miniature
259
						$taillesImgOriginale = getimagesize($originalChemin);
312
						$taillesImgOriginale = getimagesize($originalChemin);
260
						$largeurOrigine = $taillesImgOriginale[0];
313
						$largeurOrigine = $taillesImgOriginale[0];
261
						$hauteurOrigine = $taillesImgOriginale[1];
314
						$hauteurOrigine = $taillesImgOriginale[1];
262
 
315
 
263
						$largeurMin = $largeurIdeale;
316
						$largeurMin = $largeurIdeale;
264
						$hauteurMin = (int) ($hauteurOrigine * ($largeurIdeale / $largeurOrigine));
317
						$hauteurMin = (int) ($hauteurOrigine * ($largeurIdeale / $largeurOrigine));
265
						if ($hauteurMin > $hauteurIdeale) {
318
						if ($hauteurMin > $hauteurIdeale) {
266
							$hauteurMin = $hauteurIdeale;
319
							$hauteurMin = $hauteurIdeale;
267
							$largeurMin = (int)($largeurOrigine * ($hauteurMin / $hauteurOrigine));
320
							$largeurMin = (int)($largeurOrigine * ($hauteurMin / $hauteurOrigine));
268
						}
321
						}
269
 
322
 
270
						// Création de la miniature
323
						// Création de la miniature
271
						$imageOriginale = imagecreatefromjpeg($originalChemin);
324
						$imageOriginale = imagecreatefromjpeg($originalChemin);
272
						$imageMiniature = imagecreatetruecolor($largeurMin, $hauteurMin);
325
						$imageMiniature = imagecreatetruecolor($largeurMin, $hauteurMin);
273
						$couleurFond = imagecolorallocate($imageMiniature, 255, 255, 255);
326
						$couleurFond = imagecolorallocate($imageMiniature, 255, 255, 255);
274
						imagefill($imageMiniature, 0, 0, $couleurFond);
327
						imagefill($imageMiniature, 0, 0, $couleurFond);
275
						imagecopyresized($imageMiniature, $imageOriginale, 0, 0, 0, 0, $largeurMin, $hauteurMin, $largeurOrigine, $hauteurOrigine);
328
						imagecopyresized($imageMiniature, $imageOriginale, 0, 0, 0, 0, $largeurMin, $hauteurMin, $largeurOrigine, $hauteurOrigine);
276
						imagejpeg($imageMiniature, $miniatureChemin, $qualite);
329
						imagejpeg($imageMiniature, $miniatureChemin, $qualite);
277
						imagedestroy($imageMiniature);
330
						imagedestroy($imageMiniature);
278
						imagedestroy($imageOriginale);
331
						imagedestroy($imageOriginale);
279
 
332
 
280
						// Retour des infos
333
						// Retour des infos
281
						$retour['donnees']['urlMiniature'] = sprintf($this->config['chemins']['imagesTempUrlTpl'], $miniatureFichier);
334
						$retour['donnees']['urlMiniature'] = sprintf($this->config['chemins']['imagesTempUrlTpl'], $miniatureFichier);
282
						$retour['donnees']['imageNom'] = $nomFichierOriginal;
335
						$retour['donnees']['imageNom'] = $nomFichierOriginal;
283
					} else {
336
					} else {
284
						$message = "L'image n'a pu être déplacé sur le serveur.";
337
						$message = "L'image n'a pu être déplacé sur le serveur.";
285
					}
338
					}
286
				} else {
339
				} else {
287
					$message = "L'image n'est pas au format JPEG.";
340
					$message = "L'image n'est pas au format JPEG.";
288
				}
341
				}
289
			} else {
342
			} else {
290
				$message = "L'image n'a pu être téléversée.";
343
				$message = "L'image n'a pu être téléversée.";
291
				$debogage = $message.print_r($_FILES, true);
344
				$debogage = $message.print_r($_FILES, true);
292
			}
345
			}
293
		} else {
346
		} else {
294
			if ($_FILES['fichier']['error'] == UPLOAD_ERR_FORM_SIZE) {
347
			if ($_FILES['fichier']['error'] == UPLOAD_ERR_FORM_SIZE) {
295
				$message = "L'image téléversée excède la taille maximum autorisée.".
348
				$message = "L'image téléversée excède la taille maximum autorisée.".
296
						"Veuillez modifier votre image avant de la téléverser à nouveau.";
349
						"Veuillez modifier votre image avant de la téléverser à nouveau.";
297
			} else {
350
			} else {
298
				$message = "Une erreur de transfert a eu lieu (téléversement interrompu).";
351
				$message = "Une erreur de transfert a eu lieu (téléversement interrompu).";
299
			}
352
			}
300
			$debogage = "Code erreur : {$_FILES['fichier']['error']}. ".
353
			$debogage = "Code erreur : {$_FILES['fichier']['error']}. ".
301
					"Voir : http://php.net/manual/fr/features.file-upload.errors.php";
354
					"Voir : http://php.net/manual/fr/features.file-upload.errors.php";
302
		}
355
		}
303
		// Retour des infos
356
		// Retour des infos
304
		$retour['donnees']['message'] = $message;
357
		$retour['donnees']['message'] = $message;
305
		$retour['donnees']['debogage'] = $debogage;
358
		$retour['donnees']['debogage'] = $debogage;
306
		return $retour;
359
		return $retour;
307
	}
360
	}
308
 
361
 
309
	// Il ne faut pas utiliser l'index type du tableau files pour tester
362
	// Il ne faut pas utiliser l'index type du tableau files pour tester
310
	// si une image est en jpeg car le type renvoyé par les navigateurs
363
	// si une image est en jpeg car le type renvoyé par les navigateurs
311
	// peut varier (ex. sous ie qui renvoie image/pjpeg
364
	// peut varier (ex. sous ie qui renvoie image/pjpeg
312
	private function verifierFormatJpeg($chemin) {
365
	private function verifierFormatJpeg($chemin) {
313
		// get imagesize renvoie un résultat consistant par contre
366
		// get imagesize renvoie un résultat consistant par contre
314
		$infos = getimagesize($chemin, $infos);
367
		$infos = getimagesize($chemin, $infos);
315
		return (isset($infos['mime']) && $infos['mime'] == 'image/jpeg');
368
		return (isset($infos['mime']) && $infos['mime'] == 'image/jpeg');
316
	}
369
	}
317
 
370
 
318
	private function array2js($array,$show_keys) {
371
	private function array2js($array,$show_keys) {
319
		$tableauJs = '{}';
372
		$tableauJs = '{}';
320
		if (!empty($array)) {
373
		if (!empty($array)) {
321
			$total = count($array) - 1;
374
			$total = count($array) - 1;
322
			$i = 0;
375
			$i = 0;
323
			$dimensions = array();
376
			$dimensions = array();
324
			foreach ($array as $key => $value) {
377
			foreach ($array as $key => $value) {
325
				if (is_array($value)) {
378
				if (is_array($value)) {
326
					$dimensions[$i] = array2js($value,$show_keys);
379
					$dimensions[$i] = array2js($value,$show_keys);
327
					if ($show_keys) {
380
					if ($show_keys) {
328
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
381
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
329
					}
382
					}
330
				} else {
383
				} else {
331
					$dimensions[$i] = '"'.addslashes($value).'"';
384
					$dimensions[$i] = '"'.addslashes($value).'"';
332
					if ($show_keys) {
385
					if ($show_keys) {
333
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
386
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
334
					}
387
					}
335
				}
388
				}
336
				if ($i == 0) {
389
				if ($i == 0) {
337
					$dimensions[$i] = '{'.$dimensions[$i];
390
					$dimensions[$i] = '{'.$dimensions[$i];
338
				}
391
				}
339
				if ($i == $total) {
392
				if ($i == $total) {
340
					$dimensions[$i].= '}';
393
					$dimensions[$i].= '}';
341
				}
394
				}
342
				$i++;
395
				$i++;
343
			}
396
			}
344
			$tableauJs = implode(',', $dimensions);
397
			$tableauJs = implode(',', $dimensions);
345
		}
398
		}
346
		return $tableauJs;
399
		return $tableauJs;
347
	}
400
	}
348
}
401
}
349
?>
402
?>