From 642d5f02abe156b3935aff031e2f8dd880eb759e Mon Sep 17 00:00:00 2001 From: joshuaruesweg Date: Fri, 3 Sep 2021 17:26:27 +0200 Subject: [PATCH] Create page conditions during page add/edit --- .../files/lib/data/page/PageAction.class.php | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/wcfsetup/install/files/lib/data/page/PageAction.class.php b/wcfsetup/install/files/lib/data/page/PageAction.class.php index 35d9b8e5b9..954b218654 100644 --- a/wcfsetup/install/files/lib/data/page/PageAction.class.php +++ b/wcfsetup/install/files/lib/data/page/PageAction.class.php @@ -4,13 +4,17 @@ namespace wcf\data\page; use wcf\data\AbstractDatabaseObjectAction; use wcf\data\box\Box; +use wcf\data\condition\Condition; use wcf\data\ISearchAction; use wcf\data\ISortableAction; use wcf\data\IToggleAction; +use wcf\data\object\type\ObjectTypeCache; use wcf\data\page\content\PageContent; use wcf\data\page\content\PageContentEditor; use wcf\data\TDatabaseObjectToggle; use wcf\system\comment\CommentHandler; +use wcf\system\condition\ConditionHandler; +use wcf\system\condition\page\MultiPageCondition; use wcf\system\database\util\PreparedStatementConditionBuilder; use wcf\system\exception\PermissionDeniedException; use wcf\system\exception\UserInputException; @@ -150,6 +154,11 @@ class PageAction extends AbstractDatabaseObjectAction implements ISearchAction, } } + // update box conditions + foreach ($this->parameters['boxToPage'] as $boxData) { + $this->createPageConditionForBox($boxData['boxID'], [$page]); + } + // save template if ($page->pageType == 'tpl') { if (!empty($this->parameters['content'])) { @@ -289,6 +298,67 @@ class PageAction extends AbstractDatabaseObjectAction implements ISearchAction, } } } + + // update box conditions + foreach ($this->parameters['boxToPage'] as $boxData) { + $this->createPageConditionForBox($boxData['boxID'], $this->getObjects()); + } + } + + private function createPageConditionForBox(int $boxID, array $pages): void + { + $box = new Box($boxID); + + // Create matching conditions. + $pageConditions = $this->createPageConditions( + \array_column($pages, 'pageID'), + $box->visibleEverywhere, + $box + ); + + $conditionObjectTypeID = ObjectTypeCache::getInstance()->getObjectTypeIDByName( + Box::VISIBILITY_CONDITIONS_OBJECT_TYPE_NAME, + 'com.woltlab.wcf.page' + ); + $oldCondition = []; + foreach ($box->getVisibilityConditions() as $condition) { + if ($condition->objectTypeID === $conditionObjectTypeID) { + $oldCondition[] = $condition; + break; + } + } + + ConditionHandler::getInstance()->updateConditions( + $box->boxID, + $oldCondition, + $pageConditions + ); + } + + private function createPageConditions(array $pages, bool $reverseLogic, Box $box): array + { + $pageCondition = ObjectTypeCache::getInstance()->getObjectTypeByName( + Box::VISIBILITY_CONDITIONS_OBJECT_TYPE_NAME, + 'com.woltlab.wcf.page' + ); + + $pageIDs = \array_merge( + $box->getPageIDs(), + \array_column($pages, 'pageID') + ); + + \assert($pageCondition->getProcessor() instanceof MultiPageCondition); + + $pageCondition->getProcessor()->setData(new Condition(null, [ + 'conditionData' => \serialize([ + 'pageIDs' => \array_unique($pageIDs), + 'pageIDs_reverseLogic' => $reverseLogic, + ]), + ])); + + return [ + $pageCondition, + ]; } /** -- 2.20.1