1 <?php
2 3 4 5 6 7
8
9 defined('FOF_INCLUDED') or die;
10
11 12 13 14 15 16 17
18 class FOFViewCsv extends FOFViewHtml
19 {
20 21 22 23 24
25 protected = true;
26
27 28 29 30 31
32 protected $csvFilename = null;
33
34 35 36 37 38
39 protected $csvFields = array();
40
41 42 43 44 45
46 public function __construct($config = array())
47 {
48
49 if (is_object($config))
50 {
51 $config = (array) $config;
52 }
53 elseif (!is_array($config))
54 {
55 $config = array();
56 }
57
58 parent::__construct($config);
59
60 if (array_key_exists('csv_header', $config))
61 {
62 $this->csvHeader = $config['csv_header'];
63 }
64 else
65 {
66 $this->csvHeader = $this->input->getBool('csv_header', true);
67 }
68
69 if (array_key_exists('csv_filename', $config))
70 {
71 $this->csvFilename = $config['csv_filename'];
72 }
73 else
74 {
75 $this->csvFilename = $this->input->getString('csv_filename', '');
76 }
77
78 if (empty($this->csvFilename))
79 {
80 $view = $this->input->getCmd('view', 'cpanel');
81 $view = FOFInflector::pluralize($view);
82 $this->csvFilename = strtolower($view);
83 }
84
85 if (array_key_exists('csv_fields', $config))
86 {
87 $this->csvFields = $config['csv_fields'];
88 }
89 }
90
91 92 93 94 95 96 97
98 protected function onDisplay($tpl = null)
99 {
100
101 $model = $this->getModel();
102
103 $items = $model->getItemList();
104 $this->items = $items;
105
106 $platform = FOFPlatform::getInstance();
107 $document = $platform->getDocument();
108
109 if ($document instanceof JDocument)
110 {
111 $document->setMimeEncoding('text/csv');
112 }
113
114 $platform->setHeader('Pragma', 'public');
115 $platform->setHeader('Expires', '0');
116 $platform->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
117 $platform->setHeader('Cache-Control', 'public', false);
118 $platform->setHeader('Content-Description', 'File Transfer');
119 $platform->setHeader('Content-Disposition', 'attachment; filename="' . $this->csvFilename . '"');
120
121 if (is_null($tpl))
122 {
123 $tpl = 'csv';
124 }
125
126 FOFPlatform::getInstance()->setErrorHandling(E_ALL, 'ignore');
127
128 $hasFailed = false;
129
130 try
131 {
132 $result = $this->loadTemplate($tpl, true);
133
134 if ($result instanceof Exception)
135 {
136 $hasFailed = true;
137 }
138 }
139 catch (Exception $e)
140 {
141 $hasFailed = true;
142 }
143
144 if (!$hasFailed)
145 {
146 echo $result;
147 }
148 else
149 {
150
151
152 if (empty($items))
153 {
154 return;
155 }
156
157 $item = array_pop($items);
158 $keys = get_object_vars($item);
159 $keys = array_keys($keys);
160 $items[] = $item;
161 reset($items);
162
163 if (!empty($this->csvFields))
164 {
165 $temp = array();
166
167 foreach ($this->csvFields as $f)
168 {
169 if (in_array($f, $keys))
170 {
171 $temp[] = $f;
172 }
173 }
174
175 $keys = $temp;
176 }
177
178 if ($this->csvHeader)
179 {
180 $csv = array();
181
182 foreach ($keys as $k)
183 {
184 $k = str_replace('"', '""', $k);
185 $k = str_replace("\r", '\\r', $k);
186 $k = str_replace("\n", '\\n', $k);
187 $k = '"' . $k . '"';
188
189 $csv[] = $k;
190 }
191
192 echo implode(",", $csv) . "\r\n";
193 }
194
195 foreach ($items as $item)
196 {
197 $csv = array();
198 $item = (array) $item;
199
200 foreach ($keys as $k)
201 {
202 if (!isset($item[$k]))
203 {
204 $v = '';
205 }
206 else
207 {
208 $v = $item[$k];
209 }
210
211 if (is_array($v))
212 {
213 $v = 'Array';
214 }
215 elseif (is_object($v))
216 {
217 $v = 'Object';
218 }
219
220 $v = str_replace('"', '""', $v);
221 $v = str_replace("\r", '\\r', $v);
222 $v = str_replace("\n", '\\n', $v);
223 $v = '"' . $v . '"';
224
225 $csv[] = $v;
226 }
227
228 echo implode(",", $csv) . "\r\n";
229 }
230 }
231
232 return false;
233 }
234 }
235