Subversion Repositories eFlore/Applications.cel

Rev

Rev 1755 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1755 Rev 2458
Line 1... Line 1...
1
<?php
1
<?php
2
 
-
 
3
/**
-
 
4
* @category  PHP
-
 
5
* @package   jrest
-
 
6
* @author    Raphaël Droz <raphael@tela-botania.org>
-
 
7
* @copyright 2013 Tela-Botanica
2
// declare(encoding='UTF-8');
8
* @license   http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
-
 
9
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
-
 
10
*/
-
 
11
 
-
 
12
/**
3
/**
13
 * Service d'export de données d'observation du CEL au format XLS
4
 * Service d'export de données d'observation du CEL au format XLS.
14
 *
5
 *
15
 * Format du service :
6
 * Format du service :
16
 * POST /ExportXLS
7
 * POST /ExportXLS
17
 * POST /ExportXLS/<Utilisateur>
8
 * POST /ExportXLS/<Utilisateur>
18
 * TODO: GET /ExportXLS/<Utilisateur> [ sans "range" ? ]
9
 * TODO: GET /ExportXLS/<Utilisateur> [ sans "range" ? ]
Line 28... Line 19...
28
 * Si <Utilisateur> est fourni, celui-ci doit être authentifié
19
 * Si <Utilisateur> est fourni, celui-ci doit être authentifié
29
 * TODO: export des données public et non-sensible d'un utilisateur
20
 * TODO: export des données public et non-sensible d'un utilisateur
30
 *
21
 *
31
 * Si <Utilisateur> est fourni, le observations seront le résultat de l'intersection des 2 contraintes
22
 * Si <Utilisateur> est fourni, le observations seront le résultat de l'intersection des 2 contraintes
32
 *
23
 *
-
 
24
 * @internal   Mininum PHP version : 5.2
-
 
25
 * @category   CEL
-
 
26
 * @package    Services
-
 
27
 * @subpackage Observations
-
 
28
 * @version    0.1
-
 
29
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
-
 
30
 * @author     Raphaël Droz <raphael@tela-botania.org>
-
 
31
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
-
 
32
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
-
 
33
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
-
 
34
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
-
 
35
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
33
 */
36
 */
34
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(dirname(realpath(__FILE__))) . '/lib');
37
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(dirname(realpath(__FILE__))) . '/lib');
35
// TERM
-
 
36
ini_set('html_errors', 0);
38
ini_set('html_errors', 0);
37
ini_set('xdebug.cli_color', 2);
39
ini_set('xdebug.cli_color', 2);
38
require_once('lib/PHPExcel/Classes/PHPExcel.php');
40
require_once 'lib/PHPExcel/Classes/PHPExcel.php';
39
require_once('lib/FormateurGroupeColonne.php');
41
require_once 'lib/FormateurGroupeColonne.php';
Line 40... Line 42...
40
 
42
 
Line 41... Line 43...
41
class ExportXLS extends Cel  {
43
class ExportXLS extends Cel  {
42
 
44
 
43
	private $id_utilisateur = NULL;
-
 
Line 44... Line 45...
44
	private $parametres_defaut = array("range" => "*",
45
	private $id_utilisateur = NULL;
45
									   "format" => "CSV");
46
	private $parametres_defaut = array('range' => '*', 'format' => 'CSV');
46
	
47
 
47
	private $filtres_autorises = array(
48
	private $filtres_autorises = array(
Line 54... Line 55...
54
			'date_debut' => 'date_debut',
55
		'date_debut' => 'date_debut',
55
			'date_fin' => 'date_fin',
56
		'date_fin' => 'date_fin',
56
			'taxon' => 'taxon'
57
		'taxon' => 'taxon'
57
	);
58
	);
Line 58... Line 59...
58
 
59
 
59
	function ExportXLS($config) {
60
	public function __construct($config) {
60
		parent::__construct($config);
61
		parent::__construct($config);
Line 61... Line 62...
61
	}
62
	}
62
	
63
 
63
	function getRessource() {
64
	public function getRessource() {
Line 64... Line 65...
64
		return $this->getElement(array());
65
		return $this->getElement(array());
65
	}
66
	}
66
	
67
 
67
	function getElement($uid) {
68
	public function getElement($uid) {
68
		$parametres_format = $this->traiterParametresFormat($uid, $_GET);	
69
		$parametres_format = $this->traiterParametresFormat($uid, $_GET);
69
		$filtres = $this->traiterFiltres($_GET);
70
		$filtres = $this->traiterFiltres($_GET);
Line 70... Line 71...
70
		$this->export($parametres_format, $filtres);
71
		$this->export($parametres_format, $filtres);
71
		exit;
72
		exit();
72
	}
73
	}
73
			
74
 
74
	function traiterParametresFormat($uid, $params) {
75
	private function traiterParametresFormat($uid, $params) {
75
		$parametres = $this->parametres_defaut;
76
		$parametres = $this->parametres_defaut;
Line 87... Line 88...
87
		$parametres['groupe_champs'] = isset($params['colonnes']) ? $params['colonnes'] : 'standard,avance';
88
		$parametres['groupe_champs'] = isset($params['colonnes']) ? $params['colonnes'] : 'standard,avance';
Line 88... Line 89...
88
		
89
 
89
		return $parametres;
90
		return $parametres;
Line 90... Line 91...
90
	}
91
	}
91
	
92
 
92
	function traiterIdUtilisateur($uid) {
93
	private function traiterIdUtilisateur($uid) {
93
		$id_utilisateur = null;
94
		$id_utilisateur = null;
94
		// TODO: controleUtilisateur()
95
		// TODO: controleUtilisateur()
95
		if(isset($uid[0])) {
96
		if (isset($uid[0])) {
96
			$id_utilisateur = intval($uid[0]);
97
			$id_utilisateur = intval($uid[0]);
97
		}
98
		}
Line 98... Line 99...
98
		return $id_utilisateur;
99
		return $id_utilisateur;
99
	}
100
	}
100
	
101
 
101
	function traiterFiltres($params) {
102
	private function traiterFiltres($params) {
102
		$obs_ids = $this->traiterObsIds($params);
103
		$obs_ids = $this->traiterObsIds($params);
103
		$filtres = array();
-
 
104
		if(!$obs_ids || count($obs_ids) == 1 && $obs_ids[0] == '*') {
104
		$filtres = array();
105
			unset($filtres['sql_brut']);
105
		if (!$obs_ids || count($obs_ids) == 1 && $obs_ids[0] == '*') {
106
		}
106
			unset($filtres['sql_brut']);
107
		else {
107
		} else {
108
			$filtres = Array('sql_brut' =>
108
			$filtres = Array('sql_brut' =>
109
			sprintf('id_observation IN (%s)', implode(',', $obs_ids)));
109
			sprintf('id_observation IN (%s)', implode(',', $obs_ids)));
Line 114... Line 114...
114
			}
114
			}
115
		}
115
		}
116
		return $filtres;
116
		return $filtres;
117
	}
117
	}
Line 118... Line -...
118
	
-
 
119
	
118
 
120
	function traiterObsIds($params) {
119
	private function traiterObsIds($params) {
121
		$obs_ids = Array('*');
120
		$obs_ids = Array('*');
122
		if (isset($params['range']) && trim($params['range']) != '*') {
121
		if (isset($params['range']) && trim($params['range']) != '*') {
123
			// trim() car: `POST http://url<<<"range=*"`
122
			// trim() car: `POST http://url<<<"range=*"`
124
			$obs_ids = self::rangeToList(trim($params['range']));
123
			$obs_ids = self::rangeToList(trim($params['range']));
Line 129... Line 128...
129
	/*
128
	/*
130
	 * $param: Tableau associatif, indexes supportés:
129
	 * $param: Tableau associatif, indexes supportés:
131
	 * 		   - widget: le nom du widget d'origine (utilisé pour les méta-données du tableur)
130
	 *   - widget: le nom du widget d'origine (utilisé pour les méta-données du tableur)
132
	 *
131
	 *
133
	 */
132
	 */
134
	function export(Array $parametres_format = Array(),Array $filtres = array()) {
133
	private function export(Array $parametres_format = Array(),Array $filtres = array()) {
135
		$chercheur_observations = new RechercheObservation($this->config);
134
		$chercheur_observations = new RechercheObservation($this->config);
Line 136... Line 135...
136
 
135
 
137
		$observations = $chercheur_observations
136
		$observations = $chercheur_observations
138
			->rechercherObservations($parametres_format['id_utilisateur'], $filtres, $parametres_format['debut'], $parametres_format['limite'], TRUE)
137
			->rechercherObservations($parametres_format['id_utilisateur'], $filtres, $parametres_format['debut'], $parametres_format['limite'], TRUE)
139
			->get();
138
			->get();
140
		// debug //echo ($chercheur_observations->requete_selection_observations);
139
		// debug //echo ($chercheur_observations->requete_selection_observations);
141
		// XXX: malheureusement l'instance de JRest n'est pas accessible ici
140
		// XXX: malheureusement l'instance de JRest n'est pas accessible ici
142
		if(!$observations) {
141
		if (!$observations) {
143
			header('HTTP/1.0 204 No Content');
142
			header('HTTP/1.0 204 No Content');
144
			exit;
143
			exit();
Line 145... Line 144...
145
		}
144
		}
146
 
145
 
-
 
146
		if ($parametres_format['format'] == 'pdf') {
-
 
147
			if (count($observations) > 300) {
147
		if($parametres_format['format'] == 'pdf') {
148
				die('too much');
Line 148... Line 149...
148
			if(count($observations) > 300) die('too much');
149
			}
149
			require_once('GenerateurPDF.php');
150
			require_once('GenerateurPDF.php');
150
 
151
 
151
			$pdf = new GenerateurPDF();
152
			$pdf = new GenerateurPDF();
152
			$pdf->export($observations);
153
			$pdf->export($observations);
153
			//$pdf->export1($observations);
154
			//$pdf->export1($observations);
154
			//$pdf->export2($observations);
155
			//$pdf->export2($observations);
Line 155... Line 156...
155
			$pdf->pdf->Output('etiquettes.pdf', 'I');
156
			$pdf->pdf->Output('etiquettes.pdf', 'I');
156
			die;
157
			die();
157
		}
158
		}
Line 173... Line 174...
173
 
174
 
174
		$this->envoyerFeuille($objPHPExcel, $parametres_format);
175
		$this->envoyerFeuille($objPHPExcel, $parametres_format);
Line 175... Line 176...
175
	}
176
	}
176
	
177
 
177
	private function envoyerFeuille($objPHPExcel, $parametres_format) {
178
	private function envoyerFeuille($objPHPExcel, $parametres_format) {
178
		header("Content-Type: application/vnd.ms-excel");
179
		header('Content-Type: application/vnd.ms-excel');
Line 179... Line 180...
179
		header("Content-Disposition: attachment; filename=\"liste.xls\"; charset=utf-8");
180
		header('Content-Disposition: attachment; filename="liste.xls"; charset=utf-8');
180
		header("Cache-Control: max-age=0");
181
		header('Cache-Control: max-age=0');
181
		
182
 
182
		// csv|xls|xlsx => CSV|Excel5|Excel2007
183
		// csv|xls|xlsx => CSV|Excel5|Excel2007
183
		// Note: le format Excel2007 utilise un fichier temporaire
184
		// Note: le format Excel2007 utilise un fichier temporaire
184
		$generateur = PHPExcel_IOFactory::createWriter($objPHPExcel, $parametres_format['format']);
185
		$generateur = PHPExcel_IOFactory::createWriter($objPHPExcel, $parametres_format['format']);
Line 185... Line 186...
185
		$generateur->save('php://output');
186
		$generateur->save('php://output');
186
		exit;
187
		exit;
187
	}
188
	}
188
	
189
 
189
	public function traiterLigneObservation(&$obs, &$colonnes, &$feuille, $no_ligne) {
190
	private function traiterLigneObservation(&$obs, &$colonnes, &$feuille, $no_ligne) {
-
 
191
		$no_colonne = 0;
-
 
192
		foreach ($colonnes as $abbrev => $colonne) {
Line 190... Line 193...
190
		$no_colonne = 0;
193
			$valeur = null;
191
		foreach($colonnes as $abbrev => $colonne) {
194
			if ($colonne['extra'] == 2 || ! is_null($colonne['dyna'])) {
-
 
195
				continue;
-
 
196
			}
Line 192... Line 197...
192
			$valeur = null;
197
 
193
			if($colonne['extra'] == 2 || ! is_null($colonne['dyna'])) continue;
198
			// valeur direct depuis cel_obs ?
194
		
199
			if (isset($obs[$abbrev])) {
195
			// valeur direct depuis cel_obs ?
200
				$valeur = $obs[$abbrev];
196
			if(isset($obs[$abbrev])) $valeur = $obs[$abbrev];
201
			}
197
 
202
 
198
			// pré-processeur de la champs
203
			// pré-processeur de la champs
199
			if(function_exists($colonne['fonction'])) {
204
			if (function_exists($colonne['fonction'])) {
200
				$valeur = $colonne['fonction']($valeur);
205
				$valeur = $colonne['fonction']($valeur);
201
			} elseif(method_exists('FormateurGroupeColonne', $colonne['fonction'])) {
-
 
202
				$valeur = call_user_func(array('FormateurGroupeColonne', $colonne['fonction']), $valeur);
-
 
203
			} elseif(method_exists(__CLASS__, $colonne['fonction'])) {
206
			} else if (method_exists('FormateurGroupeColonne', $colonne['fonction'])) {
204
				$valeur = call_user_func(array(__CLASS__, $colonne['fonction']), $valeur);
207
				$valeur = call_user_func(array('FormateurGroupeColonne', $colonne['fonction']), $valeur);
205
			} elseif($colonne['fonction']) {
-
 
206
				die("méthode {$colonne['fonction']} introuvable");
208
			} else if (method_exists(__CLASS__, $colonne['fonction'])) {
207
			}
209
				$valeur = call_user_func(array(__CLASS__, $colonne['fonction']), $valeur);
208
			// fonction pour obtenir des champs (étendus)
210
			} else if ($colonne['fonction']) {
Line 209... Line 211...
209
			elseif(function_exists($colonne['fonction_data'])) {
211
				die("méthode {$colonne['fonction']} introuvable");
210
				$valeur = $colonne['fonction_data']($obs);
212
			} else if (function_exists($colonne['fonction_data'])) { // fonction pour obtenir des champs (étendus)
Line 237... Line 239...
237
		$objPHPExcel->getProperties()->setCreator($parametres_format['widget']) // ou $uid ?
239
		$objPHPExcel->getProperties()->setCreator($parametres_format['widget']) // ou $uid ?
238
		->setLastModifiedBy("XX") // TODO: $uid
240
			->setLastModifiedBy("XX") // TODO: $uid
239
		->setTitle("Export des observation du carnet en ligne") // TODO
241
			->setTitle("Export des observation du carnet en ligne") // TODO
240
		->setSubject("Export") // TODO
242
			->setSubject("Export") // TODO
241
		->setDescription("Export");
243
			->setDescription("Export");
242
		//->setKeywords("office PHPExcel php")
-
 
243
		//->setCategory("Test result file")
-
 
Line 244... Line 244...
244
		
244
 
245
		$objPHPExcel->getActiveSheet()->setTitle("Observations");
245
		$objPHPExcel->getActiveSheet()->setTitle("Observations");
246
		return $objPHPExcel;
246
		return $objPHPExcel;
Line 247... Line 247...
247
	}
247
	}
248
	
248
 
249
	private function formaterColonnesFeuille(&$feuille, &$colonnes, $parametres_format) {
249
	private function formaterColonnesFeuille(&$feuille, &$colonnes, $parametres_format) {
250
		$colid = 0;
250
		$colid = 0;
-
 
251
		foreach ($colonnes as $colonne) {
-
 
252
			if ($colonne['extra'] == 2) {
Line 251... Line 253...
251
		foreach($colonnes as $colonne) {
253
				continue;
252
			if($colonne['extra'] == 2) continue;
254
			}
253
		
255
 
254
			$feuille->setCellValueByColumnAndRow($colid, 1, $colonne['nom']);
256
			$feuille->setCellValueByColumnAndRow($colid, 1, $colonne['nom']);
Line 268... Line 270...
268
						'type' => PHPExcel_Style_Fill::FILL_SOLID,
270
						'type' => PHPExcel_Style_Fill::FILL_SOLID,
269
						'color' => array('rgb' => PHPExcel_Style_Color::COLOR_YELLOW)
271
						'color' => array('rgb' => PHPExcel_Style_Color::COLOR_YELLOW)
270
					)
272
					)
271
				);
273
				);
272
			}
274
			}
273
		
-
 
274
			$colid++;
275
			$colid++;
275
		}
276
		}
276
	}
277
	}
Line 277... Line 278...
277
 
278
 
Line 285... Line 286...
285
		$outSets = array();
286
		$outSets = array();
Line 286... Line 287...
286
 
287
 
287
		foreach($inSets as $inSet) {
288
		foreach ($inSets as $inSet) {
288
			list($start,$end) = explode('-', $inSet . '-' . $inSet);
289
			list($start,$end) = explode('-', $inSet . '-' . $inSet);
289
			// ignore les ranges trop importants
290
			// ignore les ranges trop importants
-
 
291
			if ($start > 10000000 || $end > 10000000 || abs($start-$end) > 10000) {
-
 
292
				continue;
290
			if($start > 10000000 || $end > 10000000 || abs($start-$end) > 10000) continue;
293
			}
291
			$outSets = array_merge($outSets,range($start,$end));
294
			$outSets = array_merge($outSets,range($start,$end));
292
		}
295
		}
293
		$outSets = array_unique($outSets);
296
		$outSets = array_unique($outSets);
294
		$outSets = array_filter($outSets, 'is_numeric');
297
		$outSets = array_filter($outSets, 'is_numeric');