Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 1631 Rev 1654
Line 25... Line 25...
25
require_once("lib/Spreadsheet/Excel/Writer.php");
25
require_once("lib/Spreadsheet/Excel/Writer.php");
Line 26... Line 26...
26
 
26
 
Line 27... Line 27...
27
class CelWidgetExport extends Cel {
27
class CelWidgetExport extends Cel {
28
	
-
 
29
	private $nom_fichier_export = 'cel_export';
-
 
30
	private $champs_a_exclure = array(
-
 
31
		'ce_utilisateur' => true, 
-
 
32
		'courriel_utilisateur' => true,
-
 
33
		'transmission' => true,
-
 
34
		'code_insee_calcule' => true);
-
 
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',
-
 
54
		'altitude' => 'Altitude',
-
 
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',
-
 
61
		'date_transmission' => 'Date Transmission',
-
 
62
		'abondance' => 'Abondance',
-
 
63
		'certitude' => 'Certitude',
-
 
64
		'phenologie' => 'Phénologie'
28
	
-
 
29
	private $nom_fichier_export = 'cel_export';
65
		);
30
	private $parametres_autorises = array(
66
	private $parametres_autorises = array(
31
		'id_utilisateur' => 'ce_utilisateur',
67
		'utilisateur' => 'courriel_utilisateur',
32
		'utilisateur' => 'courriel_utilisateur',
-
 
33
		'commune' => 'zone_geo',
-
 
34
		'dept' => 'departement',
-
 
35
		'departement' => 'departement',
68
		'commune' => 'zone_geo',
36
		'lieudit' => 'lieudit',
69
		'dept' => 'departement',
37
		'station' => 'station',
70
		'projet' => 'mots_cles',
38
		'projet' => 'mots_cles',
71
		'num_taxon' => 'nt',
39
		'num_taxon' => 'nt',
72
		'date_debut' => 'date_debut',
40
		'date_debut' => 'date_debut',
-
 
41
		'date_fin' => 'date_fin',
-
 
42
		'taxon' => 'taxon',
-
 
43
		'annee' => 'annee',
-
 
44
		'mois' => 'mois',
-
 
45
		'jour' => 'jour',
73
		'date_fin' => 'date_fin',
46
		'recherche' => 'recherche',
74
		'taxon' => 'taxon',
47
		'id_mots_cles' => 'id_mots_cles',
75
		'debut' => 'debut',
48
		'debut' => 'debut',
-
 
49
		'limite' => 'limite',
-
 
50
		'format' => 'format',
76
		'limite' => 'limite',
51
		'colonnes' => 'colonnes',
Line 77... Line 52...
77
		'format' => 'format'
52
		'transmission' => 'transmission'
Line 78... Line 53...
78
	);
53
	);
Line 107... Line 82...
107
		}
82
		}
108
	}
83
	}
Line 109... Line 84...
109
	
84
	
110
	private function getCalcul() {
85
	private function getCalcul() {
111
		$criteres = $this->traiterParametresAutorises($_GET);
-
 
-
 
86
		$criteres = $this->traiterParametresAutorises($_GET);
112
		// Seulement les observation publiques
87
 
-
 
88
		$criteres['transmission'] = 1;
-
 
89
		if($this->doitEtPeutExporterObsPrivees($criteres)) {
-
 
90
			unset($criteres['transmission']);
113
		$criteres['transmission'] = 1;
91
		}
Line 114... Line 92...
114
		$chercheur_observations = new RechercheObservation($this->config);
92
		$chercheur_observations = new RechercheObservation($this->config);
115
		
93
		
-
 
94
		$numero_page = isset($criteres['debut']) ? $criteres['debut'] : 0;
Line 116... Line 95...
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
		$colonnes = isset($criteres['colonnes']) ? $criteres['colonnes'] : 'standard,avance';
118
		
97
		
-
 
98
		unset($criteres['limite']);
Line 119... Line 99...
119
		unset($criteres['limite']);
99
		unset($criteres['debut']);
120
		unset($criteres['debut']);
100
		unset($criteres['format']);
Line 121... Line 101...
121
		unset($criteres['format']);
101
		unset($criteres['colonnes']);
Line 131... Line 111...
131
		do {
111
		do {
132
			$base_url = $this->config['settings']['baseURLAbsolu'].'CelWidgetExport/export';
112
			$base_url = $this->config['settings']['baseURLAbsolu'].'CelWidgetExport/export';
133
			$params_url['debut'] = $intervalle;
113
			$params_url['debut'] = $intervalle;
134
			$params_url['limite'] = $limite_decoupage;
114
			$params_url['limite'] = $limite_decoupage;
135
			$url_telechargement_fichier = $base_url;
115
			$url_telechargement_fichier = $base_url;
136
			$url_telechargements[] = $base_url.'?'.http_build_query($params_url).'&format='.$this->format;
116
			$url_telechargements[] = $base_url.'?'.http_build_query($params_url).'&format='.$this->format.'&colonnes='.$colonnes;
137
			$intervalle += $limite_decoupage;
117
			$intervalle += $limite_decoupage;
138
			$nb_observations -= $limite_decoupage;
118
			$nb_observations -= $limite_decoupage;
139
		} while($nb_observations >= $limite_decoupage);
119
		} while($nb_observations >= $limite_decoupage);
Line 140... Line 120...
140
		
120
		
Line 154... Line 134...
154
		return $limite;
134
		return $limite;
155
	}
135
	}
Line 156... Line 136...
156
	
136
	
157
	private function getExport() {
137
	private function getExport() {
158
		$criteres = $this->traiterParametresAutorises($_GET);
-
 
159
 
-
 
160
		// Seulement les observation publiques
138
		$criteres = $this->traiterParametresAutorises($_GET);
-
 
139
		$criteres['transmission'] = 1;
-
 
140
		if($this->doitEtPeutExporterObsPrivees($criteres)) {
-
 
141
			unset($criteres['transmission']);
161
		$criteres['transmission'] = 1;
142
		}
Line 162... Line 143...
162
		$chercheur_observations = new RechercheObservation($this->config);
143
		$chercheur_observations = new RechercheObservation($this->config);
163
		
144
		
-
 
145
		$debut = isset($criteres['debut']) ? $criteres['debut'] : 0;
Line 164... Line 146...
164
		$debut = isset($criteres['debut']) ? $criteres['debut'] : 0;
146
		$limite = isset($criteres['limite']) ? $criteres['limite'] : 0;
165
		$limite = isset($criteres['limite']) ? $criteres['limite'] : 0;
147
		$colonnes = isset($criteres['colonnes']) ? $criteres['colonnes'] : 'standard,avance';
166
		
148
		
-
 
149
		unset($criteres['limite']);
Line 167... Line 150...
167
		unset($criteres['limite']);
150
		unset($criteres['debut']);
168
		unset($criteres['debut']);
151
		unset($criteres['format']);
169
		unset($criteres['format']);
152
		unset($criteres['colonnes']);
170
		
153
		
171
		$observations = $chercheur_observations->rechercherObservations(null, $criteres, $debut, $limite)->get();
154
		$observations = $chercheur_observations->rechercherObservations(null, $criteres, $debut, $limite)->get();
172
		switch($this->format) {
155
		switch($this->format) {
173
			case 'csv':
156
			case 'csv':
174
				$csv = $this->convertirEnCsv($observations);
157
				$csv = $this->convertirEnCsv($observations, $colonnes);
175
				$this->envoyerCsv($csv);
158
				$this->envoyerCsv($csv);
176
				break;
159
				break;
177
			case 'xls':
160
			case 'xls':
178
				$xls = $this->convertirEnXls($observations);
161
				$xls = $this->convertirEnXls($observations, $colonnes);
179
				$this->envoyerXls($xls);
162
				$this->envoyerXls($xls);
Line 203... Line 186...
203
	private function envoyerXls($workbook) {
186
	private function envoyerXls($workbook) {
204
		$workbook->close();
187
		$workbook->close();
205
		exit;
188
		exit;
206
	}
189
	}
Line 207... Line 190...
207
	
190
	
208
	private function convertirEnCsv($data)
191
	private function convertirEnCsv($data, $colonnes)
209
	{
192
	{
210
		$chemin_temp = "php://temp";
193
		$chemin_temp = "php://temp";
211
		$outstream = fopen($chemin_temp, 'r+');
194
		$outstream = fopen($chemin_temp, 'r+');
-
 
195
		$intitule_champs = array();
212
		$intitule_champs = array();
196
		$groupe_colonnes = FormateurGroupeColonne::nomEnsembleVersListeColonnes($colonnes);
213
		foreach($data as &$ligne) {
197
		foreach($data as &$ligne) {
214
			$ligne = $this->filtrerDonneesSensibles($ligne);
198
			$ligne = $this->filtrerDonneesSensibles($ligne);
215
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
199
			$ligne = FormateurGroupeColonne::getLigneObservation($ligne, $groupe_colonnes);
216
			if(empty($intitule_champs)) {
200
			if(empty($intitule_champs)) {
217
				$intitule_champs = $this->creerEntetesChamps($ligne);
201
				$intitule_champs = FormateurGroupeColonne::getIntitulesColonnes($groupe_colonnes);
218
				fputcsv($outstream, $intitule_champs, ',', '"');			
202
				fputcsv($outstream, $intitule_champs, ',', '"');			
219
			}
203
			}
220
			fputcsv($outstream, $ligne, ',', '"');
204
			fputcsv($outstream, $ligne, ',', '"');
221
		}
205
		}
222
		rewind($outstream);
206
		rewind($outstream);
223
		$csv = stream_get_contents($outstream);
207
		$csv = stream_get_contents($outstream);
224
		fclose($outstream);
208
		fclose($outstream);
225
		return $csv;
209
		return $csv;
Line 226... Line 210...
226
	}
210
	}
227
	
211
	
228
	private function convertirEnXls($data) {		
212
	private function convertirEnXls($data, $colonnes) {		
Line 229... Line 213...
229
		$this->extendSpreadsheetProductor = new SpreadsheetProductor();
213
		$this->extendSpreadsheetProductor = new SpreadsheetProductor();
230
		$this->extendSpreadsheetProductor->initSpreadsheet();
214
		$this->extendSpreadsheetProductor->initSpreadsheet();
Line 236... Line 220...
236
 
220
 
237
		$worksheet->setInputEncoding('utf-8');
221
		$worksheet->setInputEncoding('utf-8');
Line 238... Line 222...
238
		$workbook->send($this->nom_fichier_export.'.xls');
222
		$workbook->send($this->nom_fichier_export.'.xls');
239
		
-
 
-
 
223
		
240
		$nb_lignes = 1;
224
		$nb_lignes = 1;
241
				
225
		$groupe_colonnes = FormateurGroupeColonne::nomEnsembleVersListeColonnes($colonnes);
242
		foreach($data as &$ligne) {
226
		foreach($data as &$ligne) {
243
			$ligne = $this->filtrerDonneesSensibles($ligne);
227
			$ligne = $this->filtrerDonneesSensibles($ligne);
244
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
-
 
245
			if(empty($intitule_champs)) {
228
			$ligne = FormateurGroupeColonne::getLigneObservation($ligne, $groupe_colonnes);
-
 
229
			if(empty($intitule_champs)) {
246
				$intitule_champs = $this->creerEntetesChamps($ligne);
230
				$indice = 0;
247
				$indice = 0;
231
				$intitule_champs = FormateurGroupeColonne::getIntitulesColonnes($groupe_colonnes);
248
				foreach ($intitule_champs as &$intitule) {	
232
				foreach ($intitule_champs as &$intitule) {	
249
					$colonne = $intitule_champs[$indice];
233
					$colonne = $intitule_champs[$indice];
250
					$worksheet->write(0,$indice,$colonne);
234
					$worksheet->write(0,$indice,$colonne);
Line 259... Line 243...
259
			$nb_lignes++;
243
			$nb_lignes++;
260
		}
244
		}
261
		return $workbook;
245
		return $workbook;
262
	}
246
	}
Line 263... Line -...
263
	
-
 
264
	private function creerEntetesChamps($noms_colonnes) {
-
 
265
		$champs_presents = array_intersect_key($this->correspondance_champs, $noms_colonnes);
-
 
266
		return array_values($champs_presents);
-
 
267
	}
-
 
268
	
247
	
269
	private function filtrerDonneesSensibles($ligne) {
248
	private function filtrerDonneesSensibles($ligne) {
270
		if(stripos($ligne['mots_cles_texte'], 'sensible') !== false) {
249
		if(stripos($ligne['mots_cles_texte'], 'sensible') !== false) {
271
			$ligne['latitude'] = '';
250
			$ligne['latitude'] = '';
272
			$ligne['longitude'] = '';
251
			$ligne['longitude'] = '';
Line 277... Line 256...
277
	private function nettoyerChaine($chaine) {
256
	private function nettoyerChaine($chaine) {
278
		$chaine = str_replace("\n",' ',$chaine);
257
		$chaine = str_replace("\n",' ',$chaine);
279
		$chaine = str_replace("\t",'',$chaine);
258
		$chaine = str_replace("\t",'',$chaine);
280
		return $chaine;
259
		return $chaine;
281
	}
260
	}
-
 
261
	
-
 
262
	private function doitEtPeutExporterObsPrivees($criteres) {
-
 
263
		return isset($criteres['ce_utilisateur']) && 
-
 
264
					$this->peutExporterObsPrivees($criteres['ce_utilisateur']);
-
 
265
	}
-
 
266
	
-
 
267
	private function peutExporterObsPrivees($id_utilisateur) {
-
 
268
		$gestion_utilisateur = new User($this->config);
-
 
269
		$utilisateur = $gestion_utilisateur->obtenirIdentiteConnectee();
-
 
270
		return $utilisateur['connecte'] && 
-
 
271
				$utilisateur['id_utilisateur'] != '' &&
-
 
272
				$id_utilisateur == $utilisateur['id_utilisateur'];
-
 
273
	}
282
}
274
}
283
?>
275
?>
284
276