1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2016 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\Core\Core; |
17
|
|
|
use Romm\Formz\Error\FormResult; |
18
|
|
|
use Romm\Formz\Exceptions\DuplicateEntryException; |
19
|
|
|
use Romm\Formz\Form\FormInterface; |
20
|
|
|
use Romm\Formz\Form\FormObject; |
21
|
|
|
use Romm\Formz\Validation\Validator\Form\DefaultFormValidator; |
22
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
23
|
|
|
use TYPO3\CMS\Extbase\Mvc\Request; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This class contains methods that help view helpers to manipulate data and |
27
|
|
|
* know more things concerning the current form state. |
28
|
|
|
* |
29
|
|
|
* It is mainly configured inside the `FormViewHelper`, and used in other |
30
|
|
|
* view helpers. |
31
|
|
|
*/ |
32
|
|
|
class FormViewHelperService implements SingletonInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $formContext = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array|FormInterface |
41
|
|
|
*/ |
42
|
|
|
protected $formInstance; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
protected $formWasSubmitted = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var FormResult |
51
|
|
|
*/ |
52
|
|
|
protected $formResult; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var FormObject |
56
|
|
|
*/ |
57
|
|
|
protected $formObject; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Reset every state that can be used by this service. |
61
|
|
|
*/ |
62
|
|
|
public function resetState() |
63
|
|
|
{ |
64
|
|
|
$this->formContext = false; |
65
|
|
|
$this->formWasSubmitted = false; |
66
|
|
|
$this->formObject = null; |
67
|
|
|
$this->formInstance = null; |
68
|
|
|
$this->formResult = null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Will activate the form context, changing the result returned by the |
73
|
|
|
* function `formContextExists()`. |
74
|
|
|
* |
75
|
|
|
* @throws \Exception |
76
|
|
|
* @return FormViewHelperService |
77
|
|
|
*/ |
78
|
|
|
public function activateFormContext() |
79
|
|
|
{ |
80
|
|
|
if (true === $this->formContext) { |
81
|
|
|
throw new DuplicateEntryException( |
82
|
|
|
'You can not use a form view helper inside another one.', |
83
|
|
|
1465242575 |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->formContext = true; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns `true` if the `FormViewHelper` context exists. |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function formContextExists() |
98
|
|
|
{ |
99
|
|
|
return $this->formContext; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* This function will check and inject the form instance and its submission |
104
|
|
|
* result. |
105
|
|
|
* |
106
|
|
|
* @param string $formName |
107
|
|
|
* @param Request $originalRequest |
108
|
|
|
* @param $formInstance |
109
|
|
|
*/ |
110
|
|
|
public function setUpData($formName, $originalRequest, $formInstance) |
111
|
|
|
{ |
112
|
|
|
if (null !== $originalRequest |
113
|
|
|
&& $originalRequest->hasArgument($formName) |
114
|
|
|
) { |
115
|
|
|
/** @var array $formInstance */ |
116
|
|
|
$formInstance = $originalRequest->getArgument($formName); |
117
|
|
|
|
118
|
|
|
$this->setFormInstance($formInstance); |
119
|
|
|
$this->setFormResult($this->formObject->getLastValidationResult()); |
120
|
|
|
$this->markFormAsSubmitted(); |
121
|
|
|
} elseif (null !== $formInstance) { |
122
|
|
|
$formValidator = $this->getFormValidator($formName); |
123
|
|
|
$formRequestResult = $formValidator->validateWithoutSavingResults($formInstance); |
124
|
|
|
|
125
|
|
|
$this->setFormInstance($formInstance); |
126
|
|
|
$this->setFormResult($formRequestResult); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Will mark the form as submitted (change the result returned by the |
132
|
|
|
* function `formWasSubmitted()`). |
133
|
|
|
*/ |
134
|
|
|
public function markFormAsSubmitted() |
135
|
|
|
{ |
136
|
|
|
$this->formWasSubmitted = true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns `true` if the form was submitted by the user. |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function formWasSubmitted() |
145
|
|
|
{ |
146
|
|
|
return $this->formWasSubmitted; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return array|FormInterface |
151
|
|
|
*/ |
152
|
|
|
public function getFormInstance() |
153
|
|
|
{ |
154
|
|
|
return $this->formInstance; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* If the form was submitted by the user, contains the array containing the |
159
|
|
|
* submitted values. |
160
|
|
|
* |
161
|
|
|
* @param array|FormInterface $formInstance |
162
|
|
|
*/ |
163
|
|
|
public function setFormInstance($formInstance) |
164
|
|
|
{ |
165
|
|
|
$this->formInstance = $formInstance; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return FormResult |
170
|
|
|
*/ |
171
|
|
|
public function getFormResult() |
172
|
|
|
{ |
173
|
|
|
return $this->formResult; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param FormResult $formResult |
178
|
|
|
*/ |
179
|
|
|
public function setFormResult(FormResult $formResult) |
180
|
|
|
{ |
181
|
|
|
$this->formResult = $formResult; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return FormObject |
186
|
|
|
*/ |
187
|
|
|
public function getFormObject() |
188
|
|
|
{ |
189
|
|
|
return $this->formObject; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param FormObject $formObject |
194
|
|
|
*/ |
195
|
|
|
public function setFormObject(FormObject $formObject) |
196
|
|
|
{ |
197
|
|
|
$this->formObject = $formObject; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string $formName |
202
|
|
|
* @return DefaultFormValidator |
203
|
|
|
*/ |
204
|
|
|
protected function getFormValidator($formName) |
205
|
|
|
{ |
206
|
|
|
return Core::instantiate(DefaultFormValidator::class, ['name' => $formName]); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|