noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
Noalyss_Csv Class Reference

Manage the CSV : manage files and write CSV record. More...

+ Collaboration diagram for Noalyss_Csv:

Public Member Functions

 __construct ($p_filename)
 
 add ($p_item, $p_type="text")
 Add column to export to csv , the string are enclosed with double-quote,.
 
 get_element ()
 
 get_encoding ()
 
 get_filename ()
 
 get_sep_dec ()
 
 get_sep_field ()
 
 get_size ()
 
 send_header ()
 Send an header for CSV , the filename is corrected.
 
 set_element ($element)
 
 set_encoding ($encoding)
 
 set_filename ($filename)
 
 set_sep_dec ($sep_dec)
 
 set_sep_field ($sep_field)
 
 set_size ($size)
 
 write ()
 the string are enclosed with double-quote, we remove problematic character and the number are formatted.
 
 write_header ($p_array)
 write header
 

Static Public Member Functions

static csv2table ($string)
 convert CSV strings (content of a file) into HTML table,
 

Protected Member Functions

 clean ()
 clean the row
 
 correct_name ()
 Correct the name of the file , remove forbidden character and add extension and date.
 
 encode ($str)
 
 nb ($p_number)
 format the number for the CSV export
 

Private Attributes

 $element
 
 $encoding
 
 $filename
 
 $sep_dec
 
 $sep_field
 
 $size
 

Detailed Description

Manage the CSV : manage files and write CSV record.

Definition at line 33 of file noalyss_csv.class.php.

Constructor & Destructor Documentation

◆ __construct()

Noalyss_Csv::__construct ( $p_filename)

Definition at line 43 of file noalyss_csv.class.php.

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 }
$name size

References size.

Member Function Documentation

◆ add()

Noalyss_Csv::add ( $p_item,
$p_type = "text" )

Add column to export to csv , the string are enclosed with double-quote,.

Parameters
$p_itemvalue to export
$p_typemust be text(default) or number
Exceptions
Exception

Definition at line 123 of file noalyss_csv.class.php.

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 }

References $p_type, $size, and size.

◆ clean()

Noalyss_Csv::clean ( )
protected

clean the row

Definition at line 167 of file noalyss_csv.class.php.

168 {
169 $this->element=array();
170 $this->size=0;
171 }

References size.

Referenced by write().

◆ correct_name()

Noalyss_Csv::correct_name ( )
protected

Correct the name of the file , remove forbidden character and add extension and date.

Definition at line 62 of file noalyss_csv.class.php.

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 }
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...

References noalyss_str_replace(), and record_log().

Referenced by send_header().

+ Here is the call graph for this function:

◆ csv2table()

static Noalyss_Csv::csv2table ( $string)
static

convert CSV strings (content of a file) into HTML table,

Parameters
$stringCSV files

Definition at line 304 of file noalyss_csv.class.php.

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 }
h( $row[ 'oa_description'])

References $aRow, $encoding, $row, $sep_dec, $sep_field, $string, and h.

◆ encode()

Noalyss_Csv::encode ( $str)
protected

Definition at line 187 of file noalyss_csv.class.php.

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 }
_("actif, passif,charge,...")

References $str, and _.

◆ get_element()

Noalyss_Csv::get_element ( )
Returns
array

Definition at line 214 of file noalyss_csv.class.php.

215 {
216 return $this->element;
217 return $this;
218 }

References $element.

◆ get_encoding()

Noalyss_Csv::get_encoding ( )
Returns
mixed

Definition at line 268 of file noalyss_csv.class.php.

269 {
270 return $this->encoding;
271 return $this;
272 }

References $encoding.

◆ get_filename()

Noalyss_Csv::get_filename ( )
Returns
mixed

Definition at line 196 of file noalyss_csv.class.php.

197 {
198 return $this->filename;
199 return $this;
200 }

References $filename.

◆ get_sep_dec()

Noalyss_Csv::get_sep_dec ( )
Returns
mixed

Definition at line 250 of file noalyss_csv.class.php.

251 {
252 return $this->sep_dec;
253 return $this;
254 }

References $sep_dec.

◆ get_sep_field()

Noalyss_Csv::get_sep_field ( )
Returns
mixed

Definition at line 232 of file noalyss_csv.class.php.

233 {
234 return $this->sep_field;
235 return $this;
236 }

References $sep_field.

◆ get_size()

Noalyss_Csv::get_size ( )
Returns
int

Definition at line 286 of file noalyss_csv.class.php.

287 {
288 return $this->size;
289 return $this;
290 }

References $size.

◆ nb()

Noalyss_Csv::nb ( $p_number)
protected

format the number for the CSV export

Parameters
$p_numbernumber

Definition at line 176 of file noalyss_csv.class.php.

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 }
isNumber($p_int)
noalyss_trim($p_string)
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r

References $r, isNumber(), and noalyss_trim().

+ Here is the call graph for this function:

◆ send_header()

Noalyss_Csv::send_header ( )

Send an header for CSV , the filename is corrected.

If TEST_UNIT is defined, the function is called from a test file (located in scenario , see manual )and no header are requested

Definition at line 83 of file noalyss_csv.class.php.

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 }
correct_name()
Correct the name of the file , remove forbidden character and add extension and date.

References correct_name().

+ Here is the call graph for this function:

◆ set_element()

Noalyss_Csv::set_element ( $element)
Parameters
array$element

Definition at line 223 of file noalyss_csv.class.php.

224 {
225 $this->element = $element;
226 return $this;
227 }

References $element.

◆ set_encoding()

Noalyss_Csv::set_encoding ( $encoding)
Parameters
mixed$encoding

Definition at line 277 of file noalyss_csv.class.php.

278 {
279 $this->encoding = $encoding;
280 return $this;
281 }

References $encoding.

◆ set_filename()

Noalyss_Csv::set_filename ( $filename)
Parameters
mixed$filename

Definition at line 205 of file noalyss_csv.class.php.

206 {
207 $this->filename = $filename;
208 return $this;
209 }

References $filename.

◆ set_sep_dec()

Noalyss_Csv::set_sep_dec ( $sep_dec)
Parameters
mixed$sep_dec

Definition at line 259 of file noalyss_csv.class.php.

260 {
261 $this->sep_dec = $sep_dec;
262 return $this;
263 }

References $sep_dec.

◆ set_sep_field()

Noalyss_Csv::set_sep_field ( $sep_field)
Parameters
mixed$sep_field

Definition at line 241 of file noalyss_csv.class.php.

242 {
243 $this->sep_field = $sep_field;
244 return $this;
245 }

References $sep_field.

◆ set_size()

Noalyss_Csv::set_size ( $size)
Parameters
int$size

Definition at line 295 of file noalyss_csv.class.php.

296 {
297 $this->size = $size;
298 return $this;
299 }

References $size, and size.

◆ write()

Noalyss_Csv::write ( )

the string are enclosed with double-quote, we remove problematic character and the number are formatted.

Clean the row after exporting

Returns
nothing

Definition at line 139 of file noalyss_csv.class.php.

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 }
clean()
clean the row

References $export, $i, $sep, $sep_field, $size, clean(), noalyss_str_replace(), and size.

+ Here is the call graph for this function:

◆ write_header()

Noalyss_Csv::write_header ( $p_array)

write header

Parameters
array$p_arrayArray of 1 dimension with the contains of a row

Definition at line 104 of file noalyss_csv.class.php.

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 }

References $i, $p_array, $sep, and $sep_field.

Field Documentation

◆ $element

Noalyss_Csv::$element
private

Definition at line 37 of file noalyss_csv.class.php.

Referenced by get_element(), and set_element().

◆ $encoding

Noalyss_Csv::$encoding
private

Definition at line 40 of file noalyss_csv.class.php.

Referenced by csv2table(), get_encoding(), and set_encoding().

◆ $filename

Noalyss_Csv::$filename
private

Definition at line 36 of file noalyss_csv.class.php.

Referenced by get_filename(), and set_filename().

◆ $sep_dec

Noalyss_Csv::$sep_dec
private

Definition at line 39 of file noalyss_csv.class.php.

Referenced by csv2table(), get_sep_dec(), and set_sep_dec().

◆ $sep_field

Noalyss_Csv::$sep_field
private

Definition at line 38 of file noalyss_csv.class.php.

Referenced by csv2table(), get_sep_field(), set_sep_field(), write(), and write_header().

◆ $size

Noalyss_Csv::$size
private

Definition at line 41 of file noalyss_csv.class.php.

Referenced by add(), get_size(), set_size(), and write().


The documentation for this class was generated from the following file: