Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 1539 Rev 1580
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_NOM = "noms";
27
	const WS_NOM = 'noms';
27
	private $NS_PROJET_VERSION = "1.01";
28
	private $NS_PROJET_VERSION = '1.01';
28
	const EFLORE_API_VERSION = "0.1";
29
	const EFLORE_API_VERSION = '0.1';
29
	private $NS_PROJET = "bdtfx";
30
	private $NS_PROJET = 'bdtfx';
30
 
31
 
31
	private $projetsVersions = array();
32
	private $projetsVersions = array();
32
	private $projet = null;
33
	private $projet = null;
33
	private $configProjet = null;
34
	private $configProjet = null;
34
 
35
 
35
	/**
36
	/**
36
	 * Méthode appelée par défaut pour charger ce widget.
37
	 * Méthode appelée par défaut pour charger ce widget.
37
	 */
38
	 */
38
	public function executer() {
39
	public function executer() {
39
		$retour = null;
40
		$retour = null;
40
		extract($this->parametres);
41
		extract($this->parametres);
41
 
42
 
42
		$this->projet = isset($projet) ? $projet : self::PROJET_DEFAUT;
43
		$this->projet = isset($projet) ? $projet : self::PROJET_DEFAUT;
43
		$this->chargerConfigProjet();
44
		$this->chargerConfigProjet();
44
		$this->chargerProjetsVersion();
45
		$this->chargerProjetsVersion();
45
 
46
 
46
		$service = isset($service) ? $service : 'widget';
47
		$service = isset($service) ? $service : 'widget';
47
		$methode = $this->traiterNomMethodeExecuter($service);
48
		$methode = $this->traiterNomMethodeExecuter($service);
48
		if (method_exists($this, $methode)) {
49
		if (method_exists($this, $methode)) {
49
			$retour = $this->$methode();
50
			$retour = $this->$methode();
50
		} else {
51
		} else {
51
			$this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
52
			$this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
52
		}
53
		}
53
 
54
 
54
		$contenu = null;
55
		$contenu = null;
55
		$mime = null;
56
		$mime = null;
56
		if (is_array($retour) && array_key_exists('squelette', $retour)) {
57
		if (is_array($retour) && array_key_exists('squelette', $retour)) {
57
			$ext = (isset($retour['squelette_ext'])) ? $retour['squelette_ext'] : '.tpl.html';
58
			$ext = (isset($retour['squelette_ext'])) ? $retour['squelette_ext'] : '.tpl.html';
58
			if($this->projetASquelette()) {
59
			if($this->projetASquelette()) {
59
				$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;
60
			} else {
61
			} else {
61
				$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;
62
			}
63
			}
63
			$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
64
			$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
64
			$mime = isset($retour['mime']) ? $retour['mime'] : null;
65
			$mime = isset($retour['mime']) ? $retour['mime'] : null;
65
		} else {
66
		} else {
66
			if (count($this->messages) == 0) {
67
			if (count($this->messages) == 0) {
67
				$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.";
68
			}
69
			}
69
			$contenu = 'Un problème est survenu : '.print_r($this->messages, true);
70
			$contenu = 'Un problème est survenu : '.print_r($this->messages, true);
70
		}
71
		}
71
 
72
 
72
		$this->envoyer($contenu, $mime);
73
		$this->envoyer($contenu, $mime);
73
	}
74
	}
74
 
75
 
75
	private function chargerConfigProjet() {
76
	private function chargerConfigProjet() {
76
		$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';
77
		if (file_exists($fichier_config)) {
78
		if (file_exists($fichier_config)) {
78
			if (!$this->configProjet = parse_ini_file($fichier_config)) {
79
			if (!$this->configProjet = parse_ini_file($fichier_config)) {
79
				$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é.";
80
			}
81
			}
81
		} else {
82
		} else {
82
			$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.";
83
		}
84
		}
84
	}
85
	}
85
 
86
 
86
	private function chargerProjetsVersion() {
87
	private function chargerProjetsVersion() {
87
		if (isset($this->configProjet)) {
88
		if (isset($this->configProjet)) {
88
			foreach ($this->configProjet as $config => $valeur) {
89
			foreach ($this->configProjet as $config => $valeur) {
89
				if(strstr($config,'.version')) {
90
				if(strstr($config,'.version')) {
90
					$this->projetsVersions[str_replace('.version', '', $config)] = $valeur;
91
					$this->projetsVersions[str_replace('.version', '', $config)] = $valeur;
91
				}
92
				}
92
			}
93
			}
93
		}
94
		}
94
	}
95
	}
95
 
96
 
96
	public function executerWidget() {
97
	public function executerWidget() {
97
		$referentiel_impose = false;
98
		$referentiel_impose = false;
98
		if (isset($_GET['referentiel']) && $_GET['referentiel'] != '') {
99
		if (isset($_GET['referentiel']) && $_GET['referentiel'] != '') {
99
			$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;
100
			$this->NS_PROJET_VERSION = $this->projetsVersions[$this->NS_PROJET];
101
			$this->NS_PROJET_VERSION = $this->projetsVersions[$this->NS_PROJET];
101
			$referentiel_impose = true;
102
			$referentiel_impose = true;
102
		}
103
		}
103
 
104
 
104
		$widget['squelette'] = $this->projet;
105
		$widget['squelette'] = $this->projet;
105
		$widget['donnees'] = array();
106
		$widget['donnees'] = array();
106
		$widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
107
		$widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
107
		$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);
108
		$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/');
109
 
111
 
110
 
112
 
111
		$widget['donnees']['logo'] = isset($_GET['logo']) ? $_GET['logo'] : 'defaut';
113
		$widget['donnees']['logo'] = isset($_GET['logo']) ? $_GET['logo'] : 'defaut';
112
		$widget['donnees']['titre'] = isset($_GET['titre']) ? $_GET['titre'] : 'defaut';
114
		$widget['donnees']['titre'] = isset($_GET['titre']) ? $_GET['titre'] : 'defaut';
113
		$widget['donnees']['titre'] = ($widget['donnees']['titre'] == '0') ? '' : $widget['donnees']['titre'];
115
		$widget['donnees']['titre'] = ($widget['donnees']['titre'] == '0') ? '' : $widget['donnees']['titre'];
114
 
116
 
115
		// 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
116
		if ($this->projet == 'defaut' || $this->projet == 'florileges' || !$this->projetASquelette()) {
118
		if ($this->projet == 'defaut' || $this->projet == 'florileges' || !$this->projetASquelette()) {
117
			$urlWsNsTpl = $this->config['chemins']['baseURLServicesEfloreTpl'];
119
			$urlWsNsTpl = $this->config['chemins']['baseURLServicesEfloreTpl'];
118
			$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);
119
			$urlWsNsSansRef = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, '{referentiel}', self::WS_NOM);
121
			$urlWsNsSansRef = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, '{referentiel}', self::WS_NOM);
120
			$widget['donnees']['url_ws_autocompletion_ns'] = $urlWsNs;
122
			$widget['donnees']['url_ws_autocompletion_ns'] = $urlWsNs;
121
			$widget['donnees']['url_ws_autocompletion_ns_tpl'] = $urlWsNsSansRef;
123
			$widget['donnees']['url_ws_autocompletion_ns_tpl'] = $urlWsNsSansRef;
122
			$widget['donnees']['ns_referentiel'] = $this->NS_PROJET.':'.$this->NS_PROJET_VERSION;
124
			$widget['donnees']['ns_referentiel'] = $this->NS_PROJET.':'.$this->NS_PROJET_VERSION;
123
			$widget['donnees']['ns_projet'] = $this->NS_PROJET;
125
			$widget['donnees']['ns_projet'] = $this->NS_PROJET;
124
			$widget['donnees']['ns_version'] = $this->NS_PROJET_VERSION;
126
			$widget['donnees']['ns_version'] = $this->NS_PROJET_VERSION;
125
			$widget['donnees']['referentiel_impose'] = $referentiel_impose;
127
			$widget['donnees']['referentiel_impose'] = $referentiel_impose;
126
			$widget['donnees']['projets_versions'] = $this->projetsVersions;
128
			$widget['donnees']['projets_versions'] = $this->projetsVersions;
127
			$widget['donnees']['espece_imposee'] = false;
129
			$widget['donnees']['espece_imposee'] = false;
128
			$widget['donnees']['nn_espece_defaut'] = '';
130
			$widget['donnees']['nn_espece_defaut'] = '';
129
			$widget['donnees']['nom_sci_espece_defaut'] = '';
131
			$widget['donnees']['nom_sci_espece_defaut'] = '';
130
			$widget['donnees']['infos_espece'] = '{}';
132
			$widget['donnees']['infos_espece'] = '{}';
131
 
133
 
132
			if ($this->especeEstImposee()) {
134
			if ($this->especeEstImposee()) {
133
				$nom = $this->executerChargementInfosTaxon($_GET['num_nom']);
135
				$nom = $this->executerChargementInfosTaxon($_GET['num_nom']);
134
				$widget['donnees']['espece_imposee'] = true;
136
				$widget['donnees']['espece_imposee'] = true;
135
				$widget['donnees']['nn_espece_defaut'] = $_GET['num_nom'];
137
				$widget['donnees']['nn_espece_defaut'] = $_GET['num_nom'];
136
				$widget['donnees']['nom_sci_espece_defaut'] = $nom['nom_sci'];
138
				$widget['donnees']['nom_sci_espece_defaut'] = $nom['nom_sci'];
137
				$widget['donnees']['infos_espece'] = $this->array2js($nom, true);
139
				$widget['donnees']['infos_espece'] = $this->array2js($nom, true);
138
			}
140
			}
139
		}
141
		}
140
		if ($this->projet != 'defaut' || $this->projetASquelette()) {
142
		if ($this->projet != 'defaut' || $this->projetASquelette()) {
141
			$widget['donnees']['taxons'] = $this->recupererListeTaxon();
143
			$widget['donnees']['taxons'] = $this->recupererListeTaxon();
142
			$widget['donnees']['milieux'] = $this->parserMilieux();
144
			$widget['donnees']['milieux'] = $this->parserMilieux();
143
		}
145
		}
144
		return  $widget;
146
		return  $widget;
145
	}
147
	}
146
 
148
 
147
	private function projetASquelette() {
149
	private function projetASquelette() {
148
		// fonction très simple qui ne teste que si le dossier du projet courant
150
		// fonction très simple qui ne teste que si le dossier du projet courant
149
		// existe, mais elle suffit pour le moment.
151
		// existe, mais elle suffit pour le moment.
150
		return file_exists(dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet);
152
		return file_exists(dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet);
151
	}
153
	}
152
 
154
 
153
	public function executerTaxons() {
155
	public function executerTaxons() {
154
		$widget['squelette'] = $this->projet.'_taxons';
156
		$widget['squelette'] = $this->projet.'_taxons';
155
		$widget['squelette_ext'] = '.tpl.js';
157
		$widget['squelette_ext'] = '.tpl.js';
156
		$widget['donnees'] = array();
158
		$widget['donnees'] = array();
157
		$taxons = $this->recupererListeTaxon();
159
		$taxons = $this->recupererListeTaxon();
158
		$taxons_tries = array();
160
		$taxons_tries = array();
159
		foreach ($taxons as $taxon) {
161
		foreach ($taxons as $taxon) {
160
			$taxons_tries[$taxon['num_nom_sel']] = $taxon;
162
			$taxons_tries[$taxon['num_nom_sel']] = $taxon;
161
		}
163
		}
162
		$widget['donnees']['taxons'] = json_encode($taxons_tries);
164
		$widget['donnees']['taxons'] = json_encode($taxons_tries);
163
		return  $widget;
165
		return  $widget;
164
	}
166
	}
165
 
167
 
166
	private function recupererListeTaxon() {
168
	private function recupererListeTaxon() {
167
		$taxons = null;
169
		$taxons = null;
168
		$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons.tsv';
170
		$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons.tsv';
169
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
171
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
170
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
172
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
171
			$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
173
			$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
172
		} else {
174
		} else {
173
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
175
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
174
		}
176
		}
175
		return $taxons;
177
		return $taxons;
176
	}
178
	}
177
 
179
 
178
	private function decomposerFichierTsv($fichier, $delimiter = "\t"){
180
	private function decomposerFichierTsv($fichier, $delimiter = "\t"){
179
		$header = null;
181
		$header = null;
180
		$data = array();
182
		$data = array();
181
		if (($handle = fopen($fichier, 'r')) !== FALSE) {
183
		if (($handle = fopen($fichier, 'r')) !== FALSE) {
182
			while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
184
			while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
183
				if (!$header) {
185
				if (!$header) {
184
					$header = $row;
186
					$header = $row;
185
				} else {
187
				} else {
186
					$data[] = array_combine($header, $row);
188
					$data[] = array_combine($header, $row);
187
				}
189
				}
188
			}
190
			}
189
			fclose($handle);
191
			fclose($handle);
190
		}
192
		}
191
		return $data;
193
		return $data;
192
	}
194
	}
193
 
195
 
194
	private function parserMilieux() {
196
	private function parserMilieux() {
195
		$infosMilieux = array();
197
		$infosMilieux = array();
196
		if (isset($this->configProjet['milieux'])) {
198
		if (isset($this->configProjet['milieux'])) {
197
			$milieux = explode('|', $this->configProjet['milieux']);
199
			$milieux = explode('|', $this->configProjet['milieux']);
198
			foreach ($milieux as $milieu) {
200
			foreach ($milieux as $milieu) {
199
				$details = explode(';', $milieu);
201
				$details = explode(';', $milieu);
200
				if (isset($details[1])) {
202
				if (isset($details[1])) {
201
					$infosMilieux[$details[0]] = $details[1];
203
					$infosMilieux[$details[0]] = $details[1];
202
				} else {
204
				} else {
203
					$infosMilieux[$details[0]] = '';
205
					$infosMilieux[$details[0]] = '';
204
				}
206
				}
205
			}
207
			}
206
			ksort($infosMilieux);
208
			ksort($infosMilieux);
207
		}
209
		}
208
		return $infosMilieux;
210
		return $infosMilieux;
209
	}
211
	}
210
 
212
 
211
	private function especeEstImposee() {
213
	private function especeEstImposee() {
212
		return (isset($_GET['num_nom']) && $_GET['num_nom'] != '');
214
		return (isset($_GET['num_nom']) && $_GET['num_nom'] != '');
213
	}
215
	}
214
 
216
 
215
	private function executerChargementInfosTaxon($num_nom) {
217
	private function executerChargementInfosTaxon($num_nom) {
216
		$url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $this->NS_PROJET, $num_nom);
218
		$url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $this->NS_PROJET, $num_nom);
217
		$infos = json_decode(file_get_contents($url_service_infos));
219
		$infos = json_decode(file_get_contents($url_service_infos));
218
		$resultat = array();
220
		$resultat = array();
219
		if (isset($infos) && !empty($infos)) {
221
		if (isset($infos) && !empty($infos)) {
220
			$infos = (array)$infos;
222
			$infos = (array)$infos;
221
			$resultat = (isset($infos['nom_sci']) && $infos['nom_sci'] != '') ? $infos : array();
223
			$resultat = (isset($infos['nom_sci']) && $infos['nom_sci'] != '') ? $infos : array();
222
		}
224
		}
223
		return $resultat;
225
		return $resultat;
224
	}
226
	}
225
 
227
 
226
	public function executerUploadImage() {
228
	public function executerUploadImage() {
227
		$retour = array(
229
		$retour = array(
228
				'squelette' => $this->projet.'_image',
230
				'squelette' => $this->projet.'_image',
229
				'squelette_ext' => '.tpl.xml',
231
				'squelette_ext' => '.tpl.xml',
230
				'mime' => 'text/xml',
232
				'mime' => 'text/xml',
231
				'donnees' => array(
233
				'donnees' => array(
232
						'urlMiniature' => '',
234
						'urlMiniature' => '',
233
						'imageNom' => '',
235
						'imageNom' => '',
234
						'message' => '',
236
						'message' => '',
235
						'debogage' => ''));
237
						'debogage' => ''));
236
		$message = '';
238
		$message = '';
237
		$debogage = '';
239
		$debogage = '';
238
		if ($_FILES['fichier']['error'] == UPLOAD_ERR_OK) {
240
		if ($_FILES['fichier']['error'] == UPLOAD_ERR_OK) {
239
			if (is_uploaded_file($_FILES['fichier']['tmp_name'])) {
241
			if (is_uploaded_file($_FILES['fichier']['tmp_name'])) {
240
				if ($this->verifierFormatJpeg($_FILES['fichier']['tmp_name'])) {
242
				if ($this->verifierFormatJpeg($_FILES['fichier']['tmp_name'])) {
241
					$dossierStockage = $this->config['chemins']['imagesTempDossier'];
243
					$dossierStockage = $this->config['chemins']['imagesTempDossier'];
242
 
244
 
243
					$nomFichierOriginal = preg_replace('/[.](jpeg|jpg)$/i', '.jpg', strtolower($_FILES['fichier']['name']));
245
					$nomFichierOriginal = preg_replace('/[.](jpeg|jpg)$/i', '.jpg', strtolower($_FILES['fichier']['name']));
244
					$originalChemin = $dossierStockage.$nomFichierOriginal;
246
					$originalChemin = $dossierStockage.$nomFichierOriginal;
245
					$deplacementOk = move_uploaded_file($_FILES['fichier']['tmp_name'], $originalChemin);
247
					$deplacementOk = move_uploaded_file($_FILES['fichier']['tmp_name'], $originalChemin);
246
 
248
 
247
					if ($deplacementOk === true) {
249
					if ($deplacementOk === true) {
248
						$miniatureFichier = str_replace('.jpg', '_min.jpg', $nomFichierOriginal);
250
						$miniatureFichier = str_replace('.jpg', '_min.jpg', $nomFichierOriginal);
249
						$miniatureChemin = $dossierStockage.$miniatureFichier;
251
						$miniatureChemin = $dossierStockage.$miniatureFichier;
250
 
252
 
251
						// Parametres
253
						// Parametres
252
						$largeurIdeale = 100;
254
						$largeurIdeale = 100;
253
						$hauteurIdeale = 100;
255
						$hauteurIdeale = 100;
254
						$qualite = 85;
256
						$qualite = 85;
255
 
257
 
256
						// Calcul de la hauteur et de la largeur optimale de la miniature
258
						// Calcul de la hauteur et de la largeur optimale de la miniature
257
						$taillesImgOriginale = getimagesize($originalChemin);
259
						$taillesImgOriginale = getimagesize($originalChemin);
258
						$largeurOrigine = $taillesImgOriginale[0];
260
						$largeurOrigine = $taillesImgOriginale[0];
259
						$hauteurOrigine = $taillesImgOriginale[1];
261
						$hauteurOrigine = $taillesImgOriginale[1];
260
 
262
 
261
						$largeurMin = $largeurIdeale;
263
						$largeurMin = $largeurIdeale;
262
						$hauteurMin = (int) ($hauteurOrigine * ($largeurIdeale / $largeurOrigine));
264
						$hauteurMin = (int) ($hauteurOrigine * ($largeurIdeale / $largeurOrigine));
263
						if ($hauteurMin > $hauteurIdeale) {
265
						if ($hauteurMin > $hauteurIdeale) {
264
							$hauteurMin = $hauteurIdeale;
266
							$hauteurMin = $hauteurIdeale;
265
							$largeurMin = (int)($largeurOrigine * ($hauteurMin / $hauteurOrigine));
267
							$largeurMin = (int)($largeurOrigine * ($hauteurMin / $hauteurOrigine));
266
						}
268
						}
267
 
269
 
268
						// Création de la miniature
270
						// Création de la miniature
269
						$imageOriginale = imagecreatefromjpeg($originalChemin);
271
						$imageOriginale = imagecreatefromjpeg($originalChemin);
270
						$imageMiniature = imagecreatetruecolor($largeurMin, $hauteurMin);
272
						$imageMiniature = imagecreatetruecolor($largeurMin, $hauteurMin);
271
						$couleurFond = imagecolorallocate($imageMiniature, 255, 255, 255);
273
						$couleurFond = imagecolorallocate($imageMiniature, 255, 255, 255);
272
						imagefill($imageMiniature, 0, 0, $couleurFond);
274
						imagefill($imageMiniature, 0, 0, $couleurFond);
273
						imagecopyresized($imageMiniature, $imageOriginale, 0, 0, 0, 0, $largeurMin, $hauteurMin, $largeurOrigine, $hauteurOrigine);
275
						imagecopyresized($imageMiniature, $imageOriginale, 0, 0, 0, 0, $largeurMin, $hauteurMin, $largeurOrigine, $hauteurOrigine);
274
						imagejpeg($imageMiniature, $miniatureChemin, $qualite);
276
						imagejpeg($imageMiniature, $miniatureChemin, $qualite);
275
						imagedestroy($imageMiniature);
277
						imagedestroy($imageMiniature);
276
						imagedestroy($imageOriginale);
278
						imagedestroy($imageOriginale);
277
 
279
 
278
						// Retour des infos
280
						// Retour des infos
279
						$retour['donnees']['urlMiniature'] = sprintf($this->config['chemins']['imagesTempUrlTpl'], $miniatureFichier);
281
						$retour['donnees']['urlMiniature'] = sprintf($this->config['chemins']['imagesTempUrlTpl'], $miniatureFichier);
280
						$retour['donnees']['imageNom'] = $nomFichierOriginal;
282
						$retour['donnees']['imageNom'] = $nomFichierOriginal;
281
					} else {
283
					} else {
282
						$message = "L'image n'a pu être déplacé sur le serveur.";
284
						$message = "L'image n'a pu être déplacé sur le serveur.";
283
					}
285
					}
284
				} else {
286
				} else {
285
					$message = "L'image n'est pas au format JPEG.";
287
					$message = "L'image n'est pas au format JPEG.";
286
				}
288
				}
287
			} else {
289
			} else {
288
				$message = "L'image n'a pu être téléversée.";
290
				$message = "L'image n'a pu être téléversée.";
289
				$debogage = $message.print_r($_FILES, true);
291
				$debogage = $message.print_r($_FILES, true);
290
			}
292
			}
291
		} else {
293
		} else {
292
			if ($_FILES['fichier']['error'] == UPLOAD_ERR_FORM_SIZE) {
294
			if ($_FILES['fichier']['error'] == UPLOAD_ERR_FORM_SIZE) {
293
				$message = "L'image téléversée excède la taille maximum autorisée.".
295
				$message = "L'image téléversée excède la taille maximum autorisée.".
294
						"Veuillez modifier votre image avant de la téléverser à nouveau.";
296
						"Veuillez modifier votre image avant de la téléverser à nouveau.";
295
			} else {
297
			} else {
296
				$message = "Une erreur de transfert a eu lieu (téléversement interrompu).";
298
				$message = "Une erreur de transfert a eu lieu (téléversement interrompu).";
297
			}
299
			}
298
			$debogage = "Code erreur : {$_FILES['fichier']['error']}. ".
300
			$debogage = "Code erreur : {$_FILES['fichier']['error']}. ".
299
					"Voir : http://php.net/manual/fr/features.file-upload.errors.php";
301
					"Voir : http://php.net/manual/fr/features.file-upload.errors.php";
300
		}
302
		}
301
		// Retour des infos
303
		// Retour des infos
302
		$retour['donnees']['message'] = $message;
304
		$retour['donnees']['message'] = $message;
303
		$retour['donnees']['debogage'] = $debogage;
305
		$retour['donnees']['debogage'] = $debogage;
304
		return $retour;
306
		return $retour;
305
	}
307
	}
306
 
308
 
307
	// Il ne faut pas utiliser l'index type du tableau files pour tester
309
	// Il ne faut pas utiliser l'index type du tableau files pour tester
308
	// si une image est en jpeg car le type renvoyé par les navigateurs
310
	// si une image est en jpeg car le type renvoyé par les navigateurs
309
	// peut varier (ex. sous ie qui renvoie image/pjpeg
311
	// peut varier (ex. sous ie qui renvoie image/pjpeg
310
	private function verifierFormatJpeg($chemin) {
312
	private function verifierFormatJpeg($chemin) {
311
		// get imagesize renvoie un résultat consistant par contre
313
		// get imagesize renvoie un résultat consistant par contre
312
		$infos = getimagesize($chemin, $infos);
314
		$infos = getimagesize($chemin, $infos);
313
		return (isset($infos['mime']) && $infos['mime'] == 'image/jpeg');
315
		return (isset($infos['mime']) && $infos['mime'] == 'image/jpeg');
314
	}
316
	}
315
 
317
 
316
	private function array2js($array,$show_keys) {
318
	private function array2js($array,$show_keys) {
317
		$tableauJs = '{}';
319
		$tableauJs = '{}';
318
		if (!empty($array)) {
320
		if (!empty($array)) {
319
			$total = count($array) - 1;
321
			$total = count($array) - 1;
320
			$i = 0;
322
			$i = 0;
321
			$dimensions = array();
323
			$dimensions = array();
322
			foreach ($array as $key => $value) {
324
			foreach ($array as $key => $value) {
323
				if (is_array($value)) {
325
				if (is_array($value)) {
324
					$dimensions[$i] = array2js($value,$show_keys);
326
					$dimensions[$i] = array2js($value,$show_keys);
325
					if ($show_keys) {
327
					if ($show_keys) {
326
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
328
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
327
					}
329
					}
328
				} else {
330
				} else {
329
					$dimensions[$i] = '"'.addslashes($value).'"';
331
					$dimensions[$i] = '"'.addslashes($value).'"';
330
					if ($show_keys) {
332
					if ($show_keys) {
331
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
333
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
332
					}
334
					}
333
				}
335
				}
334
				if ($i == 0) {
336
				if ($i == 0) {
335
					$dimensions[$i] = '{'.$dimensions[$i];
337
					$dimensions[$i] = '{'.$dimensions[$i];
336
				}
338
				}
337
				if ($i == $total) {
339
				if ($i == $total) {
338
					$dimensions[$i].= '}';
340
					$dimensions[$i].= '}';
339
				}
341
				}
340
				$i++;
342
				$i++;
341
			}
343
			}
342
			$tableauJs = implode(',', $dimensions);
344
			$tableauJs = implode(',', $dimensions);
343
		}
345
		}
344
		return $tableauJs;
346
		return $tableauJs;
345
	}
347
	}
346
}
348
}
347
?>
349
?>