Cleanup code
authorjoshuaruesweg <ruesweg@woltlab.com>
Tue, 31 Aug 2021 14:09:44 +0000 (16:09 +0200)
committerjoshuaruesweg <ruesweg@woltlab.com>
Tue, 31 Aug 2021 14:09:44 +0000 (16:09 +0200)
wcfsetup/install/files/lib/acp/form/BoxAddForm.class.php
wcfsetup/install/files/lib/data/box/Box.class.php
wcfsetup/install/files/lib/system/box/BoxHandler.class.php

index a0e4fac83cd367994c18732322c1124d61b364c9..7d6112dd807662d6e5c04a526ba8b231b61c6dd0 100644 (file)
@@ -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;
index 62204cdc63bd0f2f44b53347702be4dfa2ce8309..424d4e640fae2d792206e4153aaaf477b4384325 100644 (file)
@@ -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
         );
     }
index 8b0bc02324b06a435ac44843b80695767dafa91d..f6cb7d04025bc46e19a3bf3de3dd37b8a66df4c9 100644 (file)
@@ -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);