Add category API to react to changed parent category
authorMatthias Schmidt <gravatronics@live.com>
Tue, 24 Feb 2015 17:35:19 +0000 (18:35 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Tue, 24 Feb 2015 17:35:19 +0000 (18:35 +0100)
wcfsetup/install/files/lib/data/category/CategoryAction.class.php
wcfsetup/install/files/lib/system/category/AbstractCategoryType.class.php
wcfsetup/install/files/lib/system/category/ICategoryType.class.php

index 74be68227335f4de020a4d3edebc04c150ec1f59..547e4cdf2d1b8967dec795a9f10d082f4219b544 100644 (file)
@@ -15,7 +15,7 @@ use wcf\system\WCF;
  * Executes category-related actions.
  * 
  * @author     Matthias Schmidt
- * @copyright  2001-2014 WoltLab GmbH
+ * @copyright  2001-2015 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    com.woltlab.wcf
  * @subpackage data.category
@@ -91,16 +91,53 @@ class CategoryAction extends AbstractDatabaseObjectAction implements ISortableAc
                }
                
                parent::update();
+               
+               if ($this->parameters['data']['parentCategoryID']) {
+                       $objectType = null;
+                       $parentUpdates = array();
+                       
+                       foreach ($this->objects as $category) {
+                               if ($objectType === null) {
+                                       $objectType = $category->getObjectType();
+                               }
+                               
+                               if ($category->parentCategoryID != $this->parameters['data']['parentCategoryID']) {
+                                       $parentUpdates[$categoryID] = array(
+                                               'oldParentCategoryID' => $category->parentCategoryID,
+                                               'newParentCategoryID' => $this->parameters['data']['parentCategoryID']
+                                       );
+                               }
+                       }
+                       
+                       if (!empty($parentUpdates)) {
+                               $objectType->getProcessor()->changedParentCategories($parentUpdates);
+                       }
+               }
        }
        
        /**
         * @see \wcf\data\ISortableAction::updatePosition()
         */
        public function updatePosition() {
+               $objectType = null;
+               $parentUpdates = array();
+               
                WCF::getDB()->beginTransaction();
                foreach ($this->parameters['data']['structure'] as $parentCategoryID => $categoryIDs) {
                        $showOrder = 1;
                        foreach ($categoryIDs as $categoryID) {
+                               $category = CategoryHandler::getInstance()->getCategory($categoryID);
+                               if ($objectType === null) {
+                                       $objectType = $category->getObjectType();
+                               }
+                               
+                               if ($category->parentCategoryID != $parentCategoryID) {
+                                       $parentUpdates[$categoryID] = array(
+                                               'oldParentCategoryID' => $category->parentCategoryID,
+                                               'newParentCategoryID' => $parentCategoryID
+                                       );
+                               }
+                               
                                $this->objects[$categoryID]->update(array(
                                        'parentCategoryID' => $parentCategoryID ? $this->objects[$parentCategoryID]->categoryID : 0,
                                        'showOrder' => $showOrder++
@@ -108,6 +145,10 @@ class CategoryAction extends AbstractDatabaseObjectAction implements ISortableAc
                        }
                }
                WCF::getDB()->commitTransaction();
+               
+               if (!empty($parentUpdates)) {
+                       $objectType->getProcessor()->changedParentCategories($parentUpdates);
+               }
        }
        
        /**
index b4e17d9c66f7023cb6cd9a6fc8a89b967cb02910..0f4d01429626c77a29db1458cc0d1fe35a33819e 100644 (file)
@@ -9,7 +9,7 @@ use wcf\system\WCF;
  * Abstract implementation of a category type.
  * 
  * @author     Matthias Schmidt
- * @copyright  2001-2014 WoltLab GmbH
+ * @copyright  2001-2015 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    com.woltlab.wcf
  * @subpackage system.category
@@ -98,6 +98,13 @@ abstract class AbstractCategoryType extends SingletonFactory implements ICategor
                return WCF::getSession()->getPermission($this->permissionPrefix.'.canEditCategory');
        }
        
+       /**
+        * @see \wcf\system\category\ICategoryType::changedParentCategories()
+        */
+       public function changedParentCategories(array $categoryData) {
+               // does nothing
+       }
+       
        /**
         * @see \wcf\system\category\ICategoryType::forceDescription()
         */
index 04e9a12fe3980da08654a8fec0e3612e2f84ff04..2ca451a5e7ab3c9e7c9cadf77a9088f1f21d697c 100644 (file)
@@ -6,7 +6,7 @@ use wcf\data\category\CategoryEditor;
  * Every category type has to implement this interface.
  * 
  * @author     Matthias Schmidt
- * @copyright  2001-2014 WoltLab GmbH
+ * @copyright  2001-2015 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    com.woltlab.wcf
  * @subpackage system.category
@@ -41,6 +41,23 @@ interface ICategoryType {
         */
        public function canEditCategory();
        
+       /**
+        * Is called after categories were assigned different parent categories.
+        * 
+        * Array structure:
+        * [
+        *      categoryID => [
+        *              oldParentCategoryID => 1,
+        *              newParentCategoryID => 2
+        *      ],
+        *      categoryID => [
+        *              oldParentCategoryID => null,
+        *              newParentCategoryID => 2
+        *      ],
+        * ]
+        */
+       public function changedParentCategories(array $categoryData);
+       
        /**
         * Returns true if a category of this type may have no empty description.
         *