1 | <?php |
||
2 | |||
3 | namespace Backend\Core\Engine; |
||
4 | |||
5 | use Backend\Core\Engine\Model as BackendModel; |
||
6 | use Common\Exception\RedirectException; |
||
7 | use PhpOffice\PhpSpreadsheet\Spreadsheet; |
||
8 | use PhpOffice\PhpSpreadsheet\Writer\Csv as CsvWriter; |
||
9 | use Symfony\Component\HttpFoundation\StreamedResponse; |
||
10 | |||
11 | /** |
||
12 | * @deprecated remove this in Fork 6, just use ForkCMS\Utility\Csv\Writer |
||
13 | */ |
||
14 | class Csv extends \SpoonFileCSV |
||
15 | { |
||
16 | /** |
||
17 | * Output a CSV-file as a download |
||
18 | * |
||
19 | * @deprecated remove this in Fork 6, just use ForkCMS\Utility\Csv\Writer->output() |
||
20 | * |
||
21 | * @param string $filename The name of the file. |
||
22 | * @param array $array The array to convert. |
||
23 | * @param array $columns The column names you want to use. |
||
24 | * @param array $excludeColumns The columns you want to exclude. |
||
25 | * |
||
26 | * @throws RedirectException |
||
27 | */ |
||
28 | public static function outputCSV( |
||
29 | string $filename, |
||
30 | array $array, |
||
31 | array $columns = null, |
||
32 | array $excludeColumns = null |
||
33 | ) { |
||
34 | $headers = $columns; |
||
35 | $data = $array; |
||
36 | |||
37 | // remove data that should be excluded |
||
38 | if (!empty($excludeColumns)) { |
||
39 | $headers = array_filter( |
||
40 | $columns, |
||
41 | function ($column) use ($excludeColumns) { |
||
42 | return !in_array($column, $excludeColumns); |
||
43 | } |
||
44 | ); |
||
45 | |||
46 | foreach ($array as $rowNumber => $row) { |
||
47 | $data[$rowNumber] = array_filter( |
||
48 | $row, |
||
49 | function ($key) use ($excludeColumns) { |
||
50 | return !in_array($key, $excludeColumns); |
||
51 | }, |
||
52 | ARRAY_FILTER_USE_KEY |
||
53 | ); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | $spreadSheet = new Spreadsheet(); |
||
58 | $sheet = $spreadSheet->getActiveSheet(); |
||
59 | |||
60 | // add data |
||
61 | $sheet->fromArray($headers, null, 'A1'); |
||
62 | $sheet->fromArray($data, null, 'A2'); |
||
63 | |||
64 | $writer = new CsvWriter($spreadSheet); |
||
65 | $writer->setDelimiter(Authentication::getUser()->getSetting('csv_split_character')); |
||
66 | $writer->setEnclosure('"'); |
||
67 | $writer->setLineEnding(self::getLineEnding()); |
||
0 ignored issues
–
show
|
|||
68 | |||
69 | $response = new StreamedResponse( |
||
70 | function () use ($writer) { |
||
71 | $writer->save('php://output'); |
||
72 | } |
||
73 | ); |
||
74 | |||
75 | // set headers |
||
76 | $charset = BackendModel::getContainer()->getParameter('kernel.charset'); |
||
77 | $response->headers->set('Content-type', 'application/csv; charset=' . $charset); |
||
78 | $response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"'); |
||
79 | $response->headers->set('Cache-Control', 'max-age=0'); |
||
80 | $response->headers->set('Pragma', 'no-cache'); |
||
81 | |||
82 | throw new RedirectException( |
||
83 | 'Return the csv data', |
||
84 | $response |
||
85 | ); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @deprecated remove this in Fork 6, you should not rely on this. |
||
90 | */ |
||
91 | private static function getLineEnding(): string |
||
92 | { |
||
93 | $lineEnding = Authentication::getUser()->getSetting('csv_line_ending'); |
||
94 | |||
95 | // reformat |
||
96 | if ($lineEnding === '\n') { |
||
97 | return "\n"; |
||
98 | } |
||
99 | if ($lineEnding === '\r\n') { |
||
100 | return "\r\n"; |
||
101 | } |
||
102 | |||
103 | return $lineEnding; |
||
104 | } |
||
105 | } |
||
106 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.