/
export.lib.inc.php
executable file
·344 lines (307 loc) · 11.4 KB
/
export.lib.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/* See license terms in /license.txt */
use Ddeboer\DataImport\Writer\ExcelWriter;
use Ddeboer\DataImport\Writer\CsvWriter;
use Ddeboer\DataImport\Workflow;
use Ddeboer\DataImport\Reader\CsvReader;
use Ddeboer\DataImport\Reader\ArrayReader;
use Ddeboer\DataImport\Writer\ArrayWriter;
use Chamilo\CoreBundle\Component\Editor\Connector;
use Chamilo\CoreBundle\Component\Filesystem\Data;
use ChamiloSession as Session;
use MediaAlchemyst\Alchemyst;
use MediaAlchemyst\DriversContainer;
use Neutron\TemporaryFilesystem\Manager;
use Neutron\TemporaryFilesystem\TemporaryFilesystem;
use Symfony\Component\Filesystem\Filesystem;
/**
* This is the export library for Chamilo.
* Include/require it in your code to use its functionality.
* Several functions below are adaptations from functions distributed by www.nexen.net
*
* @package chamilo.library
*/
class Export
{
/**
* Constructor
*/
public function __construct()
{
}
/**
* Export tabular data to CSV-file
* @param array $data
* @param string $filename
*/
public static function arrayToCsv($data, $filename = 'export')
{
if (empty($data)) {
return false;
}
$filePath = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.csv';
$writer = new CsvWriter();
$writer->setStream(fopen($filePath, 'w'));
foreach ($data as $item) {
$writer->writeItem($item);
}
$writer->finish();
DocumentManager::file_send_for_download($filePath, true, $filename.'.csv');
exit;
}
/**
* Export tabular data to XLS-file
* @param array $data
* @param string $filename
*/
public static function arrayToXls($data, $filename = 'export', $encoding = 'utf-8')
{
$filePath = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xlsx';
$file = new \SplFileObject($filePath, 'w');
$writer = new ExcelWriter($file);
$writer->prepare();
foreach ($data as $index => $row) {
$writer->writeItem($row);
}
$writer->finish();
DocumentManager::file_send_for_download($filePath, true, $filename.'.xlsx');
exit;
}
/**
* Export tabular data to XLS-file (as html table)
* @param array $data
* @param string $filename
*/
public static function export_table_xls_html($data, $filename = 'export', $encoding = 'utf-8')
{
$file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xls';
$handle = fopen($file, 'a+');
$systemEncoding = api_get_system_encoding();
fwrite($handle, '<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/html" charset="utf-8" /><body><table>');
foreach ($data as $id => $row) {
foreach ($row as $id2 => $row2) {
$data[$id][$id2] = api_htmlentities($row2);
}
}
foreach ($data as $row) {
$string = implode("</td><td>", $row);
$string = '<tr><td>' . $string . '</td></tr>';
if ($encoding != 'utf-8') {
$string = api_convert_encoding($string, $encoding, $systemEncoding);
}
fwrite($handle, $string."\n");
}
fwrite($handle, '</table></body></html>');
fclose($handle);
DocumentManager::file_send_for_download($file, true, $filename.'.xls');
exit;
}
/**
* Export tabular data to XML-file
* @param array Simple array of data to put in XML
* @param string Name of file to be given to the user
* @param string Name of common tag to place each line in
* @param string Name of the root element. A root element should always be given.
* @param string Encoding in which the data is provided
*/
public static function arrayToXml(
$data,
$filename = 'export',
$item_tagname = 'item',
$wrapper_tagname = null,
$encoding = null
) {
if (empty($encoding)) {
$encoding = api_get_system_encoding();
}
$file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml';
$handle = fopen($file, 'a+');
fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n");
if (!is_null($wrapper_tagname)) {
fwrite($handle, "\t".'<'.$wrapper_tagname.'>'."\n");
}
foreach ($data as $row) {
fwrite($handle, '<'.$item_tagname.'>'."\n");
foreach ($row as $key => $value) {
fwrite($handle, "\t\t".'<'.$key.'>'.$value.'</'.$key.'>'."\n");
}
fwrite($handle, "\t".'</'.$item_tagname.'>'."\n");
}
if (!is_null($wrapper_tagname)) {
fwrite($handle, '</'.$wrapper_tagname.'>'."\n");
}
fclose($handle);
DocumentManager :: file_send_for_download($file, true, $filename.'.xml');
exit;
}
/**
* Export hierarchical tabular data to XML-file
* @param array Hierarchical array of data to put in XML, each element presenting a 'name' and a 'value' property
* @param string Name of file to be given to the user
* @param string Name of common tag to place each line in
* @param string Name of the root element. A root element should always be given.
* @param string Encoding in which the data is provided
* @return void Prompts the user for a file download
*/
public static function export_complex_table_xml(
$data,
$filename = 'export',
$wrapper_tagname,
$encoding = 'ISO-8859-1'
) {
$file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml';
$handle = fopen($file, 'a+');
fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n");
if (!is_null($wrapper_tagname)) {
fwrite($handle, '<'.$wrapper_tagname.'>');
}
$s = self::_export_complex_table_xml_helper($data);
fwrite($handle,$s);
if (!is_null($wrapper_tagname)) {
fwrite($handle, '</'.$wrapper_tagname.'>'."\n");
}
fclose($handle);
DocumentManager :: file_send_for_download($file, true, $filename.'.xml');
return false;
}
/**
* Helper for the hierarchical XML exporter
* @param array Hierarhical array composed of elements of type ('name'=>'xyz','value'=>'...')
* @param int Level of recursivity. Allows the XML to be finely presented
* @return string The XML string to be inserted into the root element
*/
public static function _export_complex_table_xml_helper($data, $level = 1)
{
if (count($data) < 1) {
return '';
}
$string = '';
foreach ($data as $row) {
$string .= "\n".str_repeat("\t",$level).'<'.$row['name'].'>';
if (is_array($row['value'])) {
$string .= self::_export_complex_table_xml_helper($row['value'],$level+1)."\n";
$string .= str_repeat("\t",$level).'</'.$row['name'].'>';
} else {
$string .= $row['value'];
$string .= '</'.$row['name'].'>';
}
}
return $string;
}
/**
* @param array $data table to be read with the HTML_table class
*/
public static function export_table_pdf($data, $params = array())
{
$table_html = self::convert_array_to_html($data, $params);
$params['format'] = isset($params['format']) ? $params['format'] : 'A4';
$params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P';
$pdf = new PDF($params['format'], $params['orientation'], $params);
$pdf->html_to_pdf_with_template($table_html);
}
/**
* @param string $html
* @param array $params
*/
public static function export_html_to_pdf($html, $params = array())
{
$params['format'] = isset($params['format']) ? $params['format'] : 'A4';
$params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P';
$pdf = new PDF($params['format'], $params['orientation'], $params);
$pdf->html_to_pdf_with_template($html);
}
/**
* @param array $data
* @param array $params
*
* @return string
*/
public static function convert_array_to_html($data, $params = array())
{
$headers = $data[0];
unset($data[0]);
$header_attributes = isset($params['header_attributes']) ? $params['header_attributes'] : array();
$table = new HTML_Table(array('class' => 'data_table', 'repeat_header' => '1'));
$row = 0;
$column = 0;
foreach ($headers as $header) {
$table->setHeaderContents($row, $column, $header);
$attributes = array();
if (isset($header_attributes) && isset($header_attributes[$column])) {
$attributes = $header_attributes[$column];
}
if (!empty($attributes)) {
$table->updateCellAttributes($row, $column, $attributes);
}
$column++;
}
$row++;
foreach ($data as &$printable_data_row) {
$column = 0;
foreach ($printable_data_row as &$printable_data_cell) {
$table->setCellContents($row, $column, $printable_data_cell);
//$table->updateCellAttributes($row, $column, $atributes);
$column++;
}
$table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
$row++;
}
$table_tp_html = $table->toHtml();
return $table_tp_html;
}
/**
* Export HTML content in a ODF document
* @param string $html
* @param string $name
* @param string $format
*
* @return bool
*/
public static function htmlToOdt($html, $name, $format = 'odt')
{
$unoconv = api_get_configuration_value('unoconv.binaries');
if (empty($unoconv)) {
return false;
}
if (!empty($html)) {
$fs = new Filesystem();
$paths = [
'root_sys' => api_get_path(SYS_PATH),
'path.temp' => api_get_path(SYS_ARCHIVE_PATH),
];
$connector = new Connector();
$drivers = new DriversContainer();
$drivers['configuration'] = array(
'unoconv.binaries' => $unoconv,
'unoconv.timeout' => 60,
);
$tempFilesystem = TemporaryFilesystem::create();
$manager = new Manager($tempFilesystem, $fs);
$alchemyst = new Alchemyst($drivers, $manager);
$dataFileSystem = new Data($paths, $fs, $connector, $alchemyst);
$content = $dataFileSystem->convertRelativeToAbsoluteUrl($html);
$filePath = $dataFileSystem->putContentInTempFile(
$content,
api_replace_dangerous_char($name),
'html'
);
$try = true;
while ($try) {
try {
$convertedFile = $dataFileSystem->transcode(
$filePath,
$format
);
$try = false;
DocumentManager::file_send_for_download(
$convertedFile,
false,
$name.'.'.$format
);
} catch (Exception $e) {
// error_log($e->getMessage());
}
}
}
}
}