Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 1625 Rev 1631
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',
75
		'debut' => 'debut',
76
		'limite' => 'limite',
76
		'limite' => 'limite',
77
		'format' => 'format'
77
		'format' => 'format'
78
	);
78
	);
79
	
79
	
80
	private $limite_decoupage_defaut = 9000; 
80
	private $limite_decoupage_defaut = 9000; 
81
	
81
	
82
	private $format = 'csv';
82
	private $format = 'csv';
83
	
83
	
84
	public function getRessource() {
84
	public function getRessource() {
85
		return $this->getElement(array());
85
		return $this->getElement(array());
86
	}
86
	}
87
		
87
		
88
	/**
88
	/**
89
	 * Méthode appelée avec une requête de type GET.
89
	 * Méthode appelée avec une requête de type GET.
90
	 */
90
	 */
91
	public function getElement($params = array()) {		
91
	public function getElement($params = array()) {		
92
		if(count($params) > 0) {
92
		if(count($params) > 0) {
93
			switch(strtolower($params[0])) {
93
			switch(strtolower($params[0])) {
94
				case 'calcul':
94
				case 'calcul':
95
					$this->getCalcul();
95
					$this->getCalcul();
96
				break;
96
				break;
97
				
97
				
98
				case 'export':
98
				case 'export':
99
					$this->getExport();
99
					$this->getExport();
100
				break;
100
				break;
101
				
101
				
102
				default:
102
				default:
103
					$this->getExport();
103
					$this->getExport();
104
			}
104
			}
105
		} else {
105
		} else {
106
			$this->getExport();
106
			$this->getExport();
107
		}
107
		}
108
	}
108
	}
109
	
109
	
110
	private function getCalcul() {
110
	private function getCalcul() {
111
		$criteres = $this->traiterParametresAutorises($_GET);
111
		$criteres = $this->traiterParametresAutorises($_GET);
112
		// Seulement les observation publiques
112
		// Seulement les observation publiques
113
		$criteres['transmission'] = 1;
113
		$criteres['transmission'] = 1;
114
		$chercheur_observations = new RechercheObservation($this->config);
114
		$chercheur_observations = new RechercheObservation($this->config);
115
		
115
		
116
		$numero_page = isset($criteres['debut']) ? $criteres['debut'] : 0;
116
		$numero_page = isset($criteres['debut']) ? $criteres['debut'] : 0;
117
		$limite = isset($criteres['limite']) ? $criteres['limite'] : 0;
117
		$limite = isset($criteres['limite']) ? $criteres['limite'] : 0;
118
		
118
		
119
		unset($criteres['limite']);
119
		unset($criteres['limite']);
120
		unset($criteres['debut']);
120
		unset($criteres['debut']);
121
		unset($criteres['format']);
121
		unset($criteres['format']);
122
		
122
		
123
		$nb_observations = $chercheur_observations->compterObservations(null, $criteres);
123
		$nb_observations = $chercheur_observations->compterObservations(null, $criteres);
124
		$limite_decoupage = $this->calculerNbLignesMaxParFichier();
124
		$limite_decoupage = $this->calculerNbLignesMaxParFichier();
125
		
125
		
126
		$url_telechargements = array();
126
		$url_telechargements = array();
127
		$intervalle = 0;
127
		$intervalle = 0;
128
		
128
		
129
		$params_url = $criteres;
129
		$params_url = $criteres;
130
		unset($params_url['transmission']);
130
		unset($params_url['transmission']);
131
		do {
131
		do {
132
			$base_url = $this->config['settings']['baseURLAbsolu'].'CelWidgetExport/export';
132
			$base_url = $this->config['settings']['baseURLAbsolu'].'CelWidgetExport/export';
133
			$params_url['debut'] = $intervalle;
133
			$params_url['debut'] = $intervalle;
134
			$params_url['limite'] = $limite_decoupage;
134
			$params_url['limite'] = $limite_decoupage;
135
			$url_telechargement_fichier = $base_url;
135
			$url_telechargement_fichier = $base_url;
136
			$url_telechargements[] = $base_url.'?'.http_build_query($params_url).'&format='.$this->format;
136
			$url_telechargements[] = $base_url.'?'.http_build_query($params_url).'&format='.$this->format;
137
			$intervalle += $limite_decoupage;
137
			$intervalle += $limite_decoupage;
138
			$nb_observations -= $limite_decoupage;
138
			$nb_observations -= $limite_decoupage;
139
		} while($nb_observations >= $limite_decoupage);
139
		} while($nb_observations >= $limite_decoupage);
140
		
140
		
141
		$this->envoyerJson($url_telechargements);
141
		$this->envoyerJson($url_telechargements);
142
	}
142
	}
143
	
143
	
144
	private function calculerNbLignesMaxParFichier() {
144
	private function calculerNbLignesMaxParFichier() {
145
		$limite = $this->limite_decoupage_defaut;
145
		$limite = $this->limite_decoupage_defaut;
146
		switch($this->format) {
146
		switch($this->format) {
147
			case 'csv':
147
			case 'csv':
148
				$limite = 20000;
148
				$limite = 20000;
149
				break;
149
				break;
150
			case 'xls':
150
			case 'xls':
151
				$limite = 8000;
151
				$limite = 8000;
152
				break;
152
				break;
153
		}
153
		}
154
		return $limite;
154
		return $limite;
155
	}
155
	}
156
	
156
	
157
	private function getExport() {
157
	private function getExport() {
158
		$criteres = $this->traiterParametresAutorises($_GET);
158
		$criteres = $this->traiterParametresAutorises($_GET);
159
 
159
 
160
		// Seulement les observation publiques
160
		// Seulement les observation publiques
161
		$criteres['transmission'] = 1;
161
		$criteres['transmission'] = 1;
162
		$chercheur_observations = new RechercheObservation($this->config);
162
		$chercheur_observations = new RechercheObservation($this->config);
163
		
163
		
164
		$debut = isset($criteres['debut']) ? $criteres['debut'] : 0;
164
		$debut = isset($criteres['debut']) ? $criteres['debut'] : 0;
165
		$limite = isset($criteres['limite']) ? $criteres['limite'] : 0;
165
		$limite = isset($criteres['limite']) ? $criteres['limite'] : 0;
166
		
166
		
167
		unset($criteres['limite']);
167
		unset($criteres['limite']);
168
		unset($criteres['debut']);
168
		unset($criteres['debut']);
169
		unset($criteres['format']);
169
		unset($criteres['format']);
170
		
170
		
171
		$observations = $chercheur_observations->rechercherObservations(null, $criteres, $debut, $limite);
171
		$observations = $chercheur_observations->rechercherObservations(null, $criteres, $debut, $limite)->get();
172
		switch($this->format) {
172
		switch($this->format) {
173
			case 'csv':
173
			case 'csv':
174
				$csv = $this->convertirEnCsv($observations);
174
				$csv = $this->convertirEnCsv($observations);
175
				$this->envoyerCsv($csv);
175
				$this->envoyerCsv($csv);
176
				break;
176
				break;
177
			case 'xls':
177
			case 'xls':
178
				$xls = $this->convertirEnXls($observations);
178
				$xls = $this->convertirEnXls($observations);
179
				$this->envoyerXls($xls);
179
				$this->envoyerXls($xls);
180
				break;
180
				break;
181
			default:
181
			default:
182
		}
182
		}
183
	}
183
	}
184
	
184
	
185
	protected function traiterParametresAutorises(Array $parametres) {
185
	protected function traiterParametresAutorises(Array $parametres) {
186
		$parametres_traites = array();
186
		$parametres_traites = array();
187
		$this->format = (isset($parametres['format']) && $parametres['format'] != '') ? $parametres['format'] : $this->format;
187
		$this->format = (isset($parametres['format']) && $parametres['format'] != '') ? $parametres['format'] : $this->format;
188
		foreach($parametres as $cle => $valeur) {
188
		foreach($parametres as $cle => $valeur) {
189
			if(trim($valeur) != '' && isset($this->parametres_autorises[$cle])) {
189
			if(trim($valeur) != '' && isset($this->parametres_autorises[$cle])) {
190
				$parametres_traites[$this->parametres_autorises[$cle]] = $valeur;
190
				$parametres_traites[$this->parametres_autorises[$cle]] = $valeur;
191
			}
191
			}
192
		}
192
		}
193
		return $parametres_traites;
193
		return $parametres_traites;
194
	}
194
	}
195
	
195
	
196
	private function envoyerCsv($csv) {
196
	private function envoyerCsv($csv) {
197
		header('Content-Type: text/csv; charset=UTF-8');
197
		header('Content-Type: text/csv; charset=UTF-8');
198
		header('Content-Disposition: attachment;filename='.$this->nom_fichier_export.'.csv');
198
		header('Content-Disposition: attachment;filename='.$this->nom_fichier_export.'.csv');
199
		echo $csv;
199
		echo $csv;
200
		exit;
200
		exit;
201
	}
201
	}
202
	
202
	
203
	private function envoyerXls($workbook) {
203
	private function envoyerXls($workbook) {
204
		$workbook->close();
204
		$workbook->close();
205
		exit;
205
		exit;
206
	}
206
	}
207
	
207
	
208
	private function convertirEnCsv($data)
208
	private function convertirEnCsv($data)
209
	{
209
	{
210
		$chemin_temp = "php://temp";
210
		$chemin_temp = "php://temp";
211
		$outstream = fopen($chemin_temp, 'r+');
211
		$outstream = fopen($chemin_temp, 'r+');
212
		$intitule_champs = array();
212
		$intitule_champs = array();
213
		foreach($data as &$ligne) {
213
		foreach($data as &$ligne) {
214
			$ligne = $this->filtrerDonneesSensibles($ligne);
214
			$ligne = $this->filtrerDonneesSensibles($ligne);
215
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
215
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
216
			if(empty($intitule_champs)) {
216
			if(empty($intitule_champs)) {
217
				$intitule_champs = $this->creerEntetesChamps($ligne);
217
				$intitule_champs = $this->creerEntetesChamps($ligne);
218
				fputcsv($outstream, $intitule_champs, ',', '"');			
218
				fputcsv($outstream, $intitule_champs, ',', '"');			
219
			}
219
			}
220
			fputcsv($outstream, $ligne, ',', '"');
220
			fputcsv($outstream, $ligne, ',', '"');
221
		}
221
		}
222
		rewind($outstream);
222
		rewind($outstream);
223
		$csv = stream_get_contents($outstream);
223
		$csv = stream_get_contents($outstream);
224
		fclose($outstream);
224
		fclose($outstream);
225
		return $csv;
225
		return $csv;
226
	}
226
	}
227
	
227
	
228
	private function convertirEnXls($data) {		
228
	private function convertirEnXls($data) {		
229
		$this->extendSpreadsheetProductor = new SpreadsheetProductor();
229
		$this->extendSpreadsheetProductor = new SpreadsheetProductor();
230
		$this->extendSpreadsheetProductor->initSpreadsheet();
230
		$this->extendSpreadsheetProductor->initSpreadsheet();
231
		
231
		
232
		$workbook = new Spreadsheet_Excel_Writer();
232
		$workbook = new Spreadsheet_Excel_Writer();
233
		$worksheet = $workbook->addWorksheet('Liste');
233
		$worksheet = $workbook->addWorksheet('Liste');
234
		$workbook->setTempDir($this->config['cel']['chemin_stockage_temp']);
234
		$workbook->setTempDir($this->config['cel']['chemin_stockage_temp']);
235
		$workbook->setVersion(8);
235
		$workbook->setVersion(8);
236
 
236
 
237
		$worksheet->setInputEncoding('utf-8');
237
		$worksheet->setInputEncoding('utf-8');
238
		$workbook->send($this->nom_fichier_export.'.xls');
238
		$workbook->send($this->nom_fichier_export.'.xls');
239
		
239
		
240
		$nb_lignes = 1;
240
		$nb_lignes = 1;
241
				
241
				
242
		foreach($data as &$ligne) {
242
		foreach($data as &$ligne) {
243
			$ligne = $this->filtrerDonneesSensibles($ligne);
243
			$ligne = $this->filtrerDonneesSensibles($ligne);
244
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
244
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
245
			if(empty($intitule_champs)) {
245
			if(empty($intitule_champs)) {
246
				$intitule_champs = $this->creerEntetesChamps($ligne);
246
				$intitule_champs = $this->creerEntetesChamps($ligne);
247
				$indice = 0;
247
				$indice = 0;
248
				foreach ($intitule_champs as &$intitule) {	
248
				foreach ($intitule_champs as &$intitule) {	
249
					$colonne = $intitule_champs[$indice];
249
					$colonne = $intitule_champs[$indice];
250
					$worksheet->write(0,$indice,$colonne);
250
					$worksheet->write(0,$indice,$colonne);
251
					$indice++;
251
					$indice++;
252
				}
252
				}
253
			}
253
			}
254
			$indice = 0;
254
			$indice = 0;
255
			foreach($ligne as &$champ) {
255
			foreach($ligne as &$champ) {
256
				$worksheet->write($nb_lignes,$indice,$champ);
256
				$worksheet->write($nb_lignes,$indice,$champ);
257
				$indice++;
257
				$indice++;
258
			}
258
			}
259
			$nb_lignes++;
259
			$nb_lignes++;
260
		}
260
		}
261
		return $workbook;
261
		return $workbook;
262
	}
262
	}
263
	
263
	
264
	private function creerEntetesChamps($noms_colonnes) {
264
	private function creerEntetesChamps($noms_colonnes) {
265
		$champs_presents = array_intersect_key($this->correspondance_champs, $noms_colonnes);
265
		$champs_presents = array_intersect_key($this->correspondance_champs, $noms_colonnes);
266
		return array_values($champs_presents);
266
		return array_values($champs_presents);
267
	}
267
	}
268
	
268
	
269
	private function filtrerDonneesSensibles($ligne) {
269
	private function filtrerDonneesSensibles($ligne) {
270
		if(stripos($ligne['mots_cles_texte'], 'sensible') !== false) {
270
		if(stripos($ligne['mots_cles_texte'], 'sensible') !== false) {
271
			$ligne['latitude'] = '';
271
			$ligne['latitude'] = '';
272
			$ligne['longitude'] = '';
272
			$ligne['longitude'] = '';
273
		}
273
		}
274
		return $ligne;
274
		return $ligne;
275
	}
275
	}
276
	
276
	
277
	private function nettoyerChaine($chaine) {
277
	private function nettoyerChaine($chaine) {
278
		$chaine = str_replace("\n",' ',$chaine);
278
		$chaine = str_replace("\n",' ',$chaine);
279
		$chaine = str_replace("\t",'',$chaine);
279
		$chaine = str_replace("\t",'',$chaine);
280
		return $chaine;
280
		return $chaine;
281
	}
281
	}
282
}
282
}
283
?>
283
?>