1
|
|
|
<?php |
2
|
|
|
namespace DERHANSEN\SfEventMgt\Service; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the TYPO3 CMS project. |
6
|
|
|
* |
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
9
|
|
|
* of the License, or any later version. |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please read the |
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
13
|
|
|
* |
14
|
|
|
* The TYPO3 project - inspiring people to share! |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
use \TYPO3\CMS\Core\Utility\GeneralUtility; |
18
|
|
|
use \RuntimeException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class ExportService |
22
|
|
|
* |
23
|
|
|
* @author Torben Hansen <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ExportService |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Repository with registrations for the events |
30
|
|
|
* |
31
|
|
|
* @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
32
|
|
|
* @inject |
33
|
|
|
*/ |
34
|
|
|
protected $registrationRepository; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* ResourceFactory |
38
|
|
|
* |
39
|
|
|
* @var \TYPO3\CMS\Core\Resource\ResourceFactory |
40
|
|
|
* @inject |
41
|
|
|
*/ |
42
|
|
|
protected $resourceFactory = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Initiates the CSV downloads for registrations of the given event uid |
46
|
|
|
* |
47
|
|
|
* @param int $eventUid EventUid |
48
|
|
|
* @param array $settings Settings |
49
|
|
|
* |
50
|
|
|
* @throws \RuntimeException RuntimeException |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
2 |
|
public function downloadRegistrationsCsv($eventUid, $settings = []) |
54
|
|
|
{ |
55
|
2 |
|
$storage = $this->resourceFactory->getDefaultStorage(); |
56
|
2 |
|
if ($storage === null) { |
57
|
1 |
|
throw new RuntimeException('Could not get the default storage', 1475590001); |
58
|
|
|
} |
59
|
1 |
|
$registrations = $this->exportRegistrationsCsv($eventUid, $settings); |
60
|
1 |
|
$tempFolder = $storage->getFolder('_temp_'); |
61
|
1 |
|
$tempFile = $storage->createFile('sf_events_export.csv', $tempFolder); |
62
|
1 |
|
$tempFile->setContents($registrations); |
63
|
1 |
|
$storage->dumpFileContents($tempFile, true, 'registrations_' . date('dmY_His') . '.csv'); |
64
|
1 |
|
$tempFile->delete(); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns all Registrations for the given eventUid as a CSV string |
69
|
|
|
* |
70
|
|
|
* @param int $eventUid EventUid |
71
|
|
|
* @param array $settings Settings |
72
|
|
|
* |
73
|
|
|
* @throws \RuntimeException RuntimeException |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
5 |
|
public function exportRegistrationsCsv($eventUid, $settings = []) |
77
|
|
|
{ |
78
|
5 |
|
$fieldsArray = array_map('trim', explode(',', $settings['fields'])); |
79
|
5 |
|
$registrations = $this->registrationRepository->findByEvent($eventUid); |
80
|
5 |
|
$exportedRegistrations = GeneralUtility::csvValues($fieldsArray, |
81
|
5 |
|
$settings['fieldDelimiter'], $settings['fieldQuoteCharacter']) . chr(10); |
82
|
5 |
|
foreach ($registrations as $registration) { |
83
|
5 |
|
$exportedRegistration = []; |
84
|
5 |
|
foreach ($fieldsArray as $field) { |
85
|
5 |
|
if ($registration->_hasProperty($field)) { |
86
|
5 |
|
$exportedRegistration[] = $this->getFieldValue($registration, $field); |
87
|
5 |
|
} else { |
88
|
1 |
|
throw new RuntimeException('Field ' . $field . |
89
|
1 |
|
' is not a Property of Model Registration, please check your TS configuration', 1475590002); |
90
|
|
|
} |
91
|
5 |
|
} |
92
|
4 |
|
$exportedRegistrations .= GeneralUtility::csvValues($exportedRegistration, |
93
|
4 |
|
$settings['fieldDelimiter'], $settings['fieldQuoteCharacter']) . chr(10); |
94
|
4 |
|
} |
95
|
4 |
|
return $exportedRegistrations; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the requested field from the given registration. If the field is a DateTime object, |
100
|
|
|
* a formatted date string is returned |
101
|
|
|
* |
102
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration |
103
|
|
|
* @param string $field |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
5 |
|
protected function getFieldValue($registration, $field) |
107
|
|
|
{ |
108
|
5 |
|
$value = $registration->_getCleanProperty($field); |
109
|
5 |
|
if ($value instanceof \DateTime) { |
110
|
|
|
$value = $value->format('d.m.Y'); |
111
|
|
|
} |
112
|
5 |
|
return $value; |
113
|
|
|
} |
114
|
|
|
} |