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\Form\FormObject; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Configuration\Form\Form; |
17
|
|
|
use Romm\Formz\Configuration\Form\Step\Step\StepItem; |
18
|
|
|
use Romm\Formz\Core\Core; |
19
|
|
|
use Romm\Formz\Domain\Model\FormMetadata; |
20
|
|
|
use Romm\Formz\Error\FormResult; |
21
|
|
|
use Romm\Formz\Exceptions\DuplicateEntryException; |
22
|
|
|
use Romm\Formz\Exceptions\PropertyNotAccessibleException; |
23
|
|
|
use Romm\Formz\Form\FormInterface; |
24
|
|
|
use Romm\Formz\Form\FormObject\Service\FormObjectRequestData; |
25
|
|
|
use Romm\Formz\Persistence\PersistenceManager; |
26
|
|
|
use TYPO3\CMS\Extbase\Error\Result; |
27
|
|
|
use TYPO3\CMS\Extbase\Mvc\Web\Request; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* This is the object representation of a form. In here we can manage which |
31
|
|
|
* properties the form does have, its configuration, and more. |
32
|
|
|
*/ |
33
|
|
|
class FormObject |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $name; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var FormObjectStatic |
42
|
|
|
*/ |
43
|
|
|
protected $static; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var FormObjectProxy |
47
|
|
|
*/ |
48
|
|
|
protected $proxy; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var FormObjectFactory |
52
|
|
|
*/ |
53
|
|
|
protected $formObjectFactory; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var PersistenceManager |
57
|
|
|
*/ |
58
|
|
|
protected $persistenceManager; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* You should never create a new instance of this class directly, use the |
62
|
|
|
* `FormObjectFactory->getInstanceFromClassName()` function instead. |
63
|
|
|
* |
64
|
|
|
* @param string $name |
65
|
|
|
* @param FormObjectStatic $static |
66
|
|
|
*/ |
67
|
|
|
public function __construct($name, FormObjectStatic $static) |
68
|
|
|
{ |
69
|
|
|
$this->name = $name; |
70
|
|
|
$this->static = $static; |
71
|
|
|
|
72
|
|
|
$this->persistenceManager = Core::instantiate(PersistenceManager::class, $this); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getName() |
79
|
|
|
{ |
80
|
|
|
return $this->name; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getClassName() |
87
|
|
|
{ |
88
|
|
|
return $this->static->getClassName(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $name |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function hasProperty($name) |
96
|
|
|
{ |
97
|
|
|
return $this->static->hasProperty($name); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return Form |
102
|
|
|
*/ |
103
|
|
|
public function getConfiguration() |
104
|
|
|
{ |
105
|
|
|
return $this->static->getConfiguration(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return Result |
110
|
|
|
*/ |
111
|
|
|
public function getConfigurationValidationResult() |
112
|
|
|
{ |
113
|
|
|
return $this->static->getConfigurationValidationResult(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return FormInterface |
118
|
|
|
*/ |
119
|
|
|
public function getForm() |
120
|
|
|
{ |
121
|
|
|
return $this->getProxy()->getForm(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public function hasForm() |
128
|
|
|
{ |
129
|
|
|
return $this->proxy !== null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param FormInterface $form |
134
|
|
|
* @throws DuplicateEntryException |
135
|
|
|
*/ |
136
|
|
|
public function setForm(FormInterface $form) |
137
|
|
|
{ |
138
|
|
|
if ($this->proxy) { |
139
|
|
|
throw DuplicateEntryException::formInstanceAlreadyAdded($this); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->proxy = $this->createProxy($form); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns `true` if the form was submitted by the user. |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function formWasSubmitted() |
151
|
|
|
{ |
152
|
|
|
return $this->hasForm() && $this->getProxy()->formWasSubmitted(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return FormResult |
157
|
|
|
*/ |
158
|
|
|
public function getFormResult() |
159
|
|
|
{ |
160
|
|
|
return $this->getProxy()->getFormResult(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
|
|
public function hasFormResult() |
167
|
|
|
{ |
168
|
|
|
return $this->getProxy()->hasFormResult(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return FormObjectRequestData |
173
|
|
|
*/ |
174
|
|
|
public function getRequestData() |
175
|
|
|
{ |
176
|
|
|
return $this->getProxy()->getRequestData(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return FormMetadata |
181
|
|
|
*/ |
182
|
|
|
public function getFormMetadata() |
183
|
|
|
{ |
184
|
|
|
return $this->getProxy()->getFormMetadata(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public function getFormHash() |
191
|
|
|
{ |
192
|
|
|
return $this->getProxy()->getFormHash(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public function getObjectHash() |
199
|
|
|
{ |
200
|
|
|
return $this->static->getObjectHash(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return PersistenceManager |
205
|
|
|
*/ |
206
|
|
|
public function getPersistenceManager() |
207
|
|
|
{ |
208
|
|
|
return $this->persistenceManager; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* This function will search among the registered steps to find the one that |
213
|
|
|
* has the same controller parameters. |
214
|
|
|
* |
215
|
|
|
* It is also possible not to find any step, in this case `null` is |
216
|
|
|
* returned. |
217
|
|
|
* |
218
|
|
|
* @param Request $request |
219
|
|
|
* @return StepItem|null |
220
|
|
|
*/ |
221
|
|
|
public function getCurrentStep(Request $request) |
222
|
|
|
{ |
223
|
|
|
$configuration = $this->getConfiguration(); |
224
|
|
|
|
225
|
|
|
if (false === $configuration->hasStep()) { |
226
|
|
|
return null; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$currentStep = null; |
230
|
|
|
$steps = $configuration->getStep()->getSteps(); |
231
|
|
|
|
232
|
|
|
foreach ($steps as $step) { |
233
|
|
|
$data = [ |
234
|
|
|
$step->getPageUid() => Core::get()->getPageController()->id, |
235
|
|
|
$step->getExtension() => $request->getControllerExtensionName(), |
236
|
|
|
$step->getController() => $request->getControllerName(), |
237
|
|
|
$step->getAction() => $request->getControllerActionName(), |
238
|
|
|
]; |
239
|
|
|
|
240
|
|
|
foreach ($data as $stepData => $requestData) { |
241
|
|
|
if (false === empty($stepData) |
242
|
|
|
&& $stepData !== $requestData |
243
|
|
|
) { |
244
|
|
|
continue 2; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
if (null !== $currentStep) { |
249
|
|
|
throw new \Exception('todo'); // @todo |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$currentStep = $step; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $currentStep; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @return FormObjectProxy |
260
|
|
|
* @throws PropertyNotAccessibleException |
261
|
|
|
*/ |
262
|
|
|
protected function getProxy() |
263
|
|
|
{ |
264
|
|
|
if (null === $this->proxy) { |
265
|
|
|
throw PropertyNotAccessibleException::formInstanceNotSet(); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return $this->proxy; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Wrapper for unit tests. |
273
|
|
|
* |
274
|
|
|
* @param FormInterface $form |
275
|
|
|
* @return FormObjectProxy |
276
|
|
|
*/ |
277
|
|
|
protected function createProxy(FormInterface $form) |
278
|
|
|
{ |
279
|
|
|
return $this->formObjectFactory->getProxy($this, $form); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param FormObjectFactory $formObjectFactory |
284
|
|
|
*/ |
285
|
|
|
public function injectFormObjectFactory(FormObjectFactory $formObjectFactory) |
286
|
|
|
{ |
287
|
|
|
$this->formObjectFactory = $formObjectFactory; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|