1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* See license terms in /license.txt */ |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; |
7
|
|
|
use Sonata\Exporter\Handler; |
8
|
|
|
use Sonata\Exporter\Source\ArraySourceIterator; |
9
|
|
|
use Sonata\Exporter\Writer\CsvWriter; |
10
|
|
|
use Sonata\Exporter\Writer\XlsWriter; |
11
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This is the export library for Chamilo. |
15
|
|
|
* Include/require it in your code to use its functionality. |
16
|
|
|
*/ |
17
|
|
|
class Export |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Constructor. |
21
|
|
|
*/ |
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Export tabular data to CSV-file. |
28
|
|
|
* |
29
|
|
|
* @return mixed csv raw data | false if no data to export | string file path if success in $writeOnly mode |
30
|
|
|
*/ |
31
|
|
|
public static function arrayToCsv(array $data, string $filename = 'export', bool $writeOnly = false, string $enclosure = '"') |
32
|
|
|
{ |
33
|
|
|
if (empty($data)) { |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$enclosure = !empty($enclosure) ? $enclosure : '"'; |
38
|
|
|
$filePath = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.csv'; |
39
|
|
|
$writer = new CsvWriter($filePath, ';', $enclosure, true); |
40
|
|
|
|
41
|
|
|
$source = new ArraySourceIterator($data); |
42
|
|
|
$handler = Handler::create($source, $writer); |
43
|
|
|
$handler->export(); |
44
|
|
|
|
45
|
|
|
if (!$writeOnly) { |
46
|
|
|
DocumentManager::file_send_for_download($filePath, true, $filename.'.csv'); |
47
|
|
|
exit; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $filePath; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Converts an array of data into a CSV file and optionally sends it for download. |
55
|
|
|
* |
56
|
|
|
* @return string|void Returns the file path if $writeOnly is true, otherwise sends the file for download and exits. |
57
|
|
|
*/ |
58
|
|
|
public static function arrayToCsvSimple(array $data, string $filename = 'export', bool $writeOnly = false) |
59
|
|
|
{ |
60
|
|
|
$file = api_get_path(SYS_ARCHIVE_PATH) . uniqid('') . '.csv'; |
61
|
|
|
|
62
|
|
|
$handle = fopen($file, 'w'); |
63
|
|
|
|
64
|
|
|
if ($handle === false) { |
65
|
|
|
throw new \RuntimeException("Unable to create or open the file: $file"); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (is_array($data)) { |
69
|
|
|
foreach ($data as $row) { |
70
|
|
|
$line = ''; |
71
|
|
|
if (is_array($row)) { |
72
|
|
|
foreach ($row as $value) { |
73
|
|
|
$line .= '"' . str_replace('"', '""', (string)$value) . '";'; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
fwrite($handle, rtrim($line, ';') . "\n"); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
fclose($handle); |
81
|
|
|
|
82
|
|
|
if (!$writeOnly) { |
83
|
|
|
DocumentManager::file_send_for_download($file, true, $filename . '.csv'); |
84
|
|
|
unlink($file); |
85
|
|
|
exit; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $file; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Export tabular data to XLS-file. |
93
|
|
|
*/ |
94
|
|
|
public static function arrayToXls(array $data, string $filename = 'export') |
95
|
|
|
{ |
96
|
|
|
if (empty($data)) { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$spreadsheet = new Spreadsheet(); |
101
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
102
|
|
|
$rowNumber = 1; |
103
|
|
|
foreach ($data as $row) { |
104
|
|
|
$colNumber = 'A'; |
105
|
|
|
foreach ($row as $cell) { |
106
|
|
|
$sheet->setCellValue($colNumber . $rowNumber, $cell); |
107
|
|
|
$colNumber++; |
108
|
|
|
} |
109
|
|
|
$rowNumber++; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$filePath = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xlsx'; |
113
|
|
|
$writer = new Xlsx($spreadsheet); |
114
|
|
|
$writer->save($filePath); |
115
|
|
|
|
116
|
|
|
DocumentManager::file_send_for_download($filePath, true, $filename.'.xlsx'); |
117
|
|
|
exit; |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Export tabular data to XLS-file (as html table). |
122
|
|
|
* |
123
|
|
|
* @param array $data |
124
|
|
|
* @param string $filename |
125
|
|
|
*/ |
126
|
|
|
public static function export_table_xls_html($data, $filename = 'export', $encoding = 'utf-8') |
127
|
|
|
{ |
128
|
|
|
$file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xls'; |
129
|
|
|
$handle = fopen($file, 'a+'); |
130
|
|
|
$systemEncoding = api_get_system_encoding(); |
131
|
|
|
fwrite($handle, '<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/html" charset="'.$encoding.'" /><body><table>'); |
132
|
|
|
foreach ($data as $id => $row) { |
133
|
|
|
foreach ($row as $id2 => $row2) { |
134
|
|
|
$data[$id][$id2] = api_htmlentities($row2); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
foreach ($data as $row) { |
138
|
|
|
$string = implode("</td><td>", $row); |
139
|
|
|
$string = '<tr><td>'.$string.'</td></tr>'; |
140
|
|
|
if ('utf-8' != $encoding) { |
141
|
|
|
$string = api_convert_encoding($string, $encoding, $systemEncoding); |
142
|
|
|
} |
143
|
|
|
fwrite($handle, $string."\n"); |
144
|
|
|
} |
145
|
|
|
fwrite($handle, '</table></body></html>'); |
146
|
|
|
fclose($handle); |
147
|
|
|
DocumentManager::file_send_for_download($file, true, $filename.'.xls'); |
148
|
|
|
exit; |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Export tabular data to XML-file. |
153
|
|
|
* |
154
|
|
|
* @param array Simple array of data to put in XML |
155
|
|
|
* @param string Name of file to be given to the user |
156
|
|
|
* @param string Name of common tag to place each line in |
157
|
|
|
* @param string Name of the root element. A root element should always be given. |
158
|
|
|
* @param string Encoding in which the data is provided |
159
|
|
|
*/ |
160
|
|
|
public static function arrayToXml( |
161
|
|
|
$data, |
162
|
|
|
$filename = 'export', |
163
|
|
|
$item_tagname = 'item', |
164
|
|
|
$wrapper_tagname = null, |
165
|
|
|
$encoding = null |
166
|
|
|
) { |
167
|
|
|
if (empty($encoding)) { |
168
|
|
|
$encoding = api_get_system_encoding(); |
169
|
|
|
} |
170
|
|
|
$file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml'; |
171
|
|
|
$handle = fopen($file, 'a+'); |
172
|
|
|
fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n"); |
173
|
|
|
if (!is_null($wrapper_tagname)) { |
174
|
|
|
fwrite($handle, "\t".'<'.$wrapper_tagname.'>'."\n"); |
175
|
|
|
} |
176
|
|
|
foreach ($data as $row) { |
177
|
|
|
fwrite($handle, '<'.$item_tagname.'>'."\n"); |
178
|
|
|
foreach ($row as $key => $value) { |
179
|
|
|
fwrite($handle, "\t\t".'<'.$key.'>'.$value.'</'.$key.'>'."\n"); |
180
|
|
|
} |
181
|
|
|
fwrite($handle, "\t".'</'.$item_tagname.'>'."\n"); |
182
|
|
|
} |
183
|
|
|
if (!is_null($wrapper_tagname)) { |
184
|
|
|
fwrite($handle, '</'.$wrapper_tagname.'>'."\n"); |
185
|
|
|
} |
186
|
|
|
fclose($handle); |
187
|
|
|
DocumentManager::file_send_for_download($file, true, $filename.'.xml'); |
188
|
|
|
exit; |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param array $data table to be read with the HTML_table class |
193
|
|
|
*/ |
194
|
|
|
public static function export_table_pdf($data, $params = []) |
195
|
|
|
{ |
196
|
|
|
$table_html = self::convert_array_to_html($data, $params); |
197
|
|
|
$params['format'] = isset($params['format']) ? $params['format'] : 'A4'; |
198
|
|
|
$params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P'; |
199
|
|
|
|
200
|
|
|
$pdf = new PDF($params['format'], $params['orientation'], $params); |
201
|
|
|
$pdf->html_to_pdf_with_template($table_html); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param string $html |
206
|
|
|
* @param array $params |
207
|
|
|
*/ |
208
|
|
|
public static function export_html_to_pdf($html, $params = []) |
209
|
|
|
{ |
210
|
|
|
$params['format'] = isset($params['format']) ? $params['format'] : 'A4'; |
211
|
|
|
$params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P'; |
212
|
|
|
|
213
|
|
|
$pdf = new PDF($params['format'], $params['orientation'], $params); |
214
|
|
|
$pdf->html_to_pdf_with_template($html); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param array $data |
219
|
|
|
* @param array $params |
220
|
|
|
* |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
|
|
public static function convert_array_to_html($data, $params = []) |
224
|
|
|
{ |
225
|
|
|
$headers = $data[0]; |
226
|
|
|
unset($data[0]); |
227
|
|
|
|
228
|
|
|
$header_attributes = isset($params['header_attributes']) ? $params['header_attributes'] : []; |
229
|
|
|
$table = new HTML_Table(['class' => 'table table-hover table-striped data_table', 'repeat_header' => '1']); |
230
|
|
|
$row = 0; |
231
|
|
|
$column = 0; |
232
|
|
|
foreach ($headers as $header) { |
233
|
|
|
$table->setHeaderContents($row, $column, $header); |
234
|
|
|
$attributes = []; |
235
|
|
|
if (isset($header_attributes) && isset($header_attributes[$column])) { |
236
|
|
|
$attributes = $header_attributes[$column]; |
237
|
|
|
} |
238
|
|
|
if (!empty($attributes)) { |
239
|
|
|
$table->updateCellAttributes($row, $column, $attributes); |
240
|
|
|
} |
241
|
|
|
$column++; |
242
|
|
|
} |
243
|
|
|
$row++; |
244
|
|
|
foreach ($data as &$printable_data_row) { |
245
|
|
|
$column = 0; |
246
|
|
|
foreach ($printable_data_row as &$printable_data_cell) { |
247
|
|
|
$table->setCellContents($row, $column, $printable_data_cell); |
248
|
|
|
//$table->updateCellAttributes($row, $column, $atributes); |
249
|
|
|
$column++; |
250
|
|
|
} |
251
|
|
|
$table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true); |
252
|
|
|
$row++; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $table->toHtml(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Export HTML content in a ODF document. |
260
|
|
|
* |
261
|
|
|
* @param string $html |
262
|
|
|
* @param string $name |
263
|
|
|
* @param string $format |
264
|
|
|
* |
265
|
|
|
* @return bool |
266
|
|
|
*/ |
267
|
|
|
public static function htmlToOdt($html, $name, $format = 'odt') |
268
|
|
|
{ |
269
|
|
|
$unoconv = api_get_setting('platform.unoconv_binaries'); |
270
|
|
|
|
271
|
|
|
if (empty($unoconv)) { |
272
|
|
|
return false; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
if (!empty($html)) { |
276
|
|
|
/*$fs = new Filesystem(); |
277
|
|
|
$paths = [ |
278
|
|
|
'root_sys' => api_get_path(SYS_PATH), |
279
|
|
|
'path.temp' => api_get_path(SYS_ARCHIVE_PATH), |
280
|
|
|
]; |
281
|
|
|
$connector = new Connector(); |
282
|
|
|
|
283
|
|
|
$drivers = new DriversContainer(); |
284
|
|
|
$drivers['configuration'] = [ |
285
|
|
|
'unoconv.binaries' => $unoconv, |
286
|
|
|
'unoconv.timeout' => 60, |
287
|
|
|
]; |
288
|
|
|
|
289
|
|
|
$tempFilesystem = TemporaryFilesystem::create(); |
290
|
|
|
$manager = new Manager($tempFilesystem, $fs); |
291
|
|
|
$alchemyst = new Alchemyst($drivers, $manager); |
292
|
|
|
|
293
|
|
|
$dataFileSystem = new Data($paths, $fs, $connector, $alchemyst); |
294
|
|
|
$content = $dataFileSystem->convertRelativeToAbsoluteUrl($html); |
295
|
|
|
$filePath = $dataFileSystem->putContentInTempFile( |
296
|
|
|
$content, |
297
|
|
|
api_replace_dangerous_char($name), |
298
|
|
|
'html' |
299
|
|
|
); |
300
|
|
|
|
301
|
|
|
$try = true; |
302
|
|
|
|
303
|
|
|
while ($try) { |
304
|
|
|
try { |
305
|
|
|
$convertedFile = $dataFileSystem->transcode( |
306
|
|
|
$filePath, |
307
|
|
|
$format |
308
|
|
|
); |
309
|
|
|
|
310
|
|
|
$try = false; |
311
|
|
|
DocumentManager::file_send_for_download( |
312
|
|
|
$convertedFile, |
313
|
|
|
false, |
314
|
|
|
$name.'.'.$format |
315
|
|
|
); |
316
|
|
|
} catch (Exception $e) { |
317
|
|
|
// error_log($e->getMessage()); |
318
|
|
|
} |
319
|
|
|
}*/ |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.