Added setMaxDepth() to prevent building the entire category tree
authorAlexander Ebert <ebert@woltlab.com>
Tue, 20 Jan 2015 11:21:23 +0000 (12:21 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 20 Jan 2015 11:21:23 +0000 (12:21 +0100)
wcfsetup/install/files/lib/data/category/CategoryNodeTree.class.php

index 55965f69b524457af881ae6e7d7ae74d4b3e7383..13a7cbb5c3ea455cbc9aa2da3d94d997397c59b2 100644 (file)
@@ -14,6 +14,13 @@ use wcf\system\exception\SystemException;
  * @category   Community Framework
  */
 class CategoryNodeTree implements \IteratorAggregate {
+       /**
+        * maximum depth considered when building the node tree.
+        * 
+        * @var integer
+        */
+       protected $maxDepth = -1;
+       
        /**
         * name of the category node class
         * @var string
@@ -52,20 +59,35 @@ class CategoryNodeTree implements \IteratorAggregate {
                }
        }
        
+       /**
+        * Sets the maximum depth considered when building the node tree, defaults
+        * to -1 which equals infinite.
+        * 
+        * @param       integer         $maxDepth
+        */
+       public function setMaxDepth($maxDepth) {
+               $this->maxDepth = $maxDepth;
+       }
+       
        /**
         * Builds the category node tree.
         */
        protected function buildTree() {
                $this->parentNode = $this->getNode($this->parentCategoryID);
-               $this->buildTreeLevel($this->parentNode);
+               $this->buildTreeLevel($this->parentNode, $this->maxDepth);
        }
        
        /**
         * Builds a certain level of the tree.
         * 
         * @param       \wcf\data\category\CategoryNode $parentNode
+        * @param       integer                         $depth
         */
-       protected function buildTreeLevel(CategoryNode $parentNode) {
+       protected function buildTreeLevel(CategoryNode $parentNode, $depth = 0) {
+               if ($this->maxDepth != -1 && $depth < 0) {
+                       return;
+               }
+               
                foreach ($this->getChildCategories($parentNode) as $childCategory) {
                        $childNode = $this->getNode($childCategory->categoryID);