| Total Complexity | 64 |
| Total Lines | 478 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BackendLayoutView often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BackendLayoutView, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class BackendLayoutView implements \TYPO3\CMS\Core\SingletonInterface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var DataProviderCollection |
||
| 37 | */ |
||
| 38 | protected $dataProviderCollection; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $selectedCombinedIdentifier = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $selectedBackendLayout = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Creates this object and initializes data providers. |
||
| 52 | */ |
||
| 53 | public function __construct() |
||
| 54 | { |
||
| 55 | $this->initializeDataProviderCollection(); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Initializes data providers |
||
| 60 | */ |
||
| 61 | protected function initializeDataProviderCollection() |
||
| 62 | { |
||
| 63 | $dataProviderCollection = GeneralUtility::makeInstance(DataProviderCollection::class); |
||
| 64 | $dataProviderCollection->add('default', DefaultDataProvider::class); |
||
| 65 | |||
| 66 | if (!empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['BackendLayoutDataProvider'])) { |
||
| 67 | $dataProviders = (array)$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['BackendLayoutDataProvider']; |
||
| 68 | foreach ($dataProviders as $identifier => $className) { |
||
| 69 | $dataProviderCollection->add($identifier, $className); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | $this->setDataProviderCollection($dataProviderCollection); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param DataProviderCollection $dataProviderCollection |
||
| 78 | */ |
||
| 79 | public function setDataProviderCollection(DataProviderCollection $dataProviderCollection) |
||
| 80 | { |
||
| 81 | $this->dataProviderCollection = $dataProviderCollection; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return DataProviderCollection |
||
| 86 | */ |
||
| 87 | public function getDataProviderCollection() |
||
| 88 | { |
||
| 89 | return $this->dataProviderCollection; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Gets backend layout items to be shown in the forms engine. |
||
| 94 | * This method is called as "itemsProcFunc" with the accordant context |
||
| 95 | * for pages.backend_layout and pages.backend_layout_next_level. |
||
| 96 | * |
||
| 97 | * @param array $parameters |
||
| 98 | */ |
||
| 99 | public function addBackendLayoutItems(array $parameters) |
||
| 100 | { |
||
| 101 | $pageId = $this->determinePageId($parameters['table'], $parameters['row']); |
||
| 102 | $pageTsConfig = (array)BackendUtility::getPagesTSconfig($pageId); |
||
|
|
|||
| 103 | $identifiersToBeExcluded = $this->getIdentifiersToBeExcluded($pageTsConfig); |
||
| 104 | |||
| 105 | $dataProviderContext = $this->createDataProviderContext() |
||
| 106 | ->setPageId($pageId) |
||
| 107 | ->setData($parameters['row']) |
||
| 108 | ->setTableName($parameters['table']) |
||
| 109 | ->setFieldName($parameters['field']) |
||
| 110 | ->setPageTsConfig($pageTsConfig); |
||
| 111 | |||
| 112 | $backendLayoutCollections = $this->getDataProviderCollection()->getBackendLayoutCollections($dataProviderContext); |
||
| 113 | foreach ($backendLayoutCollections as $backendLayoutCollection) { |
||
| 114 | $combinedIdentifierPrefix = ''; |
||
| 115 | if ($backendLayoutCollection->getIdentifier() !== 'default') { |
||
| 116 | $combinedIdentifierPrefix = $backendLayoutCollection->getIdentifier() . '__'; |
||
| 117 | } |
||
| 118 | |||
| 119 | foreach ($backendLayoutCollection->getAll() as $backendLayout) { |
||
| 120 | $combinedIdentifier = $combinedIdentifierPrefix . $backendLayout->getIdentifier(); |
||
| 121 | |||
| 122 | if (in_array($combinedIdentifier, $identifiersToBeExcluded, true)) { |
||
| 123 | continue; |
||
| 124 | } |
||
| 125 | |||
| 126 | $parameters['items'][] = [ |
||
| 127 | $this->getLanguageService()->sL($backendLayout->getTitle()), |
||
| 128 | $combinedIdentifier, |
||
| 129 | $backendLayout->getIconPath(), |
||
| 130 | ]; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Determines the page id for a given record of a database table. |
||
| 137 | * |
||
| 138 | * @param string $tableName |
||
| 139 | * @param array $data |
||
| 140 | * @return int|bool Returns page id or false on error |
||
| 141 | */ |
||
| 142 | protected function determinePageId($tableName, array $data) |
||
| 143 | { |
||
| 144 | if (strpos($data['uid'], 'NEW') === 0) { |
||
| 145 | // negative uid_pid values of content elements indicate that the element |
||
| 146 | // has been inserted after an existing element so there is no pid to get |
||
| 147 | // the backendLayout for and we have to get that first |
||
| 148 | if ($data['pid'] < 0) { |
||
| 149 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 150 | ->getQueryBuilderForTable($tableName); |
||
| 151 | $queryBuilder->getRestrictions() |
||
| 152 | ->removeAll(); |
||
| 153 | $pageId = $queryBuilder |
||
| 154 | ->select('pid') |
||
| 155 | ->from($tableName) |
||
| 156 | ->where( |
||
| 157 | $queryBuilder->expr()->eq( |
||
| 158 | 'uid', |
||
| 159 | $queryBuilder->createNamedParameter(abs($data['pid']), \PDO::PARAM_INT) |
||
| 160 | ) |
||
| 161 | ) |
||
| 162 | ->execute() |
||
| 163 | ->fetchColumn(); |
||
| 164 | } else { |
||
| 165 | $pageId = $data['pid']; |
||
| 166 | } |
||
| 167 | } elseif ($tableName === 'pages') { |
||
| 168 | $pageId = $data['uid']; |
||
| 169 | } else { |
||
| 170 | $pageId = $data['pid']; |
||
| 171 | } |
||
| 172 | |||
| 173 | return $pageId; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Returns the backend layout which should be used for this page. |
||
| 178 | * |
||
| 179 | * @param int $pageId |
||
| 180 | * @return bool|string Identifier of the backend layout to be used, or FALSE if none |
||
| 181 | */ |
||
| 182 | public function getSelectedCombinedIdentifier($pageId) |
||
| 183 | { |
||
| 184 | if (!isset($this->selectedCombinedIdentifier[$pageId])) { |
||
| 185 | $page = $this->getPage($pageId); |
||
| 186 | $this->selectedCombinedIdentifier[$pageId] = (string)$page['backend_layout']; |
||
| 187 | |||
| 188 | if ($this->selectedCombinedIdentifier[$pageId] === '-1') { |
||
| 189 | // If it is set to "none" - don't use any |
||
| 190 | $this->selectedCombinedIdentifier[$pageId] = false; |
||
| 191 | } elseif ($this->selectedCombinedIdentifier[$pageId] === '' || $this->selectedCombinedIdentifier[$pageId] === '0') { |
||
| 192 | // If it not set check the root-line for a layout on next level and use this |
||
| 193 | // (root-line starts with current page and has page "0" at the end) |
||
| 194 | $rootLine = $this->getRootLine($pageId); |
||
| 195 | // Remove first and last element (current and root page) |
||
| 196 | array_shift($rootLine); |
||
| 197 | array_pop($rootLine); |
||
| 198 | foreach ($rootLine as $rootLinePage) { |
||
| 199 | $this->selectedCombinedIdentifier[$pageId] = (string)$rootLinePage['backend_layout_next_level']; |
||
| 200 | if ($this->selectedCombinedIdentifier[$pageId] === '-1') { |
||
| 201 | // If layout for "next level" is set to "none" - don't use any and stop searching |
||
| 202 | $this->selectedCombinedIdentifier[$pageId] = false; |
||
| 203 | break; |
||
| 204 | } |
||
| 205 | if ($this->selectedCombinedIdentifier[$pageId] !== '' && $this->selectedCombinedIdentifier[$pageId] !== '0') { |
||
| 206 | // Stop searching if a layout for "next level" is set |
||
| 207 | break; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | // If it is set to a positive value use this |
||
| 213 | return $this->selectedCombinedIdentifier[$pageId]; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Gets backend layout identifiers to be excluded |
||
| 218 | * |
||
| 219 | * @param array $pageTSconfig |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | protected function getIdentifiersToBeExcluded(array $pageTSconfig) |
||
| 223 | { |
||
| 224 | $identifiersToBeExcluded = []; |
||
| 225 | |||
| 226 | if (ArrayUtility::isValidPath($pageTSconfig, 'options./backendLayout./exclude')) { |
||
| 227 | $identifiersToBeExcluded = GeneralUtility::trimExplode( |
||
| 228 | ',', |
||
| 229 | ArrayUtility::getValueByPath($pageTSconfig, 'options./backendLayout./exclude'), |
||
| 230 | true |
||
| 231 | ); |
||
| 232 | } |
||
| 233 | |||
| 234 | return $identifiersToBeExcluded; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Gets colPos items to be shown in the forms engine. |
||
| 239 | * This method is called as "itemsProcFunc" with the accordant context |
||
| 240 | * for tt_content.colPos. |
||
| 241 | * |
||
| 242 | * @param array $parameters |
||
| 243 | */ |
||
| 244 | public function colPosListItemProcFunc(array $parameters) |
||
| 245 | { |
||
| 246 | $pageId = $this->determinePageId($parameters['table'], $parameters['row']); |
||
| 247 | |||
| 248 | if ($pageId !== false) { |
||
| 249 | $parameters['items'] = $this->addColPosListLayoutItems($pageId, $parameters['items']); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Adds items to a colpos list |
||
| 255 | * |
||
| 256 | * @param int $pageId |
||
| 257 | * @param array $items |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | protected function addColPosListLayoutItems($pageId, $items) |
||
| 261 | { |
||
| 262 | $layout = $this->getSelectedBackendLayout($pageId); |
||
| 263 | if ($layout && $layout['__items']) { |
||
| 264 | $items = $layout['__items']; |
||
| 265 | } |
||
| 266 | return $items; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Gets the list of available columns for a given page id |
||
| 271 | * |
||
| 272 | * @param int $id |
||
| 273 | * @return array $tcaItems |
||
| 274 | */ |
||
| 275 | public function getColPosListItemsParsed($id) |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Merges items into an item-array, optionally with an icon |
||
| 298 | * example: |
||
| 299 | * TCEFORM.pages.doktype.addItems.13 = My Label |
||
| 300 | * TCEFORM.pages.doktype.addItems.13.icon = EXT:t3skin/icons/gfx/i/pages.gif |
||
| 301 | * |
||
| 302 | * @param array $items The existing item array |
||
| 303 | * @param array $iArray An array of items to add. NOTICE: The keys are mapped to values, and the values and mapped to be labels. No possibility of adding an icon. |
||
| 304 | * @return array The updated $item array |
||
| 305 | * @internal |
||
| 306 | */ |
||
| 307 | protected function addItems($items, $iArray) |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Gets the selected backend layout structure as an array |
||
| 331 | * |
||
| 332 | * @param int $pageId |
||
| 333 | * @return array|null $backendLayout |
||
| 334 | */ |
||
| 335 | public function getSelectedBackendLayout($pageId): ?array |
||
| 336 | { |
||
| 337 | $layout = $this->getBackendLayoutForPage((int)$pageId); |
||
| 338 | if ($layout instanceof BackendLayout) { |
||
| 339 | return $layout->getStructure(); |
||
| 340 | } |
||
| 341 | return null; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Get the BackendLayout object and parse the structure based on the UserTSconfig |
||
| 346 | * @param int $pageId |
||
| 347 | * @return BackendLayout |
||
| 348 | */ |
||
| 349 | public function getBackendLayoutForPage(int $pageId): ?BackendLayout |
||
| 350 | { |
||
| 351 | if (isset($this->selectedBackendLayout[$pageId])) { |
||
| 352 | return $this->selectedBackendLayout[$pageId]; |
||
| 353 | } |
||
| 354 | $selectedCombinedIdentifier = $this->getSelectedCombinedIdentifier($pageId); |
||
| 355 | // If no backend layout is selected, use default |
||
| 356 | if (empty($selectedCombinedIdentifier)) { |
||
| 357 | $selectedCombinedIdentifier = 'default'; |
||
| 358 | } |
||
| 359 | $backendLayout = $this->getDataProviderCollection()->getBackendLayout($selectedCombinedIdentifier, $pageId); |
||
| 360 | // If backend layout is not found available anymore, use default |
||
| 361 | if ($backendLayout === null) { |
||
| 362 | $backendLayout = $this->getDataProviderCollection()->getBackendLayout('default', $pageId); |
||
| 363 | } |
||
| 364 | |||
| 365 | $structure = null; |
||
| 366 | if ($backendLayout instanceof BackendLayout) { |
||
| 367 | $structure = $this->parseStructure($backendLayout); |
||
| 368 | // Parse the configuration and inject it back in the backend layout object |
||
| 369 | $backendLayout->setStructure($structure); |
||
| 370 | $this->selectedBackendLayout[$pageId] = $backendLayout; |
||
| 371 | } |
||
| 372 | return $backendLayout; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param BackendLayout $backendLayout |
||
| 377 | * @return array |
||
| 378 | * @internal |
||
| 379 | */ |
||
| 380 | public function parseStructure(BackendLayout $backendLayout): array |
||
| 381 | { |
||
| 382 | $parser = GeneralUtility::makeInstance(TypoScriptParser::class); |
||
| 383 | $conditionMatcher = GeneralUtility::makeInstance(ConditionMatcher::class); |
||
| 384 | $parser->parse(TypoScriptParser::checkIncludeLines($backendLayout->getConfiguration()), $conditionMatcher); |
||
| 385 | |||
| 386 | $backendLayoutData = []; |
||
| 387 | $backendLayoutData['config'] = $backendLayout->getConfiguration(); |
||
| 388 | $backendLayoutData['__config'] = $parser->setup; |
||
| 389 | $backendLayoutData['__items'] = []; |
||
| 390 | $backendLayoutData['__colPosList'] = []; |
||
| 391 | $backendLayoutData['usedColumns'] = []; |
||
| 392 | |||
| 393 | // create items and colPosList |
||
| 394 | if (!empty($backendLayoutData['__config']['backend_layout.']['rows.'])) { |
||
| 395 | foreach ($backendLayoutData['__config']['backend_layout.']['rows.'] as $row) { |
||
| 396 | if (!empty($row['columns.'])) { |
||
| 397 | foreach ($row['columns.'] as $column) { |
||
| 398 | if (!isset($column['colPos'])) { |
||
| 399 | continue; |
||
| 400 | } |
||
| 401 | $backendLayoutData['__items'][] = [ |
||
| 402 | $this->getColumnName($column), |
||
| 403 | $column['colPos'], |
||
| 404 | null |
||
| 405 | ]; |
||
| 406 | $backendLayoutData['__colPosList'][] = $column['colPos']; |
||
| 407 | $backendLayoutData['usedColumns'][(int)$column['colPos']] = $column['name']; |
||
| 408 | } |
||
| 409 | } |
||
| 410 | } |
||
| 411 | } |
||
| 412 | return $backendLayoutData; |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get default columns layout |
||
| 417 | * |
||
| 418 | * @return string Default four column layout |
||
| 419 | * @static |
||
| 420 | */ |
||
| 421 | public static function getDefaultColumnLayout() |
||
| 422 | { |
||
| 423 | return ' |
||
| 424 | backend_layout { |
||
| 425 | colCount = 1 |
||
| 426 | rowCount = 1 |
||
| 427 | rows { |
||
| 428 | 1 { |
||
| 429 | columns { |
||
| 430 | 1 { |
||
| 431 | name = LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:colPos.I.1 |
||
| 432 | colPos = 0 |
||
| 433 | } |
||
| 434 | } |
||
| 435 | } |
||
| 436 | } |
||
| 437 | } |
||
| 438 | '; |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Gets a page record. |
||
| 443 | * |
||
| 444 | * @param int $pageId |
||
| 445 | * @return array|null |
||
| 446 | */ |
||
| 447 | protected function getPage($pageId) |
||
| 448 | { |
||
| 449 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 450 | ->getQueryBuilderForTable('pages'); |
||
| 451 | $queryBuilder->getRestrictions() |
||
| 452 | ->removeAll(); |
||
| 453 | $page = $queryBuilder |
||
| 454 | ->select('uid', 'pid', 'backend_layout') |
||
| 455 | ->from('pages') |
||
| 456 | ->where( |
||
| 457 | $queryBuilder->expr()->eq( |
||
| 458 | 'uid', |
||
| 459 | $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT) |
||
| 460 | ) |
||
| 461 | ) |
||
| 462 | ->execute() |
||
| 463 | ->fetch(); |
||
| 464 | BackendUtility::workspaceOL('pages', $page); |
||
| 465 | |||
| 466 | return $page; |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Gets the page root-line. |
||
| 471 | * |
||
| 472 | * @param int $pageId |
||
| 473 | * @return array |
||
| 474 | */ |
||
| 475 | protected function getRootLine($pageId) |
||
| 476 | { |
||
| 477 | return BackendUtility::BEgetRootLine($pageId, '', true); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @return DataProviderContext |
||
| 482 | */ |
||
| 483 | protected function createDataProviderContext() |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @return LanguageService |
||
| 490 | */ |
||
| 491 | protected function getLanguageService() |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get column name from colPos item structure |
||
| 498 | * |
||
| 499 | * @param array $column |
||
| 500 | * @return string |
||
| 501 | */ |
||
| 502 | protected function getColumnName($column) |
||
| 511 | } |
||
| 512 | } |
||
| 513 |