/**
  * @see Tx_PtExtbase_Tree_TreeWalkerVisitorInterface::doLastVisit()
  *
  * @param Tx_PtExtbase_Tree_NodeInterface $node
  * @param integer &$index Holds the visitation index of treewalker
  * @param integer &$level Holds level of visitation in tree, starting at 1
  */
 public function doLastVisit(Tx_PtExtbase_Tree_NodeInterface $node, &$index, &$level)
 {
     $currentNode = $this->nodeStack->top();
     $this->nodeStack->pop();
     if (!$this->nodeStack->isEmpty()) {
         $parentNode = $this->nodeStack->top();
         $this->nodeStack->pop();
         $parentNode['children'][] = $currentNode;
         $this->nodeStack->push($parentNode);
     } else {
         $this->nodeArray = $currentNode;
     }
 }
 /**
  * @see Tx_PtExtbase_Tree_TreeWalkerVisitorInterface::doLastVisit()
  *
  * @param Tx_PtExtbase_Tree_NodeInterface $node
  * @param integer &$index Holds the visitation index of treewalker
  * @param integer &$level Holds level of visitation in tree, starting at 1
  */
 public function doLastVisit(Tx_PtExtbase_Tree_NodeInterface $node, &$index, &$level)
 {
     $currentNode = $this->nodeStack->top();
     $this->nodeStack->pop();
     if ($this->lastVisitCallback) {
         $currentNode = call_user_func(array($this->lastVisitCallback['target'], $this->lastVisitCallback['method']), $node, $currentNode);
     }
     if (!$this->nodeStack->isEmpty()) {
         $parentNode = $this->nodeStack->top();
         $this->nodeStack->pop();
         $parentNode['children'][] = $currentNode;
         $currentNode['leaf'] = 'false';
         $this->nodeStack->push($parentNode);
     } else {
         $this->nodeArray = $currentNode;
     }
 }
 /**
  * Builds a tree for given namespace.
  *
  * If there are no nodes for given namespace, a new, empty tree with a single root node will be returned.
  *
  * @param $namespace
  * @return Tx_PtExtbase_Tree_Tree
  * @throws Exception
  */
 public function buildTreeForNamespace($namespace)
 {
     $nodes = $this->nodeRepository->findByNamespace($namespace);
     // We have no nodes for given namespace, so we return empty tree with single root node
     if ($nodes->count() == 0) {
         return $this->getEmptyTree($namespace);
     }
     $stack = new Tx_PtExtbase_Tree_Stack();
     $prevLft = PHP_INT_MAX;
     foreach ($nodes as $node) {
         /* @var $node Tx_PtExtbase_Tree_Node */
         /* Assertion: Nodes must be given in descending left-value order. */
         if ($node->getLft() > $prevLft) {
             throw new Exception('Nodes must be given in descending left-value order', 1307861852);
         }
         $prevLft = $node->getLft();
         #echo "<br><br>Knoten: " . $node->toString();
         if ($stack->isEmpty() || $stack->top()->getRgt() > $node->getRgt()) {
             $stack->push($node);
             #echo "Pushed on stack:" . $stack->toString();
         } else {
             #echo "Adding children:";
             while (!$stack->isEmpty() && $stack->top()->getRgt() < $node->getRgt()) {
                 #echo "In while - current node " . $node->toString() . " current topStack: " . $stack->top()->toString();
                 $stack->top()->setParent($node, false);
                 $node->addChild($stack->top(), false);
                 $stack->pop();
                 #echo "After while-iteration: ". $stack->toString();
             }
             $stack->push($node);
             #echo "After pushing after while: <ul>" . $stack->toString() . "</ul>";
         }
     }
     $tree = Tx_PtExtbase_Tree_Tree::getInstanceByRootNode($stack->top());
     $tree->setRestrictedDepth($this->restrictedDepth);
     $tree->setRespectRestrictedDepth($this->respectRestrictedDepth);
     #echo "Finished tree: " . $tree->toString();
     return $tree;
 }