-
Notifications
You must be signed in to change notification settings - Fork 639
/
BackendLayoutView.php
346 lines (317 loc) · 13.1 KB
/
BackendLayoutView.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
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Backend\View;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\View\BackendLayout\BackendLayout;
use TYPO3\CMS\Backend\View\BackendLayout\DataProviderCollection;
use TYPO3\CMS\Backend\View\BackendLayout\DataProviderContext;
use TYPO3\CMS\Backend\View\BackendLayout\DefaultDataProvider;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\TypoScript\TypoScriptStringFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Backend layout for CMS
* @internal This class is a TYPO3 Backend implementation and is not considered part of the Public TYPO3 API.
*/
class BackendLayoutView implements SingletonInterface
{
protected array $selectedCombinedIdentifier = [];
protected array $selectedBackendLayout = [];
/**
* Create this object and initialize data providers.
*/
public function __construct(
private readonly DataProviderCollection $dataProviderCollection,
private readonly TypoScriptStringFactory $typoScriptStringFactory,
) {
$this->dataProviderCollection->add('default', DefaultDataProvider::class);
foreach ((array)($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['BackendLayoutDataProvider'] ?? []) as $identifier => $className) {
$this->dataProviderCollection->add($identifier, $className);
}
}
/**
* Gets backend layout items to be shown in the forms engine.
* This method is called as "itemsProcFunc" with the accordant context
* for pages.backend_layout and pages.backend_layout_next_level.
* Also used in the info module, since we need those items with
* the appropriate labels and backend layout identifiers there, too.
*
* @todo This method should return the items array instead of
* using the whole parameters array as reference. This
* has to be adjusted, as soon as the itemsProcFunc
* functionality is changed in this regard.
*/
public function addBackendLayoutItems(array &$parameters)
{
$pageId = $this->determinePageId($parameters['table'], $parameters['row']) ?: 0;
$pageTsConfig = BackendUtility::getPagesTSconfig($pageId);
$identifiersToBeExcluded = $this->getIdentifiersToBeExcluded($pageTsConfig);
$dataProviderContext = GeneralUtility::makeInstance(DataProviderContext::class);
$dataProviderContext
->setPageId($pageId)
->setData($parameters['row'])
->setTableName($parameters['table'])
->setFieldName($parameters['field'])
->setPageTsConfig($pageTsConfig);
$backendLayoutCollections = $this->dataProviderCollection->getBackendLayoutCollections($dataProviderContext);
foreach ($backendLayoutCollections as $backendLayoutCollection) {
$combinedIdentifierPrefix = '';
if ($backendLayoutCollection->getIdentifier() !== 'default') {
$combinedIdentifierPrefix = $backendLayoutCollection->getIdentifier() . '__';
}
foreach ($backendLayoutCollection->getAll() as $backendLayout) {
$combinedIdentifier = $combinedIdentifierPrefix . $backendLayout->getIdentifier();
if (in_array($combinedIdentifier, $identifiersToBeExcluded, true)) {
continue;
}
$parameters['items'][] = [
'label' => $backendLayout->getTitle(),
'value' => $combinedIdentifier,
'icon' => $backendLayout->getIconPath(),
];
}
}
}
/**
* Determines the page id for a given record of a database table.
*
* @return int|false Returns page id or false on error
*/
protected function determinePageId(string $tableName, array $data): int|false
{
if ($data === []) {
return false;
}
if (str_starts_with((string)$data['uid'], 'NEW')) {
// negative uid_pid values of content elements indicate that the element
// has been inserted after an existing element so there is no pid to get
// the backendLayout for and we have to get that first
if ($data['pid'] < 0) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($tableName);
$queryBuilder->getRestrictions()
->removeAll();
$pageId = $queryBuilder
->select('pid')
->from($tableName)
->where(
$queryBuilder->expr()->eq(
'uid',
$queryBuilder->createNamedParameter(abs($data['pid']), Connection::PARAM_INT)
)
)
->executeQuery()
->fetchOne();
} else {
$pageId = $data['pid'];
}
} elseif ($tableName === 'pages') {
$pageId = $data['uid'];
} else {
$pageId = $data['pid'];
}
return (int)$pageId;
}
/**
* Returns the backend layout which should be used for this page.
*
* @return false|string Identifier of the backend layout to be used, or FALSE if none
*/
protected function getSelectedCombinedIdentifier(int $pageId): string|false
{
if (!isset($this->selectedCombinedIdentifier[$pageId])) {
$page = $this->getPage($pageId);
$this->selectedCombinedIdentifier[$pageId] = (string)($page['backend_layout'] ?? null);
if ($this->selectedCombinedIdentifier[$pageId] === '-1') {
// If it is set to "none" - don't use any
$this->selectedCombinedIdentifier[$pageId] = false;
} elseif ($this->selectedCombinedIdentifier[$pageId] === '' || $this->selectedCombinedIdentifier[$pageId] === '0') {
// If it not set check the root-line for a layout on next level and use this
// (root-line starts with current page and has page "0" at the end)
$rootLine = BackendUtility::BEgetRootLine($pageId, '', true);
// Remove first and last element (current and root page)
array_shift($rootLine);
array_pop($rootLine);
foreach ($rootLine as $rootLinePage) {
$this->selectedCombinedIdentifier[$pageId] = (string)$rootLinePage['backend_layout_next_level'];
if ($this->selectedCombinedIdentifier[$pageId] === '-1') {
// If layout for "next level" is set to "none" - don't use any and stop searching
$this->selectedCombinedIdentifier[$pageId] = false;
break;
}
if ($this->selectedCombinedIdentifier[$pageId] !== '' && $this->selectedCombinedIdentifier[$pageId] !== '0') {
// Stop searching if a layout for "next level" is set
break;
}
}
}
}
// If it is set to a positive value use this
return $this->selectedCombinedIdentifier[$pageId];
}
/**
* Gets backend layout identifiers to be excluded
*/
protected function getIdentifiersToBeExcluded(array $pageTSconfig): array
{
if (isset($pageTSconfig['options.']['backendLayout.']['exclude'])) {
return GeneralUtility::trimExplode(
',',
$pageTSconfig['options.']['backendLayout.']['exclude'],
true
);
}
return [];
}
/**
* Gets colPos items to be shown in the forms engine.
* This method is called as "itemsProcFunc" with the accordant context
* for tt_content.colPos.
*/
public function colPosListItemProcFunc(array &$parameters): void
{
$pageId = $this->determinePageId($parameters['table'], $parameters['row']);
if ($pageId !== false) {
$parameters['items'] = $this->addColPosListLayoutItems($pageId, $parameters['items']);
}
}
/**
* Adds items to a colpos list
*/
protected function addColPosListLayoutItems(int $pageId, array $items): array
{
$layout = $this->getSelectedBackendLayout($pageId);
if ($layout && !empty($layout['__items'])) {
$items = $layout['__items'];
}
return $items;
}
/**
* Gets the selected backend layout structure as an array
*/
public function getSelectedBackendLayout(int $pageId): ?array
{
return $this->getBackendLayoutForPage($pageId)?->getStructure();
}
/**
* Get the BackendLayout object and parse the structure based on the UserTSconfig
*/
public function getBackendLayoutForPage(int $pageId): ?BackendLayout
{
if (isset($this->selectedBackendLayout[$pageId])) {
return $this->selectedBackendLayout[$pageId];
}
$selectedCombinedIdentifier = $this->getSelectedCombinedIdentifier($pageId);
// If no backend layout is selected, use default
if (empty($selectedCombinedIdentifier)) {
$selectedCombinedIdentifier = 'default';
}
$backendLayout = $this->dataProviderCollection->getBackendLayout($selectedCombinedIdentifier, $pageId);
// If backend layout is not found available anymore, use default
if ($backendLayout === null) {
$backendLayout = $this->dataProviderCollection->getBackendLayout('default', $pageId);
}
if ($backendLayout !== null) {
$this->selectedBackendLayout[$pageId] = $backendLayout;
}
return $backendLayout;
}
/**
* @internal
*/
public function parseStructure(BackendLayout $backendLayout): array
{
$typoScriptTree = $this->typoScriptStringFactory->parseFromStringWithIncludes('backend-layout', $backendLayout->getConfiguration());
$backendLayoutData = [];
$backendLayoutData['config'] = $backendLayout->getConfiguration();
$backendLayoutData['__config'] = $typoScriptTree->toArray();
$backendLayoutData['__items'] = [];
$backendLayoutData['__colPosList'] = [];
$backendLayoutData['usedColumns'] = [];
$backendLayoutData['colCount'] = (int)($backendLayoutData['__config']['backend_layout.']['colCount'] ?? 0);
$backendLayoutData['rowCount'] = (int)($backendLayoutData['__config']['backend_layout.']['rowCount'] ?? 0);
// create items and colPosList
if (!empty($backendLayoutData['__config']['backend_layout.']['rows.'])) {
$rows = $backendLayoutData['__config']['backend_layout.']['rows.'];
ksort($rows);
foreach ($rows as $row) {
if (!empty($row['columns.'])) {
foreach ($row['columns.'] as $column) {
if (!isset($column['colPos'])) {
continue;
}
$backendLayoutData['__items'][] = [
'label' => $column['name'],
'value' => $column['colPos'],
'icon' => null,
];
$backendLayoutData['__colPosList'][] = $column['colPos'];
$backendLayoutData['usedColumns'][(int)$column['colPos']] = $column['name'];
}
}
}
}
return $backendLayoutData;
}
/**
* Get default columns layout (main column)
*/
public static function getDefaultColumnLayout(): string
{
return '
backend_layout {
colCount = 1
rowCount = 1
rows {
1 {
columns {
1 {
name = LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:colPos.I.1
colPos = 0
}
}
}
}
}
';
}
/**
* Gets a page record.
*/
protected function getPage(int $pageId): ?array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('pages');
$queryBuilder->getRestrictions()
->removeAll();
$page = $queryBuilder
->select('uid', 'pid', 'backend_layout')
->from('pages')
->where(
$queryBuilder->expr()->eq(
'uid',
$queryBuilder->createNamedParameter($pageId, Connection::PARAM_INT)
)
)
->executeQuery()
->fetchAssociative();
if (is_array($page)) {
BackendUtility::workspaceOL('pages', $page);
}
return is_array($page) ? $page : null;
}
}