| 644 | | $descendants_by_parent = array(); |
|---|
| 645 | | if (is_array($descendants) && count($descendants)) |
|---|
| 646 | | { |
|---|
| 647 | | for ($i = 0; $i < count($descendants); $i++) |
|---|
| 648 | | { |
|---|
| 649 | | $descendant = $descendants[$i]; |
|---|
| 650 | | if (!isset($descendants_by_parent[$descendant->getParentIdValue()])) |
|---|
| 651 | | { |
|---|
| 652 | | $descendants_by_parent[$descendant->getParentIdValue()] = array(); |
|---|
| 653 | | } |
|---|
| 654 | | $descendants_by_parent[$descendant->getParentIdValue()][] = $descendant; |
|---|
| | 644 | $prev = array($node->getRightValue()); |
|---|
| | 645 | $i = 0; |
|---|
| | 646 | |
|---|
| | 647 | foreach ($descendants as $cur) { |
|---|
| | 648 | //get back to the parent |
|---|
| | 649 | while ($cur->getRightValue() > $prev[$i]) { |
|---|
| | 650 | $i--; |
|---|
| 656 | | } |
|---|
| 657 | | |
|---|
| 658 | | $parent_ids = array_keys($descendants_by_parent); |
|---|
| 659 | | foreach ($descendants_by_parent as $parent_id => $parent_descendants) |
|---|
| 660 | | { |
|---|
| 661 | | foreach ($parent_descendants as $descendant) |
|---|
| 662 | | { |
|---|
| 663 | | if (in_array($descendant->getParentIdValue(), $parent_ids)) |
|---|
| 664 | | { |
|---|
| 665 | | $descendant->setLevel($descendants_by_parent[$parent_id][0]->getLevel()); |
|---|
| 666 | | } |
|---|
| | 652 | |
|---|
| | 653 | $i++; |
|---|
| | 654 | $cur->setLevel($i); |
|---|
| | 655 | $prev[$i] = $cur->getRightValue(); |
|---|
| | 656 | } |
|---|
| | 657 | |
|---|
| | 658 | return $descendants; |
|---|
| | 659 | } |
|---|
| | 660 | |
|---|
| | 661 | /** |
|---|
| | 662 | * Returns given node descendants (n level) in reversed pre-order. |
|---|
| | 663 | * |
|---|
| | 664 | * |
|---|
| | 665 | * @param BaseObject $node |
|---|
| | 666 | * @param string $peer_method |
|---|
| | 667 | * @return array Node descendants, reversed pre-order |
|---|
| | 668 | */ |
|---|
| | 669 | public function getReversedDescendants(BaseObject $node, $peer_method = 'doSelect') |
|---|
| | 670 | { |
|---|
| | 671 | $descendants = array(); |
|---|
| | 672 | |
|---|
| | 673 | if (!$node->isLeaf()) |
|---|
| | 674 | { |
|---|
| | 675 | $c = new Criteria(); |
|---|
| | 676 | $c->addAnd(self::getColumnConstant(get_class($node), 'left'), $node->getLeftValue(), Criteria::GREATER_THAN); |
|---|
| | 677 | $c->addAnd(self::getColumnConstant(get_class($node), 'right'), $node->getRightValue(), Criteria::LESS_THAN); |
|---|
| | 678 | $c->addAnd(self::getColumnConstant(get_class($node), 'scope'), $node->getScopeIdValue(), Criteria::EQUAL); |
|---|
| | 679 | $c->addDescendingOrderByColumn(self::getColumnConstant(get_class($node), 'right')); |
|---|
| | 680 | |
|---|
| | 681 | $descendants = call_user_func(array(get_class($node->getPeer()), $peer_method), $c); |
|---|
| | 682 | } |
|---|
| | 683 | |
|---|
| | 684 | $prev = array($node); |
|---|
| | 685 | |
|---|
| | 686 | foreach ($descendants as $cur) { |
|---|
| | 687 | //get back to an ancestor |
|---|
| | 688 | //this SHOULD always be the parent |
|---|
| | 689 | while ($cur->getLeftValue() < $prev[count($prev)-1]->getLeftValue()) { |
|---|
| | 690 | array_pop($prev); |
|---|