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\ViewHelpers\Service; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Configuration\Form\Field\Field; |
17
|
|
|
use Romm\Formz\Error\FormResult; |
18
|
|
|
use Romm\Formz\Form\FormInterface; |
19
|
|
|
use Romm\Formz\Form\FormObject; |
20
|
|
|
use Romm\Formz\ViewHelpers\FieldViewHelper; |
21
|
|
|
use Romm\Formz\ViewHelpers\FormViewHelper; |
22
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* This class contains methods that help view helpers to manipulate data and |
26
|
|
|
* know more things concerning the current form state. |
27
|
|
|
* |
28
|
|
|
* It is mainly configured inside the `FormViewHelper`, and used in other |
29
|
|
|
* view helpers. |
30
|
|
|
*/ |
31
|
|
|
class FormzViewHelperService implements SingletonInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $formContext; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array|FormInterface |
40
|
|
|
*/ |
41
|
|
|
protected $formInstance; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Field |
45
|
|
|
*/ |
46
|
|
|
protected $currentField; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $formWasSubmitted; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var FormResult |
55
|
|
|
*/ |
56
|
|
|
protected $formResult; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var FormObject |
60
|
|
|
*/ |
61
|
|
|
protected $formObject; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
protected $fieldOptions = []; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Reset every state that can be used by this service. |
70
|
|
|
*/ |
71
|
|
|
public function resetState() |
72
|
|
|
{ |
73
|
|
|
$this->formContext = false; |
74
|
|
|
$this->formInstance = null; |
75
|
|
|
$this->formResult = null; |
76
|
|
|
$this->formWasSubmitted = false; |
77
|
|
|
$this->currentField = null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Will activate the form context, changing the result returned by the |
82
|
|
|
* function `formContextExists`. |
83
|
|
|
* |
84
|
|
|
* @throws \Exception |
85
|
|
|
*/ |
86
|
|
|
public function activateFormContext() |
87
|
|
|
{ |
88
|
|
|
if (true === $this->formContext) { |
89
|
|
|
throw new \Exception( |
90
|
|
|
'You can not use a form view helper inside another one.', |
91
|
|
|
1465242575 |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->formContext = true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns `true` if the `FormViewHelper` context exists. |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function formContextExists() |
104
|
|
|
{ |
105
|
|
|
return $this->formContext; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Will mark the form as submitted (change the result returned by the |
110
|
|
|
* function `formWasSubmitted()`). |
111
|
|
|
*/ |
112
|
|
|
public function markFormAsSubmitted() |
113
|
|
|
{ |
114
|
|
|
$this->formWasSubmitted = true; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns `true` if the form was submitted by the user. |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function formWasSubmitted() |
123
|
|
|
{ |
124
|
|
|
return $this->formWasSubmitted; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Checks that the `FieldViewHelper` has been called. If not, an exception |
129
|
|
|
* is thrown. |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function fieldContextExists() |
134
|
|
|
{ |
135
|
|
|
return $this->currentField instanceof Field; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the current field which was defined by the `FieldViewHelper`. |
140
|
|
|
* |
141
|
|
|
* Returns null if no current field was found. |
142
|
|
|
* |
143
|
|
|
* @return Field|null |
144
|
|
|
*/ |
145
|
|
|
public function getCurrentField() |
146
|
|
|
{ |
147
|
|
|
return $this->currentField; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param Field $field |
152
|
|
|
*/ |
153
|
|
|
public function setCurrentField(Field $field) |
154
|
|
|
{ |
155
|
|
|
$this->currentField = $field; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $name |
160
|
|
|
* @param mixed $value |
161
|
|
|
*/ |
162
|
|
|
public function setFieldOption($name, $value) |
163
|
|
|
{ |
164
|
|
|
$this->fieldOptions[$name] = $value; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
|
|
public function getFieldOptions() |
171
|
|
|
{ |
172
|
|
|
return $this->fieldOptions; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
|
|
public function resetFieldOptions() |
179
|
|
|
{ |
180
|
|
|
$this->fieldOptions = []; |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Unset the current field. |
187
|
|
|
* |
188
|
|
|
* @return $this |
189
|
|
|
*/ |
190
|
|
|
public function removeCurrentField() |
191
|
|
|
{ |
192
|
|
|
$this->currentField = null; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Checks that the current `FormViewHelper` exists. If not, an exception is |
199
|
|
|
* thrown. |
200
|
|
|
* |
201
|
|
|
* @throws \Exception |
202
|
|
|
*/ |
203
|
|
|
public function checkIsInsideFormViewHelper() |
204
|
|
|
{ |
205
|
|
|
if (false === $this->formContextExists()) { |
206
|
|
|
throw new \Exception( |
207
|
|
|
'The view helper "' . get_called_class() . '" must be used inside the view helper "' . FormViewHelper::class . '".', |
208
|
|
|
1465243085 |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Checks that the `FieldViewHelper` has been called. If not, an exception |
215
|
|
|
* is thrown. |
216
|
|
|
* |
217
|
|
|
* @throws \Exception |
218
|
|
|
*/ |
219
|
|
|
public function checkIsInsideFieldViewHelper() |
220
|
|
|
{ |
221
|
|
|
if (false === $this->fieldContextExists()) { |
222
|
|
|
throw new \Exception( |
223
|
|
|
'The view helper "' . get_called_class() . '" must be used inside the view helper "' . FieldViewHelper::class . '".', |
224
|
|
|
1465243085 |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* If the form was submitted by the user, contains the array containing the |
231
|
|
|
* submitted values. |
232
|
|
|
* |
233
|
|
|
* @param array|FormInterface $formInstance |
234
|
|
|
*/ |
235
|
|
|
public function setFormInstance($formInstance) |
236
|
|
|
{ |
237
|
|
|
$this->formInstance = $formInstance; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return array|FormInterface |
242
|
|
|
*/ |
243
|
|
|
public function getFormInstance() |
244
|
|
|
{ |
245
|
|
|
return $this->formInstance; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return FormResult |
250
|
|
|
*/ |
251
|
|
|
public function getFormResult() |
252
|
|
|
{ |
253
|
|
|
return $this->formResult; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @param FormResult $formResult |
258
|
|
|
*/ |
259
|
|
|
public function setFormResult(FormResult $formResult) |
260
|
|
|
{ |
261
|
|
|
$this->formResult = $formResult; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @return FormObject |
266
|
|
|
*/ |
267
|
|
|
public function getFormObject() |
268
|
|
|
{ |
269
|
|
|
return $this->formObject; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param FormObject $formObject |
274
|
|
|
*/ |
275
|
|
|
public function setFormObject(FormObject $formObject) |
276
|
|
|
{ |
277
|
|
|
$this->formObject = $formObject; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|