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\ViewHelpers\Registration\Field; |
13
|
|
|
|
14
|
|
|
use DERHANSEN\SfEventMgt\Domain\Model\Registration\Field; |
15
|
|
|
use DERHANSEN\SfEventMgt\ViewHelpers\AbstractPrefillViewHelper; |
16
|
|
|
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters; |
17
|
|
|
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication; |
18
|
|
|
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* PrefillField ViewHelper for registration fields |
22
|
|
|
*/ |
23
|
|
|
class PrefillFieldViewHelper extends AbstractPrefillViewHelper |
24
|
|
|
{ |
25
|
|
|
public function initializeArguments(): void |
26
|
|
|
{ |
27
|
|
|
parent::initializeArguments(); |
28
|
|
|
$this->registerArgument('registrationField', 'object', 'The registrationField object', true); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns a string to be used as prefill value for the given registration field (type=input). If the form |
33
|
|
|
* has already been submitted, the submitted value for the field is returned. |
34
|
|
|
* |
35
|
|
|
* 1. Default value |
36
|
|
|
* 2. fe_user record value |
37
|
|
|
*/ |
38
|
|
|
public function render(): string |
39
|
|
|
{ |
40
|
|
|
/** @var Field $registrationField */ |
41
|
|
|
$registrationField = $this->arguments['registrationField']; |
42
|
|
|
|
43
|
|
|
// If mapping errors occurred for form, return value that has been submitted from POST data |
44
|
|
|
/** @var ExtbaseRequestParameters $extbaseRequestParameters */ |
45
|
|
|
$extbaseRequestParameters = $this->renderingContext->getRequest()->getAttribute('extbase'); |
|
|
|
|
46
|
|
|
$originalRequest = $extbaseRequestParameters->getOriginalRequest(); |
47
|
|
|
|
48
|
|
|
if ($originalRequest) { |
49
|
|
|
$registrationData = $originalRequest->getParsedBody()[$this->getPluginNamespace($originalRequest)] ?? []; |
50
|
|
|
return $this->getFieldValueFromSubmittedData($registrationData, $registrationField->getUid()); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$value = $registrationField->getDefaultValue(); |
54
|
|
|
return $this->prefillFromFeuserData($registrationField, $value); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the submitted value for the given field uid |
59
|
|
|
*/ |
60
|
|
|
protected function getFieldValueFromSubmittedData(array $submittedData, int $fieldUid): string |
61
|
|
|
{ |
62
|
|
|
$result = ''; |
63
|
|
|
foreach ($submittedData['registration']['fields'] ?? [] as $submittedFieldUid => $fieldValue) { |
64
|
|
|
if ((int)$submittedFieldUid === $fieldUid) { |
65
|
|
|
$result = $fieldValue; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $result; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Prefills $value with fe_users data if configured in registration field |
74
|
|
|
* |
75
|
|
|
* @param Field $field |
76
|
|
|
* @param string $value |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function prefillFromFeuserData(Field $field, string $value): string |
80
|
|
|
{ |
81
|
|
|
if (!$this->getTypoScriptFrontendController() || |
82
|
|
|
!$this->getFrontendUser()->user || |
83
|
|
|
$field->getFeuserValue() === '' || |
84
|
|
|
!array_key_exists($field->getFeuserValue(), $this->getFrontendUser()->user) |
85
|
|
|
) { |
86
|
|
|
return $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return (string)$this->getFrontendUser()->user[$field->getFeuserValue()]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function getFrontendUser(): FrontendUserAuthentication |
93
|
|
|
{ |
94
|
|
|
return $this->getTypoScriptFrontendController()->fe_user; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function getTypoScriptFrontendController(): ?TypoScriptFrontendController |
98
|
|
|
{ |
99
|
|
|
return $GLOBALS['TSFE'] ?? null; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|