Subversion Repositories eFlore/Applications.cel

Rev

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

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