| Total Complexity | 41 |
| Total Lines | 310 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BackendLayoutRenderer 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 BackendLayoutRenderer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 56 | class BackendLayoutRenderer |
||
| 57 | { |
||
| 58 | use LoggerAwareTrait; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var IconFactory |
||
| 62 | */ |
||
| 63 | protected $iconFactory; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var PageLayoutContext |
||
| 67 | */ |
||
| 68 | protected $context; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var ContentFetcher |
||
| 72 | */ |
||
| 73 | protected $contentFetcher; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var Clipboard |
||
| 77 | */ |
||
| 78 | protected $clipboard; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var TemplateView |
||
| 82 | */ |
||
| 83 | protected $view; |
||
| 84 | |||
| 85 | public function __construct(PageLayoutContext $context) |
||
| 86 | { |
||
| 87 | $this->context = $context; |
||
| 88 | $this->contentFetcher = GeneralUtility::makeInstance(ContentFetcher::class, $context); |
||
| 89 | $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class); |
||
| 90 | $this->initializeClipboard(); |
||
| 91 | $objectManager = GeneralUtility::makeInstance(ObjectManager::class); |
||
| 92 | $request = $objectManager->get(Request::class); |
||
| 93 | $this->view = GeneralUtility::makeInstance(TemplateView::class); |
||
| 94 | $this->view->getRenderingContext()->setRequest($request); |
||
| 95 | $this->view->getRenderingContext()->getTemplatePaths()->fillDefaultsByPackageName('backend'); |
||
| 96 | $this->view->getRenderingContext()->setControllerName('PageLayout'); |
||
| 97 | $this->view->assign('context', $context); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function getGridForPageLayoutContext(PageLayoutContext $context): Grid |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return LanguageColumn[] |
||
| 130 | */ |
||
| 131 | public function getLanguageColumnsForPageLayoutContext(PageLayoutContext $context): iterable |
||
| 160 | } |
||
| 161 | |||
| 162 | protected function getLanguageColumnsWithDefLangBindingForPageLayoutContext(PageLayoutContext $context): iterable |
||
| 163 | { |
||
| 164 | $languageColumns = []; |
||
| 165 | |||
| 166 | // default language |
||
| 167 | $translationInfo = $this->contentFetcher->getTranslationData( |
||
| 168 | $this->contentFetcher->getFlatContentRecords(0), |
||
| 169 | 0 |
||
| 170 | ); |
||
| 171 | |||
| 172 | $defaultLanguageColumnObject = GeneralUtility::makeInstance( |
||
| 173 | LanguageColumn::class, |
||
| 174 | $context, |
||
| 175 | $this->getGridForPageLayoutContext($context), |
||
| 176 | $translationInfo |
||
| 177 | ); |
||
| 178 | foreach ($context->getLanguagesToShow() as $siteLanguage) { |
||
| 179 | $localizedLanguageId = $siteLanguage->getLanguageId(); |
||
| 180 | if ($localizedLanguageId <= 0) { |
||
| 181 | continue; |
||
| 182 | } |
||
| 183 | |||
| 184 | $localizedContext = $context->cloneForLanguage($siteLanguage); |
||
| 185 | if (!$localizedContext->getLocalizedPageRecord()) { |
||
| 186 | continue; |
||
| 187 | } |
||
| 188 | |||
| 189 | $translationInfo = $this->contentFetcher->getTranslationData( |
||
| 190 | $this->contentFetcher->getFlatContentRecords($localizedLanguageId), |
||
| 191 | $localizedContext->getSiteLanguage()->getLanguageId() |
||
| 192 | ); |
||
| 193 | |||
| 194 | $translatedRows = $this->contentFetcher->getFlatContentRecords($localizedLanguageId); |
||
| 195 | |||
| 196 | $grid = $defaultLanguageColumnObject->getGrid(); |
||
| 197 | if ($grid === null) { |
||
| 198 | continue; |
||
| 199 | } |
||
| 200 | |||
| 201 | foreach ($grid->getRows() as $rows) { |
||
| 202 | foreach ($rows->getColumns() as $column) { |
||
| 203 | if ($translationInfo['mode'] === 'connected') { |
||
| 204 | foreach ($column->getItems() as $item) { |
||
| 205 | // check if translation exists |
||
| 206 | foreach ($translatedRows as $translation) { |
||
| 207 | if ($translation['l18n_parent'] === $item->getRecord()['uid']) { |
||
| 208 | $translatedItem = GeneralUtility::makeInstance(GridColumnItem::class, $this->context, $column, $translation); |
||
| 209 | $item->addTranslation($localizedLanguageId, $translatedItem); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | $languageColumnObject = GeneralUtility::makeInstance( |
||
| 218 | LanguageColumn::class, |
||
| 219 | $localizedContext, |
||
| 220 | $this->getGridForPageLayoutContext($localizedContext), |
||
| 221 | $translationInfo |
||
| 222 | ); |
||
| 223 | $languageColumns[$localizedLanguageId] = $languageColumnObject; |
||
| 224 | } |
||
| 225 | $languageColumns = [$defaultLanguageColumnObject] + $languageColumns; |
||
| 226 | |||
| 227 | return $languageColumns; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param bool $renderUnused If true, renders the bottom column with unused records |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public function drawContent(bool $renderUnused = true): string |
||
| 235 | { |
||
| 236 | $this->view->assign('hideRestrictedColumns', (bool)(BackendUtility::getPagesTSconfig($this->context->getPageId())['mod.']['web_layout.']['hideRestrictedCols'] ?? false)); |
||
| 237 | $this->view->assign('newContentTitle', $this->getLanguageService()->getLL('newContentElement')); |
||
| 238 | $this->view->assign('newContentTitleShort', $this->getLanguageService()->getLL('content')); |
||
| 239 | $this->view->assign('allowEditContent', $this->getBackendUser()->check('tables_modify', 'tt_content')); |
||
| 240 | |||
| 241 | if ($this->context->getDrawingConfiguration()->getLanguageMode()) { |
||
| 242 | if ($this->context->getDrawingConfiguration()->getDefaultLanguageBinding()) { |
||
| 243 | $this->view->assign('languageColumns', $this->getLanguageColumnsWithDefLangBindingForPageLayoutContext($this->context)); |
||
| 244 | } else { |
||
| 245 | $this->view->assign('languageColumns', $this->getLanguageColumnsForPageLayoutContext($this->context)); |
||
| 246 | } |
||
| 247 | } else { |
||
| 248 | $this->view->assign('grid', $this->getGridForPageLayoutContext($this->context)); |
||
| 249 | } |
||
| 250 | |||
| 251 | $rendered = $this->view->render('PageLayout'); |
||
| 252 | if ($renderUnused) { |
||
| 253 | $unusedRecords = $this->contentFetcher->getUnusedRecords(); |
||
| 254 | |||
| 255 | if (!empty($unusedRecords)) { |
||
| 256 | $unusedElementsMessage = GeneralUtility::makeInstance( |
||
| 257 | FlashMessage::class, |
||
| 258 | $this->getLanguageService()->getLL('staleUnusedElementsWarning'), |
||
| 259 | $this->getLanguageService()->getLL('staleUnusedElementsWarningTitle'), |
||
| 260 | FlashMessage::WARNING |
||
| 261 | ); |
||
| 262 | $service = GeneralUtility::makeInstance(FlashMessageService::class); |
||
| 263 | $queue = $service->getMessageQueueByIdentifier(); |
||
| 264 | $queue->addMessage($unusedElementsMessage); |
||
| 265 | |||
| 266 | $unusedGrid = GeneralUtility::makeInstance(Grid::class, $this->context); |
||
| 267 | $unusedRow = GeneralUtility::makeInstance(GridRow::class, $this->context); |
||
| 268 | $unusedColumn = GeneralUtility::makeInstance(GridColumn::class, $this->context, ['colPos' => false, 'name' => 'unused']); |
||
| 269 | |||
| 270 | $unusedGrid->addRow($unusedRow); |
||
| 271 | $unusedRow->addColumn($unusedColumn); |
||
| 272 | |||
| 273 | foreach ($unusedRecords as $unusedRecord) { |
||
| 274 | $item = GeneralUtility::makeInstance(GridColumnItem::class, $this->context, $unusedColumn, $unusedRecord); |
||
| 275 | $unusedColumn->addItem($item); |
||
| 276 | } |
||
| 277 | |||
| 278 | $this->view->assign('grid', $unusedGrid); |
||
| 279 | $rendered .= $this->view->render('UnusedRecords'); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | return $rendered; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Initializes the clipboard for generating paste links |
||
| 287 | * |
||
| 288 | * @see \TYPO3\CMS\Backend\Controller\ContextMenuController::clipboardAction() |
||
| 289 | * @see \TYPO3\CMS\Filelist\Controller\FileListController::indexAction() |
||
| 290 | */ |
||
| 291 | protected function initializeClipboard(): void |
||
| 292 | { |
||
| 293 | $this->clipboard = GeneralUtility::makeInstance(Clipboard::class); |
||
| 294 | $this->clipboard->initializeClipboard(); |
||
| 295 | $this->clipboard->lockToNormal(); |
||
| 296 | $this->clipboard->cleanCurrent(); |
||
| 297 | $this->clipboard->endClipboard(); |
||
| 298 | |||
| 299 | $elFromTable = $this->clipboard->elFromTable('tt_content'); |
||
| 300 | if (!empty($elFromTable) && $this->isContentEditable()) { |
||
| 301 | $pasteItem = (int)substr((string)key($elFromTable), 11); |
||
| 302 | $pasteRecord = BackendUtility::getRecord('tt_content', (int)$pasteItem); |
||
| 303 | $pasteTitle = (string)($pasteRecord['header'] ?: $pasteItem); |
||
| 304 | $copyMode = $this->clipboard->clipData['normal']['mode'] ? '-' . $this->clipboard->clipData['normal']['mode'] : ''; |
||
| 305 | $addExtOnReadyCode = ' |
||
| 306 | top.pasteIntoLinkTemplate = ' |
||
| 307 | . $this->drawPasteIcon($pasteItem, $pasteTitle, $copyMode, 't3js-paste-into', 'pasteIntoColumn') |
||
| 308 | . '; |
||
| 309 | top.pasteAfterLinkTemplate = ' |
||
| 310 | . $this->drawPasteIcon($pasteItem, $pasteTitle, $copyMode, 't3js-paste-after', 'pasteAfterRecord') |
||
| 311 | . ';'; |
||
| 312 | } else { |
||
| 313 | $addExtOnReadyCode = ' |
||
| 314 | top.pasteIntoLinkTemplate = \'\'; |
||
| 315 | top.pasteAfterLinkTemplate = \'\';'; |
||
| 316 | } |
||
| 317 | GeneralUtility::makeInstance(PageRenderer::class)->addJsInlineCode('pasteLinkTemplates', $addExtOnReadyCode); |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Draw a paste icon either for pasting into a column or for pasting after a record |
||
| 322 | * |
||
| 323 | * @param int $pasteItem ID of the item in the clipboard |
||
| 324 | * @param string $pasteTitle Title for the JS modal |
||
| 325 | * @param string $copyMode copy or cut |
||
| 326 | * @param string $cssClass CSS class to determine if pasting is done into column or after record |
||
| 327 | * @param string $title title attribute of the generated link |
||
| 328 | * |
||
| 329 | * @return string Generated HTML code with link and icon |
||
| 330 | */ |
||
| 331 | private function drawPasteIcon(int $pasteItem, string $pasteTitle, string $copyMode, string $cssClass, string $title): string |
||
| 332 | { |
||
| 333 | $pasteIcon = json_encode( |
||
| 334 | ' <button type="button"' |
||
| 335 | . ' data-content="' . htmlspecialchars((string)$pasteItem) . '"' |
||
| 336 | . ' data-title="' . htmlspecialchars($pasteTitle) . '"' |
||
| 337 | . ' data-severity="warning"' |
||
| 338 | . ' class="t3js-paste t3js-paste' . htmlspecialchars($copyMode) . ' ' . htmlspecialchars($cssClass) . ' btn btn-default btn-sm"' |
||
| 339 | . ' title="' . htmlspecialchars($this->getLanguageService()->getLL($title)) . '">' |
||
| 340 | . $this->iconFactory->getIcon('actions-document-paste-into', Icon::SIZE_SMALL)->render() |
||
| 341 | . '</button>' |
||
| 342 | ); |
||
| 343 | return $pasteIcon; |
||
| 344 | } |
||
| 345 | |||
| 346 | protected function isContentEditable(): bool |
||
| 347 | { |
||
| 348 | if ($this->getBackendUser()->isAdmin()) { |
||
| 349 | return true; |
||
| 350 | } |
||
| 351 | |||
| 352 | $pageRecord = $this->context->getPageRecord(); |
||
| 353 | return !$pageRecord['editlock'] |
||
| 354 | && $this->getBackendUser()->check('tables_modify', 'tt_content') |
||
| 355 | && $this->getBackendUser()->doesUserHaveAccess($pageRecord, Permission::CONTENT_EDIT); |
||
| 356 | } |
||
| 357 | |||
| 358 | protected function getBackendUser(): BackendUserAuthentication |
||
| 359 | { |
||
| 360 | return $GLOBALS['BE_USER']; |
||
| 361 | } |
||
| 362 | |||
| 363 | protected function getLanguageService(): LanguageService |
||
| 366 | } |
||
| 367 | } |
||
| 368 |