|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Extension "sf_event_mgt" for TYPO3 CMS. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please read the |
|
7
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace DERHANSEN\SfEventMgt\ViewHelpers\Registration\Field; |
|
11
|
|
|
|
|
12
|
|
|
use DERHANSEN\SfEventMgt\Domain\Model\Registration\Field; |
|
13
|
|
|
use DERHANSEN\SfEventMgt\ViewHelpers\AbstractPrefillViewHelper; |
|
14
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* PrefillMultiValueField ViewHelper for registration fields |
|
18
|
|
|
*/ |
|
19
|
|
|
class PrefillMultiValueFieldViewHelper extends AbstractPrefillViewHelper |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Initialize arguments |
|
23
|
|
|
*/ |
|
24
|
|
|
public function initializeArguments() |
|
25
|
|
|
{ |
|
26
|
|
|
parent::initializeArguments(); |
|
27
|
|
|
$this->registerArgument('registrationField', 'object', 'RegistrationField', true); |
|
28
|
|
|
$this->registerArgument('currentValue', 'strong', 'Current value', true); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Returns, if the given $currentValue is selected/checked for the given registration field is selected |
|
33
|
|
|
* If no originalRequest exist (form is not submitted), true is returned if the given $currentValue |
|
34
|
|
|
* matches the default value of the field |
|
35
|
|
|
* |
|
36
|
|
|
* @return bool |
|
37
|
|
|
*/ |
|
38
|
|
|
public function render() |
|
39
|
|
|
{ |
|
40
|
|
|
/** @var Field $registrationField */ |
|
41
|
|
|
$registrationField = $this->arguments['registrationField']; |
|
42
|
|
|
$currentValue = $this->arguments['currentValue']; |
|
43
|
|
|
|
|
44
|
|
|
// If mapping errors occured for form, return value that has been submitted |
|
45
|
|
|
$originalRequest = $this->renderingContext->getRequest()->getOriginalRequest(); |
|
|
|
|
|
|
46
|
|
|
if ($originalRequest) { |
|
47
|
|
|
$registrationData = $originalRequest->getParsedBody()[$this->getPluginNamespace($originalRequest)] ?? []; |
|
48
|
|
|
|
|
49
|
|
|
return $this->getFieldValueFromSubmittedData( |
|
50
|
|
|
$registrationData, |
|
51
|
|
|
$registrationField->getUid(), |
|
|
|
|
|
|
52
|
|
|
$currentValue |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->getFieldValueFromDefaultProperty($registrationField, $currentValue); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns if the submitted field value is selected |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $submittedData |
|
63
|
|
|
* @param int $fieldUid |
|
64
|
|
|
* @param string $currentValue |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getFieldValueFromSubmittedData(array $submittedData, int $fieldUid, string $currentValue): bool |
|
68
|
|
|
{ |
|
69
|
|
|
$result = false; |
|
70
|
|
|
|
|
71
|
|
|
foreach ($submittedData['registration']['fields'] ?? [] as $submittedFieldUid => $fieldValue) { |
|
72
|
|
|
if ((int)$submittedFieldUid === $fieldUid) { |
|
73
|
|
|
$result = $this->isGivenValueSelected($fieldValue, $currentValue); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $result; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param mixed $fieldValue |
|
82
|
|
|
* @param string $currentValue |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function isGivenValueSelected($fieldValue, string $currentValue): bool |
|
86
|
|
|
{ |
|
87
|
|
|
if (is_array($fieldValue)) { |
|
88
|
|
|
return in_array($currentValue, $fieldValue); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $currentValue === $fieldValue; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Registration\Field $registrationField |
|
96
|
|
|
* @param string $currentValue |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function getFieldValueFromDefaultProperty(Field $registrationField, string $currentValue) |
|
100
|
|
|
{ |
|
101
|
|
|
$defaultValues = GeneralUtility::trimExplode(',', $registrationField->getDefaultValue()); |
|
102
|
|
|
|
|
103
|
|
|
return in_array($currentValue, $defaultValues); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|