1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ForkCMS\Utility\Csv; |
4
|
|
|
|
5
|
|
|
use Backend\Core\Engine\Authentication; |
6
|
|
|
use Backend\Core\Engine\User; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Csv; |
10
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
11
|
|
|
use ZipStream\Stream; |
12
|
|
|
|
13
|
|
|
class Writer |
14
|
|
|
{ |
15
|
|
|
private $charset; |
16
|
|
|
|
17
|
|
|
public function __construct(string $charset) |
18
|
|
|
{ |
19
|
|
|
$this->charset = $charset; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
private function getDefaultOptions(): array |
23
|
|
|
{ |
24
|
|
|
$options['Enclosure'] = '"'; |
|
|
|
|
25
|
|
|
$options['Delimiter'] = ','; |
26
|
|
|
$options['LineEnding'] = "\n"; |
27
|
|
|
$options['UseBOM'] = true; |
28
|
|
|
|
29
|
|
|
return $options; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private function getUserOptions(User $user): array |
33
|
|
|
{ |
34
|
|
|
$options['Delimiter'] = $user->getSetting('csv_split_character'); |
|
|
|
|
35
|
|
|
|
36
|
|
|
$lineEnding = $user->getSetting('csv_line_ending'); |
37
|
|
|
if ($lineEnding === '\n') { |
38
|
|
|
$options['LineEnding'] = "\n"; |
39
|
|
|
} |
40
|
|
|
if ($lineEnding === '\r\n') { |
41
|
|
|
$options['LineEnding'] = "\r\n"; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $options; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function getWriter(Spreadsheet $spreadsheet, array $options = []): Csv |
48
|
|
|
{ |
49
|
|
|
$writer = IOFactory::createWriter($spreadsheet, 'Csv'); |
50
|
|
|
|
51
|
|
|
if (!empty($options)) { |
52
|
|
|
foreach ($options as $option => $value) { |
53
|
|
|
$methodName = 'set' . $option; |
54
|
|
|
if (method_exists($writer, $methodName)) { |
55
|
|
|
call_user_func([$writer, $methodName], $value); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $writer; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function getStreamedResponse(Csv $writer, string $filename): StreamedResponse |
64
|
|
|
{ |
65
|
|
|
$response = new StreamedResponse( |
66
|
|
|
function () use ($writer) { |
67
|
|
|
$writer->save('php://output'); |
68
|
|
|
} |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
// set headers |
72
|
|
|
$response->headers->set('Content-type', 'application/csv; charset=' . $this->charset); |
73
|
|
|
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"'); |
74
|
|
|
$response->headers->set('Cache-Control', 'max-age=0'); |
75
|
|
|
$response->headers->set('Pragma', 'no-cache'); |
76
|
|
|
|
77
|
|
|
return $response; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getResponse(Spreadsheet $spreadsheet, string $filename, array $options = []): StreamedResponse |
81
|
|
|
{ |
82
|
|
|
$options = array_merge($this->getDefaultOptions(), $options); |
83
|
|
|
|
84
|
|
|
return $this->getStreamedResponse( |
85
|
|
|
$this->getWriter($spreadsheet, $options), |
86
|
|
|
$filename |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getResponseForUser(Spreadsheet $spreadsheet, string $filename, User $user): StreamedResponse |
91
|
|
|
{ |
92
|
|
|
$options = array_merge($this->getDefaultOptions(), $this->getUserOptions($user)); |
93
|
|
|
|
94
|
|
|
return $this->getStreamedResponse( |
95
|
|
|
$this->getWriter($spreadsheet, $options), |
96
|
|
|
$filename |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|