/
PageProvider.php
415 lines (385 loc) · 13.8 KB
/
PageProvider.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
413
414
415
<?php
namespace FluidTYPO3\Flux\Provider;
/*
* 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\Form;
use FluidTYPO3\Flux\Integration\PreviewView;
use FluidTYPO3\Flux\Service\FluxService;
use FluidTYPO3\Flux\Service\PageService;
use FluidTYPO3\Flux\Utility\ExtensionNamingUtility;
use FluidTYPO3\Flux\Utility\RecursiveArrayUtility;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\RootlineUtility;
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
use TYPO3\CMS\Fluid\View\TemplatePaths;
/**
* Page Configuration Provider
*
* Main Provider - triggers only on
* records which have a selected action.
* All other page records will be associated
* with the SubPageProvider instead.
*/
class PageProvider extends AbstractProvider implements ProviderInterface
{
const FIELD_NAME_MAIN = 'tx_fed_page_flexform';
const FIELD_NAME_SUB = 'tx_fed_page_flexform_sub';
const FIELD_ACTION_MAIN = 'tx_fed_page_controller_action';
const FIELD_ACTION_SUB = 'tx_fed_page_controller_action_sub';
/**
* @var string
*/
protected $tableName = 'pages';
/**
* @var string
*/
protected $parentFieldName = 'pid';
/**
* @var string
*/
protected $fieldName = self::FIELD_NAME_MAIN;
/**
* @var string
*/
protected $extensionKey = 'flux';
/**
* @var string
*/
protected $controllerName = 'Page';
/**
* @var string
*/
protected $configurationSectionName = 'Configuration';
/**
* @var PageService
*/
protected $pageService;
/**
* @var array
*/
private static $cache = [];
/**
* Returns TRUE that this Provider should trigger if:
*
* - table matches 'pages'
* - field is NULL or matches self::FIELD_NAME
* - a selection was made in the "template for this page" field
*
* @param array $row
* @param string $table
* @param string $field
* @param string|NULL $extensionKey
* @return boolean
*/
public function trigger(array $row, $table, $field, $extensionKey = null)
{
$isRightTable = ($table === $this->tableName);
$isRightField = (null === $field || $field === $this->fieldName);
return (true === $isRightTable && true === $isRightField);
}
/**
* @param PageService $pageService
* @return void
*/
public function injectPageService(PageService $pageService)
{
$this->pageService = $pageService;
}
/**
* @param FluxService $pageConfigurationService
* @return void
*/
public function injectPageConfigurationService(FluxService $pageConfigurationService)
{
$this->configurationService = $pageConfigurationService;
}
/**
* @param array $row
* @return Form|null
*/
public function getForm(array $row)
{
$form = parent::getForm($row);
if ($form) {
$form->setOption(PreviewView::OPTION_PREVIEW, [PreviewView::OPTION_MODE => 'none']);
$form = $this->setDefaultValuesInFieldsWithInheritedValues($form, $row);
}
return $form;
}
/**
* @param array $row
* @return string
*/
public function getExtensionKey(array $row)
{
$controllerExtensionKey = $this->getControllerExtensionKeyFromRecord($row);
if (false === empty($controllerExtensionKey)) {
return ExtensionNamingUtility::getExtensionKey($controllerExtensionKey);
}
return $this->extensionKey;
}
/**
* @param array $row
* @return string
*/
public function getTemplatePathAndFilename(array $row)
{
$templatePathAndFilename = $this->templatePathAndFilename;
$action = $this->getControllerActionReferenceFromRecord($row);
if (false === empty($action)) {
$pathsOrExtensionKey = $this->templatePaths ?? ExtensionNamingUtility::getExtensionKey($this->getControllerExtensionKeyFromRecord($row));
$templatePaths = GeneralUtility::makeInstance(TemplatePaths::class, $pathsOrExtensionKey);
$action = $this->getControllerActionFromRecord($row);
$action = ucfirst($action);
$templatePathAndFilename = $templatePaths->resolveTemplateFileForControllerAndActionAndFormat(
$this->getControllerNameFromRecord($row),
$action
);
}
return $templatePathAndFilename;
}
/**
* @param array $row
* @return string
*/
public function getControllerExtensionKeyFromRecord(array $row)
{
$action = $this->getControllerActionReferenceFromRecord($row);
$offset = strpos($action, '->');
if (false !== $offset) {
return substr($action, 0, $offset);
}
return $this->extensionKey;
}
/**
* @param array $row
* @throws \RuntimeException
* @return string
*/
public function getControllerActionFromRecord(array $row)
{
$action = $this->getControllerActionReferenceFromRecord($row);
$parts = explode('->', $action);
$controllerActionName = end($parts);
if (empty($controllerActionName)) {
return 'default';
}
$controllerActionName[0] = strtolower($controllerActionName[0]);
return $controllerActionName;
}
/**
* @param array $row
* @return string
*/
public function getControllerActionReferenceFromRecord(array $row)
{
if (!empty($row[self::FIELD_ACTION_MAIN])) {
return is_array($row[self::FIELD_ACTION_MAIN]) ? $row[self::FIELD_ACTION_MAIN][0] : $row[self::FIELD_ACTION_MAIN];
}
return ($this->pageService->getPageTemplateConfiguration($row['uid'])[self::FIELD_ACTION_SUB] ?? 'flux->default') ?: 'flux->default';
}
/**
* @param array $row
* @return array
*/
public function getFlexFormValues(array $row)
{
$immediateConfiguration = $this->getFlexFormValuesSingle($row);
$inheritedConfiguration = $this->getInheritedConfiguration($row);
return RecursiveArrayUtility::merge($inheritedConfiguration, $immediateConfiguration);
}
/**
* @param array $row
* @return array
*/
public function getFlexFormValuesSingle(array $row)
{
$fieldName = $this->getFieldName($row);
$form = $this->getForm($row);
$immediateConfiguration = $this->configurationService->convertFlexFormContentToArray(
$row[$fieldName],
$form,
null,
null
);
return $immediateConfiguration;
}
/**
* @param string $operation
* @param integer $id
* @param array $row
* @param DataHandler $reference
* @param array $removals Additional array of field names to remove from the stored Flux value
*/
public function postProcessRecord($operation, $id, array &$row, DataHandler $reference, array $removals = [])
{
if ('update' === $operation) {
$record = $this->recordService->getSingle($this->getTableName($row), '*', $id);
if (!is_array($record)) {
return;
}
if (isset($reference->datamap[$this->tableName][$id])) {
$record = RecursiveArrayUtility::mergeRecursiveOverrule(
$record,
$reference->datamap[$this->tableName][$id]
);
}
$form = $this->getForm($record);
if ($form) {
$tableFieldName = $this->getFieldName($record);
foreach ($form->getFields() as $field) {
$fieldName = $field->getName();
$sheetName = $field->getParent()->getName();
$inherit = (boolean) $field->getInherit();
$inheritEmpty = (boolean) $field->getInheritEmpty();
if (isset($record[$tableFieldName]['data']) && is_array($record[$tableFieldName]['data'])) {
$value = $record[$tableFieldName]['data'][$sheetName]['lDEF'][$fieldName]['vDEF'];
$inheritedConfiguration = $this->getInheritedConfiguration($record);
$inheritedValue = $this->getInheritedPropertyValueByDottedPath($inheritedConfiguration, $fieldName);
$empty = (true === empty($value) && $value !== '0' && $value !== 0);
$same = ($inheritedValue == $value);
if (true === $same && true === $inherit || (true === $inheritEmpty && true === $empty)) {
$removals[] = $fieldName;
}
}
}
}
}
parent::postProcessRecord($operation, $id, $row, $reference, $removals);
}
/**
* Gets an inheritance tree (ordered parent -> ... -> this record)
* of record arrays containing raw values.
*
* @param array $row
* @return array
*/
protected function getInheritanceTree(array $row)
{
$records = $this->loadRecordTreeFromDatabase($row);
if (0 === count($records)) {
return $records;
}
$template = $records[0][self::FIELD_ACTION_SUB];
foreach ($records as $index => $record) {
$hasMainAction = false === empty($record[self::FIELD_ACTION_MAIN]);
$hasSubAction = false === empty($record[self::FIELD_ACTION_SUB]);
$shouldUseMainTemplate = $template !== $record[self::FIELD_ACTION_SUB];
$shouldUseSubTemplate = $template !== $record[self::FIELD_ACTION_MAIN];
if (($hasMainAction && $shouldUseSubTemplate) || ($hasSubAction && $shouldUseMainTemplate)) {
return array_slice($records, $index);
}
}
return $records;
}
/**
* @param Form $form
* @param array $row
* @return Form
*/
protected function setDefaultValuesInFieldsWithInheritedValues(Form $form, array $row)
{
$inheritedConfiguration = $this->getInheritedConfiguration($row);
foreach ($form->getFields() as $field) {
$name = $field->getName();
$inheritedValue = $this->getInheritedPropertyValueByDottedPath($inheritedConfiguration, $name);
if (null !== $inheritedValue && true === $field instanceof Form\FieldInterface) {
$field->setDefault($inheritedValue);
}
}
return $form;
}
/**
* @param array $row
* @return array
*/
protected function getInheritedConfiguration(array $row)
{
$tableName = $this->getTableName($row);
$tableFieldName = $this->getFieldName($row);
$cacheKey = $tableName . $tableFieldName . $row['uid'];
if (false === isset(self::$cache[$cacheKey])) {
$tree = $this->getInheritanceTree($row);
$data = [];
foreach ($tree as $branch) {
/** @var SubPageProvider $provider */
$provider = $this->configurationService->resolvePrimaryConfigurationProvider(
$this->tableName,
self::FIELD_NAME_SUB,
$branch
);
$form = $provider->getForm($branch);
if (null === $form) {
continue;
}
$fields = $form->getFields();
$values = $provider->getFlexFormValuesSingle($branch);
foreach ($fields as $field) {
$values = $this->unsetInheritedValues($field, $values);
}
$data = RecursiveArrayUtility::merge($data, $values);
}
self::$cache[$cacheKey] = $data;
}
return self::$cache[$cacheKey];
}
/**
* @param array $inheritedConfiguration
* @param string $propertyPath
* @return mixed
*/
protected function getInheritedPropertyValueByDottedPath($inheritedConfiguration, $propertyPath)
{
if (true === empty($propertyPath)) {
return null;
} elseif (false === strpos($propertyPath, '.')) {
if (isset($inheritedConfiguration[$propertyPath])) {
return ObjectAccess::getProperty($inheritedConfiguration, $propertyPath);
}
return null;
}
return ObjectAccess::getPropertyPath($inheritedConfiguration, $propertyPath);
}
/**
* @param Form\FormInterface $field
* @param array $values
* @return array
*/
protected function unsetInheritedValues(Form\FormInterface $field, $values)
{
$name = $field->getName();
$inherit = (boolean) $field->getInherit();
$inheritEmpty = (boolean) $field->getInheritEmpty();
$empty = (true === empty($values[$name]) && $values[$name] !== '0' && $values[$name] !== 0);
if (false === $inherit || (true === $inheritEmpty && true === $empty)) {
unset($values[$name]);
}
return $values;
}
/**
* @param array $row
* @return mixed
*/
protected function getParentFieldValue(array $row)
{
$parentFieldName = $this->getParentFieldName($row);
if (null !== $parentFieldName && false === isset($row[$parentFieldName])) {
$row = $this->recordService->getSingle($this->getTableName($row), '*', $row[$parentFieldName]);
}
return $row[$parentFieldName];
}
/**
* @param array $record
* @return array
*/
protected function loadRecordTreeFromDatabase($record)
{
$rootLineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $record['uid']);
return array_slice($rootLineUtility->get(), 1);
}
}