Added method to get all child categories of a category recursively
authorMarcel Werk <burntime@woltlab.com>
Sun, 25 Dec 2016 15:05:34 +0000 (16:05 +0100)
committerMarcel Werk <burntime@woltlab.com>
Sun, 25 Dec 2016 15:05:44 +0000 (16:05 +0100)
wcfsetup/install/files/lib/data/article/CategoryArticleList.class.php
wcfsetup/install/files/lib/data/article/category/ArticleCategory.class.php
wcfsetup/install/files/lib/data/category/AbstractDecoratedCategory.class.php
wcfsetup/install/files/lib/data/category/Category.class.php
wcfsetup/install/files/lib/data/smiley/category/SmileyCategory.class.php

index 9599f84465031f774738cb064d682a1f948debba..c7db4b3014b3043ac54f74d1c7e2c2648e413c3f 100644 (file)
@@ -29,7 +29,7 @@ class CategoryArticleList extends AccessibleArticleList {
                        if ($category === null) {
                                throw new \InvalidArgumentException("invalid category id '".$categoryID."' given");
                        }
-                       foreach ($category->getChildCategories() as $category) {
+                       foreach ($category->getAllChildCategories() as $category) {
                                if ($category->isAccessible()) {
                                        $categoryIDs[] = $category->categoryID;
                                }
index fc856c5cefedbb7bc7f57e53d5213f255d4c58e1..076fa98980cf4fd227e6b7b03a022f138906bd4b 100644 (file)
@@ -20,6 +20,7 @@ use wcf\system\WCF;
  * @since      3.0
  * 
  * @method             ArticleCategory[]       getChildCategories()
+ * @method             ArticleCategory[]       getAllChildCategories()
  * @method             ArticleCategory         getParentCategory()
  * @method             ArticleCategory[]       getParentCategories()
  * @method static      ArticleCategory|null    getCategory($categoryID)
index a00b5644dd8784fc2396a12501f04033678f3c71..c599b537e56c4a5a2ae85bc74dc8957ba677595e 100644 (file)
@@ -17,11 +17,17 @@ use wcf\system\exception\PermissionDeniedException;
  */
 abstract class AbstractDecoratedCategory extends DatabaseObjectDecorator {
        /**
-        * list of all child categories of this category
+        * list of child categories of this category
         * @var Category[]
         */
        protected $childCategories = null;
        
+       /**
+        * list of all child categories of this category
+        * @var Category[]
+        */
+       protected $allChildCategories = null;
+       
        /**
         * list of all parent category generations of this category
         * @var AbstractDecoratedCategory[]
@@ -64,6 +70,20 @@ abstract class AbstractDecoratedCategory extends DatabaseObjectDecorator {
                return $this->childCategories;
        }
        
+       /**
+        * @inheritDoc
+        */
+       public function getAllChildCategories() {
+               if ($this->allChildCategories === null) {
+                       $this->allChildCategories = [];
+                       foreach ($this->getDecoratedObject()->getAllChildCategories() as $category) {
+                               $this->allChildCategories[$category->categoryID] = new static($category);
+                       }
+               }
+               
+               return $this->allChildCategories;
+       }
+       
        /**
         * @inheritDoc
         */
index a85bed13532edfa86b1c992994aa7c1a129ef9dc..452470838d2ef43cc400f3fdf65f8c1684c39ed6 100644 (file)
@@ -31,11 +31,17 @@ use wcf\system\WCF;
  */
 class Category extends ProcessibleDatabaseObject implements IPermissionObject, IRouteController {
        /**
-        * list of all child categories of this category
+        * list of child categories of this category
         * @var Category[]
         */
        protected $childCategories = null;
        
+       /**
+        * list of all child categories of this category
+        * @var Category[]
+        */
+       protected $allChildCategories = null;
+       
        /**
         * list of all parent category generations of this category
         * @var Category[]
@@ -115,7 +121,7 @@ class Category extends ProcessibleDatabaseObject implements IPermissionObject, I
        }
        
        /**
-        * Returns the child categories of this category.
+        * Returns the direct child categories of this category.
         * 
         * @return      Category[]
         */
@@ -127,6 +133,25 @@ class Category extends ProcessibleDatabaseObject implements IPermissionObject, I
                return $this->childCategories;
        }
        
+       /**
+        * Returns the child categories of this category recursively.
+        *
+        * @return      Category[]
+        */
+       public function getAllChildCategories() {
+               if ($this->allChildCategories === null) {
+                       $directChildCategories = CategoryHandler::getInstance()->getChildCategories($this->categoryID);
+                       $childCategories = [];
+                       foreach ($directChildCategories as $childCategory) {
+                               $childCategories = array_replace($childCategories, $childCategory->getAllChildCategories());
+                       }
+                       
+                       $this->allChildCategories = array_replace($directChildCategories, $childCategories);
+               }
+               
+               return $this->allChildCategories;
+       }
+       
        /**
         * Returns the parent category of the category or `null` if the category has no parent category.
         * 
index d38eb46d4fefa81f99f6c3342a151441e50694ff..dad0e30ef0dd20dffa5cb7e97ee5f33dd467847f 100644 (file)
@@ -16,6 +16,7 @@ use wcf\system\WCF;
  * @package    WoltLabSuite\Core\Data\Smiley\Category
  * 
  * @method             SmileyCategory[]        getChildCategories()
+ * @method             SmileyCategory[]        getAllChildCategories()
  * @method             SmileyCategory          getParentCategory()
  * @method             SmileyCategory[]        getParentCategories()
  * @method static      SmileyCategory|null     getCategory($categoryID)