Subversion Repositories eFlore/Applications.cel

Rev

Rev 1571 | Rev 1610 | 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';
1579 aurelien 65
 
66
	public function getRessource() {
67
		return $this->getElement(array());
68
	}
1374 aurelien 69
 
70
	/**
71
	 * Méthode appelée avec une requête de type GET.
72
	 */
73
	public function getElement($params = array()) {
74
 
1376 aurelien 75
		$criteres = $this->traiterParametres($_GET);
1402 aurelien 76
 
1374 aurelien 77
		// Seulement les observation publiques
78
		$criteres['transmission'] = 1;
79
		$chercheur_observations = new RechercheObservation($this->config);
80
 
1379 aurelien 81
		$numero_page = isset($criteres['debut']) ? $criteres['debut'] : 0;
82
		$limite = isset($criteres['limite']) ? $criteres['limite'] : 0;
1374 aurelien 83
 
84
		unset($criteres['limite']);
85
		unset($criteres['debut']);
86
 
87
		$observations = $chercheur_observations->rechercherObservations(null, $criteres, $numero_page, $limite);
1579 aurelien 88
 
1402 aurelien 89
		switch($this->format) {
90
			case 'csv':
91
				$csv = $this->convertirEnCsv($observations);
92
				$this->envoyerCsv($csv);
93
			break;
94
			case 'xls':
95
				$xls = $this->convertirEnXls($observations);
96
				$this->envoyerXls($xls);
97
			break;
98
			default:
99
		}
1374 aurelien 100
	}
101
 
1376 aurelien 102
	protected function traiterParametres(Array $parametres) {
103
		$parametres_traites = array();
1402 aurelien 104
		$this->format = (isset($parametres['format']) && $parametres['format'] != '') ? $parametres['format'] : $this->format;
1376 aurelien 105
		foreach($parametres as $cle => $valeur) {
106
			if(trim($valeur) != '' && isset($this->parametres_autorises[$cle])) {
107
				$parametres_traites[$this->parametres_autorises[$cle]] = $valeur;
108
			}
109
		}
110
		return $parametres_traites;
111
	}
112
 
1374 aurelien 113
	private function envoyerCsv($csv) {
114
		header('Content-Type: text/csv; charset=UTF-8');
1408 aurelien 115
		header('Content-Disposition: attachment;filename='.$this->nom_fichier_export.'.csv');
1374 aurelien 116
		echo $csv;
1376 aurelien 117
		exit;
1374 aurelien 118
	}
119
 
1402 aurelien 120
	private function envoyerXls($workbook) {
121
		$workbook->close();
122
		exit;
123
	}
124
 
1374 aurelien 125
	private function convertirEnCsv($data)
126
	{
127
		$chemin_temp = "php://temp";
128
		$outstream = fopen($chemin_temp, 'r+');
129
		$intitule_champs = array();
130
		foreach($data as $ligne) {
1429 aurelien 131
			$ligne = $this->filtrerDonneesSensibles($ligne);
1374 aurelien 132
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
133
			if(empty($intitule_champs)) {
134
				$intitule_champs = $this->creerEntetesChamps($ligne);
135
				fputcsv($outstream, $intitule_champs, ',', '"');
136
			}
137
			fputcsv($outstream, $ligne, ',', '"');
138
		}
139
		rewind($outstream);
140
		$csv = stream_get_contents($outstream);
141
		fclose($outstream);
142
		return $csv;
143
	}
144
 
1402 aurelien 145
	private function convertirEnXls($data) {
146
		$this->extendSpreadsheetProductor = new SpreadsheetProductor();
147
		$this->extendSpreadsheetProductor->initSpreadsheet();
148
 
149
		$workbook = new Spreadsheet_Excel_Writer();
150
 
151
		$worksheet = $workbook->addWorksheet('Liste');
1408 aurelien 152
		$workbook->send($this->nom_fichier_export.'.xls');
1402 aurelien 153
 
154
		$nb_lignes = 1;
155
 
156
		foreach($data as $ligne) {
1429 aurelien 157
			$ligne = $this->filtrerDonneesSensibles($ligne);
1402 aurelien 158
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
159
			if(empty($intitule_champs)) {
160
				$intitule_champs = $this->creerEntetesChamps($ligne);
161
				$indice = 0;
162
				foreach ($intitule_champs as $intitule) {
1465 aurelien 163
					$colonne = $intitule_champs[$indice];
164
					$colonne = mb_convert_encoding($colonne, 'ISO-8859-15', 'UTF-8');
165
					$worksheet->write(0,$indice,$colonne);
1402 aurelien 166
					$indice++;
167
				}
168
			}
169
			$indice = 0;
170
			foreach($ligne as $champ) {
1465 aurelien 171
				$champ = mb_convert_encoding($champ, 'ISO-8859-15', 'UTF-8');
1402 aurelien 172
				$worksheet->write($nb_lignes,$indice,$champ);
173
				$indice++;
174
			}
175
			$nb_lignes++;
176
		}
177
		return $workbook;
178
	}
179
 
1374 aurelien 180
	private function creerEntetesChamps($noms_colonnes) {
181
		$champs_presents = array_intersect_key($this->correspondance_champs, $noms_colonnes);
182
		return array_values($champs_presents);
183
	}
1401 aurelien 184
 
1429 aurelien 185
	private function filtrerDonneesSensibles($ligne) {
186
		if(stripos($ligne['mots_cles_texte'], 'sensible') !== false) {
187
			$ligne['latitude'] = '';
188
			$ligne['longitude'] = '';
189
		}
190
		return $ligne;
191
	}
192
 
1401 aurelien 193
	private function nettoyerChaine($chaine) {
194
		$chaine = str_replace("\n",' ',$chaine);
195
		$chaine = str_replace("\t",'',$chaine);
196
		return $chaine;
197
	}
1374 aurelien 198
}
199
?>