Makes it easier to use own category nodes
authorMatthias Schmidt <gravatronics@live.com>
Fri, 6 Jul 2012 09:01:33 +0000 (11:01 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Fri, 6 Jul 2012 09:01:40 +0000 (11:01 +0200)
With the changes of this commit you can easily extend the given node classes (without having to copy&paste the `__construct` methods and just change the class name). This makes it possible for your own implementation to add additional conditions for a category to be included in the list (like acl options "canViewCategory" or "canViewDisabledCategory").

wcfsetup/install/files/lib/data/category/CategoryNode.class.php
wcfsetup/install/files/lib/data/category/CategoryNodeList.class.php

index 5128420211aa5f891baddbcbf249adfc02e07837..d3ed548dc4fbab6b92126df1197edeed4008d3c3 100644 (file)
@@ -21,6 +21,18 @@ class CategoryNode extends DatabaseObjectDecorator implements \RecursiveIterator
         */
        protected $childCategories = array();
        
+       /**
+        * indicates of disabled categories are included
+        * @var integer
+        */
+       protected $inludeDisabledCategories = false;
+       
+       /**
+        * list of object type category ids of excluded categories
+        * @var array<integer>
+        */
+       protected $excludedObjectTypeCategoryIDs = false;
+       
        /**
         * @see wcf\data\DatabaseObjectDecorator::$baseClass
         */
@@ -32,9 +44,13 @@ class CategoryNode extends DatabaseObjectDecorator implements \RecursiveIterator
        public function __construct(DatabaseObject $object, $inludeDisabledCategories = false, array $excludedObjectTypeCategoryIDs = array()) {
                parent::__construct($object);
                
+               $this->inludeDisabledCategories = $inludeDisabledCategories;
+               $this->excludedObjectTypeCategoryIDs = $excludedObjectTypeCategoryIDs;
+               
+               $className = get_class();
                foreach (CategoryHandler::getInstance()->getChildCategories($this->getDecoratedObject()) as $category) {
-                       if (!in_array($category->objectTypeCategoryID, $excludedObjectTypeCategoryIDs) && ($inludeDisabledCategories || !$category->isDisabled)) {
-                               $this->childCategories[] = new CategoryNode($category, $inludeDisabledCategories, $excludedObjectTypeCategoryIDs);
+                       if ($this->fulfillsConditions($category)) {
+                               $this->childCategories[] = new $className($category, $inludeDisabledCategories, $excludedObjectTypeCategoryIDs);
                        }
                }
        }
@@ -53,6 +69,17 @@ class CategoryNode extends DatabaseObjectDecorator implements \RecursiveIterator
                return $this->childCategories[$this->index];
        }
        
+       /**
+        * Returns true if the given category fulfills all needed conditions to
+        * be included in the list.
+        * 
+        * @param       wcf\data\category\Category      $category
+        * @return      boolean
+        */
+       public function fulfillsConditions(Category $category) {
+               return !in_array($category->objectTypeCategoryID, $this->excludedObjectTypeCategoryIDs) && ($this->includeDisabledCategories || !$category->isDisabled);
+       }
+       
        /**
         * @see \RecursiveIterator::getChildren()
         */
index 23bf7ccf366e3d08d99a3a1126ae99a8b36ef531..211a3a477aff8401903f13bdf22726ff20d912a1 100644 (file)
@@ -20,6 +20,12 @@ class CategoryNodeList extends \RecursiveIteratorIterator implements \Countable
         */
        protected $count = null;
        
+       /**
+        * name of the category node class
+        * @var string
+        */
+       protected $nodeClassName = 'wcf\data\category\CategoryNode';
+       
        /**
         * id of the parent category
         * @var integer
@@ -53,7 +59,7 @@ class CategoryNodeList extends \RecursiveIteratorIterator implements \Countable
                        }
                }
                
-               parent::__construct(new CategoryNode($parentCategory, $inludeDisabledCategories, $excludedObjectTypeCategoryIDs), \RecursiveIteratorIterator::SELF_FIRST);
+               parent::__construct(new $this->nodeClassName($parentCategory, $inludeDisabledCategories, $excludedObjectTypeCategoryIDs), \RecursiveIteratorIterator::SELF_FIRST);
        }
        
        /**