Passed
Push — master ( 89dd8a...a5d151 )
by Angel Fernando Quiroz
10:22
created

Export::arrayToXls()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
c 0
b 0
f 0
nc 13
nop 3
dl 0
loc 31
rs 8.9777
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 a CSV file.
28
     *
29
     * @param array  $data           Data rows as an array of associative arrays
30
     * @param string $filename       Base filename without extension
31
     * @param bool   $writeOnly      If true returns the file path instead of forcing download
32
     * @param string $enclosure      Field enclosure character (default: ")
33
     * @param bool   $includeHeader  Whether to output a header row (keys of the first array)
34
     *
35
     * @return mixed Raw CSV data | false if $data is empty | string file path if $writeOnly = true
36
     */
37
    public static function arrayToCsv(
38
        array  $data,
39
        string $filename      = 'export',
40
        bool   $writeOnly     = false,
41
        string $enclosure     = '"',
42
        bool   $includeHeader = true
43
    ) {
44
        if (empty($data)) {
45
            return false;
46
        }
47
48
        $enclosure = $enclosure !== '' ? $enclosure : '"';
49
        $filePath  = api_get_path(SYS_ARCHIVE_PATH);
50
        $filePath .= $writeOnly ? $filename : uniqid('');
51
        $filePath .= '.csv';
52
53
        // define a single-character escape
54
        $escapeChar = '\\';
55
56
        // CsvWriter signature: __construct($fileName, $delimiter, $enclosure, $escape, $withBOM, $withHeader)
57
        // we pass false for BOM, and $includeHeader for header toggle
58
        $writer = new CsvWriter($filePath, ',', $enclosure, $escapeChar, false, $includeHeader);
59
60
        $source = new ArraySourceIterator($data);
61
        $handler = Handler::create($source, $writer);
62
        $handler->export();
63
64
        if (!$writeOnly) {
65
            DocumentManager::file_send_for_download($filePath, true, $filename.'.csv');
66
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
67
        }
68
69
        return $filePath;
70
    }
71
72
    /**
73
     * Converts an array of data into a CSV file and optionally sends it for download.
74
     *
75
     * @return string|void Returns the file path if $writeOnly is true, otherwise sends the file for download and exits.
76
     */
77
    public static function arrayToCsvSimple(array $data, string $filename = 'export', bool $writeOnly = false, array $header = [])
78
    {
79
        $file = api_get_path(SYS_ARCHIVE_PATH);
80
        $file .= $writeOnly ? $filename : uniqid('');
81
        $file .= '.csv';
82
83
        $handle = fopen($file, 'w');
84
85
        if ($handle === false) {
86
            throw new \RuntimeException("Unable to create or open the file: $file");
87
        }
88
89
        if (!empty($header)) {
90
            fputcsv($handle, $header, ',');
91
        }
92
93
        foreach ($data as $row) {
94
            fputcsv($handle, (array)$row, ',');
95
        }
96
97
        fclose($handle);
98
99
        if (!$writeOnly) {
100
            DocumentManager::file_send_for_download($file, true, $filename . '.csv');
101
            unlink($file);
102
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
103
        }
104
105
        return $file;
106
    }
107
108
    /**
109
     * Export tabular data to XLS-file.
110
     */
111
    public static function arrayToXls(array $data, string $filename = 'export', bool $writeOnly = false)
112
    {
113
        if (empty($data)) {
114
            return false;
115
        }
116
117
        $spreadsheet = new Spreadsheet();
118
        $sheet = $spreadsheet->getActiveSheet();
119
        $rowNumber = 1;
120
        foreach ($data as $row) {
121
            $colNumber = 'A';
122
            foreach ($row as $cell) {
123
                $sheet->setCellValue($colNumber . $rowNumber, $cell);
124
                $colNumber++;
125
            }
126
            $rowNumber++;
127
        }
128
129
        $filePath = api_get_path(SYS_ARCHIVE_PATH);
130
        $filePath .= $writeOnly ? $filename : uniqid('');
131
        $filePath .= '.xlsx';
132
133
        $writer = new Xlsx($spreadsheet);
134
        $writer->save($filePath);
135
136
        if (!$writeOnly) {
137
            DocumentManager::file_send_for_download($filePath, true, $filename.'.xlsx');
138
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
139
        }
140
141
        return $filePath;
142
    }
143
144
    /**
145
     * Export tabular data to XLS-file (as html table).
146
     *
147
     * @param array  $data
148
     * @param string $filename
149
     */
150
    public static function export_table_xls_html($data, $filename = 'export', $encoding = 'utf-8')
151
    {
152
        $file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xls';
153
        $handle = fopen($file, 'a+');
154
        $systemEncoding = api_get_system_encoding();
155
        fwrite($handle, '<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/html" charset="'.$encoding.'" /><body><table>');
156
        foreach ($data as $id => $row) {
157
            foreach ($row as $id2 => $row2) {
158
                $data[$id][$id2] = api_htmlentities($row2);
159
            }
160
        }
161
        foreach ($data as $row) {
162
            $string = implode("</td><td>", $row);
163
            $string = '<tr><td>'.$string.'</td></tr>';
164
            if ('utf-8' != $encoding) {
165
                $string = api_convert_encoding($string, $encoding, $systemEncoding);
166
            }
167
            fwrite($handle, $string."\n");
168
        }
169
        fwrite($handle, '</table></body></html>');
170
        fclose($handle);
171
        DocumentManager::file_send_for_download($file, true, $filename.'.xls');
172
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
173
    }
174
175
    /**
176
     * Export tabular data to XML-file.
177
     *
178
     * @param array  Simple array of data to put in XML
179
     * @param string Name of file to be given to the user
180
     * @param string Name of common tag to place each line in
181
     * @param string Name of the root element. A root element should always be given.
182
     * @param string Encoding in which the data is provided
183
     */
184
    public static function arrayToXml(
185
        $data,
186
        $filename = 'export',
187
        $item_tagname = 'item',
188
        $wrapper_tagname = null,
189
        $encoding = null
190
    ) {
191
        if (empty($encoding)) {
192
            $encoding = api_get_system_encoding();
193
        }
194
        $file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml';
195
        $handle = fopen($file, 'a+');
196
        fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n");
197
        if (!is_null($wrapper_tagname)) {
198
            fwrite($handle, "\t".'<'.$wrapper_tagname.'>'."\n");
199
        }
200
        foreach ($data as $row) {
201
            fwrite($handle, '<'.$item_tagname.'>'."\n");
202
            foreach ($row as $key => $value) {
203
                fwrite($handle, "\t\t".'<'.$key.'>'.$value.'</'.$key.'>'."\n");
204
            }
205
            fwrite($handle, "\t".'</'.$item_tagname.'>'."\n");
206
        }
207
        if (!is_null($wrapper_tagname)) {
208
            fwrite($handle, '</'.$wrapper_tagname.'>'."\n");
209
        }
210
        fclose($handle);
211
        DocumentManager::file_send_for_download($file, true, $filename.'.xml');
212
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
213
    }
214
215
    /**
216
     * @param array $data table to be read with the HTML_table class
217
     */
218
    public static function export_table_pdf($data, $params = [])
219
    {
220
        $table_html = self::convert_array_to_html($data, $params);
221
        $params['format'] = isset($params['format']) ? $params['format'] : 'A4';
222
        $params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P';
223
224
        $pdf = new PDF($params['format'], $params['orientation'], $params);
225
        $pdf->html_to_pdf_with_template($table_html);
226
    }
227
228
    /**
229
     * @param string $html
230
     * @param array  $params
231
     */
232
    public static function export_html_to_pdf($html, $params = [])
233
    {
234
        $params['format'] = isset($params['format']) ? $params['format'] : 'A4';
235
        $params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P';
236
237
        $pdf = new PDF($params['format'], $params['orientation'], $params);
238
        $pdf->html_to_pdf_with_template($html);
239
    }
240
241
    /**
242
     * @param array $data
243
     * @param array $params
244
     *
245
     * @return string
246
     */
247
    public static function convert_array_to_html($data, $params = [])
248
    {
249
        $headers = $data[0];
250
        unset($data[0]);
251
252
        $header_attributes = isset($params['header_attributes']) ? $params['header_attributes'] : [];
253
        $table = new HTML_Table(['class' => 'table table-hover table-striped data_table', 'repeat_header' => '1']);
254
        $row = 0;
255
        $column = 0;
256
        foreach ($headers as $header) {
257
            $table->setHeaderContents($row, $column, $header);
258
            $attributes = [];
259
            if (isset($header_attributes) && isset($header_attributes[$column])) {
260
                $attributes = $header_attributes[$column];
261
            }
262
            if (!empty($attributes)) {
263
                $table->updateCellAttributes($row, $column, $attributes);
264
            }
265
            $column++;
266
        }
267
        $row++;
268
        foreach ($data as &$printable_data_row) {
269
            $column = 0;
270
            foreach ($printable_data_row as &$printable_data_cell) {
271
                $table->setCellContents($row, $column, $printable_data_cell);
272
                //$table->updateCellAttributes($row, $column, $atributes);
273
                $column++;
274
            }
275
            $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
276
            $row++;
277
        }
278
279
        return $table->toHtml();
280
    }
281
282
    /**
283
     * Export HTML content in a ODF document.
284
     *
285
     * @param string $html
286
     * @param string $name
287
     * @param string $format
288
     *
289
     * @return bool
290
     */
291
    public static function htmlToOdt($html, $name, $format = 'odt')
292
    {
293
        $unoconv = api_get_setting('platform.unoconv_binaries');
294
295
        if (empty($unoconv)) {
296
            return false;
297
        }
298
299
        if (!empty($html)) {
300
            /*$fs = new Filesystem();
301
            $paths = [
302
                'root_sys' => api_get_path(SYS_PATH),
303
                'path.temp' => api_get_path(SYS_ARCHIVE_PATH),
304
            ];
305
            $connector = new Connector();
306
307
            $drivers = new DriversContainer();
308
            $drivers['configuration'] = [
309
                'unoconv.binaries' => $unoconv,
310
                'unoconv.timeout' => 60,
311
            ];
312
313
            $tempFilesystem = TemporaryFilesystem::create();
314
            $manager = new Manager($tempFilesystem, $fs);
315
            $alchemyst = new Alchemyst($drivers, $manager);
316
317
            $dataFileSystem = new Data($paths, $fs, $connector, $alchemyst);
318
            $content = $dataFileSystem->convertRelativeToAbsoluteUrl($html);
319
            $filePath = $dataFileSystem->putContentInTempFile(
320
                $content,
321
                api_replace_dangerous_char($name),
322
                'html'
323
            );
324
325
            $try = true;
326
327
            while ($try) {
328
                try {
329
                    $convertedFile = $dataFileSystem->transcode(
330
                        $filePath,
331
                        $format
332
                    );
333
334
                    $try = false;
335
                    DocumentManager::file_send_for_download(
336
                        $convertedFile,
337
                        false,
338
                        $name.'.'.$format
339
                    );
340
                } catch (Exception $e) {
341
                    // error_log($e->getMessage());
342
                }
343
            }*/
344
        }
345
    }
346
}
347