From 6d868311c8334292ab51e82c4115d4311b0db8a0 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Fri, 6 Jul 2012 11:01:33 +0200 Subject: [PATCH] Makes it easier to use own category nodes 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"). --- .../lib/data/category/CategoryNode.class.php | 31 +++++++++++++++++-- .../data/category/CategoryNodeList.class.php | 8 ++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/wcfsetup/install/files/lib/data/category/CategoryNode.class.php b/wcfsetup/install/files/lib/data/category/CategoryNode.class.php index 5128420211..d3ed548dc4 100644 --- a/wcfsetup/install/files/lib/data/category/CategoryNode.class.php +++ b/wcfsetup/install/files/lib/data/category/CategoryNode.class.php @@ -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 + */ + 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() */ diff --git a/wcfsetup/install/files/lib/data/category/CategoryNodeList.class.php b/wcfsetup/install/files/lib/data/category/CategoryNodeList.class.php index 23bf7ccf36..211a3a477a 100644 --- a/wcfsetup/install/files/lib/data/category/CategoryNodeList.class.php +++ b/wcfsetup/install/files/lib/data/category/CategoryNodeList.class.php @@ -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); } /** -- 2.20.1