Subversion Repositories eFlore/Applications.cel

Rev

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

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