Subversion Repositories eFlore/Applications.cel

Rev

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

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