noalyss Version-9
noalyss_csv.class.php
Go to the documentation of this file.
1<?php
2
3/*
4 * Copyright (C) 2015 Dany De Bontridder <dany@alchimerys.be>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21
22/**
23 * @file
24 * @brief Manage the CSV : manage files and write CSV record
25 *
26 */
27
28
29/**
30 * @brief Manage the CSV : manage files and write CSV record
31 *
32 */
34{
35
36 private $filename;
37 private $element;
38 private $sep_field;
39 private $sep_dec;
40 private $encoding;
41 private $size;
42
43 function __construct($p_filename)
44 {
45 $this->filename=$p_filename;
46 $this->element=array();
47 $this->size=0;
48
49 $a_field=[';',','];
50 $this->sep_field=$a_field[$_SESSION[SESSION_KEY.'csv_fieldsep']];
51 $a_field=['.',','];
52 $this->sep_dec=$a_field[$_SESSION[SESSION_KEY.'csv_decimal']];
53 $this->encoding=$_SESSION[SESSION_KEY.'csv_encoding'];
54
55 }
56
57 /***
58 * @brief
59 * Correct the name of the file , remove forbidden character and
60 * add extension and date
61 */
62 protected function correct_name()
63 {
64 if (trim(strlen($this->filename))==0) {
65 record_log('CSV->correct_name filename is empty');
66 throw new Exception('CSV-CORRECT_NAME');
67 }
68 $this->filename.="-".date("ymd-Hi");
69 $this->filename.=".csv";
70
71 $this->filename=noalyss_str_replace(";", "", $this->filename);
72 $this->filename=noalyss_str_replace("/", "", $this->filename);
73 $this->filename=noalyss_str_replace(":", "", $this->filename);
74 $this->filename=noalyss_str_replace("*", "", $this->filename);
75 $this->filename=noalyss_str_replace(" ", "_", $this->filename);
76 $this->filename=strtolower($this->filename);
77 }
78
79 /***
80 *@brief Send an header for CSV , the filename is corrected
81 */
82 function send_header()
83 {
84 $this->correct_name();
85 header('Pragma: public');
86 header('Content-type: application/csv');
87 header("Content-Disposition: attachment;filename=\"{$this->filename}\"",
88 FALSE);
89 header('Cache-Control: no-store, no-cache, must-revalidate');
90 header('Expires: Sun, 1 Jan 2000 12:00:00 GMT');
91 header('Last-Modified: '.gmdate('D, d M Y H:i:s').'GMT');
92 }
93
94 /***
95 * @brief write header
96 * @param array $p_array Array of 1 dimension with the contains of a row
97 *
98 */
100 {
101 $size_array=count($p_array);
102 $sep="";
103 for ($i=0; $i<$size_array; $i++)
104 {
105
106 printf($sep.'"%s"', $this->encode($p_array[$i]));
108 }
109 printf("\r\n");
110 }
111 /***
112 * @brief Add column to export to csv , the string are enclosed with
113 * double-quote,
114 * @param $p_item value to export
115 * @param $p_type must be text(default) or number
116 * @throws Exception
117 */
118 function add($p_item,$p_type="text")
119 {
120 if ( ! in_array($p_type, array("text","number"))) {
121 throw new Exception("NOALYSS_CSV::ADD");
122 }
123 $this->element[$this->size]['value']=$p_item;
124 $this->element[$this->size]['type']=$p_type;
125 $this->size++;
126 }
127 /***
128 * @brief the string are enclosed with double-quote,
129 * we remove problematic character and
130 * the number are formatted.
131 * Clean the row after exporting
132 * @return nothing
133 */
134 function write()
135 {
136 if ($this->size == 0 ) return;
137 $sep="";
138 for ($i=0;$i < $this->size;$i++)
139 {
140 if ($this->element[$i]['type'] == 'number' )
141 {
142 printf($sep.'%s', $this->nb($this->element[$i]['value']));
143 }
144 else
145 {
146 $export=($this->element[$i]['value']==null)?"":$this->element[$i]['value'];
147 // remove break-line,
150 // remove double quote
152 printf($sep.'"%s"', $this->encode($export));
153 }
155 }
156 printf("\r\n");
157 $this->clean();
158 }
159 /**
160 * clean the row
161 */
162 protected function clean()
163 {
164 $this->element=array();
165 $this->size=0;
166 }
167 /**
168 * format the number for the CSV export
169 * @param $p_number number
170 */
171 protected function nb($p_number)
172 {
173 $p_number=noalyss_trim($p_number);
174 if ($p_number=="") {return $p_number;}
175 if ( isNumber($p_number) == 1 ) {
176 $r=number_format($p_number, 4, $this->sep_dec,'');
177 } else {
178 $r=$p_number;
179 }
180 return $r;
181 }
182 protected function encode($str)
183 {
184 if ($this->encoding=="utf8") return $str;
185 if ($this->encoding=="latin1") return mb_convert_encoding($str,'ISO-8859-1','UTF-8');
186 throw new Exception(_("Encodage invalide"));
187 }
188 /**
189 * @return mixed
190 */
191 public function get_filename()
192 {
193 return $this->filename;
194 return $this;
195 }
196
197 /**
198 * @param mixed $filename
199 */
200 public function set_filename($filename)
201 {
202 $this->filename = $filename;
203 return $this;
204 }
205
206 /**
207 * @return array
208 */
209 public function get_element()
210 {
211 return $this->element;
212 return $this;
213 }
214
215 /**
216 * @param array $element
217 */
218 public function set_element($element)
219 {
220 $this->element = $element;
221 return $this;
222 }
223
224 /**
225 * @return mixed
226 */
227 public function get_sep_field()
228 {
229 return $this->sep_field;
230 return $this;
231 }
232
233 /**
234 * @param mixed $sep_field
235 */
236 public function set_sep_field($sep_field)
237 {
238 $this->sep_field = $sep_field;
239 return $this;
240 }
241
242 /**
243 * @return mixed
244 */
245 public function get_sep_dec()
246 {
247 return $this->sep_dec;
248 return $this;
249 }
250
251 /**
252 * @param mixed $sep_dec
253 */
254 public function set_sep_dec($sep_dec)
255 {
256 $this->sep_dec = $sep_dec;
257 return $this;
258 }
259
260 /**
261 * @return mixed
262 */
263 public function get_encoding()
264 {
265 return $this->encoding;
266 return $this;
267 }
268
269 /**
270 * @param mixed $encoding
271 */
272 public function set_encoding($encoding)
273 {
274 $this->encoding = $encoding;
275 return $this;
276 }
277
278 /**
279 * @return int
280 */
281 public function get_size()
282 {
283 return $this->size;
284 return $this;
285 }
286
287 /**
288 * @param int $size
289 */
290 public function set_size($size)
291 {
292 $this->size = $size;
293 return $this;
294 }
295
296}
isNumber($p_int)
Definition: ac_common.php:215
noalyss_str_replace($search, $replace, $string)
Definition: ac_common.php:1553
record_log($p_message)
Record an error message into the log file of the server.
Definition: ac_common.php:1342
noalyss_trim($p_string)
Definition: ac_common.php:1545
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
$name size
Manage the CSV : manage files and write CSV record.
set_filename($filename)
set_element($element)
set_encoding($encoding)
clean()
clean the row
set_sep_field($sep_field)
set_sep_dec($sep_dec)
write_header($p_array)
__construct($p_filename)
add($p_item, $p_type="text")
nb($p_number)
format the number for the CSV export
$str
Definition: fiche.inc.php:91