From 19d915a149d8f3cf4b21b203c4ae49955a06aba3 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Tue, 2 Jul 2013 21:09:26 +0200 Subject: [PATCH] Improved iterator for nested categories --- .../lib/data/category/CategoryNode.class.php | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/wcfsetup/install/files/lib/data/category/CategoryNode.class.php b/wcfsetup/install/files/lib/data/category/CategoryNode.class.php index 832a2959ba..190e9cc2c3 100644 --- a/wcfsetup/install/files/lib/data/category/CategoryNode.class.php +++ b/wcfsetup/install/files/lib/data/category/CategoryNode.class.php @@ -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() */ -- 2.20.1