Subversion Repositories eFlore/Applications.cel

Rev

Rev 1401 | Rev 1408 | 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
 
22
	private $nom_fichier_export = 'cel_export.csv';
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);
1402 aurelien 84
 
85
		switch($this->format) {
86
			case 'csv':
87
				$csv = $this->convertirEnCsv($observations);
88
				$this->envoyerCsv($csv);
89
			break;
90
			case 'xls':
91
				$xls = $this->convertirEnXls($observations);
92
				$this->envoyerXls($xls);
93
			break;
94
			default:
95
		}
1374 aurelien 96
	}
97
 
1376 aurelien 98
	protected function traiterParametres(Array $parametres) {
99
		$parametres_traites = array();
1402 aurelien 100
		$this->format = (isset($parametres['format']) && $parametres['format'] != '') ? $parametres['format'] : $this->format;
1376 aurelien 101
		foreach($parametres as $cle => $valeur) {
102
			if(trim($valeur) != '' && isset($this->parametres_autorises[$cle])) {
103
				$parametres_traites[$this->parametres_autorises[$cle]] = $valeur;
104
			}
105
		}
106
		return $parametres_traites;
107
	}
108
 
1374 aurelien 109
	private function envoyerCsv($csv) {
110
		header('Content-Type: text/csv; charset=UTF-8');
111
		header('Content-Disposition: attachment;filename='.$this->nom_fichier_export);
112
		echo $csv;
1376 aurelien 113
		exit;
1374 aurelien 114
	}
115
 
1402 aurelien 116
	private function envoyerXls($workbook) {
117
		$workbook->close();
118
		exit;
119
	}
120
 
1374 aurelien 121
	private function convertirEnCsv($data)
122
	{
123
		$chemin_temp = "php://temp";
124
		$outstream = fopen($chemin_temp, 'r+');
125
		$intitule_champs = array();
126
		foreach($data as $ligne) {
127
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
128
			if(empty($intitule_champs)) {
129
				$intitule_champs = $this->creerEntetesChamps($ligne);
130
				fputcsv($outstream, $intitule_champs, ',', '"');
131
			}
132
			fputcsv($outstream, $ligne, ',', '"');
133
		}
134
		rewind($outstream);
135
		$csv = stream_get_contents($outstream);
136
		fclose($outstream);
137
		return $csv;
138
	}
139
 
1402 aurelien 140
	private function convertirEnXls($data) {
141
		$this->extendSpreadsheetProductor = new SpreadsheetProductor();
142
		$this->extendSpreadsheetProductor->initSpreadsheet();
143
 
144
		$workbook = new Spreadsheet_Excel_Writer();
145
 
146
		$worksheet = $workbook->addWorksheet('Liste');
147
		$workbook->send($this->nom_fichier_export);
148
 
149
		$nb_lignes = 1;
150
 
151
		foreach($data as $ligne) {
152
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
153
			if(empty($intitule_champs)) {
154
				$intitule_champs = $this->creerEntetesChamps($ligne);
155
				$indice = 0;
156
				foreach ($intitule_champs as $intitule) {
157
					$worksheet->write(0,$indice,$intitule_champs[$indice]);
158
					$indice++;
159
				}
160
			}
161
			$indice = 0;
162
			foreach($ligne as $champ) {
163
				$worksheet->write($nb_lignes,$indice,$champ);
164
				$indice++;
165
			}
166
			$nb_lignes++;
167
		}
168
		return $workbook;
169
	}
170
 
1374 aurelien 171
	private function creerEntetesChamps($noms_colonnes) {
172
		$champs_presents = array_intersect_key($this->correspondance_champs, $noms_colonnes);
173
		return array_values($champs_presents);
174
	}
1401 aurelien 175
 
176
	private function nettoyerChaine($chaine) {
177
		$chaine = str_replace("\n",' ',$chaine);
178
		$chaine = str_replace("\t",'',$chaine);
179
		return $chaine;
180
	}
1374 aurelien 181
}
182
?>