Subversion Repositories eFlore/Applications.cel

Rev

Rev 1376 | Go to most recent revision | Details | 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
		);
53
 
54
	/**
55
	 * Méthode appelée avec une requête de type GET.
56
	 */
57
	public function getElement($params = array()) {
58
 
59
		$criteres = $_GET;
60
		// Seulement les observation publiques
61
		$criteres['transmission'] = 1;
62
		$chercheur_observations = new RechercheObservation($this->config);
63
 
64
		$numero_page = isset($criteres['debut']) ? $criteres['debut'] : 1;
65
		$limite = isset($criteres['limite']) ? $criteres['limite'] : 50;
66
 
67
		unset($criteres['limite']);
68
		unset($criteres['debut']);
69
 
70
		$observations = $chercheur_observations->rechercherObservations(null, $criteres, $numero_page, $limite);
71
		$csv = $this->convertirEnCsv($observations);
72
		$this->envoyerCsv($csv);
73
	}
74
 
75
	private function envoyerCsv($csv) {
76
		header('Content-Type: text/csv; charset=UTF-8');
77
		header('Content-Disposition: attachment;filename='.$this->nom_fichier_export);
78
		echo $csv;
79
	}
80
 
81
	private function convertirEnCsv($data)
82
	{
83
		$chemin_temp = "php://temp";
84
		$outstream = fopen($chemin_temp, 'r+');
85
		$intitule_champs = array();
86
		foreach($data as $ligne) {
87
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
88
			if(empty($intitule_champs)) {
89
				$intitule_champs = $this->creerEntetesChamps($ligne);
90
				fputcsv($outstream, $intitule_champs, ',', '"');
91
			}
92
			fputcsv($outstream, $ligne, ',', '"');
93
		}
94
		rewind($outstream);
95
		$csv = stream_get_contents($outstream);
96
		fclose($outstream);
97
		return $csv;
98
	}
99
 
100
	private function creerEntetesChamps($noms_colonnes) {
101
		$champs_presents = array_intersect_key($this->correspondance_champs, $noms_colonnes);
102
		return array_values($champs_presents);
103
	}
104
}
105
?>