1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\Service\ViewHelper; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\AssetHandler\Html\DataAttributesAssetHandler; |
17
|
|
|
use Romm\Formz\Behaviours\BehavioursManager; |
18
|
|
|
use Romm\Formz\Core\Core; |
19
|
|
|
use Romm\Formz\Exceptions\DuplicateEntryException; |
20
|
|
|
use Romm\Formz\Form\FormObject\FormObject; |
21
|
|
|
use Romm\Formz\Validation\Validator\Form\DefaultFormValidator; |
22
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
23
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
24
|
|
|
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This class contains methods that help view helpers to manipulate data and |
28
|
|
|
* know more things concerning the current form state. |
29
|
|
|
* |
30
|
|
|
* It is mainly configured inside the `FormViewHelper`, and used in other |
31
|
|
|
* view helpers. |
32
|
|
|
*/ |
33
|
|
|
class FormViewHelperService implements SingletonInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
protected $formContext = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var FormObject |
42
|
|
|
*/ |
43
|
|
|
protected $formObject; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Reset every state that can be used by this service. |
47
|
|
|
*/ |
48
|
|
|
public function resetState() |
49
|
|
|
{ |
50
|
|
|
$this->formContext = false; |
51
|
|
|
$this->formObject = null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Will activate the form context, changing the result returned by the |
56
|
|
|
* function `formContextExists()`. |
57
|
|
|
* |
58
|
|
|
* @return FormViewHelperService |
59
|
|
|
* @throws DuplicateEntryException |
60
|
|
|
*/ |
61
|
|
|
public function activateFormContext() |
62
|
|
|
{ |
63
|
|
|
if (true === $this->formContext) { |
64
|
|
|
throw DuplicateEntryException::duplicatedFormContext(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->formContext = true; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns `true` if the `FormViewHelper` context exists. |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function formContextExists() |
78
|
|
|
{ |
79
|
|
|
return $this->formContext; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Will loop on the submitted form fields and apply behaviours if their |
84
|
|
|
* configuration contains. |
85
|
|
|
* |
86
|
|
|
* @param ControllerContext $controllerContext |
87
|
|
|
*/ |
88
|
|
|
public function applyBehavioursOnSubmittedForm(ControllerContext $controllerContext) |
89
|
|
|
{ |
90
|
|
|
if ($this->formObject->formWasSubmitted()) { |
91
|
|
|
$request = $controllerContext->getRequest()->getOriginalRequest(); |
92
|
|
|
$formName = $this->formObject->getName(); |
93
|
|
|
|
94
|
|
|
if ($request |
95
|
|
|
&& $request->hasArgument($formName) |
96
|
|
|
) { |
97
|
|
|
/** @var BehavioursManager $behavioursManager */ |
98
|
|
|
$behavioursManager = GeneralUtility::makeInstance(BehavioursManager::class); |
99
|
|
|
|
100
|
|
|
/** @var array $originalForm */ |
101
|
|
|
$originalForm = $request->getArgument($formName); |
102
|
|
|
|
103
|
|
|
$formProperties = $behavioursManager->applyBehaviourOnPropertiesArray( |
104
|
|
|
$originalForm, |
105
|
|
|
$this->formObject->getDefinition() |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$request->setArgument($formName, $formProperties); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @todo |
115
|
|
|
* |
116
|
|
|
* @param DataAttributesAssetHandler $dataAttributesAssetHandler |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function getDataAttributes(DataAttributesAssetHandler $dataAttributesAssetHandler) |
120
|
|
|
{ |
121
|
|
|
$dataAttributes = []; |
122
|
|
|
|
123
|
|
|
if ($this->formObject->hasForm()) { |
124
|
|
|
if (false === $this->formObject->formWasValidated()) { |
125
|
|
|
$form = $this->formObject->getForm(); |
126
|
|
|
$formValidator = $this->getFormValidator($this->formObject->getName()); |
127
|
|
|
$formResult = $formValidator->validateGhost($form); |
128
|
|
|
} else { |
129
|
|
|
$formResult = $this->formObject->getFormResult(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$dataAttributes += $dataAttributesAssetHandler->getFieldsValuesDataAttributes($formResult); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (true === $this->formObject->formWasSubmitted()) { |
136
|
|
|
$dataAttributes += [DataAttributesAssetHandler::getFieldSubmissionDone() => '1']; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (true === $this->formObject->formWasValidated()) { |
140
|
|
|
$dataAttributes += $dataAttributesAssetHandler->getFieldsValidDataAttributes(); |
141
|
|
|
$dataAttributes += $dataAttributesAssetHandler->getFieldsMessagesDataAttributes(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $dataAttributes; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $formName |
149
|
|
|
* @return DefaultFormValidator |
150
|
|
|
*/ |
151
|
|
|
protected function getFormValidator($formName) |
152
|
|
|
{ |
153
|
|
|
/** @var DefaultFormValidator $validation */ |
154
|
|
|
$validation = Core::instantiate(DefaultFormValidator::class, ['name' => $formName]); |
155
|
|
|
|
156
|
|
|
return $validation; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return FormObject |
161
|
|
|
*/ |
162
|
|
|
public function getFormObject() |
163
|
|
|
{ |
164
|
|
|
return $this->formObject; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param FormObject $formObject |
169
|
|
|
*/ |
170
|
|
|
public function setFormObject(FormObject $formObject) |
171
|
|
|
{ |
172
|
|
|
$this->formObject = $formObject; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|