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\Definition; |
15
|
|
|
|
16
|
|
|
use Romm\ConfigurationObject\ConfigurationObjectInterface; |
17
|
|
|
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor; |
18
|
|
|
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface; |
19
|
|
|
use Romm\ConfigurationObject\Service\ServiceFactory; |
20
|
|
|
use Romm\ConfigurationObject\Traits\ConfigurationObject\ArrayConversionTrait; |
21
|
|
|
use Romm\ConfigurationObject\Traits\ConfigurationObject\DefaultConfigurationObjectTrait; |
22
|
|
|
use Romm\Formz\Condition\Items\ConditionItemInterface; |
23
|
|
|
use Romm\Formz\Configuration\Configuration; |
24
|
|
|
use Romm\Formz\Core\Core; |
25
|
|
|
use Romm\Formz\Exceptions\EntryNotFoundException; |
26
|
|
|
use Romm\Formz\Form\Definition\Field\Field; |
27
|
|
|
use Romm\Formz\Form\Definition\Settings\FormSettings; |
28
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
29
|
|
|
use TYPO3\CMS\Extbase\Validation\Error; |
30
|
|
|
|
31
|
|
|
class FormDefinition extends AbstractFormDefinition implements ConfigurationObjectInterface, DataPreProcessorInterface |
32
|
|
|
{ |
33
|
|
|
use DefaultConfigurationObjectTrait; |
34
|
|
|
use ArrayConversionTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Romm\Formz\Form\Definition\Field\Field[] |
38
|
|
|
* @validate NotEmpty |
39
|
|
|
*/ |
40
|
|
|
protected $fields = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \Romm\Formz\Condition\Items\ConditionItemInterface[] |
44
|
|
|
* @mixedTypesResolver \Romm\Formz\Form\Definition\Condition\ConditionItemResolver |
45
|
|
|
*/ |
46
|
|
|
protected $conditionList = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \Romm\Formz\Form\Definition\Settings\FormSettings |
50
|
|
|
*/ |
51
|
|
|
protected $settings; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var FormDefinitionState |
55
|
|
|
*/ |
56
|
|
|
private $state; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Constructor. |
60
|
|
|
*/ |
61
|
|
|
public function __construct() |
62
|
|
|
{ |
63
|
|
|
$this->settings = GeneralUtility::makeInstance(FormSettings::class); |
64
|
|
|
$this->state = GeneralUtility::makeInstance(FormDefinitionState::class); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Will initialize correctly the configuration object settings. |
69
|
|
|
* |
70
|
|
|
* @return ServiceFactory |
71
|
|
|
*/ |
72
|
|
|
public static function getConfigurationObjectServices() |
73
|
|
|
{ |
74
|
|
|
return Configuration::getConfigurationObjectServices(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns FormZ root configuration object. |
79
|
|
|
* |
80
|
|
|
* @return Configuration |
81
|
|
|
*/ |
82
|
|
|
public function getRootConfiguration() |
83
|
|
|
{ |
84
|
|
|
/** @var Configuration $configuration */ |
85
|
|
|
$configuration = $this->getFirstParent(Configuration::class); |
86
|
|
|
|
87
|
|
|
return $configuration; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return Field[] |
92
|
|
|
*/ |
93
|
|
|
public function getFields() |
94
|
|
|
{ |
95
|
|
|
return $this->fields; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $name |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function hasField($name) |
103
|
|
|
{ |
104
|
|
|
return true === isset($this->fields[$name]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $name |
109
|
|
|
* @return Field |
110
|
|
|
* @throws EntryNotFoundException |
111
|
|
|
*/ |
112
|
|
|
public function getField($name) |
113
|
|
|
{ |
114
|
|
|
if (false === $this->hasField($name)) { |
115
|
|
|
throw EntryNotFoundException::configurationFieldNotFound($name); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->fields[$name]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $name |
123
|
|
|
* @return Field |
124
|
|
|
*/ |
125
|
|
|
public function addField($name) |
126
|
|
|
{ |
127
|
|
|
$this->checkDefinitionFreezeState(); |
128
|
|
|
|
129
|
|
|
if ($this->hasField($name)) { |
130
|
|
|
throw new \Exception('todo'); // @todo |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** @var Field $field */ |
134
|
|
|
$field = GeneralUtility::makeInstance(Field::class, $name); |
135
|
|
|
$field->setParents([$this]); |
136
|
|
|
|
137
|
|
|
$this->fields[$name] = $field; |
138
|
|
|
|
139
|
|
|
return $field; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return ConditionItemInterface[] |
144
|
|
|
*/ |
145
|
|
|
public function getConditionList() |
146
|
|
|
{ |
147
|
|
|
return $this->conditionList; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $name |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
public function hasCondition($name) |
155
|
|
|
{ |
156
|
|
|
return true === isset($this->conditionList[$name]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $name |
161
|
|
|
* @return ConditionItemInterface |
162
|
|
|
*/ |
163
|
|
|
public function getCondition($name) |
164
|
|
|
{ |
165
|
|
|
if (false === $this->hasCondition($name)) { |
166
|
|
|
throw new \Exception('todo'); // @todo |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this->conditionList[$name]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $name |
174
|
|
|
* @param string $className |
175
|
|
|
* @param array $arguments |
176
|
|
|
*/ |
177
|
|
|
public function addCondition($name, $className, $arguments = []) |
178
|
|
|
{ |
179
|
|
|
$this->checkDefinitionFreezeState(); |
180
|
|
|
|
181
|
|
|
if ($this->hasCondition($name)) { |
182
|
|
|
throw new \Exception('todo'); // @todo |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if (false === class_exists($className)) { |
186
|
|
|
throw new \Exception('todo'); // @todo |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if (false === in_array(ConditionItemInterface::class, class_implements($className))) { |
190
|
|
|
throw new \Exception('todo'); // @todo |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// @todo handle \InvalidArgumentException |
194
|
|
|
/** @var ConditionItemInterface $condition */ |
195
|
|
|
$condition = call_user_func_array( |
196
|
|
|
[Core::class, 'instantiate'], |
197
|
|
|
array_merge([$className], $arguments) |
198
|
|
|
); |
199
|
|
|
|
200
|
|
|
$this->conditionList[$name] = $condition; |
201
|
|
|
$condition->setParent($this); |
202
|
|
|
|
203
|
|
|
return $condition; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return FormSettings |
208
|
|
|
*/ |
209
|
|
|
public function getSettings() |
210
|
|
|
{ |
211
|
|
|
return $this->settings; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return FormDefinitionState |
216
|
|
|
*/ |
217
|
|
|
public function getState() |
218
|
|
|
{ |
219
|
|
|
return $this->state; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param DataPreProcessor $processor |
224
|
|
|
*/ |
225
|
|
|
public static function dataPreProcessor(DataPreProcessor $processor) |
226
|
|
|
{ |
227
|
|
|
$data = $processor->getData(); |
228
|
|
|
|
229
|
|
|
/* |
230
|
|
|
* Forcing the names of the fields: they are the keys of the array |
231
|
|
|
* entries. |
232
|
|
|
*/ |
233
|
|
|
foreach ($data['fields'] as $key => $field) { |
234
|
|
|
$data['fields'][$key]['name'] = $key; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$processor->setData($data); |
238
|
|
|
|
239
|
|
|
if (isset($data['activationCondition'])) { |
240
|
|
|
$error = new Error( |
241
|
|
|
'The property "activationCondition" has been deprecated and renamed to "conditionList", please change your TypoScript configuration.', |
242
|
|
|
1489763042 |
243
|
|
|
); |
244
|
|
|
$processor->addError($error); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|