From 0eb8acdd46df644ec72ed7f27728f98dd4824570 Mon Sep 17 00:00:00 2001 From: joshuaruesweg Date: Tue, 31 Aug 2021 16:09:44 +0200 Subject: [PATCH] Cleanup code --- .../files/lib/acp/form/BoxAddForm.class.php | 36 ++++++++++++++++--- .../install/files/lib/data/box/Box.class.php | 7 +++- .../files/lib/system/box/BoxHandler.class.php | 11 +++--- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/wcfsetup/install/files/lib/acp/form/BoxAddForm.class.php b/wcfsetup/install/files/lib/acp/form/BoxAddForm.class.php index a0e4fac83c..7d6112dd80 100644 --- a/wcfsetup/install/files/lib/acp/form/BoxAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/BoxAddForm.class.php @@ -399,31 +399,57 @@ class BoxAddForm extends AbstractForm private function readConditions(): void { + $pageConditionObjectTypeID = ObjectTypeCache::getInstance()->getObjectTypeIDByName( + Box::VISIBILITY_CONDITIONS_OBJECT_TYPE_NAME, + 'com.woltlab.wcf.page' + ); + + $this->pageIDs = $this->visibleEverywhere = null; foreach ($this->groupedConditionObjectTypes as $groupedObjectTypes) { foreach ($groupedObjectTypes as $objectTypes) { if (\is_array($objectTypes)) { foreach ($objectTypes as $objectType) { $objectType->getProcessor()->readFormParameters(); - if ($objectType->getProcessor() instanceof MultiPageCondition) { + if ($objectType->objectTypeID == $pageConditionObjectTypeID) { + \assert($objectTypes->getProcessor() instanceof MultiPageCondition); + $data = $objectType->getProcessor()->getData(); if ($data !== null) { $this->pageIDs = $data['pageIDs']; - $this->visibleEverywhere = !$data['pageIDs_reverseLogic']; + $this->visibleEverywhere = !$data['pageIDs_reverseLogic'] ? 1 : 0; + } else { + $this->pageIDs = []; + $this->visibleEverywhere = 1; } } } } else { $objectTypes->getProcessor()->readFormParameters(); - if ($objectTypes->getProcessor() instanceof MultiPageCondition) { + if ($objectTypes->objectTypeID == $pageConditionObjectTypeID) { + \assert($objectTypes->getProcessor() instanceof MultiPageCondition); + $data = $objectTypes->getProcessor()->getData(); if ($data !== null) { $this->pageIDs = $data['pageIDs']; - $this->visibleEverywhere = !$data['pageIDs_reverseLogic']; + $this->visibleEverywhere = !$data['pageIDs_reverseLogic'] ? 1 : 0; + } else { + $this->pageIDs = []; + $this->visibleEverywhere = 1; } } } } } + + if ($this->pageIDs === null || $this->visibleEverywhere === null) { + throw new \LogicException( + \sprintf( + "The '%d' condition for the definition '%d' is missing.", + 'com.woltlab.wcf.page', + Box::VISIBILITY_CONDITIONS_OBJECT_TYPE_NAME + ) + ); + } } /** @@ -726,7 +752,7 @@ class BoxAddForm extends AbstractForm */ public function readData() { - $objectTypes = ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.condition.box'); + $objectTypes = ObjectTypeCache::getInstance()->getObjectTypes(Box::VISIBILITY_CONDITIONS_OBJECT_TYPE_NAME); foreach ($objectTypes as $objectType) { if (!$objectType->conditionobject) { continue; diff --git a/wcfsetup/install/files/lib/data/box/Box.class.php b/wcfsetup/install/files/lib/data/box/Box.class.php index 62204cdc63..424d4e640f 100644 --- a/wcfsetup/install/files/lib/data/box/Box.class.php +++ b/wcfsetup/install/files/lib/data/box/Box.class.php @@ -56,6 +56,11 @@ use wcf\system\WCF; */ class Box extends DatabaseObject { + /** + * @since 5.5 + */ + public const VISIBILITY_CONDITIONS_OBJECT_TYPE_NAME = 'com.woltlab.wcf.condition.box'; + /** * image media object * @var ViewableMedia @@ -570,7 +575,7 @@ class Box extends DatabaseObject public function getVisibilityConditions(): array { return ConditionHandler::getInstance()->getConditions( - 'com.woltlab.wcf.condition.box', // @TODO make const + self::VISIBILITY_CONDITIONS_OBJECT_TYPE_NAME, $this->boxID ); } diff --git a/wcfsetup/install/files/lib/system/box/BoxHandler.class.php b/wcfsetup/install/files/lib/system/box/BoxHandler.class.php index 8b0bc02324..f6cb7d0402 100644 --- a/wcfsetup/install/files/lib/system/box/BoxHandler.class.php +++ b/wcfsetup/install/files/lib/system/box/BoxHandler.class.php @@ -191,7 +191,7 @@ class BoxHandler extends SingletonFactory } $conditionObjectTypeID = ObjectTypeCache::getInstance()->getObjectTypeIDByName( - 'com.woltlab.wcf.condition.box', + Box::VISIBILITY_CONDITIONS_OBJECT_TYPE_NAME, 'com.woltlab.wcf.page' ); $oldCondition = []; @@ -219,13 +219,14 @@ class BoxHandler extends SingletonFactory private function createPageConditions(array $pages, bool $reverseLogic, Box $box): array { $pageCondition = ObjectTypeCache::getInstance()->getObjectTypeByName( - 'com.woltlab.wcf.condition.box', + Box::VISIBILITY_CONDITIONS_OBJECT_TYPE_NAME, 'com.woltlab.wcf.page' ); - $pageIDs = \array_merge($box->getPageIDs(), \array_map(static function ($page) { - return $page->pageID; - }, $pages)); + $pageIDs = \array_merge( + $box->getPageIDs(), + \array_column($pages, 'pageID') + ); \assert($pageCondition->getProcessor() instanceof MultiPageCondition); -- 2.20.1