1
|
|
|
<?php |
2
|
|
|
namespace DERHANSEN\SfEventMgt\Service; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the Extension "sf_event_mgt" for TYPO3 CMS. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please read the |
8
|
|
|
* LICENSE.txt file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
use DERHANSEN\SfEventMgt\Domain\Model\Event; |
12
|
|
|
use DERHANSEN\SfEventMgt\Domain\Model\Registration; |
13
|
|
|
use DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository; |
14
|
|
|
use DERHANSEN\SfEventMgt\Exception; |
15
|
|
|
use TYPO3\CMS\Core\Utility\CsvUtility; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ExportService |
19
|
|
|
* |
20
|
|
|
* @author Torben Hansen <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class ExportService |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
26
|
|
|
*/ |
27
|
|
|
protected $registrationRepository = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \DERHANSEN\SfEventMgt\Domain\Repository\EventRepository |
31
|
|
|
*/ |
32
|
|
|
protected $eventRepository = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param RegistrationRepository $registrationRepository |
36
|
|
|
*/ |
37
|
|
|
public function injectRegistrationRepository( |
38
|
|
|
\DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository |
39
|
|
|
) { |
40
|
|
|
$this->registrationRepository = $registrationRepository; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Repository\EventRepository $eventRepository |
45
|
|
|
*/ |
46
|
|
|
public function injectEventRepository(\DERHANSEN\SfEventMgt\Domain\Repository\EventRepository $eventRepository) |
47
|
|
|
{ |
48
|
|
|
$this->eventRepository = $eventRepository; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Initiates the CSV downloads for registrations of the given event uid |
53
|
4 |
|
* |
54
|
|
|
* @param int $eventUid EventUid |
55
|
4 |
|
* @param array $settings Settings |
56
|
4 |
|
* @throws Exception RuntimeException |
57
|
2 |
|
* @return void |
58
|
|
|
*/ |
59
|
2 |
|
public function downloadRegistrationsCsv($eventUid, $settings = []) |
60
|
2 |
|
{ |
61
|
2 |
|
$content = $this->exportRegistrationsCsv($eventUid, $settings); |
62
|
2 |
|
header('Content-Disposition: attachment; filename="event_' . $eventUid . '_reg_' . date('dmY_His') . '.csv"'); |
63
|
2 |
|
header('Content-Type: text/csv'); |
64
|
2 |
|
header('Content-Length: ' . strlen($content)); |
65
|
2 |
|
header('Expires: 0'); |
66
|
|
|
header('Cache-Control: must-revalidate'); |
67
|
|
|
header('Pragma: no-cache'); |
68
|
|
|
echo $content; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns all Registrations for the given eventUid as a CSV string |
73
|
|
|
* |
74
|
|
|
* @param int $eventUid EventUid |
75
|
|
|
* @param array $settings Settings |
76
|
10 |
|
* @throws Exception RuntimeException |
77
|
|
|
* @return string |
78
|
10 |
|
*/ |
79
|
10 |
|
public function exportRegistrationsCsv($eventUid, $settings = []) |
80
|
10 |
|
{ |
81
|
10 |
|
$hasRegistrationFields = false; |
82
|
10 |
|
$registrationFieldData = []; |
83
|
10 |
|
$fieldsArray = array_map('trim', explode(',', $settings['fields'])); |
84
|
10 |
|
|
85
|
10 |
|
if (in_array('registration_fields', $fieldsArray)) { |
86
|
10 |
|
$hasRegistrationFields = true; |
87
|
10 |
|
$registrationFieldData = $this->getRegistrationFieldData($eventUid); |
88
|
2 |
|
$fieldsArray = array_diff($fieldsArray, ['registration_fields']); |
89
|
2 |
|
} |
90
|
|
|
$registrations = $this->registrationRepository->findByEvent($eventUid); |
|
|
|
|
91
|
10 |
|
$exportedRegistrations = CsvUtility::csvValues( |
92
|
8 |
|
array_merge($fieldsArray, $registrationFieldData), |
93
|
8 |
|
$settings['fieldDelimiter'], |
94
|
8 |
|
$settings['fieldQuoteCharacter'] |
95
|
8 |
|
) . chr(10); |
96
|
|
|
/** @var Registration $registration */ |
97
|
|
|
foreach ($registrations as $registration) { |
98
|
|
|
$exportedRegistration = []; |
99
|
|
|
foreach ($fieldsArray as $field) { |
100
|
|
|
if ($registration->_hasProperty($field)) { |
101
|
|
|
$exportedRegistration[] = $this->getFieldValue($registration, $field); |
102
|
|
|
} else { |
103
|
|
|
throw new Exception('Field ' . $field . |
104
|
|
|
' is not a Property of Model Registration, please check your TS configuration', 1475590002); |
105
|
|
|
} |
106
|
10 |
|
} |
107
|
|
|
if ($hasRegistrationFields) { |
108
|
10 |
|
$exportedRegistration = array_merge( |
109
|
10 |
|
$exportedRegistration, |
110
|
|
|
$this->getRegistrationFieldValues($registration, $registrationFieldData) |
111
|
|
|
); |
112
|
10 |
|
} |
113
|
|
|
$exportedRegistrations .= CsvUtility::csvValues( |
114
|
|
|
$exportedRegistration, |
115
|
|
|
$settings['fieldDelimiter'], |
116
|
|
|
$settings['fieldQuoteCharacter'] |
117
|
|
|
) . chr(10); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->prependByteOrderMark($exportedRegistrations, $settings); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns an array with fieldvalues for the given registration |
125
|
|
|
* |
126
|
|
|
* @param Registration $registration |
127
|
|
|
* @param array $registrationFieldData |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
protected function getRegistrationFieldValues($registration, $registrationFieldData) |
131
|
|
|
{ |
132
|
|
|
$result = []; |
133
|
|
|
$registrationFieldValues = []; |
134
|
|
|
/** @var Registration\FieldValue $fieldValue */ |
135
|
|
|
foreach ($registration->getFieldValues() as $fieldValue) { |
136
|
|
|
$registrationFieldValues[$fieldValue->getField()->getUid()] = |
137
|
|
|
$this->replaceLineBreaks($fieldValue->getValueForCsvExport()); |
138
|
|
|
} |
139
|
|
|
foreach ($registrationFieldData as $fieldUid => $fieldTitle) { |
140
|
|
|
if (isset($registrationFieldValues[$fieldUid])) { |
141
|
|
|
$result[] = $registrationFieldValues[$fieldUid]; |
142
|
|
|
} else { |
143
|
|
|
$result[] = ''; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $result; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns an array of registration field uids and title |
152
|
|
|
* |
153
|
|
|
* @param int $eventUid |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
protected function getRegistrationFieldData($eventUid) |
157
|
|
|
{ |
158
|
|
|
$result = []; |
159
|
|
|
/** @var Event $event */ |
160
|
|
|
$event = $this->eventRepository->findByUid($eventUid); |
161
|
|
|
if ($event) { |
162
|
|
|
$result = $event->getRegistrationFieldUidsWithTitle(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $result; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Prepends Byte Order Mark to exported registrations |
170
|
|
|
* |
171
|
|
|
* @param string $exportedRegistrations |
172
|
|
|
* @param array $settings |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
protected function prependByteOrderMark($exportedRegistrations, $settings) |
176
|
|
|
{ |
177
|
|
|
if ((bool)$settings['prependBOM']) { |
178
|
|
|
$exportedRegistrations = chr(239) . chr(187) . chr(191) . $exportedRegistrations; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $exportedRegistrations; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Returns the requested field from the given registration. If the field is a DateTime object, |
186
|
|
|
* a formatted date string is returned |
187
|
|
|
* |
188
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration |
189
|
|
|
* @param string $field |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
protected function getFieldValue($registration, $field) |
193
|
|
|
{ |
194
|
|
|
$value = $registration->_getCleanProperty($field); |
195
|
|
|
if ($value instanceof \DateTime) { |
196
|
|
|
$value = $value->format('d.m.Y'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $this->replaceLineBreaks($value); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Replaces all line breaks with a space |
204
|
|
|
* |
205
|
|
|
* @param mixed $value |
206
|
|
|
* @return mixed |
207
|
|
|
*/ |
208
|
|
|
protected function replaceLineBreaks($value) |
209
|
|
|
{ |
210
|
|
|
return str_replace(["\r\n", "\r", "\n"], ' ', $value); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication |
215
|
|
|
*/ |
216
|
|
|
protected function getBackendUser() |
217
|
|
|
{ |
218
|
|
|
return $GLOBALS['BE_USER']; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.