Subversion Repositories eFlore/Applications.cel

Rev

Rev 1408 | Rev 1465 | 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
*/
20
class CelWidgetExport extends Cel {
21
 
1408 aurelien 22
	private $nom_fichier_export = 'cel_export';
1374 aurelien 23
	private $champs_a_exclure = array('ce_utilisateur' => 'ce_utilisateur',
24
		'courriel_utilisateur' => 'courriel_utilisateur',
25
		'transmission' => 'transmission');
26
	private $correspondance_champs = array(
27
		'id_observation' => 'Identifiant Observation',
28
		'ordre' => 'Ordre Observation',
29
		'prenom_utilisateur' => 'Prénom',
30
		'nom_utilisateur' => 'Nom',
31
		'nom_sel' => 'Nom Sélectionné',
32
		'nom_sel_nn' => 'Numéro Nomenclatural Nom Selectionné',
33
		'nom_ret' => 'Nom Retenu',
34
		'nom_ret_nn' => 'Numéro Nomenclatural Nom Retenu',
35
		'nt' => 'Numéro Taxonomique',
36
		'famille' => 'Famille',
37
		'nom_referentiel' => 'Référentiel Taxonomique',
38
		'ce_zone_geo' => 'Code Insee',
39
		'zone_geo' => 'Commune',
40
		'lieudit' => 'Lieu-Dit',
41
		'station' => 'Station',
42
		'milieu' => 'Milieu',
43
		'latitude' => 'Latitude',
44
		'longitude' => 'Longitude',
45
		'geodatum' => 'Référentiel Géographique',
46
		'date_observation' => 'Date Observation',
47
		'mots_cles_texte' => 'Mots Clés',
48
		'commentaire' => 'Commentaires',
49
		'date_creation' => 'Date Création',
50
		'date_modification' => 'Date Modification',
51
		'date_transmission' => 'Date Transmission'
52
		);
1376 aurelien 53
	private $parametres_autorises = array(
54
		'utilisateur' => 'courriel_utilisateur',
55
		'commune' => 'zone_geo',
56
		'dept' => 'departement',
57
		'projet' => 'mots_cles',
1379 aurelien 58
		'num_taxon' => 'nt',
1378 aurelien 59
		'date_debut' => 'date_debut',
1387 aurelien 60
		'date_fin' => 'date_fin',
61
		'taxon' => 'taxon'
1376 aurelien 62
	);
1402 aurelien 63
 
64
	private $format = 'csv';
1374 aurelien 65
 
66
	/**
67
	 * Méthode appelée avec une requête de type GET.
68
	 */
69
	public function getElement($params = array()) {
70
 
1376 aurelien 71
		$criteres = $this->traiterParametres($_GET);
1402 aurelien 72
 
1374 aurelien 73
		// Seulement les observation publiques
74
		$criteres['transmission'] = 1;
75
		$chercheur_observations = new RechercheObservation($this->config);
76
 
1379 aurelien 77
		$numero_page = isset($criteres['debut']) ? $criteres['debut'] : 0;
78
		$limite = isset($criteres['limite']) ? $criteres['limite'] : 0;
1374 aurelien 79
 
80
		unset($criteres['limite']);
81
		unset($criteres['debut']);
82
 
83
		$observations = $chercheur_observations->rechercherObservations(null, $criteres, $numero_page, $limite);
1429 aurelien 84
		$observations = array_slice($observations, 0, 10000);
85
		//echo count($observations);exit;
1402 aurelien 86
		switch($this->format) {
87
			case 'csv':
88
				$csv = $this->convertirEnCsv($observations);
89
				$this->envoyerCsv($csv);
90
			break;
91
			case 'xls':
92
				$xls = $this->convertirEnXls($observations);
93
				$this->envoyerXls($xls);
94
			break;
95
			default:
96
		}
1374 aurelien 97
	}
98
 
1376 aurelien 99
	protected function traiterParametres(Array $parametres) {
100
		$parametres_traites = array();
1402 aurelien 101
		$this->format = (isset($parametres['format']) && $parametres['format'] != '') ? $parametres['format'] : $this->format;
1376 aurelien 102
		foreach($parametres as $cle => $valeur) {
103
			if(trim($valeur) != '' && isset($this->parametres_autorises[$cle])) {
104
				$parametres_traites[$this->parametres_autorises[$cle]] = $valeur;
105
			}
106
		}
107
		return $parametres_traites;
108
	}
109
 
1374 aurelien 110
	private function envoyerCsv($csv) {
111
		header('Content-Type: text/csv; charset=UTF-8');
1408 aurelien 112
		header('Content-Disposition: attachment;filename='.$this->nom_fichier_export.'.csv');
1374 aurelien 113
		echo $csv;
1376 aurelien 114
		exit;
1374 aurelien 115
	}
116
 
1402 aurelien 117
	private function envoyerXls($workbook) {
118
		$workbook->close();
119
		exit;
120
	}
121
 
1374 aurelien 122
	private function convertirEnCsv($data)
123
	{
124
		$chemin_temp = "php://temp";
125
		$outstream = fopen($chemin_temp, 'r+');
126
		$intitule_champs = array();
127
		foreach($data as $ligne) {
1429 aurelien 128
			$ligne = $this->filtrerDonneesSensibles($ligne);
1374 aurelien 129
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
130
			if(empty($intitule_champs)) {
131
				$intitule_champs = $this->creerEntetesChamps($ligne);
132
				fputcsv($outstream, $intitule_champs, ',', '"');
133
			}
134
			fputcsv($outstream, $ligne, ',', '"');
135
		}
136
		rewind($outstream);
137
		$csv = stream_get_contents($outstream);
138
		fclose($outstream);
139
		return $csv;
140
	}
141
 
1402 aurelien 142
	private function convertirEnXls($data) {
143
		$this->extendSpreadsheetProductor = new SpreadsheetProductor();
144
		$this->extendSpreadsheetProductor->initSpreadsheet();
145
 
146
		$workbook = new Spreadsheet_Excel_Writer();
147
 
148
		$worksheet = $workbook->addWorksheet('Liste');
1408 aurelien 149
		$workbook->send($this->nom_fichier_export.'.xls');
1402 aurelien 150
 
151
		$nb_lignes = 1;
152
 
153
		foreach($data as $ligne) {
1429 aurelien 154
			$ligne = $this->filtrerDonneesSensibles($ligne);
1402 aurelien 155
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
156
			if(empty($intitule_champs)) {
157
				$intitule_champs = $this->creerEntetesChamps($ligne);
158
				$indice = 0;
159
				foreach ($intitule_champs as $intitule) {
160
					$worksheet->write(0,$indice,$intitule_champs[$indice]);
161
					$indice++;
162
				}
163
			}
164
			$indice = 0;
165
			foreach($ligne as $champ) {
166
				$worksheet->write($nb_lignes,$indice,$champ);
167
				$indice++;
168
			}
169
			$nb_lignes++;
170
		}
171
		return $workbook;
172
	}
173
 
1374 aurelien 174
	private function creerEntetesChamps($noms_colonnes) {
175
		$champs_presents = array_intersect_key($this->correspondance_champs, $noms_colonnes);
176
		return array_values($champs_presents);
177
	}
1401 aurelien 178
 
1429 aurelien 179
	private function filtrerDonneesSensibles($ligne) {
180
		if(stripos($ligne['mots_cles_texte'], 'sensible') !== false) {
181
			$ligne['latitude'] = '';
182
			$ligne['longitude'] = '';
183
		}
184
		return $ligne;
185
	}
186
 
1401 aurelien 187
	private function nettoyerChaine($chaine) {
188
		$chaine = str_replace("\n",' ',$chaine);
189
		$chaine = str_replace("\t",'',$chaine);
190
		return $chaine;
191
	}
1374 aurelien 192
}
193
?>