Subversion Repositories eFlore/Applications.cel

Rev

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

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