Subversion Repositories eFlore/Applications.cel

Rev

Rev 917 | Rev 1345 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 917 Rev 1050
Line 17... Line 17...
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 = 'biodiversite34';
24
	const PROJET_DEFAUT = 'biodiversite34';
-
 
25
	const WS_SAISIE = "CelWidgetSaisie";
-
 
26
 
25
	private $projet = null;
27
	private $projet = null;
26
	private $configProjet = null;
28
	private $configProjet = null;
27
	 
29
 
28
	/**
30
	/**
29
	 * Méthode appelée par défaut pour charger ce widget.
31
	 * Méthode appelée par défaut pour charger ce widget.
30
	 */
32
	 */
31
	public function executer() {
33
	public function executer() {
32
		$retour = null;
34
		$retour = null;
Line 36... Line 38...
36
			$this->projet = self::PROJET_DEFAUT;
38
			$this->projet = self::PROJET_DEFAUT;
37
		} else {
39
		} else {
38
			$this->projet = $projet;
40
			$this->projet = $projet;
39
		}
41
		}
40
		$this->chargerConfigProjet();
42
		$this->chargerConfigProjet();
41
		
43
 
42
		$service = (isset($service)) ? $service : $this->projet;
44
		$service = (isset($service)) ? $service : $this->projet;
43
		$methode = $this->traiterNomMethodeExecuter($service);
45
		$methode = $this->traiterNomMethodeExecuter($service);
44
		if (method_exists($this, $methode)) {
46
		if (method_exists($this, $methode)) {
45
			$retour = $this->$methode();
47
			$retour = $this->$methode();
46
		} else {
48
		} else {
47
			$this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
49
			$this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
48
		}
50
		}
Line 49... Line 51...
49
 
51
 
50
		if (is_null($retour)) {
-
 
51
			$contenu = 'Un problème est survenu : '.print_r($this->messages, true);
52
		$contenu = null;
-
 
53
		$mime = null;
52
		} else {
54
		if (is_array($retour) && array_key_exists('squelette', $retour)) {
53
			$ext = (isset($retour['squelette_ext'])) ? $retour['squelette_ext'] : '.tpl.html';
55
			$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;
56
			$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet.self::DS.$retour['squelette'].$ext;
-
 
57
			$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
-
 
58
			$mime = isset($retour['mime']) ? $retour['mime'] : null;
-
 
59
		} else {
-
 
60
			if (count($this->messages) == 0) {
-
 
61
				$this->messages[] = "La méthode du sous-service ne renvoie pas les données dans le bon format.";
-
 
62
			}
55
			$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
63
			$contenu = 'Un problème est survenu : '.print_r($this->messages, true);
-
 
64
		}
56
		}
65
 
57
		$this->envoyer($contenu);
66
		$this->envoyer($contenu, $mime);
58
	}
67
	}
59
	
68
 
60
	private function chargerConfigProjet() {
69
	private function chargerConfigProjet() {
61
		$fichier_config = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'.ini';
70
		$fichier_config = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'.ini';
62
		if (file_exists($fichier_config)) {
71
		if (file_exists($fichier_config)) {
63
			if (!$this->configProjet	= parse_ini_file($fichier_config)) {
72
			if (!$this->configProjet	= parse_ini_file($fichier_config)) {
64
				$this->messages[] = "Le fichier ini '$fichier_config' du projet n'a pu être chargé.";
73
				$this->messages[] = "Le fichier ini '$fichier_config' du projet n'a pu être chargé.";
65
			} 
74
			}
66
		} else {
75
		} else {
67
			$this->messages[] = "Le fichier ini '$fichier_config' du projet n'existe pas.";
76
			$this->messages[] = "Le fichier ini '$fichier_config' du projet n'existe pas.";
68
		}
77
		}
69
	}
78
	}
70
	
-
 
71
	public function executerBiodiversite34() {
-
 
72
		$widget['squelette'] = $this->projet;
-
 
73
		$widget['donnees'] = array();
-
 
74
		$widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
-
 
75
		$widget['donnees']['taxons'] = $this->recupererListeTaxonBiodiversite34();
-
 
76
		$widget['donnees']['milieux'] = $this->parserMilieuxBiodiversite34();
-
 
77
		return  $widget;
-
 
78
	}
79
 
79
	public function executerTaxons() {
80
	public function executerTaxons() {
80
		$widget['squelette'] = $this->projet.'_taxons';
81
		$widget['squelette'] = $this->projet.'_taxons';
81
		$widget['squelette_ext'] = '.tpl.js';
82
		$widget['squelette_ext'] = '.tpl.js';
82
		$widget['donnees'] = array();
83
		$widget['donnees'] = array();
Line 87... Line 88...
87
			$taxons_tries[$taxon['num_nom_sel']] = $taxon;
88
			$taxons_tries[$taxon['num_nom_sel']] = $taxon;
88
		}
89
		}
89
		$widget['donnees']['taxons'] = json_encode($taxons_tries);
90
		$widget['donnees']['taxons'] = json_encode($taxons_tries);
90
		return  $widget;
91
		return  $widget;
91
	}
92
	}
-
 
93
 
-
 
94
	public function executerUploadImage() {
-
 
95
		$retour = array();
-
 
96
		if ($_FILES['fichier']['error'] == 0) {
-
 
97
			if (is_uploaded_file($_FILES['fichier']['tmp_name'])) {
-
 
98
				if ($_FILES['fichier']['type'] == 'image/jpeg') {
-
 
99
					$dossierStockage = $this->config['chemins']['imagesTempDossier'];
-
 
100
 
-
 
101
					$nomFichierOriginal = preg_replace('/[.](jpeg|jpg)$/i', '.jpg', strtolower($_FILES['fichier']['name']));
-
 
102
					$originalChemin = $dossierStockage.$nomFichierOriginal;
-
 
103
					$deplacementOk = move_uploaded_file($_FILES['fichier']['tmp_name'], $originalChemin);
-
 
104
 
-
 
105
					if ($deplacementOk === true) {
-
 
106
						$miniatureFichier = str_replace('.jpg', '_min.jpg', $nomFichierOriginal);
-
 
107
						$miniatureChemin = $dossierStockage.$miniatureFichier;
-
 
108
 
-
 
109
						// Parametres
-
 
110
						$largeurIdeale = 100;
-
 
111
						$hauteurIdeale = 100;
-
 
112
						$qualite = 85;
-
 
113
 
-
 
114
						// Calcul de la hauteur et de la largeur optimale de la miniature
-
 
115
						$taillesImgOriginale = getimagesize($originalChemin);
-
 
116
						$largeurOrigine = $taillesImgOriginale[0];
-
 
117
						$hauteurOrigine = $taillesImgOriginale[1];
-
 
118
 
-
 
119
						$largeurMin = $largeurIdeale;
-
 
120
						$hauteurMin = (int) ($hauteurOrigine * ($largeurIdeale / $largeurOrigine));
-
 
121
						if ($hauteurMin > $hauteurIdeale) {
-
 
122
							$hauteurMin = $hauteurIdeale;
-
 
123
							$largeurMin = (int)($largeurOrigine * ($hauteurMin / $hauteurOrigine));
-
 
124
						}
-
 
125
 
-
 
126
						// Création de la miniature
-
 
127
						$imageOriginale = imagecreatefromjpeg($originalChemin);
-
 
128
						$imageMiniature = imagecreatetruecolor($largeurMin, $hauteurMin);
-
 
129
						$couleurFond = imagecolorallocate($imageMiniature, 255, 255, 255);
-
 
130
						imagefill($imageMiniature, 0, 0, $couleurFond);
-
 
131
						imagecopyresized($imageMiniature, $imageOriginale, 0, 0, 0, 0, $largeurMin, $hauteurMin, $largeurOrigine, $hauteurOrigine);
-
 
132
						imagejpeg($imageMiniature, $miniatureChemin, $qualite);
-
 
133
						imagedestroy($imageMiniature);
-
 
134
						imagedestroy($imageOriginale);
-
 
135
 
-
 
136
						// Retour des infos
-
 
137
						$retour['squelette'] = $this->projet.'_image';
-
 
138
						$retour['squelette_ext'] = '.tpl.xml';
-
 
139
						$retour['donnees']['urlMiniature'] = sprintf($this->config['chemins']['imagesTempUrlTpl'], $miniatureFichier);
-
 
140
						$retour['donnees']['imageNom'] = $nomFichierOriginal;
-
 
141
						$retour['mime'] = 'text/xml';
-
 
142
					} else {
-
 
143
						$this->messages[] = "L'image n'a pu être déplacé sur le serveur.";
-
 
144
					}
-
 
145
				} else {
-
 
146
					$this->messages[] = "L'image n'est pas au format JPEG.";
-
 
147
				}
-
 
148
			} else {
-
 
149
				$this->messages[] = "L'image n'a pu être uploadée :".print_r($_FILES, true);
-
 
150
			}
-
 
151
		} else {
-
 
152
			$this->messages[] = "Une erreur de transfert a eu lieu (téléversement interrompu). ".
-
 
153
				"Code erreur : {$_FILES['fichier']['error']}. ".
-
 
154
				"Voir : http://php.net/manual/fr/features.file-upload.errors.php";
-
 
155
		}
-
 
156
		return $retour;
-
 
157
	}
-
 
158
 
-
 
159
	public function executerBiodiversite34() {
-
 
160
		$widget['squelette'] = $this->projet;
-
 
161
		$widget['donnees'] = array();
-
 
162
		$widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
-
 
163
		$widget['donnees']['url_ws_saisie'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_SAISIE);
-
 
164
 
-
 
165
		$widget['donnees']['taxons'] = $this->recupererListeTaxonBiodiversite34();
-
 
166
		$widget['donnees']['milieux'] = $this->parserMilieuxBiodiversite34();
-
 
167
		return  $widget;
-
 
168
	}
92
	
169
 
93
	private function parserMilieuxBiodiversite34() {
170
	private function parserMilieuxBiodiversite34() {
94
		$infosMilieux = array();
171
		$infosMilieux = array();
95
		$milieux = explode('|', $this->configProjet['milieux']);
172
		$milieux = explode('|', $this->configProjet['milieux']);
96
		foreach ($milieux as $milieu) {
173
		foreach ($milieux as $milieu) {
97
			$details = explode(';', $milieu);
174
			$details = explode(';', $milieu);
Line 102... Line 179...
102
			}
179
			}
103
		}
180
		}
104
		ksort($infosMilieux);
181
		ksort($infosMilieux);
105
		return $infosMilieux;
182
		return $infosMilieux;
106
	}
183
	}
107
	
184
 
108
	private function recupererListeTaxonBiodiversite34() {
185
	private function recupererListeTaxonBiodiversite34() {
109
		$taxons = null;
186
		$taxons = null;
110
		$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.'biodiversite34_taxons.tsv';
187
		$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.'biodiversite34_taxons.tsv';
111
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
188
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
112
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
189
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
113
		} else {
190
		} else {
114
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
191
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
115
		}
192
		}
116
		
193
 
117
		$taxons_tries = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
194
		$taxons_tries = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
118
		
195
 
119
		return $taxons_tries;
196
		return $taxons_tries;
120
	}
197
	}
121
	
198
 
-
 
199
	public function executerSauvages() {
-
 
200
		$widget['squelette'] = $this->projet;
-
 
201
		$widget['donnees'] = array();
-
 
202
		$widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
-
 
203
		$widget['donnees']['url_ws_saisie'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_SAISIE);
-
 
204
 
-
 
205
		$widget['donnees']['taxons'] = $this->recupererListeTaxonSauvages();
-
 
206
		$widget['donnees']['milieux'] = $this->parserMilieuxSauvages();
-
 
207
		return  $widget;
-
 
208
	}
-
 
209
 
-
 
210
	private function parserMilieuxSauvages() {
-
 
211
		$infosMilieux = array();
-
 
212
		$milieux = explode('|', $this->configProjet['milieux']);
-
 
213
		foreach ($milieux as $milieu) {
-
 
214
			$details = explode(';', $milieu);
-
 
215
			if (isset($details[1])) {
-
 
216
				$infosMilieux[$details[0]] = $details[1];
-
 
217
			} else {
-
 
218
				$infosMilieux[$details[0]] = '';
-
 
219
			}
-
 
220
		}
-
 
221
		return $infosMilieux;
-
 
222
	}
-
 
223
 
-
 
224
	private function recupererListeTaxonSauvages() {
-
 
225
		$taxons = null;
-
 
226
		$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.'sauvages_taxons.tsv';
-
 
227
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
-
 
228
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
-
 
229
		} else {
-
 
230
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
-
 
231
		}
-
 
232
		return $taxons;
-
 
233
	}
-
 
234
 
122
	private function decomposerFichierTsv($fichier, $delimiter = "\t"){
235
	private function decomposerFichierTsv($fichier, $delimiter = "\t"){
123
		$header = NULL;
236
		$header = NULL;
124
	    $data = array();
237
	    $data = array();
125
	    if (($handle = fopen($fichier, 'r')) !== FALSE) {
238
	    if (($handle = fopen($fichier, 'r')) !== FALSE) {
126
	        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
239
	        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
127
	            if (!$header) {
240
	            if (!$header) {
128
	                $header = $row;
241
	                $header = $row;
129
	            } else {  
242
	            } else {
130
	                $data[] = array_combine($header, $row);
243
	                $data[] = array_combine($header, $row);
131
	            }
244
	            }
132
	        }
245
	        }
133
	        fclose($handle);
246
	        fclose($handle);
134
	    }
247
	    }
135
	    return $data; 
248
	    return $data;
136
	}
249
	}
137
}
250
}
138
251