| Total Complexity | 77 |
| Total Lines | 639 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like FileListController 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 FileListController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 58 | class FileListController implements LoggerAwareInterface |
||
| 59 | { |
||
| 60 | use LoggerAwareTrait; |
||
| 61 | |||
| 62 | protected array $MOD_MENU = []; |
||
| 63 | protected array $MOD_SETTINGS = []; |
||
| 64 | protected string $id = ''; |
||
| 65 | protected string $cmd = ''; |
||
| 66 | protected string $searchTerm = ''; |
||
| 67 | protected int $pointer = 0; |
||
| 68 | protected ?Folder $folderObject = null; |
||
| 69 | protected ?DuplicationBehavior $overwriteExistingFiles = null; |
||
| 70 | |||
| 71 | protected UriBuilder $uriBuilder; |
||
| 72 | protected PageRenderer $pageRenderer; |
||
| 73 | protected IconFactory $iconFactory; |
||
| 74 | protected ResourceFactory $resourceFactory; |
||
| 75 | protected ModuleTemplateFactory $moduleTemplateFactory; |
||
| 76 | protected ResponseFactoryInterface $responseFactory; |
||
| 77 | |||
| 78 | protected ?ModuleTemplate $moduleTemplate = null; |
||
| 79 | protected ?ViewInterface $view = null; |
||
| 80 | protected ?FileList $filelist = null; |
||
| 81 | |||
| 82 | public function __construct( |
||
| 96 | } |
||
| 97 | |||
| 98 | public function handleRequest(ServerRequestInterface $request): ResponseInterface |
||
| 205 | } |
||
| 206 | |||
| 207 | protected function processRequest(ServerRequestInterface $request): ResponseInterface |
||
| 208 | { |
||
| 209 | $lang = $this->getLanguageService(); |
||
| 210 | |||
| 211 | // Initialize FileList, including the clipboard actions |
||
| 212 | $this->initializeFileList($request); |
||
| 213 | |||
| 214 | // Generate the file listing markup |
||
| 215 | $this->generateFileList(); |
||
| 216 | |||
| 217 | // Generate the clipboard, if enabled |
||
| 218 | if ($this->MOD_SETTINGS['clipBoard'] ?? false) { |
||
| 219 | $this->view->assign('clipBoardHtml', $this->filelist->clipObj->printClipboard()); |
||
| 220 | } |
||
| 221 | |||
| 222 | // Register drag-uploader |
||
| 223 | $this->registerDrapUploader(); |
||
| 224 | |||
| 225 | // Register the display thumbnails / show clipboard checkboxes |
||
| 226 | $this->registerFileListCheckboxes(); |
||
| 227 | |||
| 228 | // Register additional doc header buttons |
||
| 229 | $this->registerAdditionalDocHeaderButtons($request); |
||
| 230 | |||
| 231 | // Add additional view variables |
||
| 232 | $this->view->assignMultiple([ |
||
| 233 | 'headline' => $this->getModuleHeadline(), |
||
| 234 | 'folderIdentifier' => $this->folderObject->getCombinedIdentifier(), |
||
| 235 | 'searchTerm' => $this->searchTerm, |
||
| 236 | ]); |
||
| 237 | |||
| 238 | // Overwrite the default module title, adding the specific module headline (the folder name) |
||
| 239 | $this->moduleTemplate->setTitle( |
||
| 240 | $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:mlang_tabs_tab'), |
||
| 241 | $this->getModuleHeadline() |
||
| 242 | ); |
||
| 243 | |||
| 244 | // Additional doc header information: current path and folder info |
||
| 245 | $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation([ |
||
| 246 | '_additional_info' => $this->filelist->getFolderInfo(), |
||
| 247 | ]); |
||
| 248 | $this->moduleTemplate->getDocHeaderComponent()->setMetaInformationForResource($this->folderObject); |
||
| 249 | |||
| 250 | // Render view and return the response |
||
| 251 | return $this->htmlResponse( |
||
| 252 | $this->moduleTemplate->setContent($this->view->render())->renderContent() |
||
| 253 | ); |
||
| 254 | } |
||
| 255 | |||
| 256 | protected function initializeView(): void |
||
| 257 | { |
||
| 258 | $this->view = GeneralUtility::makeInstance(StandaloneView::class); |
||
| 259 | $this->view->setTemplateRootPaths(['EXT:filelist/Resources/Private/Templates/FileList']); |
||
| 260 | $this->view->setPartialRootPaths(['EXT:filelist/Resources/Private/Partials']); |
||
| 261 | $this->view->setLayoutRootPaths(['EXT:filelist/Resources/Private/Layouts']); |
||
| 262 | $this->view->setTemplatePathAndFilename( |
||
| 263 | GeneralUtility::getFileAbsFileName('EXT:filelist/Resources/Private/Templates/File/List.html') |
||
| 264 | ); |
||
| 265 | $this->view->assign('currentIdentifier', $this->id); |
||
| 266 | |||
| 267 | // @todo: These modules should be merged into one module |
||
| 268 | $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Filelist/FileListLocalisation'); |
||
| 269 | $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Filelist/FileList'); |
||
| 270 | $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Filelist/FileDelete'); |
||
| 271 | $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu'); |
||
| 272 | $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/ClipboardComponent'); |
||
| 273 | $this->pageRenderer->addInlineLanguageLabelFile( |
||
| 274 | 'EXT:backend/Resources/Private/Language/locallang_alt_doc.xlf', |
||
| 275 | 'buttons' |
||
| 276 | ); |
||
| 277 | } |
||
| 278 | |||
| 279 | protected function initializeModule(ServerRequestInterface $request): void |
||
| 280 | { |
||
| 281 | // Configure the "menu" - which is used internally to save the values of sorting, displayThumbs etc. |
||
| 282 | // Values NOT in this array will not be saved in the settings-array for the module. |
||
| 283 | $this->MOD_MENU = ['sort' => '', 'reverse' => '', 'displayThumbs' => '', 'clipBoard' => '']; |
||
| 284 | $this->MOD_SETTINGS = BackendUtility::getModuleData( |
||
| 285 | $this->MOD_MENU, |
||
| 286 | $request->getParsedBody()['SET'] ?? $request->getQueryParams()['SET'] ?? null, |
||
| 287 | 'file_list' |
||
| 288 | ); |
||
| 289 | |||
| 290 | $userTsConfig = $this->getBackendUser()->getTSConfig(); |
||
| 291 | |||
| 292 | // Set predefined value for DisplayThumbnails: |
||
| 293 | if (($userTsConfig['options.']['file_list.']['enableDisplayThumbnails'] ?? '') === 'activated') { |
||
| 294 | $this->MOD_SETTINGS['displayThumbs'] = true; |
||
| 295 | } elseif (($userTsConfig['options.']['file_list.']['enableDisplayThumbnails'] ?? '') === 'deactivated') { |
||
| 296 | $this->MOD_SETTINGS['displayThumbs'] = false; |
||
| 297 | } |
||
| 298 | // Set predefined value for Clipboard: |
||
| 299 | if (($userTsConfig['options.']['file_list.']['enableClipBoard'] ?? '') === 'activated') { |
||
| 300 | $this->MOD_SETTINGS['clipBoard'] = true; |
||
| 301 | } elseif (($userTsConfig['options.']['file_list.']['enableClipBoard'] ?? '') === 'deactivated') { |
||
| 302 | $this->MOD_SETTINGS['clipBoard'] = false; |
||
| 303 | } |
||
| 304 | if (!isset($this->MOD_SETTINGS['sort'])) { |
||
| 305 | // Set default sorting |
||
| 306 | $this->MOD_SETTINGS['sort'] = 'file'; |
||
| 307 | $this->MOD_SETTINGS['reverse'] = 0; |
||
| 308 | } |
||
| 309 | |||
| 310 | // Finally add the help button doc header button to the module |
||
| 311 | $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar(); |
||
| 312 | $cshButton = $buttonBar->makeHelpButton() |
||
| 313 | ->setModuleName('xMOD_csh_corebe') |
||
| 314 | ->setFieldName('filelist_module'); |
||
| 315 | $buttonBar->addButton($cshButton); |
||
| 316 | } |
||
| 317 | |||
| 318 | protected function initializeFileList(ServerRequestInterface $request): void |
||
| 373 | ); |
||
| 374 | } |
||
| 375 | |||
| 376 | protected function generateFileList(): void |
||
| 400 | ); |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | protected function registerDrapUploader(): void |
||
| 421 | ]); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | protected function registerFileListCheckboxes(): void |
||
| 426 | { |
||
| 427 | $lang = $this->getLanguageService(); |
||
| 428 | $userTsConfig = $this->getBackendUser()->getTSConfig(); |
||
| 429 | $addParams = ''; |
||
| 430 | |||
| 431 | if ($this->searchTerm) { |
||
| 432 | $addParams .= '&searchTerm=' . htmlspecialchars($this->searchTerm); |
||
| 433 | } |
||
| 434 | if ($this->pointer) { |
||
| 435 | $addParams .= '&pointer=' . $this->pointer; |
||
| 436 | } |
||
| 437 | |||
| 438 | $this->view->assign('displayThumbs', [ |
||
| 439 | 'enabled' => $GLOBALS['TYPO3_CONF_VARS']['GFX']['thumbnails'] && $userTsConfig['options.']['file_list.']['enableDisplayThumbnails'] === 'selectable', |
||
| 440 | 'label' => htmlspecialchars($lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:displayThumbs')), |
||
| 441 | 'html' => BackendUtility::getFuncCheck( |
||
| 442 | $this->id, |
||
| 443 | 'SET[displayThumbs]', |
||
| 444 | $this->MOD_SETTINGS['displayThumbs'] ?? '', |
||
| 445 | '', |
||
| 446 | $addParams, |
||
| 447 | 'id="checkDisplayThumbs"' |
||
| 448 | ) |
||
| 449 | ]); |
||
| 450 | $this->view->assign('enableClipBoard', [ |
||
| 451 | 'enabled' => $userTsConfig['options.']['file_list.']['enableClipBoard'] === 'selectable', |
||
| 452 | 'label' => htmlspecialchars($lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:clipBoard')), |
||
| 453 | 'html' => BackendUtility::getFuncCheck( |
||
| 454 | $this->id, |
||
| 455 | 'SET[clipBoard]', |
||
| 456 | $this->MOD_SETTINGS['clipBoard'] ?? '', |
||
| 457 | '', |
||
| 458 | $addParams, |
||
| 459 | 'id="checkClipBoard"' |
||
| 460 | ), |
||
| 461 | ]); |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Create the panel of buttons for submitting the form or otherwise perform operations. |
||
| 466 | */ |
||
| 467 | protected function registerAdditionalDocHeaderButtons(ServerRequestInterface $request): void |
||
| 468 | { |
||
| 469 | $lang = $this->getLanguageService(); |
||
| 470 | $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar(); |
||
| 471 | |||
| 472 | // Refresh |
||
| 473 | $refreshButton = $buttonBar->makeLinkButton() |
||
| 474 | ->setHref($request->getAttribute('normalizedParams')->getRequestUri()) |
||
| 475 | ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.reload')) |
||
| 476 | ->setIcon($this->iconFactory->getIcon('actions-refresh', Icon::SIZE_SMALL)); |
||
| 477 | $buttonBar->addButton($refreshButton, ButtonBar::BUTTON_POSITION_RIGHT); |
||
| 478 | |||
| 479 | // Level up |
||
| 480 | try { |
||
| 481 | $currentStorage = $this->folderObject->getStorage(); |
||
| 482 | $parentFolder = $this->folderObject->getParentFolder(); |
||
| 483 | if ($currentStorage->isWithinFileMountBoundaries($parentFolder) |
||
| 484 | && $parentFolder->getIdentifier() !== $this->folderObject->getIdentifier() |
||
| 485 | ) { |
||
| 486 | $levelUpButton = $buttonBar->makeLinkButton() |
||
| 487 | ->setDataAttributes([ |
||
| 488 | 'tree-update-request' => htmlspecialchars('folder' . GeneralUtility::md5int($parentFolder->getCombinedIdentifier())), |
||
| 489 | ]) |
||
| 490 | ->setHref( |
||
| 491 | (string)$this->uriBuilder->buildUriFromRoute( |
||
| 492 | 'file_FilelistList', |
||
| 493 | ['id' => $parentFolder->getCombinedIdentifier()] |
||
| 494 | ) |
||
| 495 | ) |
||
| 496 | ->setShowLabelText(true) |
||
| 497 | ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.upOneLevel')) |
||
| 498 | ->setIcon($this->iconFactory->getIcon('actions-view-go-up', Icon::SIZE_SMALL)); |
||
| 499 | $buttonBar->addButton($levelUpButton, ButtonBar::BUTTON_POSITION_LEFT, 1); |
||
| 500 | } |
||
| 501 | } catch (\Exception $e) { |
||
| 502 | } |
||
| 503 | |||
| 504 | // Shortcut |
||
| 505 | $shortCutButton = $buttonBar->makeShortcutButton() |
||
| 506 | ->setRouteIdentifier('file_FilelistList') |
||
| 507 | ->setDisplayName(sprintf( |
||
| 508 | '%s: %s', |
||
| 509 | $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:mlang_tabs_tab'), |
||
| 510 | $this->folderObject->getName() ?: $this->folderObject->getIdentifier() |
||
| 511 | )) |
||
| 512 | ->setArguments(array_filter([ |
||
| 513 | 'id' => $this->id, |
||
| 514 | 'searchTerm' => $this->searchTerm |
||
| 515 | ])); |
||
| 516 | $buttonBar->addButton($shortCutButton, ButtonBar::BUTTON_POSITION_RIGHT); |
||
| 517 | |||
| 518 | // Upload button (only if upload to this directory is allowed) |
||
| 519 | if ($this->folderObject |
||
| 520 | && $this->folderObject->checkActionPermission('write') |
||
| 521 | && $this->folderObject->getStorage()->checkUserActionPermission('add', 'File') |
||
| 522 | ) { |
||
| 523 | $uploadButton = $buttonBar->makeLinkButton() |
||
| 524 | ->setHref((string)$this->uriBuilder->buildUriFromRoute( |
||
| 525 | 'file_upload', |
||
| 526 | [ |
||
| 527 | 'target' => $this->folderObject->getCombinedIdentifier(), |
||
| 528 | 'returnUrl' => $this->filelist->listURL(), |
||
| 529 | ] |
||
| 530 | )) |
||
| 531 | ->setClasses('t3js-drag-uploader-trigger') |
||
| 532 | ->setShowLabelText(true) |
||
| 533 | ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.upload')) |
||
| 534 | ->setIcon($this->iconFactory->getIcon('actions-edit-upload', Icon::SIZE_SMALL)); |
||
| 535 | $buttonBar->addButton($uploadButton, ButtonBar::BUTTON_POSITION_LEFT, 2); |
||
| 536 | } |
||
| 537 | |||
| 538 | // New folder button |
||
| 539 | if ($this->folderObject && $this->folderObject->checkActionPermission('write') |
||
| 540 | && ($this->folderObject->getStorage()->checkUserActionPermission( |
||
| 541 | 'add', |
||
| 542 | 'File' |
||
| 543 | ) || $this->folderObject->checkActionPermission('add')) |
||
| 544 | ) { |
||
| 545 | $newButton = $buttonBar->makeLinkButton() |
||
| 546 | ->setHref((string)$this->uriBuilder->buildUriFromRoute( |
||
| 547 | 'file_newfolder', |
||
| 548 | [ |
||
| 549 | 'target' => $this->folderObject->getCombinedIdentifier(), |
||
| 550 | 'returnUrl' => $this->filelist->listURL(), |
||
| 551 | ] |
||
| 552 | )) |
||
| 553 | ->setShowLabelText(true) |
||
| 554 | ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.new')) |
||
| 555 | ->setIcon($this->iconFactory->getIcon('actions-add', Icon::SIZE_SMALL)); |
||
| 556 | $buttonBar->addButton($newButton, ButtonBar::BUTTON_POSITION_LEFT, 3); |
||
| 557 | } |
||
| 558 | |||
| 559 | // Add paste button if clipboard is initialized |
||
| 560 | if ($this->filelist->clipObj instanceof Clipboard && $this->folderObject->checkActionPermission('write')) { |
||
| 561 | $elFromTable = $this->filelist->clipObj->elFromTable('_FILE'); |
||
| 562 | if (!empty($elFromTable)) { |
||
| 563 | $addPasteButton = true; |
||
| 564 | $elToConfirm = []; |
||
| 565 | foreach ($elFromTable as $key => $element) { |
||
| 566 | $clipBoardElement = $this->resourceFactory->retrieveFileOrFolderObject($element); |
||
| 567 | if ($clipBoardElement instanceof Folder && $clipBoardElement->getStorage()->isWithinFolder( |
||
| 568 | $clipBoardElement, |
||
| 569 | $this->folderObject |
||
| 570 | ) |
||
| 571 | ) { |
||
| 572 | $addPasteButton = false; |
||
| 573 | } |
||
| 574 | $elToConfirm[$key] = $clipBoardElement->getName(); |
||
| 575 | } |
||
| 576 | if ($addPasteButton) { |
||
| 577 | $confirmText = $this->filelist->clipObj |
||
| 578 | ->confirmMsgText('_FILE', $this->folderObject->getReadablePath(), 'into', $elToConfirm); |
||
| 579 | $pastButtonTitle = $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:clip_paste'); |
||
| 580 | $pasteButton = $buttonBar->makeLinkButton() |
||
| 581 | ->setHref($this->filelist->clipObj |
||
| 582 | ->pasteUrl('_FILE', $this->folderObject->getCombinedIdentifier())) |
||
| 583 | ->setClasses('t3js-modal-trigger') |
||
| 584 | ->setDataAttributes([ |
||
| 585 | 'severity' => 'warning', |
||
| 586 | 'bs-content' => $confirmText, |
||
| 587 | 'title' => $pastButtonTitle |
||
| 588 | ]) |
||
| 589 | ->setShowLabelText(true) |
||
| 590 | ->setTitle($pastButtonTitle) |
||
| 591 | ->setIcon($this->iconFactory->getIcon('actions-document-paste-into', Icon::SIZE_SMALL)); |
||
| 592 | $buttonBar->addButton($pasteButton, ButtonBar::BUTTON_POSITION_LEFT, 4); |
||
| 593 | } |
||
| 594 | } |
||
| 595 | } |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Get main headline based on active folder or storage for backend module |
||
| 600 | * Folder names are resolved to their special names like done in the tree view. |
||
| 601 | * |
||
| 602 | * @return string |
||
| 603 | */ |
||
| 604 | protected function getModuleHeadline(): string |
||
| 618 | } |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Return the default duplication behaviour action, set in TSconfig |
||
| 622 | * |
||
| 623 | * @return string |
||
| 624 | */ |
||
| 625 | protected function getDefaultDuplicationBehaviourAction(): string |
||
| 626 | { |
||
| 627 | $defaultAction = $this->getBackendUser()->getTSConfig() |
||
| 628 | ['options.']['file_list.']['uploader.']['defaultAction'] ?? ''; |
||
| 629 | |||
| 630 | if ($defaultAction === '') { |
||
| 631 | return DuplicationBehavior::CANCEL; |
||
| 632 | } |
||
| 633 | |||
| 634 | if (!in_array($defaultAction, [ |
||
| 635 | DuplicationBehavior::REPLACE, |
||
| 636 | DuplicationBehavior::RENAME, |
||
| 637 | DuplicationBehavior::CANCEL |
||
| 638 | ], true)) { |
||
| 639 | $this->logger->warning('TSConfig: options.file_list.uploader.defaultAction contains an invalid value ("{value}"), fallback to default value: "{default}"', [ |
||
| 640 | 'value' => $defaultAction, |
||
| 641 | 'default' => DuplicationBehavior::CANCEL |
||
| 642 | ]); |
||
| 643 | $defaultAction = DuplicationBehavior::CANCEL; |
||
| 644 | } |
||
| 645 | return $defaultAction; |
||
| 646 | } |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Generate a response by either the given $html or by rendering the module content. |
||
| 650 | * |
||
| 651 | * @param string $html |
||
| 652 | * @return ResponseInterface |
||
| 653 | */ |
||
| 654 | protected function htmlResponse(string $html): ResponseInterface |
||
| 662 | } |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Adds a flash message to the default flash message queue |
||
| 666 | * |
||
| 667 | * @param string $message |
||
| 668 | * @param string $title |
||
| 669 | * @param int $severity |
||
| 670 | */ |
||
| 671 | protected function addFlashMessage(string $message, string $title = '', int $severity = FlashMessage::INFO): void |
||
| 672 | { |
||
| 673 | $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $message, $title, $severity, true); |
||
| 674 | $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class); |
||
| 675 | $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier(); |
||
| 676 | $defaultFlashMessageQueue->enqueue($flashMessage); |
||
| 677 | } |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Returns an instance of LanguageService |
||
| 681 | * |
||
| 682 | * @return LanguageService |
||
| 683 | */ |
||
| 684 | protected function getLanguageService(): LanguageService |
||
| 687 | } |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Returns the current BE user. |
||
| 691 | * |
||
| 692 | * @return BackendUserAuthentication |
||
| 693 | */ |
||
| 694 | protected function getBackendUser(): BackendUserAuthentication |
||
| 697 | } |
||
| 698 | } |
||
| 699 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.