noalyss  Version-6.9.1.8
 All Data Structures Namespaces Files Functions Variables Pages
class_noalyss_csv.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 
29 {
30 
31  private $filename;
32  private $element;
33 
34  function __construct($p_filename)
35  {
36  $this->filename=$p_filename;
37  $this->element=array();
38  $this->size=0;
39  }
40 
41  /***
42  * @brief
43  * Correct the name of the file , remove forbidden character and
44  * add extension and date
45  */
46  private function correct_name()
47  {
48  if (trim(strlen($this->filename))==0) {
49  error_log('CSV->correct_name filename is empty');
50  throw new Exception('CSV-CORRECT_NAME');
51  }
52  $this->filename.="-".date("ymd-Hi");
53  $this->filename.=".csv";
54 
55  $this->filename=str_replace(";", "", $this->filename);
56  $this->filename=str_replace("/", "", $this->filename);
57  $this->filename=str_replace(":", "", $this->filename);
58  $this->filename=str_replace("*", "", $this->filename);
59  $this->filename=str_replace(" ", "_", $this->filename);
60  $this->filename=strtolower($this->filename);
61  }
62 
63  /***
64  * Send an header for CSV , the filename is corrected
65  */
66  function send_header()
67  {
68  $this->correct_name();
69  header('Pragma: public');
70  header('Content-type: application/csv');
71  header("Content-Disposition: attachment;filename=\"{$this->filename}\"",
72  FALSE);
73  header('Cache-Control: no-store, no-cache, must-revalidate');
74  header('Expires: Sun, 1 Jan 2000 12:00:00 GMT');
75  header('Last-Modified: '.gmdate('D, d M Y H:i:s').'GMT');
76  }
77 
78  /***
79  * write header
80  * @param array $p_array Array of 1 dimension with the contains of a row
81  *
82  */
84  {
85  $size_array=count($p_array);
86  $sep="";
87  for ($i=0; $i<$size_array; $i++)
88  {
89  printf($sep.'"%s"', $p_array[$i]);
90  $sep=";";
91  }
92  printf("\r\n");
93  }
94  /***
95  * Add column to export to csv , the string are enclosed with
96  * double-quote,
97  * @param $p_item value to export
98  * @param $p_type must be text(default) or number
99  * @throws Exception
100  */
101  function add($p_item,$p_type="text")
102  {
103  if ( ! in_array($p_type, array("text","number"))) {
104  throw new Exception("NOALYSS_CSV::ADD");
105  }
106  $this->element[$this->size]['value']=$p_item;
107  $this->element[$this->size]['type']=$p_type;
108  $this->size++;
109  }
110  /***
111  * the string are enclosed with double-quote,
112  * we remove problematic character and
113  * the number are formatted.
114  * Clean the row after exporting
115  * @return nothing
116  */
117  function write()
118  {
119  if ($this->size == 0 ) return;
120  $sep="";
121  for ($i=0;$i < $this->size;$i++)
122  {
123  if ($this->element[$i]['type'] == 'number' )
124  {
125  printf($sep.'%s', nb($this->element[$i]['value']));
126  }
127  else
128  {
129  // remove break-line,
130  $export=str_replace("\n"," ",$this->element[$i]['value']);
131  $export=str_replace("\r"," ", $export);
132  // remove double quote
133  $export=str_replace('"',"", $export);
134  printf($sep.'"%s"', $export);
135  }
136  $sep=";";
137  }
138  printf("\r\n");
139  $this->clean();
140  }
141  /**
142  * clean the row
143  */
144  private function clean()
145  {
146  $this->element=array();
147  $this->size=0;
148  }
149 
150 }
nb($p_number)
format the number for the CSV export
Definition: ac_common.php:107
clean()
clean the row
$size
add($p_item, $p_type="text")
$name size
__construct($p_filename)
write_header($p_array)
function trim(s)
remove trailing and heading space
Definition: scripts.js:95