Subversion Repositories eFlore/Applications.cel

Rev

Rev 1617 | Rev 1631 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1374 aurelien 1
<?php
2
/**
3
* Service fournissant des informations concernant le CEL au format RSS1, RSS2 ou ATOM.
4
* Encodage en entrée : utf8
5
* Encodage en sortie : utf8
6
* Format du service :
7
* /CelWidgetExport/format
8
* /CelWidgetExport/csv
9
*
10
* Les paramêtres :
11
*  - "start" indique le numéro du premier item à afficher
12
*  - "limit" nombre d'items à afficher
13
*
14
* @author Aurélien Peronnet <aurelien@tela-botanica.org>
15
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
16
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
17
* @version $Id$
18
* @copyright 2012
19
*/
1610 raphael 20
 
21
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(dirname(realpath(__FILE__))) . '/lib');
22
// la sortie est binaire (xls), mais OLE n'est pas compatible E_ALL en PHP-5.4
1617 aurelien 23
//error_reporting(error_reporting() ^ E_STRICT);
1610 raphael 24
require_once("lib/OLE.php");
25
require_once("lib/Spreadsheet/Excel/Writer.php");
26
 
1374 aurelien 27
class CelWidgetExport extends Cel {
28
 
1408 aurelien 29
	private $nom_fichier_export = 'cel_export';
1617 aurelien 30
	private $champs_a_exclure = array(
31
		'ce_utilisateur' => true,
32
		'courriel_utilisateur' => true,
33
		'transmission' => true,
34
		'code_insee_calcule' => true);
1374 aurelien 35
	private $correspondance_champs = array(
36
		'id_observation' => 'Identifiant Observation',
37
		'ordre' => 'Ordre Observation',
38
		'prenom_utilisateur' => 'Prénom',
39
		'nom_utilisateur' => 'Nom',
40
		'nom_sel' => 'Nom Sélectionné',
41
		'nom_sel_nn' => 'Numéro Nomenclatural Nom Selectionné',
42
		'nom_ret' => 'Nom Retenu',
43
		'nom_ret_nn' => 'Numéro Nomenclatural Nom Retenu',
44
		'nt' => 'Numéro Taxonomique',
45
		'famille' => 'Famille',
46
		'nom_referentiel' => 'Référentiel Taxonomique',
47
		'ce_zone_geo' => 'Code Insee',
48
		'zone_geo' => 'Commune',
49
		'lieudit' => 'Lieu-Dit',
50
		'station' => 'Station',
51
		'milieu' => 'Milieu',
52
		'latitude' => 'Latitude',
53
		'longitude' => 'Longitude',
1617 aurelien 54
		'altitude' => 'Altitude',
1374 aurelien 55
		'geodatum' => 'Référentiel Géographique',
56
		'date_observation' => 'Date Observation',
57
		'mots_cles_texte' => 'Mots Clés',
58
		'commentaire' => 'Commentaires',
59
		'date_creation' => 'Date Création',
60
		'date_modification' => 'Date Modification',
1617 aurelien 61
		'date_transmission' => 'Date Transmission',
62
		'abondance' => 'Abondance',
63
		'certitude' => 'Certitude',
64
		'phenologie' => 'Phénologie'
1374 aurelien 65
		);
1376 aurelien 66
	private $parametres_autorises = array(
67
		'utilisateur' => 'courriel_utilisateur',
68
		'commune' => 'zone_geo',
69
		'dept' => 'departement',
70
		'projet' => 'mots_cles',
1379 aurelien 71
		'num_taxon' => 'nt',
1378 aurelien 72
		'date_debut' => 'date_debut',
1387 aurelien 73
		'date_fin' => 'date_fin',
1625 aurelien 74
		'taxon' => 'taxon',
75
		'debut' => 'debut',
76
		'limite' => 'limite',
77
		'format' => 'format'
1376 aurelien 78
	);
1402 aurelien 79
 
1625 aurelien 80
	private $limite_decoupage_defaut = 9000;
81
 
1402 aurelien 82
	private $format = 'csv';
1579 aurelien 83
 
84
	public function getRessource() {
85
		return $this->getElement(array());
86
	}
1374 aurelien 87
 
88
	/**
89
	 * Méthode appelée avec une requête de type GET.
90
	 */
1625 aurelien 91
	public function getElement($params = array()) {
92
		if(count($params) > 0) {
93
			switch(strtolower($params[0])) {
94
				case 'calcul':
95
					$this->getCalcul();
96
				break;
97
 
98
				case 'export':
99
					$this->getExport();
100
				break;
101
 
102
				default:
103
					$this->getExport();
104
			}
105
		} else {
106
			$this->getExport();
107
		}
108
	}
109
 
110
	private function getCalcul() {
1611 raphael 111
		$criteres = $this->traiterParametresAutorises($_GET);
1374 aurelien 112
		// Seulement les observation publiques
113
		$criteres['transmission'] = 1;
114
		$chercheur_observations = new RechercheObservation($this->config);
115
 
1379 aurelien 116
		$numero_page = isset($criteres['debut']) ? $criteres['debut'] : 0;
117
		$limite = isset($criteres['limite']) ? $criteres['limite'] : 0;
1374 aurelien 118
 
119
		unset($criteres['limite']);
120
		unset($criteres['debut']);
1625 aurelien 121
		unset($criteres['format']);
122
 
123
		$nb_observations = $chercheur_observations->compterObservations(null, $criteres);
124
		$limite_decoupage = $this->calculerNbLignesMaxParFichier();
125
 
126
		$url_telechargements = array();
127
		$intervalle = 0;
128
 
129
		$params_url = $criteres;
130
		unset($params_url['transmission']);
131
		do {
132
			$base_url = $this->config['settings']['baseURLAbsolu'].'CelWidgetExport/export';
133
			$params_url['debut'] = $intervalle;
134
			$params_url['limite'] = $limite_decoupage;
135
			$url_telechargement_fichier = $base_url;
136
			$url_telechargements[] = $base_url.'?'.http_build_query($params_url).'&format='.$this->format;
137
			$intervalle += $limite_decoupage;
138
			$nb_observations -= $limite_decoupage;
139
		} while($nb_observations >= $limite_decoupage);
140
 
141
		$this->envoyerJson($url_telechargements);
142
	}
1374 aurelien 143
 
1625 aurelien 144
	private function calculerNbLignesMaxParFichier() {
145
		$limite = $this->limite_decoupage_defaut;
1402 aurelien 146
		switch($this->format) {
147
			case 'csv':
1625 aurelien 148
				$limite = 20000;
149
				break;
150
			case 'xls':
151
				$limite = 8000;
152
				break;
153
		}
154
		return $limite;
155
	}
156
 
157
	private function getExport() {
158
		$criteres = $this->traiterParametresAutorises($_GET);
159
 
160
		// Seulement les observation publiques
161
		$criteres['transmission'] = 1;
162
		$chercheur_observations = new RechercheObservation($this->config);
163
 
164
		$debut = isset($criteres['debut']) ? $criteres['debut'] : 0;
165
		$limite = isset($criteres['limite']) ? $criteres['limite'] : 0;
166
 
167
		unset($criteres['limite']);
168
		unset($criteres['debut']);
169
		unset($criteres['format']);
170
 
171
		$observations = $chercheur_observations->rechercherObservations(null, $criteres, $debut, $limite);
172
		switch($this->format) {
173
			case 'csv':
1402 aurelien 174
				$csv = $this->convertirEnCsv($observations);
175
				$this->envoyerCsv($csv);
1625 aurelien 176
				break;
1402 aurelien 177
			case 'xls':
178
				$xls = $this->convertirEnXls($observations);
179
				$this->envoyerXls($xls);
1625 aurelien 180
				break;
1402 aurelien 181
			default:
182
		}
1374 aurelien 183
	}
184
 
1611 raphael 185
	protected function traiterParametresAutorises(Array $parametres) {
1376 aurelien 186
		$parametres_traites = array();
1402 aurelien 187
		$this->format = (isset($parametres['format']) && $parametres['format'] != '') ? $parametres['format'] : $this->format;
1376 aurelien 188
		foreach($parametres as $cle => $valeur) {
189
			if(trim($valeur) != '' && isset($this->parametres_autorises[$cle])) {
190
				$parametres_traites[$this->parametres_autorises[$cle]] = $valeur;
191
			}
192
		}
193
		return $parametres_traites;
194
	}
195
 
1374 aurelien 196
	private function envoyerCsv($csv) {
197
		header('Content-Type: text/csv; charset=UTF-8');
1408 aurelien 198
		header('Content-Disposition: attachment;filename='.$this->nom_fichier_export.'.csv');
1374 aurelien 199
		echo $csv;
1376 aurelien 200
		exit;
1374 aurelien 201
	}
202
 
1402 aurelien 203
	private function envoyerXls($workbook) {
204
		$workbook->close();
205
		exit;
206
	}
207
 
1374 aurelien 208
	private function convertirEnCsv($data)
209
	{
210
		$chemin_temp = "php://temp";
211
		$outstream = fopen($chemin_temp, 'r+');
212
		$intitule_champs = array();
1617 aurelien 213
		foreach($data as &$ligne) {
1429 aurelien 214
			$ligne = $this->filtrerDonneesSensibles($ligne);
1374 aurelien 215
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
216
			if(empty($intitule_champs)) {
217
				$intitule_champs = $this->creerEntetesChamps($ligne);
218
				fputcsv($outstream, $intitule_champs, ',', '"');
219
			}
220
			fputcsv($outstream, $ligne, ',', '"');
221
		}
222
		rewind($outstream);
223
		$csv = stream_get_contents($outstream);
224
		fclose($outstream);
225
		return $csv;
226
	}
227
 
1402 aurelien 228
	private function convertirEnXls($data) {
229
		$this->extendSpreadsheetProductor = new SpreadsheetProductor();
230
		$this->extendSpreadsheetProductor->initSpreadsheet();
231
 
232
		$workbook = new Spreadsheet_Excel_Writer();
1625 aurelien 233
		$worksheet = $workbook->addWorksheet('Liste');
234
		$workbook->setTempDir($this->config['cel']['chemin_stockage_temp']);
1612 raphael 235
		$workbook->setVersion(8);
1402 aurelien 236
 
1612 raphael 237
		$worksheet->setInputEncoding('utf-8');
1408 aurelien 238
		$workbook->send($this->nom_fichier_export.'.xls');
1402 aurelien 239
 
240
		$nb_lignes = 1;
1617 aurelien 241
 
242
		foreach($data as &$ligne) {
1429 aurelien 243
			$ligne = $this->filtrerDonneesSensibles($ligne);
1402 aurelien 244
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
245
			if(empty($intitule_champs)) {
246
				$intitule_champs = $this->creerEntetesChamps($ligne);
247
				$indice = 0;
1617 aurelien 248
				foreach ($intitule_champs as &$intitule) {
1465 aurelien 249
					$colonne = $intitule_champs[$indice];
250
					$worksheet->write(0,$indice,$colonne);
1402 aurelien 251
					$indice++;
252
				}
253
			}
254
			$indice = 0;
1617 aurelien 255
			foreach($ligne as &$champ) {
1402 aurelien 256
				$worksheet->write($nb_lignes,$indice,$champ);
257
				$indice++;
258
			}
259
			$nb_lignes++;
260
		}
261
		return $workbook;
262
	}
263
 
1374 aurelien 264
	private function creerEntetesChamps($noms_colonnes) {
265
		$champs_presents = array_intersect_key($this->correspondance_champs, $noms_colonnes);
266
		return array_values($champs_presents);
267
	}
1401 aurelien 268
 
1429 aurelien 269
	private function filtrerDonneesSensibles($ligne) {
270
		if(stripos($ligne['mots_cles_texte'], 'sensible') !== false) {
271
			$ligne['latitude'] = '';
272
			$ligne['longitude'] = '';
273
		}
274
		return $ligne;
275
	}
276
 
1401 aurelien 277
	private function nettoyerChaine($chaine) {
278
		$chaine = str_replace("\n",' ',$chaine);
279
		$chaine = str_replace("\t",'',$chaine);
280
		return $chaine;
281
	}
1374 aurelien 282
}
283
?>