From 463237ef5a59447aca10b70252821bbb98090c1b Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Wed, 11 Sep 2024 15:04:28 +0200 Subject: [PATCH] Add abstract implementation for DBO lists --- com.woltlab.wcf/templates/shared_gridView.tpl | 64 ++++++++------- .../files/acp/templates/userRankList.tpl | 78 +------------------ .../lib/acp/page/UserRankListPage.class.php | 52 ++----------- .../lib/page/AbstractGridViewPage.class.php | 72 +++++++++++++++++ .../core/gridViews/GetRows.class.php | 3 +- .../view/grid/AbstractGridView.class.php | 35 ++++++--- .../grid/DatabaseObjectListGridView.class.php | 66 ++++++++++++++++ .../view/grid/UserRankGridView.class.php | 25 +----- 8 files changed, 212 insertions(+), 183 deletions(-) create mode 100644 wcfsetup/install/files/lib/page/AbstractGridViewPage.class.php create mode 100644 wcfsetup/install/files/lib/system/view/grid/DatabaseObjectListGridView.class.php diff --git a/com.woltlab.wcf/templates/shared_gridView.tpl b/com.woltlab.wcf/templates/shared_gridView.tpl index 8957dad2ac..328d58e383 100644 --- a/com.woltlab.wcf/templates/shared_gridView.tpl +++ b/com.woltlab.wcf/templates/shared_gridView.tpl @@ -1,33 +1,37 @@ -
- -
+{if $view->countRows()} +
+ +
-
- - - - {unsafe:$view->renderHeader()} - - - - {unsafe:$view->renderRows()} - -
-
+
+ + + + {unsafe:$view->renderHeader()} + + + + {unsafe:$view->renderRows()} + +
+
-
- -
+
+ +
- + +{else} + {lang}wcf.global.noItems{/lang} +{/if} diff --git a/wcfsetup/install/files/acp/templates/userRankList.tpl b/wcfsetup/install/files/acp/templates/userRankList.tpl index 8fabc66d57..e4917f8cf8 100644 --- a/wcfsetup/install/files/acp/templates/userRankList.tpl +++ b/wcfsetup/install/files/acp/templates/userRankList.tpl @@ -19,7 +19,7 @@
-

{lang}wcf.acp.user.rank.list{/lang} {#$items}

+

{lang}wcf.acp.user.rank.list{/lang} {#$gridView->countRows()}

-{hascontent} -
- {content}{pages print=true assign=pagesLinks controller="UserRankList" link="pageNo=%d&sortField=$sortField&sortOrder=$sortOrder"}{/content} -
-{/hascontent} - -{if $objects|count} - {unsafe:$view->render()} - - {*
- - - - - - - - - - - {event name='columnHeads'} - - - - - {foreach from=$objects item=userRank} - - - - - - - - - - {event name='columns'} - - {/foreach} - -
{lang}wcf.global.objectID{/lang}{lang}wcf.acp.user.rank.title{/lang}{lang}wcf.acp.user.rank.image{/lang}{lang}wcf.user.group{/lang}{lang}wcf.user.option.gender{/lang}{lang}wcf.acp.user.rank.requiredPoints{/lang}
- {icon name='pencil'} - {objectAction action="delete" objectTitle=$userRank->getTitle()} - - {event name='rowButtons'} - {@$userRank->rankID}{$userRank->getTitle()}{if $userRank->rankImage}{@$userRank->getImage()}{/if}{$userRank->groupName|phrase} - {if $userRank->requiredGender} - {if $userRank->requiredGender == 1} - {lang}wcf.user.gender.male{/lang} - {elseif $userRank->requiredGender == 2} - {lang}wcf.user.gender.female{/lang} - {else} - {lang}wcf.user.gender.other{/lang} - {/if} - {/if} - {#$userRank->requiredPoints}
-
*} - - -{else} - {lang}wcf.global.noItems{/lang} -{/if} +{unsafe:$gridView->render()} {include file='footer'} diff --git a/wcfsetup/install/files/lib/acp/page/UserRankListPage.class.php b/wcfsetup/install/files/lib/acp/page/UserRankListPage.class.php index fe199719d6..c636808254 100644 --- a/wcfsetup/install/files/lib/acp/page/UserRankListPage.class.php +++ b/wcfsetup/install/files/lib/acp/page/UserRankListPage.class.php @@ -2,22 +2,19 @@ namespace wcf\acp\page; -use wcf\data\user\rank\I18nUserRankList; -use wcf\page\SortablePage; -use wcf\system\request\LinkHandler; +use wcf\page\AbstractGridViewPage; use wcf\system\view\grid\UserRankGridView; -use wcf\system\WCF; /** * Lists available user ranks. * - * @author Marcel Werk - * @copyright 2001-2019 WoltLab GmbH - * @license GNU Lesser General Public License + * @author Marcel Werk + * @copyright 2001-2024 WoltLab GmbH + * @license GNU Lesser General Public License * - * @property I18nUserRankList $objectList + * @property UserRankGridView $gridView */ -class UserRankListPage extends SortablePage +class UserRankListPage extends AbstractGridViewPage { /** * @inheritDoc @@ -37,40 +34,5 @@ class UserRankListPage extends SortablePage /** * @inheritDoc */ - public $objectListClassName = I18nUserRankList::class; - - /** - * @inheritDoc - */ - public $defaultSortField = 'rankTitleI18n'; - - /** - * @inheritDoc - */ - public $validSortFields = ['rankID', 'groupID', 'requiredPoints', 'rankTitleI18n', 'rankImage', 'requiredGender']; - - /** - * @inheritDoc - */ - protected function initObjectList() - { - parent::initObjectList(); - - $this->objectList->sqlSelects .= (!empty($this->objectList->sqlSelects) ? ', ' : '') . 'user_group.groupName'; - $this->objectList->sqlJoins .= ' - LEFT JOIN wcf' . WCF_N . '_user_group user_group - ON user_group.groupID = user_rank.groupID'; - } - - public function assignVariables() - { - parent::assignVariables(); - - $view = new UserRankGridView(isset($_GET['pageNo']) ? \intval($_GET['pageNo']) : 1); - $view->setBaseUrl(LinkHandler::getInstance()->getControllerLink(self::class)); - - WCF::getTPL()->assign([ - 'view' => $view, - ]); - } + protected string $gridViewClassName = UserRankGridView::class; } diff --git a/wcfsetup/install/files/lib/page/AbstractGridViewPage.class.php b/wcfsetup/install/files/lib/page/AbstractGridViewPage.class.php new file mode 100644 index 0000000000..06b279f5b1 --- /dev/null +++ b/wcfsetup/install/files/lib/page/AbstractGridViewPage.class.php @@ -0,0 +1,72 @@ +pageNo = \intval($_REQUEST['pageNo']); + } + if (isset($_REQUEST['sortField'])) { + $this->sortField = $_REQUEST['sortField']; + } + if (isset($_REQUEST['sortOrder']) && ($_REQUEST['sortOrder'] === 'ASC' || $_REQUEST['sortOrder'] === 'DESC')) { + $this->sortOrder = $_REQUEST['sortOrder']; + } + } + + #[\Override] + public function readData() + { + parent::readData(); + + $this->initGridView(); + } + + #[\Override] + public function assignVariables() + { + parent::assignVariables(); + + WCF::getTPL()->assign([ + 'gridView' => $this->gridView, + ]); + } + + protected function initGridView(): void + { + if (!isset($this->gridViewClassName)) { + throw new SystemException('Grid view class name not specified.'); + } + + if (!\is_subclass_of($this->gridViewClassName, AbstractGridView::class)) { + throw new ParentClassException($this->gridViewClassName, AbstractGridView::class); + } + + $this->gridView = new $this->gridViewClassName; + + if ($this->sortField) { + $this->gridView->setSortField($this->sortField); + } + $this->gridView->setSortOrder($this->sortOrder); + $this->gridView->setPageNo($this->pageNo); + $this->gridView->setBaseUrl(LinkHandler::getInstance()->getControllerLink(static::class)); + } +} diff --git a/wcfsetup/install/files/lib/system/endpoint/controller/core/gridViews/GetRows.class.php b/wcfsetup/install/files/lib/system/endpoint/controller/core/gridViews/GetRows.class.php index 204e1ffb83..9d43b27c6e 100644 --- a/wcfsetup/install/files/lib/system/endpoint/controller/core/gridViews/GetRows.class.php +++ b/wcfsetup/install/files/lib/system/endpoint/controller/core/gridViews/GetRows.class.php @@ -24,13 +24,14 @@ final class GetRows implements IController throw new UserInputException('gridView', 'invalid'); } - $view = new $parameters->gridView($parameters->pageNo); + $view = new $parameters->gridView(); \assert($view instanceof AbstractGridView); if (!$view->isAccessible()) { throw new PermissionDeniedException(); } + $view->setPageNo($parameters->pageNo); if ($parameters->sortField) { $view->setSortField($parameters->sortField); } diff --git a/wcfsetup/install/files/lib/system/view/grid/AbstractGridView.class.php b/wcfsetup/install/files/lib/system/view/grid/AbstractGridView.class.php index 1ebf9b4021..ee68dbe2c6 100644 --- a/wcfsetup/install/files/lib/system/view/grid/AbstractGridView.class.php +++ b/wcfsetup/install/files/lib/system/view/grid/AbstractGridView.class.php @@ -11,8 +11,9 @@ abstract class AbstractGridView private string $baseUrl = ''; private string $sortField = ''; private string $sortOrder = 'ASC'; + private int $pageNo = 1; - public function __construct(private readonly int $pageNo = 1) + public function __construct() { $this->init(); } @@ -68,7 +69,7 @@ abstract class AbstractGridView { $result = ''; - foreach ($this->getRows($this->rowsPerPage, ($this->pageNo - 1) * $this->rowsPerPage) as $row) { + foreach ($this->getRows() as $row) { $result .= << EOT; @@ -94,17 +95,13 @@ abstract class AbstractGridView return $row[$identifer] ?? ''; } - public abstract function getRows(int $limit, int $offset = 0): array; + protected abstract function getRows(): array; - - public function getPageNo(): int - { - return $this->pageNo; - } + public abstract function countRows(): int; public function countPages(): int { - return 3; + return \ceil($this->countRows() / $this->getRowsPerPage()); } public function getClassName(): string @@ -169,4 +166,24 @@ abstract class AbstractGridView { return $this->sortOrder; } + + public function getPageNo(): int + { + return $this->pageNo; + } + + public function setPageNo(int $pageNo): void + { + $this->pageNo = $pageNo; + } + + public function getRowsPerPage(): int + { + return $this->rowsPerPage; + } + + public function setRowsPerPage(int $rowsPerPage): void + { + $this->rowsPerPage = $rowsPerPage; + } } diff --git a/wcfsetup/install/files/lib/system/view/grid/DatabaseObjectListGridView.class.php b/wcfsetup/install/files/lib/system/view/grid/DatabaseObjectListGridView.class.php new file mode 100644 index 0000000000..0c3e536506 --- /dev/null +++ b/wcfsetup/install/files/lib/system/view/grid/DatabaseObjectListGridView.class.php @@ -0,0 +1,66 @@ +getObjectList()->readObjects(); + + return $this->getObjectList()->getObjects(); + } + + public function countRows(): int + { + if (!isset($this->objectCount)) { + $this->objectCount = $this->getObjectList()->countObjects(); + } + + return $this->objectCount; + } + + protected function getData(mixed $row, string $identifer): mixed + { + \assert($row instanceof DatabaseObject); + + return $row->__get($identifer); + } + + protected function initObjectList(): void + { + if (!isset($this->objectListClassName)) { + throw new SystemException('Database object list class name not specified.'); + } + + if (!\is_subclass_of($this->objectListClassName, DatabaseObjectList::class)) { + throw new ParentClassException($this->objectListClassName, DatabaseObjectList::class); + } + + $this->objectList = new $this->objectListClassName; + $this->objectList->sqlLimit = $this->getRowsPerPage(); + $this->objectList->sqlOffset = ($this->getPageNo() - 1) * $this->getRowsPerPage(); + //wcfDebug($this->objectList->sqlLimit, $this->objectList->sqlOffset); + if ($this->getSortField()) { + $this->objectList->sqlOrderBy = $this->getSortField() . ' ' . $this->getSortOrder(); + } + } + + public function getObjectList(): DatabaseObjectList + { + if (!isset($this->objectList)) { + $this->initObjectList(); + } + + return $this->objectList; + } +} diff --git a/wcfsetup/install/files/lib/system/view/grid/UserRankGridView.class.php b/wcfsetup/install/files/lib/system/view/grid/UserRankGridView.class.php index dbc68c3ded..1389c7e5d6 100644 --- a/wcfsetup/install/files/lib/system/view/grid/UserRankGridView.class.php +++ b/wcfsetup/install/files/lib/system/view/grid/UserRankGridView.class.php @@ -3,7 +3,6 @@ namespace wcf\system\view\grid; use wcf\acp\form\UserRankEditForm; -use wcf\data\DatabaseObject; use wcf\data\user\group\UserGroup; use wcf\data\user\rank\UserRank; use wcf\data\user\rank\UserRankList; @@ -14,8 +13,10 @@ use wcf\system\view\grid\renderer\TitleColumnRenderer; use wcf\system\WCF; use wcf\util\StringUtil; -final class UserRankGridView extends AbstractGridView +final class UserRankGridView extends DatabaseObjectListGridView { + protected string $objectListClassName = UserRankList::class; + #[\Override] protected function init(): void { @@ -92,26 +93,6 @@ final class UserRankGridView extends AbstractGridView $this->setSortField('rankTitle'); } - public function getRows(int $limit, int $offset = 0): array - { - $list = new UserRankList(); - $list->sqlLimit = $limit; - $list->sqlOffset = $offset; - if ($this->getSortField()) { - $list->sqlOrderBy = $this->getSortField() . ' ' . $this->getSortOrder(); - } - $list->readObjects(); - - return $list->getObjects(); - } - - protected function getData(mixed $row, string $identifer): mixed - { - \assert($row instanceof DatabaseObject); - - return $row->__get($identifer); - } - public function isAccessible(): bool { return \MODULE_USER_RANK && WCF::getSession()->getPermission('admin.user.rank.canManageRank'); -- 2.20.1