From 7fd874e63b1cba09c01956ea9ce4cc1b58f35ee4 Mon Sep 17 00:00:00 2001 From: Daniel Windloff Date: Sat, 5 Sep 2020 09:52:04 +0200 Subject: [PATCH] [TASK] RecordListController: Merge init() and main() to mainAction() Merge the methods `init()` and `main()` to the `mainAction()` method. Remove unnecessary properties, which were only used to transfer values between these methods. Initialize `siteLanguages` with the current page uid. Releases: master Resolves: #92308 Resolves: #90966 Change-Id: Iafd45ebeb06e8c61dac9e9ed865e40bc999ab54a Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/65739 Tested-by: TYPO3com Tested-by: Anja Leichsenring Tested-by: Christian Kuhn Reviewed-by: Anja Leichsenring Reviewed-by: Christian Kuhn --- .../Controller/RecordListController.php | 376 +++++------------- 1 file changed, 110 insertions(+), 266 deletions(-) diff --git a/typo3/sysext/recordlist/Classes/Controller/RecordListController.php b/typo3/sysext/recordlist/Classes/Controller/RecordListController.php index 2cfe3c7bd2b5..2f58f2943381 100644 --- a/typo3/sysext/recordlist/Classes/Controller/RecordListController.php +++ b/typo3/sysext/recordlist/Classes/Controller/RecordListController.php @@ -34,7 +34,6 @@ use TYPO3\CMS\Core\Localization\LanguageService; use TYPO3\CMS\Core\Messaging\FlashMessage; use TYPO3\CMS\Core\Messaging\FlashMessageService; -use TYPO3\CMS\Core\Page\PageRenderer; use TYPO3\CMS\Core\Site\Entity\SiteLanguage; use TYPO3\CMS\Core\Type\Bitmask\Permission; use TYPO3\CMS\Core\TypoScript\TypoScriptService; @@ -48,116 +47,6 @@ */ class RecordListController { - /** - * Page Id for which to make the listing - * - * @var int - */ - protected $id; - - /** - * Pointer - for browsing list of records. - * - * @var int - */ - protected $pointer; - - /** - * Which table to make extended listing for - * - * @var string - */ - protected $table; - - /** - * Search-fields - * - * @var string - */ - protected $search_field; - - /** - * Search-levels - * - * @var int - */ - protected $search_levels; - - /** - * Show-limit - * - * @var int - */ - protected $showLimit; - - /** - * Return URL - * - * @var string - */ - protected $returnUrl; - - /** - * Command: Eg. "delete" or "setCB" (for DataHandler / clipboard operations) - * - * @var string - */ - protected $cmd; - - /** - * Table on which the cmd-action is performed. - * - * @var string - */ - protected $cmd_table; - - /** - * Page select perms clause - * - * @var int - */ - protected $perms_clause; - - /** - * Module TSconfig - * - * @var array - * @internal - */ - protected $modTSconfig; - - /** - * Current ids page record - * - * @var mixed[]|bool - */ - protected $pageinfo; - - /** - * Menu configuration - * - * @var string[] - */ - protected $MOD_MENU = []; - - /** - * Module settings (session variable) - * - * @var string[] - * @internal - */ - protected $MOD_SETTINGS = []; - - /** - * @var string - */ - protected $body = ''; - - /** - * @var PageRenderer - */ - protected $pageRenderer; - /** * @var IconFactory */ @@ -180,126 +69,118 @@ class RecordListController */ protected $eventDispatcher; + /** + * @var UriBuilder + */ + protected $uriBuilder; + /** * Constructor */ - public function __construct(IconFactory $iconFactory, ModuleTemplate $moduleTemplate, EventDispatcherInterface $eventDispatcher) + public function __construct(IconFactory $iconFactory, ModuleTemplate $moduleTemplate, EventDispatcherInterface $eventDispatcher, UriBuilder $uriBuilder) { $this->moduleTemplate = $moduleTemplate; $this->getLanguageService()->includeLLFile('EXT:core/Resources/Private/Language/locallang_mod_web_list.xlf'); $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Recordlist/FieldSelectBox'); $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Recordlist/Recordlist'); $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Recordlist/ClearCache'); + $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/AjaxDataHandler'); + $this->moduleTemplate->getPageRenderer()->addInlineLanguageLabelFile('EXT:core/Resources/Private/Language/locallang_mod_web_list.xlf'); + $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Recordlist/Tooltip'); + $this->iconFactory = $iconFactory; $this->eventDispatcher = $eventDispatcher; + $this->uriBuilder = $uriBuilder; } /** - * Initializing the module + * Injects the request object for the current request or subrequest * - * @param ServerRequestInterface $request + * @param ServerRequestInterface $request the current request + * @return ResponseInterface the response with the content */ - protected function init(ServerRequestInterface $request): void + public function mainAction(ServerRequestInterface $request): ResponseInterface { + // init + BackendUtility::lockRecords(); $parsedBody = $request->getParsedBody(); $queryParams = $request->getQueryParams(); $backendUser = $this->getBackendUserAuthentication(); - $this->perms_clause = $backendUser->getPagePermsClause(Permission::PAGE_SHOW); - // Get session data - $sessionData = $backendUser->getSessionData(__CLASS__); - $this->search_field = !empty($sessionData['search_field']) ? $sessionData['search_field'] : ''; + $perms_clause = $backendUser->getPagePermsClause(Permission::PAGE_SHOW); // GPvars: - $this->id = (int)($parsedBody['id'] ?? $queryParams['id'] ?? 0); - $this->pointer = max(0, (int)($parsedBody['pointer'] ?? $queryParams['pointer'] ?? 0)); - $this->table = (string)($parsedBody['table'] ?? $queryParams['table'] ?? ''); - $this->search_field = (string)($parsedBody['search_field'] ?? $queryParams['search_field'] ?? ''); - $this->search_levels = (int)($parsedBody['search_levels'] ?? $queryParams['search_levels'] ?? 0); - $this->showLimit = (int)($parsedBody['showLimit'] ?? $queryParams['showLimit'] ?? 0); - $this->returnUrl = GeneralUtility::sanitizeLocalUrl((string)($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '')); - $this->cmd = (string)($parsedBody['cmd'] ?? $queryParams['cmd'] ?? ''); - $this->cmd_table = (string)($parsedBody['cmd_table'] ?? $queryParams['cmd_table'] ?? ''); - $sessionData['search_field'] = $this->search_field; - // Initialize menu - $this->MOD_MENU = [ - 'bigControlPanel' => '', - 'clipBoard' => '', - ]; + $id = (int)($parsedBody['id'] ?? $queryParams['id'] ?? 0); + $pointer = max(0, (int)($parsedBody['pointer'] ?? $queryParams['pointer'] ?? 0)); + $table = (string)($parsedBody['table'] ?? $queryParams['table'] ?? ''); + $search_field = (string)($parsedBody['search_field'] ?? $queryParams['search_field'] ?? ''); + $search_levels = (int)($parsedBody['search_levels'] ?? $queryParams['search_levels'] ?? 0); + $showLimit = (int)($parsedBody['showLimit'] ?? $queryParams['showLimit'] ?? 0); + $returnUrl = GeneralUtility::sanitizeLocalUrl((string)($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '')); + $cmd = (string)($parsedBody['cmd'] ?? $queryParams['cmd'] ?? ''); + $cmd_table = (string)($parsedBody['cmd_table'] ?? $queryParams['cmd_table'] ?? ''); + // Set site languages + $site = $request->getAttribute('site'); + $this->siteLanguages = $site->getAvailableLanguages($this->getBackendUserAuthentication(), false, $id); // Loading module configuration: - $this->modTSconfig['properties'] = BackendUtility::getPagesTSconfig($this->id)['mod.']['web_list.'] ?? []; + $modTSconfig['properties'] = BackendUtility::getPagesTSconfig($id)['mod.']['web_list.'] ?? []; // Clean up settings: - $this->MOD_SETTINGS = BackendUtility::getModuleData($this->MOD_MENU, (array)($parsedBody['SET'] ?? $queryParams['SET'] ?? []), 'web_list'); - // Store session data - $backendUser->setAndSaveSessionData(self::class, $sessionData); - $this->getPageRenderer()->addInlineLanguageLabelFile('EXT:core/Resources/Private/Language/locallang_mod_web_list.xlf'); - } - - /** - * Main function, starting the rendering of the list. - * - * @param ServerRequestInterface $request - * @return string - */ - protected function main(ServerRequestInterface $request): string - { + $MOD_SETTINGS = BackendUtility::getModuleData(['bigControlPanel' => '', 'clipBoard' => ''], (array)($parsedBody['SET'] ?? $queryParams['SET'] ?? []), 'web_list'); + // main $backendUser = $this->getBackendUserAuthentication(); $lang = $this->getLanguageService(); // Loading current page record and checking access: - $this->pageinfo = BackendUtility::readPageAccess($this->id, $this->perms_clause); - $access = is_array($this->pageinfo); + $pageinfo = BackendUtility::readPageAccess($id, $perms_clause); + $access = is_array($pageinfo); - $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/AjaxDataHandler'); - $calcPerms = new Permission($backendUser->calcPerms($this->pageinfo)); - $userCanEditPage = $calcPerms->editPagePermissionIsGranted() && !empty($this->id) && ($backendUser->isAdmin() || (int)$this->pageinfo['editlock'] === 0); + $calcPerms = new Permission($backendUser->calcPerms($pageinfo)); + $userCanEditPage = $calcPerms->editPagePermissionIsGranted() && !empty($id) && ($backendUser->isAdmin() || (int)$pageinfo['editlock'] === 0); $pageActionsCallback = null; if ($userCanEditPage) { $pageActionsCallback = 'function(PageActions) { - PageActions.setPageId(' . (int)$this->id . '); + PageActions.setPageId(' . (int)$id . '); }'; } - $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/PageActions', $pageActionsCallback); - $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Recordlist/Tooltip'); + $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/PageActions', $pageActionsCallback); // Apply predefined values for hidden checkboxes // Set predefined value for DisplayBigControlPanel: - if ($this->modTSconfig['properties']['enableDisplayBigControlPanel'] === 'activated') { - $this->MOD_SETTINGS['bigControlPanel'] = true; - } elseif ($this->modTSconfig['properties']['enableDisplayBigControlPanel'] === 'deactivated') { - $this->MOD_SETTINGS['bigControlPanel'] = false; + if ($modTSconfig['properties']['enableDisplayBigControlPanel'] === 'activated') { + $MOD_SETTINGS['bigControlPanel'] = true; + } elseif ($modTSconfig['properties']['enableDisplayBigControlPanel'] === 'deactivated') { + $MOD_SETTINGS['bigControlPanel'] = false; } // Set predefined value for Clipboard: - if ($this->modTSconfig['properties']['enableClipBoard'] === 'activated') { - $this->MOD_SETTINGS['clipBoard'] = true; - } elseif ($this->modTSconfig['properties']['enableClipBoard'] === 'deactivated') { - $this->MOD_SETTINGS['clipBoard'] = false; + if ($modTSconfig['properties']['enableClipBoard'] === 'activated') { + $MOD_SETTINGS['clipBoard'] = true; + } elseif ($modTSconfig['properties']['enableClipBoard'] === 'deactivated') { + $MOD_SETTINGS['clipBoard'] = false; } else { - if ($this->MOD_SETTINGS['clipBoard'] === null) { - $this->MOD_SETTINGS['clipBoard'] = true; + if ($MOD_SETTINGS['clipBoard'] === null) { + $MOD_SETTINGS['clipBoard'] = true; } } // Initialize the dblist object: $dblist = GeneralUtility::makeInstance(DatabaseRecordList::class); - $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); - $dblist->setModuleData($this->MOD_SETTINGS ?? []); + $dblist->setModuleData($MOD_SETTINGS ?? []); $dblist->calcPerms = $calcPerms; - $dblist->returnUrl = $this->returnUrl; - $dblist->allFields = $this->MOD_SETTINGS['bigControlPanel'] || $this->table ? 1 : 0; - $dblist->showClipboard = 1; - $dblist->disableSingleTableView = $this->modTSconfig['properties']['disableSingleTableView']; - $dblist->listOnlyInSingleTableMode = $this->modTSconfig['properties']['listOnlyInSingleTableView']; - $dblist->hideTables = $this->modTSconfig['properties']['hideTables']; - $dblist->hideTranslations = $this->modTSconfig['properties']['hideTranslations']; - $dblist->tableTSconfigOverTCA = $this->modTSconfig['properties']['table.']; - $dblist->allowedNewTables = GeneralUtility::trimExplode(',', $this->modTSconfig['properties']['allowedNewTables'], true); - $dblist->deniedNewTables = GeneralUtility::trimExplode(',', $this->modTSconfig['properties']['deniedNewTables'], true); - $dblist->pageRow = $this->pageinfo; + $dblist->returnUrl = $returnUrl; + $dblist->allFields = $MOD_SETTINGS['bigControlPanel'] || $table ? 1 : 0; + $dblist->showClipboard = true; + $dblist->disableSingleTableView = $modTSconfig['properties']['disableSingleTableView']; + $dblist->listOnlyInSingleTableMode = $modTSconfig['properties']['listOnlyInSingleTableView']; + $dblist->hideTables = $modTSconfig['properties']['hideTables']; + $dblist->hideTranslations = $modTSconfig['properties']['hideTranslations']; + $dblist->tableTSconfigOverTCA = $modTSconfig['properties']['table.']; + $dblist->allowedNewTables = GeneralUtility::trimExplode(',', $modTSconfig['properties']['allowedNewTables'], true); + $dblist->deniedNewTables = GeneralUtility::trimExplode(',', $modTSconfig['properties']['deniedNewTables'], true); + $dblist->pageRow = $pageinfo; $dblist->MOD_MENU = ['bigControlPanel' => '', 'clipBoard' => '']; - $dblist->modTSconfig = $this->modTSconfig; + $dblist->modTSconfig = $modTSconfig; $dblist->setLanguagesAllowedForUser($this->siteLanguages); - $clickTitleMode = trim($this->modTSconfig['properties']['clickTitleMode']); + $clickTitleMode = trim($modTSconfig['properties']['clickTitleMode']); $dblist->clickTitleMode = $clickTitleMode === '' ? 'edit' : $clickTitleMode; - if (isset($this->modTSconfig['properties']['tableDisplayOrder.'])) { + if (isset($modTSconfig['properties']['tableDisplayOrder.'])) { $typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class); - $dblist->setTableDisplayOrder($typoScriptService->convertTypoScriptArrayToPlainArray($this->modTSconfig['properties']['tableDisplayOrder.'])); + $dblist->setTableDisplayOrder($typoScriptService->convertTypoScriptArrayToPlainArray($modTSconfig['properties']['tableDisplayOrder.'])); } // Clipboard is initialized: // Start clipboard @@ -308,14 +189,14 @@ protected function main(ServerRequestInterface $request): string $dblist->clipObj->initializeClipboard(); // Clipboard actions are handled: // CB is the clipboard command array - $CB = GeneralUtility::_GET('CB'); - if ($this->cmd === 'setCB') { + $CB = $queryParams['CB'] ?? []; + if ($cmd === 'setCB') { // CBH is all the fields selected for the clipboard, CBC is the checkbox fields which were checked. // By merging we get a full array of checked/unchecked elements // This is set to the 'el' array of the CB after being parsed so only the table in question is registered. - $CB['el'] = $dblist->clipObj->cleanUpCBC(array_merge(GeneralUtility::_POST('CBH'), (array)GeneralUtility::_POST('CBC')), $this->cmd_table); + $CB['el'] = $dblist->clipObj->cleanUpCBC(array_merge($parsedBody['CBH'] ?? [], (array)($parsedBody['CBC'] ?? [])), $cmd_table); } - if (!$this->MOD_SETTINGS['clipBoard']) { + if (!$MOD_SETTINGS['clipBoard']) { // If the clipboard is NOT shown, set the pad to 'normal'. $CB['setP'] = 'normal'; } @@ -327,14 +208,14 @@ protected function main(ServerRequestInterface $request): string $dblist->clipObj->endClipboard(); // This flag will prevent the clipboard panel in being shown. // It is set, if the clickmenu-layer is active AND the extended view is not enabled. - $dblist->dontShowClipControlPanels = ($dblist->clipObj->current === 'normal' && !$this->modTSconfig['properties']['showClipControlPanelsDespiteOfCMlayers']); + $dblist->dontShowClipControlPanels = ($dblist->clipObj->current === 'normal' && !$modTSconfig['properties']['showClipControlPanelsDespiteOfCMlayers']); // If there is access to the page or root page is used for searching, then render the list contents and set up the document template object: $tableOutput = ''; - if ($access || ($this->id === 0 && $this->search_levels !== 0 && $this->search_field !== '')) { + if ($access || ($id === 0 && $search_levels !== 0 && $search_field !== '')) { // Deleting records...: // Has not to do with the clipboard but is simply the delete action. The clipboard object is used to clean up the submitted entries to only the selected table. - if ($this->cmd === 'delete') { - $items = $dblist->clipObj->cleanUpCBC(GeneralUtility::_POST('CBC'), $this->cmd_table, 1); + if ($cmd === 'delete') { + $items = $dblist->clipObj->cleanUpCBC($parsedBody['CBC'] ?? [], $cmd_table, 1); if (!empty($items)) { $cmd = []; foreach ($items as $iK => $value) { @@ -351,14 +232,13 @@ protected function main(ServerRequestInterface $request): string } } // Initialize the listing object, dblist, for rendering the list: - $dblist->start($this->id, $this->table, $this->pointer, $this->search_field, $this->search_levels, $this->showLimit); + $dblist->start($id, $table, $pointer, $search_field, $search_levels, $showLimit); $dblist->setDispFields(); // Render the list of tables: $tableOutput = $dblist->generateList(); - $listUrl = $dblist->listURL(); // Add JavaScript functions to the page: - $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/ClipboardComponent'); + $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ClipboardComponent'); $this->moduleTemplate->addJavaScriptCode( 'RecordListInlineJS', @@ -372,10 +252,10 @@ function setHighlight(id) { } } function editRecords(table,idList,addParams,CBflag) { - window.location.href="' . (string)$uriBuilder->buildUriFromRoute('record_edit', ['returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')]) . '&edit["+table+"]["+idList+"]=edit"+addParams; + window.location.href="' . (string)$this->uriBuilder->buildUriFromRoute('record_edit', ['returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')]) . '&edit["+table+"]["+idList+"]=edit"+addParams; } - if (top.fsMod) top.fsMod.recentIds["web"] = ' . (int)$this->id . '; + if (top.fsMod) top.fsMod.recentIds["web"] = ' . (int)$id . '; ' ); @@ -384,41 +264,42 @@ function editRecords(table,idList,addParams,CBflag) { } // access // Begin to compile the whole page, starting out with page header: - if (!$this->id) { + if (!$id) { $title = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']; } else { - $title = $this->pageinfo['title']; + $title = $pageinfo['title']; } - $this->body = $this->moduleTemplate->header($title); + $body = $this->moduleTemplate->header($title); // Additional header content /** @var RenderAdditionalContentToRecordListEvent $additionalRecordListEvent */ $additionalRecordListEvent = $this->eventDispatcher->dispatch(new RenderAdditionalContentToRecordListEvent($request)); - $this->body .= $additionalRecordListEvent->getAdditionalContentAbove(); + $body .= $additionalRecordListEvent->getAdditionalContentAbove(); $this->moduleTemplate->setTitle($title); $output = ''; // Show the selector to add page translations and the list of translations of the current page // but only when in "default" mode - if ($this->id && !$dblist->csvOutput && !$this->search_field && !$this->cmd && !$this->table) { - $output .= $this->languageSelector($this->id); + if ($id && !$dblist->csvOutput && !$search_field && !$cmd && !$table) { + $output .= $this->languageSelector($id); $pageTranslationsDatabaseRecordList = clone $dblist; $pageTranslationsDatabaseRecordList->listOnlyInSingleTableMode = false; $pageTranslationsDatabaseRecordList->disableSingleTableView = true; $pageTranslationsDatabaseRecordList->deniedNewTables = ['pages']; $pageTranslationsDatabaseRecordList->hideTranslations = ''; + $pageTranslationsDatabaseRecordList->setLanguagesAllowedForUser($this->siteLanguages); $pageTranslationsDatabaseRecordList->showOnlyTranslatedRecords(true); - $output .= $pageTranslationsDatabaseRecordList->getTable('pages', $this->id); + $output .= $pageTranslationsDatabaseRecordList->getTable('pages', $id); } if (!empty($tableOutput)) { $output .= $tableOutput; } else { - if (isset($this->table, $GLOBALS['TCA'][$this->table]['ctrl']['title'])) { - if (strpos($GLOBALS['TCA'][$this->table]['ctrl']['title'], 'LLL:') === 0) { - $ll = sprintf($lang->getLL('noRecordsOfTypeOnThisPage'), $lang->sL($GLOBALS['TCA'][$this->table]['ctrl']['title'])); + if (isset($GLOBALS['TCA'][$table]['ctrl']['title'])) { + if (strpos($GLOBALS['TCA'][$table]['ctrl']['title'], 'LLL:') === 0) { + $ll = sprintf($lang->getLL('noRecordsOfTypeOnThisPage'), $lang->sL($GLOBALS['TCA'][$table]['ctrl']['title'])); } else { - $ll = sprintf($lang->getLL('noRecordsOfTypeOnThisPage'), $GLOBALS['TCA'][$this->table]['ctrl']['title']); + $ll = sprintf($lang->getLL('noRecordsOfTypeOnThisPage'), $GLOBALS['TCA'][$table]['ctrl']['title']); } } else { $ll = $lang->getLL('noRecordsOnThisPage'); @@ -437,17 +318,17 @@ function editRecords(table,idList,addParams,CBflag) { $defaultFlashMessageQueue->enqueue($flashMessage); } - $this->body .= '
'; - $this->body .= $output; - $this->body .= '
'; + $body .= '
'; + $body .= $output; + $body .= '
'; // If a listing was produced, create the page footer with search form etc: if ($tableOutput) { // Making field select box (when extended view for a single table is enabled): if ($dblist->table) { - $this->body .= $dblist->fieldSelectBox($dblist->table); + $body .= $dblist->fieldSelectBox($dblist->table); } // Adding checkbox options for extended listing and clipboard display: - $this->body .= ' + $body .= '