Fix handling of box conditions and additional data upon controller change
authorMatthias Schmidt <gravatronics@live.com>
Sat, 1 Oct 2016 12:38:06 +0000 (14:38 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 1 Oct 2016 12:38:09 +0000 (14:38 +0200)
wcfsetup/install/files/lib/acp/form/BoxAddForm.class.php
wcfsetup/install/files/lib/acp/form/BoxEditForm.class.php
wcfsetup/install/files/lib/system/box/AbstractBoxController.class.php
wcfsetup/install/files/lib/system/box/AbstractDatabaseObjectListBoxController.class.php
wcfsetup/install/files/lib/system/box/IBoxController.class.php
wcfsetup/install/files/lib/system/box/IConditionBoxController.class.php

index 9df2a4279339b708c42059745ceb8ddca8b0ad2e..e2c5692acea88199187969e9dc94803f64fd1983 100644 (file)
@@ -500,9 +500,15 @@ class BoxAddForm extends AbstractForm {
                        'identifier' => 'com.woltlab.wcf.genericBox'.$boxEditor->boxID
                ]);
                
-               if ($this->boxController && $this->boxController->getProcessor() instanceof IConditionBoxController) {
-                       $this->boxController->getProcessor()->setBox($box, false);
-                       $this->boxController->getProcessor()->saveConditions();
+               if ($this->boxController) {
+                       if ($this->boxController->getProcessor() instanceof IConditionBoxController) {
+                               $this->boxController->getProcessor()->setBox($box, false);
+                       }
+                       else {
+                               $this->boxController->getProcessor()->setBox($box);
+                       }
+                       
+                       $this->boxController->getProcessor()->saveAdditionalData();
                }
                
                // save acl
index b526dffa63277ec8b15de532ac4154304b9f69b6..ab1c5dfe9b4f687f5135532b99b95192522fca51 100644 (file)
@@ -6,6 +6,7 @@ use wcf\data\object\type\ObjectTypeCache;
 use wcf\form\AbstractForm;
 use wcf\system\acl\simple\SimpleAclHandler;
 use wcf\system\box\IConditionBoxController;
+use wcf\system\condition\ConditionHandler;
 use wcf\system\exception\IllegalLinkException;
 use wcf\system\language\LanguageFactory;
 use wcf\system\WCF;
@@ -117,9 +118,22 @@ class BoxEditForm extends BoxAddForm {
                $this->objectAction = new BoxAction([$this->box], 'update', ['data' => array_merge($this->additionalFields, $data), 'content' => $content, 'pageIDs' => $this->pageIDs]);
                $this->objectAction->executeAction();
                
-               if ($this->boxController && $this->boxController->getProcessor() instanceof IConditionBoxController) {
-                       $this->boxController->getProcessor()->setBox($this->box, false);
-                       $this->boxController->getProcessor()->saveConditions();
+               // delete old conditions
+               if ($this->box->getController() && $this->box->getController() instanceof IConditionBoxController && $this->box->getController()->getConditionDefinition() && (!$this->boxController || (!($this->boxController->getProcessor() instanceof IConditionBoxController)) || !$this->boxController->getProcessor()->getConditionDefinition())) {
+                       ConditionHandler::getInstance()->deleteConditions($this->box->getController()->getConditionDefinition(), [$this->box->boxID]);
+               }
+               
+               if ($this->boxController) {
+                       // pass updated box to box controller as in `BoxAddForm::save()`
+                       $box = new Box($this->box->boxID);
+                       if ($this->boxController->getProcessor() instanceof IConditionBoxController) {
+                               $this->boxController->getProcessor()->setBox($box, false);
+                       }
+                       else {
+                               $this->boxController->getProcessor()->setBox($box);
+                       }
+                       
+                       $this->boxController->getProcessor()->saveAdditionalData();
                }
                
                SimpleAclHandler::getInstance()->setValues('com.woltlab.wcf.box', $this->box->boxID, $this->aclValues);
index 73742704eb3198c4552a6033401d72ed3a8efcee..2a7dd9ef69287d648d08513127673965d37bd413 100644 (file)
@@ -1,6 +1,9 @@
 <?php
 namespace wcf\system\box;
 use wcf\data\box\Box;
+use wcf\data\box\BoxAction;
+use wcf\data\condition\Condition;
+use wcf\system\condition\ConditionHandler;
 use wcf\system\event\EventHandler;
 
 /**
@@ -97,6 +100,26 @@ abstract class AbstractBoxController implements IBoxController {
                $this->box = $box;
        }
        
+       /**
+        * @inheritDoc
+        */
+       public function saveAdditionalData() {
+               // always write additional data to make sure that the additional data of the previous box controller
+               // are properly overwritten
+               (new BoxAction([$this->box], 'update', [
+                       'data' => ['additionalData' => serialize($this->getAdditionalData())]
+               ]))->executeAction();
+       }
+       
+       /**
+        * Returns the additional data of the box.
+        * 
+        * @return      array
+        */
+       protected function getAdditionalData() {
+               return [];
+       }
+       
        /**
         * @inheritDoc
         */
index e768df7d71e17f6b7bb63de18fc58c78b8898920..7ceac1d1e782cd9d1e8dfafae6579c1a50f9ac0c 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 namespace wcf\system\box;
 use wcf\data\box\Box;
-use wcf\data\box\BoxAction;
+use wcf\data\condition\ConditionAction;
 use wcf\data\object\type\ObjectType;
 use wcf\data\object\type\ObjectTypeCache;
 use wcf\data\DatabaseObjectList;
@@ -126,9 +126,7 @@ abstract class AbstractDatabaseObjectListBoxController extends AbstractBoxContro
        }
        
        /**
-        * Returns the additional data saved with the box..
-        * 
-        * @return      array
+        * @inheritDoc
         */
        protected function getAdditionalData() {
                return [
@@ -205,13 +203,13 @@ abstract class AbstractDatabaseObjectListBoxController extends AbstractBoxContro
        protected function loadContent() {
                $this->objectList = $this->getObjectList();
                
-               if ($this->box->limit) {
+               if ($this->limit) {
                        $this->objectList->sqlLimit = $this->box->limit;
                }
                
-               if ($this->box->sortOrder && $this->box->sortField) {
+               if ($this->sortOrder && $this->sortField) {
                        $alias = $this->objectList->getDatabaseTableAlias();
-                       $this->objectList->sqlOrderBy = $this->box->sortField . ' ' . $this->box->sortOrder . ", " . ($alias ? $alias . "." : "") . $this->objectList->getDatabaseTableIndexName() . " " . $this->box->sortOrder;
+                       $this->objectList->sqlOrderBy = $this->sortField . ' ' . $this->sortOrder . ", " . ($alias ? $alias . "." : "") . $this->objectList->getDatabaseTableIndexName() . " " . $this->sortOrder;
                }
                
                if ($this->conditionDefinition) {
@@ -252,19 +250,17 @@ abstract class AbstractDatabaseObjectListBoxController extends AbstractBoxContro
        /**
         * @inheritDoc
         */
-       public function saveConditions() {
-               if (($this->sortField && $this->sortOrder) || $this->limit) {
-                       (new BoxAction([$this->box], 'update', [
-                               'data' => [
-                                       'additionalData' => serialize(array_merge($this->box->additionalData, $this->getAdditionalData()))
-                               ]
-                       ]))->executeAction();
-               }
+       public function saveAdditionalData() {
+               parent::saveAdditionalData();
                
                if ($this->conditionDefinition) {
                        // do not use Box::getConditions() here to avoid setting box data by internally calling
                        // Box::getController()
-                       ConditionHandler::getInstance()->updateConditions($this->box->boxID, ConditionHandler::getInstance()->getConditions($this->conditionDefinition, $this->box->boxID), $this->conditionObjectTypes);
+                       ConditionHandler::getInstance()->updateConditions(
+                               $this->box->boxID,
+                               ConditionHandler::getInstance()->getConditions($this->conditionDefinition, $this->box->boxID),
+                               $this->conditionObjectTypes
+                       );
                }
        }
        
@@ -279,9 +275,14 @@ abstract class AbstractDatabaseObjectListBoxController extends AbstractBoxContro
                parent::setBox($box);
                
                if ($setConditionData) {
-                       $this->limit = $this->box->limit;
-                       $this->sortOrder = $this->box->sortOrder;
-                       $this->sortField = $this->box->sortField;
+                       if ($this->defaultLimit !== null) {
+                               $this->limit = $this->box->limit;
+                       }
+                       
+                       if (!empty($this->validSortFields)) {
+                               $this->sortOrder = $this->box->sortOrder;
+                               $this->sortField = $this->box->sortField;
+                       }
                        
                        if ($this->conditionDefinition) {
                                $conditions = [];
index d4db587735c46dc0800ae134a1cfb14a6601b986..525e4deac19f7f1296e74a51037486c95cce0343 100644 (file)
@@ -64,6 +64,11 @@ interface IBoxController {
         */
        public function setBox(Box $box);
        
+       /**
+        * Saves additional box data for box set via `setBox()`.
+        */
+       public function saveAdditionalData();
+       
        /**
         * Returns a list of supported box positions.
         * 
index c9f02ad860ac847584c50c0b99e8ab9ea2dbff86..d36d6046f1e67af3da9806df12292e6f779ced48 100644 (file)
@@ -38,11 +38,6 @@ interface IConditionBoxController extends IBoxController {
         */
        public function readConditions();
        
-       /**
-        * Saves the conditions for the box.
-        */
-       public function saveConditions();
-       
        /**
         * Validates the read conditions for the box.
         */