1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Extension "sf_event_mgt" for TYPO3 CMS. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the |
9
|
|
|
* LICENSE.txt file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DERHANSEN\SfEventMgt\Service; |
13
|
|
|
|
14
|
|
|
use DateTime; |
15
|
|
|
use DERHANSEN\SfEventMgt\Domain\Model\Event; |
16
|
|
|
use DERHANSEN\SfEventMgt\Domain\Model\Registration; |
17
|
|
|
use DERHANSEN\SfEventMgt\Domain\Repository\EventRepository; |
18
|
|
|
use DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository; |
19
|
|
|
use DERHANSEN\SfEventMgt\Event\ModifyDownloadRegistrationCsvEvent; |
20
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
21
|
|
|
use TYPO3\CMS\Core\Utility\CsvUtility; |
22
|
|
|
use TYPO3\CMS\Extbase\Reflection\ObjectAccess; |
|
|
|
|
23
|
|
|
|
24
|
|
|
class ExportService |
25
|
|
|
{ |
26
|
|
|
protected RegistrationRepository $registrationRepository; |
27
|
|
|
protected EventRepository $eventRepository; |
28
|
|
|
protected EventDispatcherInterface $eventDispatcher; |
29
|
|
|
|
30
|
|
|
public function __construct( |
31
|
|
|
RegistrationRepository $registrationRepository, |
32
|
|
|
EventRepository $eventRepository, |
33
|
|
|
EventDispatcherInterface $eventDispatcher |
34
|
|
|
) { |
35
|
|
|
$this->registrationRepository = $registrationRepository; |
36
|
|
|
$this->eventRepository = $eventRepository; |
37
|
|
|
$this->eventDispatcher = $eventDispatcher; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Initiates the CSV downloads for registrations of the given event uid |
42
|
|
|
*/ |
43
|
|
|
public function downloadRegistrationsCsv(int $eventUid, array $settings = []): void |
44
|
|
|
{ |
45
|
|
|
$modifyDownloadRegistrationCsvEvent = new ModifyDownloadRegistrationCsvEvent( |
46
|
|
|
$this->exportRegistrationsCsv($eventUid, $settings), |
47
|
|
|
'event_' . $eventUid . '_reg_' . date('dmY_His') . '.csv', |
48
|
|
|
$eventUid, |
49
|
|
|
$settings |
50
|
|
|
); |
51
|
|
|
$this->eventDispatcher->dispatch($modifyDownloadRegistrationCsvEvent); |
52
|
|
|
|
53
|
|
|
$content = $modifyDownloadRegistrationCsvEvent->getCsvContent(); |
54
|
|
|
$filename = $modifyDownloadRegistrationCsvEvent->getDownloadFilename(); |
55
|
|
|
|
56
|
|
|
header('Content-Disposition: attachment; filename="' . $filename . '"'); |
57
|
|
|
header('Content-Type: text/csv'); |
58
|
|
|
header('Content-Length: ' . strlen($content)); |
59
|
|
|
header('Expires: 0'); |
60
|
|
|
header('Cache-Control: must-revalidate'); |
61
|
|
|
header('Pragma: no-cache'); |
62
|
|
|
echo $content; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns all Registrations for the given eventUid as a CSV string |
67
|
|
|
*/ |
68
|
|
|
public function exportRegistrationsCsv(int $eventUid, array $settings = []): string |
69
|
|
|
{ |
70
|
|
|
$hasRegistrationFields = false; |
71
|
|
|
$registrationFieldData = []; |
72
|
|
|
$fieldsArray = array_map('trim', explode(',', ($settings['fields'] ?? ''))); |
73
|
|
|
|
74
|
|
|
if (in_array('registration_fields', $fieldsArray, true)) { |
75
|
|
|
$hasRegistrationFields = true; |
76
|
|
|
$registrationFieldData = $this->getRegistrationFieldData($eventUid); |
77
|
|
|
$fieldsArray = array_diff($fieldsArray, ['registration_fields']); |
78
|
|
|
} |
79
|
|
|
$registrations = $this->registrationRepository->findByEvent($eventUid); |
80
|
|
|
$exportedRegistrations = CsvUtility::csvValues( |
81
|
|
|
array_merge($fieldsArray, $registrationFieldData), |
82
|
|
|
$settings['fieldDelimiter'] ?? ',', |
83
|
|
|
$settings['fieldQuoteCharacter'] ?? '"' |
84
|
|
|
) . chr(10); |
85
|
|
|
/** @var Registration $registration */ |
86
|
|
|
foreach ($registrations as $registration) { |
87
|
|
|
$exportedRegistration = []; |
88
|
|
|
foreach ($fieldsArray as $field) { |
89
|
|
|
$exportedRegistration[] = $this->getFieldValue($registration, $field, $settings); |
90
|
|
|
} |
91
|
|
|
if ($hasRegistrationFields) { |
92
|
|
|
$exportedRegistration = array_merge( |
93
|
|
|
$exportedRegistration, |
94
|
|
|
$this->getRegistrationFieldValues($registration, $registrationFieldData) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
$exportedRegistrations .= CsvUtility::csvValues( |
98
|
|
|
$exportedRegistration, |
99
|
|
|
$settings['fieldDelimiter'] ?? ',', |
100
|
|
|
$settings['fieldQuoteCharacter'] ?? '"' |
101
|
|
|
) . chr(10); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->prependByteOrderMark($exportedRegistrations, $settings); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns an array with fieldvalues for the given registration |
109
|
|
|
*/ |
110
|
|
|
protected function getRegistrationFieldValues(Registration $registration, array $registrationFieldData): array |
111
|
|
|
{ |
112
|
|
|
$result = []; |
113
|
|
|
$registrationFieldValues = []; |
114
|
|
|
/** @var Registration\FieldValue $fieldValue */ |
115
|
|
|
foreach ($registration->getFieldValues() as $fieldValue) { |
116
|
|
|
$field = $fieldValue->getField(); |
117
|
|
|
if ($field !== null) { |
118
|
|
|
$registrationFieldValues[$field->getUid()] = |
119
|
|
|
$this->replaceLineBreaks($fieldValue->getValueForCsvExport()); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
foreach ($registrationFieldData as $fieldUid => $fieldTitle) { |
123
|
|
|
if (isset($registrationFieldValues[$fieldUid])) { |
124
|
|
|
$result[] = $registrationFieldValues[$fieldUid]; |
125
|
|
|
} else { |
126
|
|
|
$result[] = ''; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $result; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns an array of registration field uids and title |
135
|
|
|
*/ |
136
|
|
|
protected function getRegistrationFieldData(int $eventUid): array |
137
|
|
|
{ |
138
|
|
|
$result = []; |
139
|
|
|
/** @var Event $event */ |
140
|
|
|
$event = $this->eventRepository->findByUid($eventUid); |
141
|
|
|
if ($event !== null) { |
142
|
|
|
$result = $event->getRegistrationFieldUidsWithTitle(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $result; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Prepends Byte Order Mark to exported registrations |
150
|
|
|
*/ |
151
|
|
|
protected function prependByteOrderMark(string $exportedRegistrations, array $settings): string |
152
|
|
|
{ |
153
|
|
|
if ((bool)($settings['prependBOM'] ?? false)) { |
154
|
|
|
$exportedRegistrations = chr(239) . chr(187) . chr(191) . $exportedRegistrations; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $exportedRegistrations; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns the requested field from the given registration. If the field is a DateTime object, |
162
|
|
|
* a formatted date string is returned |
163
|
|
|
*/ |
164
|
|
|
protected function getFieldValue(Registration $registration, string $field, array $settings): string |
165
|
|
|
{ |
166
|
|
|
$value = ObjectAccess::getPropertyPath($registration, $field); |
167
|
|
|
if ($value instanceof DateTime) { |
168
|
|
|
$dateFormat = $settings['dateFieldFormat'] ?? 'd.m.Y'; |
169
|
|
|
$value = $value->format($dateFormat); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $this->replaceLineBreaks((string)$value); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Replaces all line breaks with a space |
177
|
|
|
*/ |
178
|
|
|
protected function replaceLineBreaks(string $value): string |
179
|
|
|
{ |
180
|
|
|
return str_replace(["\r\n", "\r", "\n"], ' ', $value); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths