Subversion Repositories eFlore/Applications.cel

Rev

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

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