noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
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. If TEST_UNIT is defined, the function is called
81 * from a test file (located in scenario , see manual )and no header are requested
82 */
83 function send_header()
84 {
85 $this->correct_name();
86 if ( defined('TEST_UNIT')) {
87
88 return;
89 }
90 header('Pragma: public');
91 header('Content-type: application/csv');
92 header("Content-Disposition: attachment;filename=\"{$this->filename}\"",
93 FALSE);
94 header('Cache-Control: no-store, no-cache, must-revalidate');
95 header('Expires: Sun, 1 Jan 2000 12:00:00 GMT');
96 header('Last-Modified: '.gmdate('D, d M Y H:i:s').'GMT');
97 }
98
99 /***
100 * @brief write header
101 * @param array $p_array Array of 1 dimension with the contains of a row
102 *
103 */
105 {
106 $size_array=count($p_array);
107 $sep="";
108 for ($i=0; $i<$size_array; $i++)
109 {
110
111 printf($sep.'"%s"', $this->encode($p_array[$i]));
113 }
114 printf("\r\n");
115 }
116 /***
117 * @brief Add column to export to csv , the string are enclosed with
118 * double-quote,
119 * @param $p_item value to export
120 * @param $p_type must be text(default) or number
121 * @throws Exception
122 */
123 function add($p_item,$p_type="text")
124 {
125 if ( ! in_array($p_type, array("text","number"))) {
126 throw new Exception("NOALYSS_CSV::ADD");
127 }
128 $this->element[$this->size]['value']=$p_item;
129 $this->element[$this->size]['type']=$p_type;
130 $this->size++;
131 }
132 /***
133 * @brief the string are enclosed with double-quote,
134 * we remove problematic character and
135 * the number are formatted.
136 * Clean the row after exporting
137 * @return nothing
138 */
139 function write()
140 {
141 if ($this->size == 0 ) return;
142 $sep="";
143 for ($i=0;$i < $this->size;$i++)
144 {
145 if ($this->element[$i]['type'] == 'number' )
146 {
147 printf($sep.'%s', $this->nb($this->element[$i]['value']));
148 }
149 else
150 {
151 $export=($this->element[$i]['value']==null)?"":$this->element[$i]['value'];
152 // remove break-line,
155 // remove double quote
157 printf($sep.'"%s"', $this->encode($export));
158 }
160 }
161 printf("\r\n");
162 $this->clean();
163 }
164 /**
165 * clean the row
166 */
167 protected function clean()
168 {
169 $this->element=array();
170 $this->size=0;
171 }
172 /**
173 * format the number for the CSV export
174 * @param $p_number number
175 */
176 protected function nb($p_number)
177 {
178 $p_number=noalyss_trim($p_number);
179 if ($p_number=="") {return $p_number;}
180 if ( isNumber($p_number) == 1 ) {
181 $r=number_format($p_number, 4, $this->sep_dec,'');
182 } else {
183 $r=$p_number;
184 }
185 return $r;
186 }
187 protected function encode($str)
188 {
189 if ($this->encoding=="utf8") return $str;
190 if ($this->encoding=="latin1") return mb_convert_encoding($str,'ISO-8859-1','UTF-8');
191 throw new Exception(_("Encodage invalide"));
192 }
193 /**
194 * @return mixed
195 */
196 public function get_filename()
197 {
198 return $this->filename;
199 return $this;
200 }
201
202 /**
203 * @param mixed $filename
204 */
205 public function set_filename($filename)
206 {
207 $this->filename = $filename;
208 return $this;
209 }
210
211 /**
212 * @return array
213 */
214 public function get_element()
215 {
216 return $this->element;
217 return $this;
218 }
219
220 /**
221 * @param array $element
222 */
223 public function set_element($element)
224 {
225 $this->element = $element;
226 return $this;
227 }
228
229 /**
230 * @return mixed
231 */
232 public function get_sep_field()
233 {
234 return $this->sep_field;
235 return $this;
236 }
237
238 /**
239 * @param mixed $sep_field
240 */
241 public function set_sep_field($sep_field)
242 {
243 $this->sep_field = $sep_field;
244 return $this;
245 }
246
247 /**
248 * @return mixed
249 */
250 public function get_sep_dec()
251 {
252 return $this->sep_dec;
253 return $this;
254 }
255
256 /**
257 * @param mixed $sep_dec
258 */
259 public function set_sep_dec($sep_dec)
260 {
261 $this->sep_dec = $sep_dec;
262 return $this;
263 }
264
265 /**
266 * @return mixed
267 */
268 public function get_encoding()
269 {
270 return $this->encoding;
271 return $this;
272 }
273
274 /**
275 * @param mixed $encoding
276 */
277 public function set_encoding($encoding)
278 {
279 $this->encoding = $encoding;
280 return $this;
281 }
282
283 /**
284 * @return int
285 */
286 public function get_size()
287 {
288 return $this->size;
289 return $this;
290 }
291
292 /**
293 * @param int $size
294 */
295 public function set_size($size)
296 {
297 $this->size = $size;
298 return $this;
299 }
300 /**
301 * @brief convert CSV strings (content of a file) into HTML table,
302 * @param $string CSV files
303 */
304 static function csv2table ($string)
305 {
306 $a_field=[';',','];
307 $sep_field=$a_field[$_SESSION[SESSION_KEY.'csv_fieldsep']];
308 $a_field=['.',','];
309 $sep_dec=$a_field[$_SESSION[SESSION_KEY.'csv_decimal']];
310 $encoding=$_SESSION[SESSION_KEY.'csv_encoding'];
311
312 $aRow=explode("\r\n", $string);
313
314 echo '<table class="result">';
315 foreach ($aRow as $row) {
316 echo '<tr>';
317 $aCol=explode($sep_field, $row);
318 foreach ($aCol as $col) {
319 echo '<td>';
320 echo h($col);
321 echo '</td>';
322 }
323 echo '</tr>';
324
325 }
326 echo '</table>';
327 }
328}
isNumber($p_int)
noalyss_str_replace($search, $replace, $string)
record_log($p_message)
Record an error message into the log file of the server or in the log folder of NOALYSS Record also t...
noalyss_trim($p_string)
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
h( $row[ 'oa_description'])
$name size
_("actif, passif,charge,...")
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)
write header
__construct($p_filename)
add($p_item, $p_type="text")
Add column to export to csv , the string are enclosed with double-quote,.
static csv2table($string)
convert CSV strings (content of a file) into HTML table,
write()
the string are enclosed with double-quote, we remove problematic character and the number are formatt...
correct_name()
Correct the name of the file , remove forbidden character and add extension and date.
send_header()
Send an header for CSV , the filename is corrected.
nb($p_number)
format the number for the CSV export