forked from FluidTYPO3/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm.php
412 lines (375 loc) · 10.1 KB
/
Form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
namespace FluidTYPO3\Flux;
/*
* This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Flux\Outlet\OutletInterface;
use FluidTYPO3\Flux\Utility\ExtensionNamingUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* @package Flux
*/
class Form extends Form\AbstractFormContainer implements Form\FieldContainerInterface {
const OPTION_TRANSLATION = 'translation';
const OPTION_GROUP = 'group';
const OPTION_ICON = 'icon';
const OPTION_TCA_LABELS = 'labels';
const OPTION_TCA_HIDE = 'hide';
const OPTION_TCA_START = 'start';
const OPTION_TCA_END = 'end';
const OPTION_TCA_DELETE = 'delete';
const OPTION_TCA_FEGROUP = 'frontendUserGroup';
const OPTION_TEMPLATEFILE = 'templateFile';
const OPTION_RECORD = 'record';
const OPTION_RECORD_FIELD = 'recordField';
const OPTION_RECORD_TABLE = 'recordTable';
const TRANSLATION_DISABLED = 'disabled';
const TRANSLATION_SEPARATE = 'separate';
const TRANSLATION_INHERIT = 'inherit';
const POSITION_TOP = 'top';
const POSITION_BOTTOM = 'bottom';
const POSITION_BOTH = 'both';
const POSITION_NONE = 'none';
const CONTROL_INFO = 'info';
const CONTROL_NEW = 'new';
const CONTROL_DRAGDROP = 'dragdrop';
const CONTROL_SORT = 'sort';
const CONTROL_HIDE = 'hide';
const CONTROL_DELETE = 'delete';
const CONTROL_LOCALISE = 'localize';
const DEFAULT_LANGUAGEFILE = '/Resources/Private/Language/locallang.xlf';
/**
* Machine-readable, lowerCamelCase ID of this form. DOM compatible.
*
* @var string
*/
protected $id;
/**
* Should be set to contain the extension name in UpperCamelCase of
* the extension implementing this form object.
*
* @var string
*/
protected $extensionName;
/**
* If TRUE, removes sheet wrappers if there is only a single sheet.
*
* @var boolean
*/
protected $compact = FALSE;
/**
* @var string
*/
protected $description;
/**
* @var array
*/
protected $options = array();
/**
* @var OutletInterface
*/
protected $outlet;
/**
* @return void
*/
public function initializeObject() {
/** @var Form\Container\Sheet $defaultSheet */
$defaultSheet = $this->getObjectManager()->get('FluidTYPO3\Flux\Form\Container\Sheet');
$defaultSheet->setName('options');
$defaultSheet->setLabel('LLL:EXT:flux/' . $this->localLanguageFileRelativePath . ':tt_content.tx_flux_options');
$this->add($defaultSheet);
$this->outlet = $this->getObjectManager()->get('FluidTYPO3\Flux\Outlet\StandardOutlet');
}
/**
* @param Form\FormInterface $child
* @return Form\FormInterface
*/
public function add(Form\FormInterface $child) {
if (FALSE === $child instanceof Form\Container\Sheet) {
/** @var Form\Container\Sheet $last */
$last = $this->last();
$last->add($child);
} else {
$children = $this->children;
foreach ($children as $existingChild) {
if ($child->getName() === $existingChild->getName()) {
return $this;
}
}
$this->children->attach($child);
$child->setParent($this);
}
return $this;
}
/**
* @return array
*/
public function build() {
$translateOption = $this->getOption(self::OPTION_TRANSLATION);
$translateOption = TRUE === empty($translateOption) ? self::TRANSLATION_DISABLED : $translateOption;
$disableLocalisation = self::TRANSLATION_DISABLED === $translateOption ? 1 : 0;
$inheritLocalisation = self::TRANSLATION_INHERIT === $translateOption ? 1 : 0;
$dataStructArray = array(
'meta' => array(
'langDisable' => $disableLocalisation,
'langChildren' => $inheritLocalisation
),
);
$copy = clone $this;
foreach ($this->getSheets(TRUE) as $sheet) {
if (FALSE === $sheet->hasChildren()) {
$copy->remove($sheet->getName());
}
}
$sheets = $copy->getSheets();
$compactExtensionToggleOn = 0 < $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['flux']['setup']['compact'];
$compactConfigurationToggleOn = 0 < $copy->getCompact();
if (($compactExtensionToggleOn || $compactConfigurationToggleOn) && 1 === count($sheets)) {
$dataStructArray = $copy->last()->build();
$dataStructArray['meta'] = array('langDisable' => $disableLocalisation);
unset($dataStructArray['ROOT']['TCEforms']);
} elseif (0 < count($sheets)) {
$dataStructArray['sheets'] = $copy->buildChildren($this->children);
} else {
$dataStructArray['ROOT'] = array(
'type' => 'array',
'el' => array()
);
}
return $dataStructArray;
}
/**
* @param boolean $includeEmpty
* @return Form\Container\Sheet[]
*/
public function getSheets($includeEmpty = FALSE) {
$sheets = array();
foreach ($this->children as $sheet) {
if (FALSE === $sheet->hasChildren() && FALSE === $includeEmpty) {
continue;
}
$name = $sheet->getName();
$sheets[$name] = $sheet;
}
return $sheets;
}
/**
* @return Form\FieldInterface[]
*/
public function getFields() {
$fields = array();
foreach ($this->getSheets() as $sheet) {
$fieldsInSheet = $sheet->getFields();
$fields = array_merge($fields, $fieldsInSheet);
}
return $fields;
}
/**
* @param boolean $compact
* @return Form\FormInterface
*/
public function setCompact($compact) {
$this->compact = $compact;
return $this;
}
/**
* @return boolean
*/
public function getCompact() {
return $this->compact;
}
/**
* @param string $extensionName
* @return Form\FormInterface
*/
public function setExtensionName($extensionName) {
$this->extensionName = $extensionName;
return $this;
}
/**
* @return string
*/
public function getExtensionName() {
return $this->extensionName;
}
/**
* @param string $group
* @return Form\FormInterface
*/
public function setGroup($group) {
GeneralUtility::logDeprecatedFunction();
$this->setOption(self::OPTION_GROUP, $group);
return $this;
}
/**
* @return string
*/
public function getGroup() {
GeneralUtility::logDeprecatedFunction();
return $this->getOption(self::OPTION_GROUP);
}
/**
* @param string $icon
* @return Form\FormInterface
* @deprecated
*/
public function setIcon($icon) {
GeneralUtility::logDeprecatedFunction();
$this->setOption(self::OPTION_ICON, $icon);
return $this;
}
/**
* @return string
* @deprecated
*/
public function getIcon() {
GeneralUtility::logDeprecatedFunction();
$icon = $this->getOption(self::OPTION_ICON);
if (0 === strpos($icon, 'EXT:')) {
$icon = GeneralUtility::getFileAbsFileName($icon);
}
return $icon;
}
/**
* @param string $id
* @return Form\FormInterface
*/
public function setId($id) {
$allowed = 'a-z0-9_';
$pattern = '/[^' . $allowed . ']+/i';
if (preg_match($pattern, $id)) {
$this->getConfigurationService()->message('Flux FlexForm with id "' . $id . '" uses invalid characters in the ID; valid characters
are: "' . $allowed . '" and the pattern used for matching is "' . $pattern . '". This bad ID name will prevent
you from utilising some features, fx automatic LLL reference building, but is not fatal', GeneralUtility::SYSLOG_SEVERITY_NOTICE);
}
$this->id = $id;
if (TRUE === empty($this->name)) {
$this->name = $id;
}
return $this;
}
/**
* @return string
*/
public function getId() {
return $this->id;
}
/**
* @param string $description
* @return Form\FormInterface
*/
public function setDescription($description) {
$this->description = $description;
return $this;
}
/**
* @return string
*/
public function getDescription() {
$description = $this->description;
$translated = NULL;
$extensionKey = ExtensionNamingUtility::getExtensionKey($this->extensionName);
if (TRUE === empty($description)) {
$relativeFilePath = $this->getLocalLanguageFileRelativePath();
$relativeFilePath = ltrim($relativeFilePath, '/');
$filePrefix = 'LLL:EXT:' . $extensionKey . '/' . $relativeFilePath;
$description = $filePrefix . ':' . trim('flux.' . $this->id . '.description');
}
if (0 === strpos($description, 'LLL:EXT:')) {
$translated = LocalizationUtility::translate($description, $extensionKey);
}
return $translated !== NULL ? $translated : $description;
}
/**
* @param array $options
* @return Form\FormInterface
*/
public function setOptions(array $options) {
$this->options = $options;
return $this;
}
/**
* @return array
*/
public function getOptions() {
return $this->options;
}
/**
* @param string $name
* @param mixed $value
* @return Form\FormInterface
*/
public function setOption($name, $value) {
$this->options[$name] = $value;
return $this;
}
/**
* @param string $name
* @return mixed
*/
public function getOption($name) {
return ObjectAccess::getPropertyPath($this->options, $name);
}
/**
* @param string $name
* @return boolean
*/
public function hasOption($name) {
return TRUE === isset($this->options[$name]);
}
/**
* @return boolean
*/
public function hasChildren() {
foreach ($this->children as $child) {
if (TRUE === $child->hasChildren()) {
return TRUE;
}
}
return FALSE;
}
/**
* @param OutletInterface $outlet
* @return Form\FormInterface
*/
public function setOutlet(OutletInterface $outlet) {
$this->outlet = $outlet;
return $this;
}
/**
* @return OutletInterface
*/
public function getOutlet() {
return $this->outlet;
}
/**
* @param array $structure
* @return ContainerInterface
*/
public function modify(array $structure) {
if (TRUE === isset($structure['options']) && TRUE === is_array($structure['options'])) {
foreach ($structure['options'] as $name => $value) {
$this->setOption($name, $value);
}
unset($structure['options']);
}
if (TRUE === isset($structure['sheets'])) {
foreach ((array) $structure['sheets'] as $index => $sheetData) {
$sheetName = TRUE === isset($sheetData['name']) ? $sheetData['name'] : $index;
// check if field already exists - if it does, modify it. If it does not, create it.
if (TRUE === $this->has($sheetName)) {
$sheet = $this->get($sheetName);
} else {
$sheet = $this->createContainer('Sheet', $sheetName);
}
$sheet->modify($sheetData);
}
}
return parent::modify($structure);
}
}