From c0e5ef0b70cfe9c83eeb7df65ad6772e578c0ccb Mon Sep 17 00:00:00 2001 From: Niklas Grieger Date: Mon, 27 Feb 2023 15:22:28 +0100 Subject: [PATCH 1/3] [BUGFIX] Make backend module work in TYPO3 v11 and refactor its pagination --- Classes/Controller/LogController.php | 43 ++++-- Resources/Private/Layouts/Default.html | 3 +- .../Private/Partials/Backend/Filter.html | 80 +++++------ Resources/Private/Partials/Backend/View.html | 135 +++++++++++++----- 4 files changed, 171 insertions(+), 90 deletions(-) diff --git a/Classes/Controller/LogController.php b/Classes/Controller/LogController.php index add47ee..9633951 100644 --- a/Classes/Controller/LogController.php +++ b/Classes/Controller/LogController.php @@ -22,6 +22,8 @@ use TYPO3\CMS\Backend\View\BackendTemplateView; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Page\PageRenderer; +use TYPO3\CMS\Core\Pagination\ArrayPaginator; +use TYPO3\CMS\Core\Pagination\SimplePagination; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException; @@ -58,27 +60,41 @@ public function initializeAction(): void } if ($this->request->hasArgument('reset') && (bool)$this->request->getArgument('reset') === true) { - $GLOBALS['BE_USER']->setSessionData(self::FILTER_SESSION_KEY, null); + $GLOBALS['BE_USER']->setSessionData(self::FILTER_SESSION_KEY, serialize(new Filter())); + } + + if ($GLOBALS['BE_USER']->getSessionData(self::FILTER_SESSION_KEY) === null) { + $GLOBALS['BE_USER']->setSessionData(self::FILTER_SESSION_KEY, serialize(new Filter())); } } /** * @param Filter|null $filter The filter object */ - public function listAction(Filter $filter = null): void + public function listAction(?Filter $filter = null): void { - $filter = $filter ?? $GLOBALS['BE_USER']->getSessionData(self::FILTER_SESSION_KEY) ?? (new Filter()); + $filter = $filter ?? unserialize($GLOBALS['BE_USER']->getSessionData(self::FILTER_SESSION_KEY)) ?? (new Filter()); + $filter->setPageId(0); $logEntries = $this->logRepository->findByFilter($filter); // Store filter data in session of backend user (used for pagination) - $GLOBALS['BE_USER']->setSessionData(self::FILTER_SESSION_KEY, $filter); + $GLOBALS['BE_USER']->setSessionData(self::FILTER_SESSION_KEY, serialize($filter)); + + $itemsPerPage = 20; + $currentPage = GeneralUtility::_GP('currentPage') ? (int)GeneralUtility::_GP('currentPage') : 1; + + $paginator = new ArrayPaginator($logEntries->toArray(), $currentPage, $itemsPerPage); + $pagination = new SimplePagination($paginator); $this->view->assignMultiple([ - 'logs' => $logEntries, + 'logs' => $paginator->getPaginatedItems(), 'users' => $this->getUsers(), 'fileTypes' => $this->getFileTypes(), 'filter' => $filter, 'statistic' => new Statistic($logEntries), + 'paginator' => $paginator, + 'pagination' => $pagination, + 'totalResultCount' => count($logEntries), ]); } @@ -119,7 +135,7 @@ private function getFileTypes(): array * @param Filter|null $filter The filter object * @throws StopActionException */ - public function showAction(Filter $filter = null): void + public function showAction(?Filter $filter = null): void { $pageId = (int)GeneralUtility::_GP('id'); @@ -127,20 +143,29 @@ public function showAction(Filter $filter = null): void $this->redirect('list'); } - $filter = $filter ?? $GLOBALS['BE_USER']->getSessionData('filter') ?? (new Filter()); + $filter = $filter ?? unserialize($GLOBALS['BE_USER']->getSessionData(self::FILTER_SESSION_KEY)) ?? (new Filter()); $filter->setPageId($pageId); $logEntries = $this->logRepository->findByFilter($filter); // Store filter data in session of backend user (used for pagination) - $GLOBALS['BE_USER']->setSessionData('filter', $filter); + $GLOBALS['BE_USER']->setSessionData(self::FILTER_SESSION_KEY, serialize($filter)); + + $itemsPerPage = 20; + $currentPage = GeneralUtility::_GP('currentPage') ? (int)GeneralUtility::_GP('currentPage') : 1; + + $paginator = new ArrayPaginator($logEntries->toArray(), $currentPage, $itemsPerPage); + $pagination = new SimplePagination($paginator); $this->view->assignMultiple([ - 'logs' => $logEntries, + 'logs' => $paginator->getPaginatedItems(), 'page' => BackendUtility::getRecord('pages', $pageId), 'users' => $this->getUsers(), 'fileTypes' => $this->getFileTypes(), 'filter' => $filter, 'statistic' => new Statistic($logEntries), + 'paginator' => $paginator, + 'pagination' => $pagination, + 'totalResultCount' => count($logEntries), ]); } diff --git a/Resources/Private/Layouts/Default.html b/Resources/Private/Layouts/Default.html index 2512f8e..7600649 100644 --- a/Resources/Private/Layouts/Default.html +++ b/Resources/Private/Layouts/Default.html @@ -6,12 +6,11 @@
- {logs -> f:count() -> f:format.number(decimals: 0)} {statistic.traffic -> f:format.bytes()} {statistic.from -> f:format.date(format: '{f:translate(key: \'dateformat\')}')} {statistic.till -> f:format.date(format: '{f:translate(key: \'dateformat\')}')} - {f:translate(key: 'module.subheading', arguments: '{0: count, 1: traffic, 2: from, 3: till}') -> f:format.html()} + {f:translate(key: 'module.subheading', arguments: '{0: totalResultCount, 1: traffic, 2: from, 3: till}') -> f:format.html()}
diff --git a/Resources/Private/Partials/Backend/Filter.html b/Resources/Private/Partials/Backend/Filter.html index 6c6e4f8..ec892c2 100644 --- a/Resources/Private/Partials/Backend/Filter.html +++ b/Resources/Private/Partials/Backend/Filter.html @@ -1,67 +1,61 @@ - -
- -
-
- -
+ +
+
+ +
- - -
-
- -
+
+ +
-
-