From 9ae4ec26208be57ab6cc263d8caaa6648cf8e587 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Sun, 25 Dec 2016 16:05:34 +0100 Subject: [PATCH] Added method to get all child categories of a category recursively --- .../article/CategoryArticleList.class.php | 2 +- .../category/ArticleCategory.class.php | 1 + .../AbstractDecoratedCategory.class.php | 22 +++++++++++++- .../lib/data/category/Category.class.php | 29 +++++++++++++++++-- .../smiley/category/SmileyCategory.class.php | 1 + 5 files changed, 51 insertions(+), 4 deletions(-) diff --git a/wcfsetup/install/files/lib/data/article/CategoryArticleList.class.php b/wcfsetup/install/files/lib/data/article/CategoryArticleList.class.php index 9599f84465..c7db4b3014 100644 --- a/wcfsetup/install/files/lib/data/article/CategoryArticleList.class.php +++ b/wcfsetup/install/files/lib/data/article/CategoryArticleList.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/data/article/category/ArticleCategory.class.php b/wcfsetup/install/files/lib/data/article/category/ArticleCategory.class.php index fc856c5cef..076fa98980 100644 --- a/wcfsetup/install/files/lib/data/article/category/ArticleCategory.class.php +++ b/wcfsetup/install/files/lib/data/article/category/ArticleCategory.class.php @@ -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) diff --git a/wcfsetup/install/files/lib/data/category/AbstractDecoratedCategory.class.php b/wcfsetup/install/files/lib/data/category/AbstractDecoratedCategory.class.php index a00b5644dd..c599b537e5 100644 --- a/wcfsetup/install/files/lib/data/category/AbstractDecoratedCategory.class.php +++ b/wcfsetup/install/files/lib/data/category/AbstractDecoratedCategory.class.php @@ -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 */ diff --git a/wcfsetup/install/files/lib/data/category/Category.class.php b/wcfsetup/install/files/lib/data/category/Category.class.php index a85bed1353..452470838d 100644 --- a/wcfsetup/install/files/lib/data/category/Category.class.php +++ b/wcfsetup/install/files/lib/data/category/Category.class.php @@ -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. * diff --git a/wcfsetup/install/files/lib/data/smiley/category/SmileyCategory.class.php b/wcfsetup/install/files/lib/data/smiley/category/SmileyCategory.class.php index d38eb46d4f..dad0e30ef0 100644 --- a/wcfsetup/install/files/lib/data/smiley/category/SmileyCategory.class.php +++ b/wcfsetup/install/files/lib/data/smiley/category/SmileyCategory.class.php @@ -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) -- 2.20.1