Improved iterator for nested categories
authorAlexander Ebert <ebert@woltlab.com>
Tue, 2 Jul 2013 19:09:26 +0000 (21:09 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 2 Jul 2013 19:09:26 +0000 (21:09 +0200)
wcfsetup/install/files/lib/data/category/CategoryNode.class.php

index 832a2959ba0531edcc2268e02b640d90371a2248..190e9cc2c34fe4b3fb92d65208393787b4d018e5 100644 (file)
@@ -25,6 +25,12 @@ class CategoryNode extends DatabaseObjectDecorator implements \RecursiveIterator
         */
        protected $index = 0;
        
+       /**
+        * parent node object
+        * @var wcf\data\category\CategoryNode
+        */
+       protected $parentNode = null;
+       
        /**
         * @see wcf\data\DatabaseObjectDecorator::$baseClass
         */
@@ -36,9 +42,51 @@ class CategoryNode extends DatabaseObjectDecorator implements \RecursiveIterator
         * @param       wcf\data\category\CategoryNode          $categoryNode
         */
        public function addChild(CategoryNode $categoryNode) {
+               $categoryNode->setParentNode($this);
+               
                $this->children[] = $categoryNode;
        }
        
+       /**
+        * Sets parent node object.
+        * 
+        * @param       wcf\data\category\CategoryNode          $parentNode
+        */
+       public function setParentNode(CategoryNode $parentNode) {
+               $this->parentNode = $parentNode;
+       }
+       
+       /**
+        * Returns true if this element is the last sibling.
+        * 
+        * @return      boolean
+        */
+       public function isLastSibling() {
+               foreach ($this->parentNode as $key => $child) {
+                       if ($child === $this) {
+                               if ($key == count($this->parentNode) - 1) return true;
+                               return false;
+                       }
+               }
+       }
+       
+       /**
+        * Returns the number of open parent nodes.
+        * 
+        * @return      integer
+        */
+       public function getOpenParentNodes() {
+               $element = $this;
+               $i = 0;
+       
+               while ($element->parentNode->parentNode != null && $element->isLastSibling()) {
+                       $i++;
+                       $element = $element->parentNode;
+               }
+       
+               return $i;
+       }
+       
        /**
         * @see \Countable::count()
         */