Subversion Repositories eFlore/Applications.cel

Rev

Rev 1401 | Rev 1408 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1401 Rev 1402
Line 58... Line 58...
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
	
-
 
64
	private $format = 'csv';
Line 63... Line 65...
63
		
65
		
64
	/**
66
	/**
65
	 * Méthode appelée avec une requête de type GET.
67
	 * Méthode appelée avec une requête de type GET.
66
	 */
68
	 */
Line 67... Line 69...
67
	public function getElement($params = array()) {
69
	public function getElement($params = array()) {
-
 
70
		
68
		
71
		$criteres = $this->traiterParametres($_GET);
69
		$criteres = $this->traiterParametres($_GET);
72
		
70
		// Seulement les observation publiques
73
		// Seulement les observation publiques
Line 71... Line 74...
71
		$criteres['transmission'] = 1;
74
		$criteres['transmission'] = 1;
Line 76... Line 79...
76
		
79
		
77
		unset($criteres['limite']);
80
		unset($criteres['limite']);
Line 78... Line 81...
78
		unset($criteres['debut']);
81
		unset($criteres['debut']);
-
 
82
	
-
 
83
		$observations = $chercheur_observations->rechercherObservations(null, $criteres, $numero_page, $limite);
-
 
84
		
79
	
85
		switch($this->format) {
80
		$observations = $chercheur_observations->rechercherObservations(null, $criteres, $numero_page, $limite);
86
			case 'csv':
-
 
87
				$csv = $this->convertirEnCsv($observations);
-
 
88
				$this->envoyerCsv($csv);
-
 
89
			break;
-
 
90
			case 'xls':
-
 
91
				$xls = $this->convertirEnXls($observations);
-
 
92
				$this->envoyerXls($xls);
-
 
93
			break;
81
		$csv = $this->convertirEnCsv($observations);
94
			default:
Line 82... Line 95...
82
		$this->envoyerCsv($csv);
95
		}
83
	}
96
	}
-
 
97
	
84
	
98
	protected function traiterParametres(Array $parametres) {
85
	protected function traiterParametres(Array $parametres) {
99
		$parametres_traites = array();
86
		$parametres_traites = array();
100
		$this->format = (isset($parametres['format']) && $parametres['format'] != '') ? $parametres['format'] : $this->format;
87
		foreach($parametres as $cle => $valeur) {
101
		foreach($parametres as $cle => $valeur) {
88
			if(trim($valeur) != '' && isset($this->parametres_autorises[$cle])) {
102
			if(trim($valeur) != '' && isset($this->parametres_autorises[$cle])) {
Line 97... Line 111...
97
		header('Content-Disposition: attachment;filename='.$this->nom_fichier_export);
111
		header('Content-Disposition: attachment;filename='.$this->nom_fichier_export);
98
		echo $csv;
112
		echo $csv;
99
		exit;
113
		exit;
100
	}
114
	}
Line -... Line 115...
-
 
115
	
-
 
116
	private function envoyerXls($workbook) {
-
 
117
		$workbook->close();
-
 
118
		exit;
-
 
119
	}
101
	
120
	
102
	private function convertirEnCsv($data)
121
	private function convertirEnCsv($data)
103
	{
122
	{
104
		$chemin_temp = "php://temp";
123
		$chemin_temp = "php://temp";
105
		$outstream = fopen($chemin_temp, 'r+');
124
		$outstream = fopen($chemin_temp, 'r+');
Line 116... Line 135...
116
		$csv = stream_get_contents($outstream);
135
		$csv = stream_get_contents($outstream);
117
		fclose($outstream);
136
		fclose($outstream);
118
		return $csv;
137
		return $csv;
119
	}
138
	}
Line -... Line 139...
-
 
139
	
-
 
140
	private function convertirEnXls($data) {		
-
 
141
		$this->extendSpreadsheetProductor = new SpreadsheetProductor();
-
 
142
		$this->extendSpreadsheetProductor->initSpreadsheet();
-
 
143
		
-
 
144
		$workbook = new Spreadsheet_Excel_Writer();
-
 
145
 
-
 
146
		$worksheet = $workbook->addWorksheet('Liste');
-
 
147
		$workbook->send($this->nom_fichier_export);
-
 
148
		
-
 
149
		$nb_lignes = 1;
-
 
150
		
-
 
151
		foreach($data as $ligne) {
-
 
152
			$ligne = array_diff_key($ligne, $this->champs_a_exclure);
-
 
153
			if(empty($intitule_champs)) {
-
 
154
				$intitule_champs = $this->creerEntetesChamps($ligne);
-
 
155
				$indice = 0;
-
 
156
				foreach ($intitule_champs as $intitule) {	
-
 
157
					$worksheet->write(0,$indice,$intitule_champs[$indice]);
-
 
158
					$indice++;
-
 
159
				}
-
 
160
			}
-
 
161
			$indice = 0;
-
 
162
			foreach($ligne as $champ) {
-
 
163
				$worksheet->write($nb_lignes,$indice,$champ);
-
 
164
				$indice++;
-
 
165
			}
-
 
166
			$nb_lignes++;
-
 
167
		}
-
 
168
		return $workbook;
-
 
169
	}
120
	
170
	
121
	private function creerEntetesChamps($noms_colonnes) {
171
	private function creerEntetesChamps($noms_colonnes) {
122
		$champs_presents = array_intersect_key($this->correspondance_champs, $noms_colonnes);
172
		$champs_presents = array_intersect_key($this->correspondance_champs, $noms_colonnes);
123
		return array_values($champs_presents);
173
		return array_values($champs_presents);