Subversion Repositories eFlore/Applications.cel

Rev

Rev 1465 | Rev 1571 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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