Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.77% covered (success)
92.77%
77 / 83
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ScopeAppointmentsByDayXlsExport
92.77% covered (success)
92.77%
77 / 83
66.67% covered (warning)
66.67%
2 / 3
16.10
0.00% covered (danger)
0.00%
0 / 1
 readResponse
91.30% covered (success)
91.30%
63 / 69
0.00% covered (danger)
0.00%
0 / 1
10.07
 escapeSpreadsheetFormula
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 convertspecialchars
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 *
5 * @package Zmsadmin
6 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
7 *
8 */
9
10namespace BO\Zmsadmin;
11
12use PhpOffice\PhpSpreadsheet\IOFactory;
13use PhpOffice\PhpSpreadsheet\Spreadsheet;
14
15/**
16 * Handle requests concerning services
17 */
18class ScopeAppointmentsByDayXlsExport extends BaseController
19{
20    /**
21     *
22     * @return \Psr\Http\Message\ResponseInterface
23     */
24    #[\Override]
25    public function readResponse(
26        \Psr\Http\Message\RequestInterface $request,
27        \Psr\Http\Message\ResponseInterface $response,
28        array $args
29    ): \Psr\Http\Message\ResponseInterface {
30        $workstation = \App::$http->readGetResult('/workstation/', [
31            'resolveReferences' => 1,
32            'gql' => Helper\GraphDefaults::getWorkstation()
33        ])->getEntity();
34        $workstationRequest = new \BO\Zmsclient\WorkstationRequests(\App::$http, $workstation);
35        $selectedDateTime = ScopeAppointmentsByDay::readSelectedDateTime($args['date']);
36        $scope = ScopeAppointmentsByDay::readSelectedScope($workstation, $workstationRequest, $args['id']);
37        $processList = ScopeAppointmentsByDay::readProcessList($workstationRequest, $selectedDateTime);
38
39        $xlsSheetTitle = $selectedDateTime->format('d.m.Y');
40        $clusterColumn = $workstation->isClusterEnabled() ? 'Kürzel' : 'Lfd. Nummer';
41        $customTextfieldActivated = (int) $scope->getCustomTextfieldActivated() === 1;
42        $customTextfield2Activated = (int) $scope->getCustomTextfield2Activated() === 1;
43
44        $xlsHeaders = [
45            $clusterColumn,
46            'Uhrzeit/Ankunftszeit',
47            'Nr.',
48            'Name',
49            'Telefon',
50            'Email',
51            'Dienstleistung',
52            'Anmerkungen',
53        ];
54
55        if ($customTextfieldActivated) {
56            $label = trim((string) $scope->getCustomTextfieldLabel());
57            $xlsHeaders[] = $label !== '' ? $label : 'Freitextfeld 1';
58        }
59        if ($customTextfield2Activated) {
60            $label = trim((string) $scope->getCustomTextfield2Label());
61            $xlsHeaders[] = $label !== '' ? $label : 'Freitextfeld 2';
62        }
63
64        $rows = [];
65        $key = 1;
66        foreach ($processList as $queueItem) {
67            $client = $queueItem->getFirstClient();
68            $row = [
69                $workstation->isClusterEnabled() ? $queueItem->getCurrentScope()->shortName : $key++,
70                $queueItem->getArrivalTime()->setTimezone(\App::$now->getTimezone())->format('H:i:s'),
71                $queueItem->queue['number'],
72                $client['familyName'],
73                $client['telephone'],
74                $client['email'],
75                $queueItem->requests->getCsvForProperty('name'),
76                $queueItem->amendment,
77            ];
78            if ($customTextfieldActivated) {
79                $row[] = $queueItem->customTextfield;
80            }
81            if ($customTextfield2Activated) {
82                $row[] = $queueItem->customTextfield2;
83            }
84            $rows[] = array_map([$this, 'escapeSpreadsheetFormula'], $row);
85        }
86
87        $spreadsheet = new Spreadsheet();
88        $spreadsheet->getProperties()
89            ->setCreator('eappointment')
90            ->setTitle('Tagesübersicht ' . $xlsSheetTitle);
91        $spreadsheet->getActiveSheet()->fromArray(
92            array_merge([$xlsHeaders], $rows),
93            null,
94            'A1'
95        );
96
97        $resource = fopen('php://temp', 'r+');
98        IOFactory::createWriter($spreadsheet, 'Xlsx')->save($resource);
99        rewind($resource);
100        $response->getBody()->write(stream_get_contents($resource));
101        fclose($resource);
102
103        $fileName = sprintf("Tagesübersicht_%s_%s.xlsx", $scope->contact['name'], $xlsSheetTitle);
104        return $response
105            ->withHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
106            ->withHeader('Content-Description', 'File Transfer')
107            ->withHeader(
108                'Content-Disposition',
109                sprintf('attachment; filename="%s"', $this->convertspecialChars($fileName))
110            );
111    }
112
113    protected function escapeSpreadsheetFormula($value)
114    {
115        if (!is_string($value) || $value === '') {
116            return $value;
117        }
118        if (preg_match('/^[=\-+@\t\r]/u', $value)) {
119            return "'" . $value;
120        }
121        return $value;
122    }
123
124    protected function convertspecialchars($string)
125    {
126
127        $convert = array (
128            array ('ä','ae',),
129            array ('ö','oe',),
130            array ('ü','ue',),
131            array ('ß','ss',),
132        );
133
134
135        foreach ($convert as $array) {
136            $string = str_replace($array[0], $array[1], $string);
137        }
138        return $string;
139    }
140}