2007-01-15 23:12:22 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
2008-01-23 01:52:53 +03:00
|
|
|
* <http://www.phpdoctrine.org>.
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-15 23:12:22 +03:00
|
|
|
/**
|
|
|
|
* Doctrine_Node_NestedSet
|
|
|
|
*
|
2007-05-08 23:35:31 +04:00
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Node
|
2007-05-08 23:35:31 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2008-02-22 21:11:35 +03:00
|
|
|
* @link www.phpdoctrine.org
|
2007-05-08 23:35:31 +04:00
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Joe Simms <joe.simms@websites4.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
|
|
|
class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Interface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* test if node has previous sibling
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasPrevSibling()
|
|
|
|
{
|
|
|
|
return $this->isValidNode($this->getPrevSibling());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test if node has next sibling
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasNextSibling()
|
|
|
|
{
|
|
|
|
return $this->isValidNode($this->getNextSibling());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test if node has children
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasChildren()
|
|
|
|
{
|
2008-02-11 20:08:22 +03:00
|
|
|
return (($this->getRightValue() - $this->getLeftValue()) > 1);
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test if node has parent
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasParent()
|
|
|
|
{
|
|
|
|
return !$this->isRoot();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets record of prev sibling or empty record
|
|
|
|
*
|
|
|
|
* @return object Doctrine_Record
|
|
|
|
*/
|
|
|
|
public function getPrevSibling()
|
|
|
|
{
|
2007-08-04 18:33:15 +04:00
|
|
|
$baseAlias = $this->_tree->getBaseAlias();
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->getBaseQuery();
|
2007-08-04 18:33:15 +04:00
|
|
|
$q = $q->addWhere("$baseAlias.rgt = ?", $this->getLeftValue() - 1);
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->returnQueryWithRootId($q, $this->getRootValue());
|
|
|
|
$result = $q->execute();
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
if (count($result) <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result instanceof Doctrine_Collection) {
|
|
|
|
$sibling = $result->getFirst();
|
|
|
|
} else if (is_array($result)) {
|
|
|
|
$sibling = array_shift($result);
|
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
return $sibling;
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets record of next sibling or empty record
|
|
|
|
*
|
|
|
|
* @return object Doctrine_Record
|
|
|
|
*/
|
|
|
|
public function getNextSibling()
|
|
|
|
{
|
2007-08-04 18:33:15 +04:00
|
|
|
$baseAlias = $this->_tree->getBaseAlias();
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->getBaseQuery();
|
2007-08-04 18:33:15 +04:00
|
|
|
$q = $q->addWhere("$baseAlias.lft = ?", $this->getRightValue() + 1);
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->returnQueryWithRootId($q, $this->getRootValue());
|
|
|
|
$result = $q->execute();
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
if (count($result) <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
if ($result instanceof Doctrine_Collection) {
|
|
|
|
$sibling = $result->getFirst();
|
|
|
|
} else if (is_array($result)) {
|
|
|
|
$sibling = array_shift($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sibling;
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets siblings for node
|
|
|
|
*
|
|
|
|
* @return array array of sibling Doctrine_Record objects
|
|
|
|
*/
|
|
|
|
public function getSiblings($includeNode = false)
|
|
|
|
{
|
|
|
|
$parent = $this->getParent();
|
|
|
|
$siblings = array();
|
2007-07-08 16:57:52 +04:00
|
|
|
if ($parent->exists()) {
|
|
|
|
foreach ($parent->getNode()->getChildren() as $child) {
|
|
|
|
if ($this->isEqualTo($child) && !$includeNode) {
|
2007-01-15 23:12:22 +03:00
|
|
|
continue;
|
2007-07-08 16:57:52 +04:00
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
$siblings[] = $child;
|
2007-07-08 16:57:52 +04:00
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
return $siblings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets record of first child or empty record
|
|
|
|
*
|
|
|
|
* @return object Doctrine_Record
|
|
|
|
*/
|
|
|
|
public function getFirstChild()
|
|
|
|
{
|
2007-08-04 18:33:15 +04:00
|
|
|
$baseAlias = $this->_tree->getBaseAlias();
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->getBaseQuery();
|
2007-08-04 18:33:15 +04:00
|
|
|
$q->addWhere("$baseAlias.lft = ?", $this->getLeftValue() + 1);
|
2007-07-08 16:57:52 +04:00
|
|
|
$this->_tree->returnQueryWithRootId($q, $this->getRootValue());
|
|
|
|
$result = $q->execute();
|
|
|
|
|
|
|
|
if (count($result) <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
if ($result instanceof Doctrine_Collection) {
|
|
|
|
$child = $result->getFirst();
|
|
|
|
} else if (is_array($result)) {
|
|
|
|
$child = array_shift($result);
|
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
return $child;
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets record of last child or empty record
|
|
|
|
*
|
|
|
|
* @return object Doctrine_Record
|
|
|
|
*/
|
|
|
|
public function getLastChild()
|
|
|
|
{
|
2007-08-04 18:33:15 +04:00
|
|
|
$baseAlias = $this->_tree->getBaseAlias();
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->getBaseQuery();
|
2007-08-04 18:33:15 +04:00
|
|
|
$q->addWhere("$baseAlias.rgt = ?", $this->getRightValue() - 1);
|
2007-07-08 16:57:52 +04:00
|
|
|
$this->_tree->returnQueryWithRootId($q, $this->getRootValue());
|
|
|
|
$result = $q->execute();
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
if (count($result) <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result instanceof Doctrine_Collection) {
|
|
|
|
$child = $result->getFirst();
|
|
|
|
} else if (is_array($result)) {
|
|
|
|
$child = array_shift($result);
|
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
return $child;
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets children for node (direct descendants only)
|
|
|
|
*
|
2007-07-08 16:57:52 +04:00
|
|
|
* @return mixed The children of the node or FALSE if the node has no children.
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
|
|
|
public function getChildren()
|
|
|
|
{
|
2007-07-08 16:57:52 +04:00
|
|
|
return $this->getDescendants(1);
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets descendants for node (direct descendants only)
|
|
|
|
*
|
2007-07-08 16:57:52 +04:00
|
|
|
* @return mixed The descendants of the node or FALSE if the node has no descendants.
|
|
|
|
* @todo Currently all descendants are fetched, no matter the depth. Maybe there is a better
|
|
|
|
* solution with less overhead.
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
2007-07-08 16:57:52 +04:00
|
|
|
public function getDescendants($depth = null, $includeNode = false)
|
2007-01-15 23:12:22 +03:00
|
|
|
{
|
2007-08-04 18:33:15 +04:00
|
|
|
$baseAlias = $this->_tree->getBaseAlias();
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->getBaseQuery();
|
|
|
|
$params = array($this->record->get('lft'), $this->record->get('rgt'));
|
|
|
|
|
|
|
|
if ($includeNode) {
|
2007-08-04 18:33:15 +04:00
|
|
|
$q->addWhere("$baseAlias.lft >= ? AND $baseAlias.rgt <= ?", $params)->addOrderBy("$baseAlias.lft asc");
|
2007-07-08 16:57:52 +04:00
|
|
|
} else {
|
2007-08-04 18:33:15 +04:00
|
|
|
$q->addWhere("$baseAlias.lft > ? AND $baseAlias.rgt < ?", $params)->addOrderBy("$baseAlias.lft asc");
|
2007-07-08 16:57:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($depth !== null) {
|
2007-08-04 18:33:15 +04:00
|
|
|
$q->addWhere("$baseAlias.level <= ?", $this->record['level'] + $depth);
|
2007-07-08 16:57:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$q = $this->_tree->returnQueryWithRootId($q, $this->getRootValue());
|
|
|
|
$result = $q->execute();
|
|
|
|
|
|
|
|
if (count($result) <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets record of parent or empty record
|
|
|
|
*
|
|
|
|
* @return object Doctrine_Record
|
|
|
|
*/
|
|
|
|
public function getParent()
|
|
|
|
{
|
2007-08-04 18:33:15 +04:00
|
|
|
$baseAlias = $this->_tree->getBaseAlias();
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->getBaseQuery();
|
2007-08-04 18:33:15 +04:00
|
|
|
$q->addWhere("$baseAlias.lft < ? AND $baseAlias.rgt > ?", array($this->getLeftValue(), $this->getRightValue()))
|
|
|
|
->addOrderBy("$baseAlias.rgt asc");
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->returnQueryWithRootId($q, $this->getRootValue());
|
|
|
|
$result = $q->execute();
|
2007-02-10 21:07:05 +03:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
if (count($result) <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result instanceof Doctrine_Collection) {
|
|
|
|
$parent = $result->getFirst();
|
|
|
|
} else if (is_array($result)) {
|
|
|
|
$parent = array_shift($result);
|
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
return $parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets ancestors for node
|
|
|
|
*
|
2007-07-08 16:57:52 +04:00
|
|
|
* @param integer $deth The depth 'upstairs'.
|
|
|
|
* @return mixed The ancestors of the node or FALSE if the node has no ancestors (this
|
|
|
|
* basically means it's a root node).
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
2007-07-08 16:57:52 +04:00
|
|
|
public function getAncestors($depth = null)
|
2007-01-15 23:12:22 +03:00
|
|
|
{
|
2007-08-04 18:33:15 +04:00
|
|
|
$baseAlias = $this->_tree->getBaseAlias();
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->getBaseQuery();
|
2007-08-04 18:33:15 +04:00
|
|
|
$q->addWhere("$baseAlias.lft < ? AND $baseAlias.rgt > ?", array($this->getLeftValue(), $this->getRightValue()))
|
|
|
|
->addOrderBy("$baseAlias.lft asc");
|
2007-07-08 16:57:52 +04:00
|
|
|
if ($depth !== null) {
|
2007-08-04 18:33:15 +04:00
|
|
|
$q->addWhere("$baseAlias.level >= ?", $this->record['level'] - $depth);
|
2007-07-08 16:57:52 +04:00
|
|
|
}
|
|
|
|
$q = $this->_tree->returnQueryWithRootId($q, $this->getRootValue());
|
2007-02-10 20:32:26 +03:00
|
|
|
$ancestors = $q->execute();
|
2007-07-08 16:57:52 +04:00
|
|
|
if (count($ancestors) <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
return $ancestors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets path to node from root, uses record::toString() method to get node names
|
|
|
|
*
|
|
|
|
* @param string $seperator path seperator
|
|
|
|
* @param bool $includeNode whether or not to include node at end of path
|
|
|
|
* @return string string representation of path
|
|
|
|
*/
|
|
|
|
public function getPath($seperator = ' > ', $includeRecord = false)
|
|
|
|
{
|
|
|
|
$path = array();
|
|
|
|
$ancestors = $this->getAncestors();
|
2008-02-04 00:29:57 +03:00
|
|
|
if ($ancestors) {
|
|
|
|
foreach ($ancestors as $ancestor) {
|
|
|
|
$path[] = $ancestor->__toString();
|
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
2007-07-08 16:57:52 +04:00
|
|
|
if ($includeRecord) {
|
2007-01-15 23:12:22 +03:00
|
|
|
$path[] = $this->getRecord()->__toString();
|
2007-07-08 16:57:52 +04:00
|
|
|
}
|
|
|
|
|
2007-01-15 23:12:22 +03:00
|
|
|
return implode($seperator, $path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets number of children (direct descendants)
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getNumberChildren()
|
|
|
|
{
|
2007-08-20 11:45:29 +04:00
|
|
|
return count($this->getChildren());
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets number of descendants (children and their children)
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getNumberDescendants()
|
|
|
|
{
|
|
|
|
return ($this->getRightValue() - $this->getLeftValue() - 1) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* inserts node as parent of dest record
|
|
|
|
*
|
2007-07-08 16:57:52 +04:00
|
|
|
* @return bool
|
|
|
|
* @todo Wrap in transaction
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
|
|
|
public function insertAsParentOf(Doctrine_Record $dest)
|
|
|
|
{
|
|
|
|
// cannot insert a node that has already has a place within the tree
|
2007-07-08 16:57:52 +04:00
|
|
|
if ($this->isValidNode()) {
|
2007-01-15 23:12:22 +03:00
|
|
|
return false;
|
2007-07-08 16:57:52 +04:00
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
// cannot insert as parent of root
|
2007-07-08 16:57:52 +04:00
|
|
|
if ($dest->getNode()->isRoot()) {
|
2007-01-15 23:12:22 +03:00
|
|
|
return false;
|
2007-07-08 16:57:52 +04:00
|
|
|
}
|
2007-05-07 19:49:23 +04:00
|
|
|
$newRoot = $dest->getNode()->getRootValue();
|
|
|
|
$this->shiftRLValues($dest->getNode()->getLeftValue(), 1, $newRoot);
|
|
|
|
$this->shiftRLValues($dest->getNode()->getRightValue() + 2, 1, $newRoot);
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
$newLeft = $dest->getNode()->getLeftValue();
|
2007-01-16 03:12:52 +03:00
|
|
|
$newRight = $dest->getNode()->getRightValue() + 2;
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
$this->record['level'] = $dest['level'] - 1;
|
2007-01-15 23:12:22 +03:00
|
|
|
$this->insertNode($newLeft, $newRight, $newRoot);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* inserts node as previous sibling of dest record
|
|
|
|
*
|
2007-07-08 16:57:52 +04:00
|
|
|
* @return bool
|
|
|
|
* @todo Wrap in transaction
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
|
|
|
public function insertAsPrevSiblingOf(Doctrine_Record $dest)
|
|
|
|
{
|
|
|
|
// cannot insert a node that has already has a place within the tree
|
2007-07-08 16:57:52 +04:00
|
|
|
if ($this->isValidNode())
|
2007-01-15 23:12:22 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
$newLeft = $dest->getNode()->getLeftValue();
|
|
|
|
$newRight = $dest->getNode()->getLeftValue() + 1;
|
|
|
|
$newRoot = $dest->getNode()->getRootValue();
|
|
|
|
|
|
|
|
$this->shiftRLValues($newLeft, 2, $newRoot);
|
2007-07-08 16:57:52 +04:00
|
|
|
$this->record['level'] = $dest['level'];
|
2007-01-15 23:12:22 +03:00
|
|
|
$this->insertNode($newLeft, $newRight, $newRoot);
|
|
|
|
// update destination left/right values to prevent a refresh
|
|
|
|
// $dest->getNode()->setLeftValue($dest->getNode()->getLeftValue() + 2);
|
|
|
|
// $dest->getNode()->setRightValue($dest->getNode()->getRightValue() + 2);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* inserts node as next sibling of dest record
|
|
|
|
*
|
2007-07-08 16:57:52 +04:00
|
|
|
* @return bool
|
|
|
|
* @todo Wrap in transaction
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
|
|
|
public function insertAsNextSiblingOf(Doctrine_Record $dest)
|
|
|
|
{
|
|
|
|
// cannot insert a node that has already has a place within the tree
|
2007-05-07 19:49:23 +04:00
|
|
|
if ($this->isValidNode())
|
2007-01-15 23:12:22 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
$newLeft = $dest->getNode()->getRightValue() + 1;
|
|
|
|
$newRight = $dest->getNode()->getRightValue() + 2;
|
|
|
|
$newRoot = $dest->getNode()->getRootValue();
|
|
|
|
|
|
|
|
$this->shiftRLValues($newLeft, 2, $newRoot);
|
2007-07-08 16:57:52 +04:00
|
|
|
$this->record['level'] = $dest['level'];
|
2007-01-15 23:12:22 +03:00
|
|
|
$this->insertNode($newLeft, $newRight, $newRoot);
|
|
|
|
|
|
|
|
// update destination left/right values to prevent a refresh
|
|
|
|
// no need, node not affected
|
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
return true;
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* inserts node as first child of dest record
|
|
|
|
*
|
2007-07-08 16:57:52 +04:00
|
|
|
* @return bool
|
|
|
|
* @todo Wrap in transaction
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
|
|
|
public function insertAsFirstChildOf(Doctrine_Record $dest)
|
|
|
|
{
|
|
|
|
// cannot insert a node that has already has a place within the tree
|
2007-05-07 19:49:23 +04:00
|
|
|
if ($this->isValidNode())
|
2007-01-15 23:12:22 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
$newLeft = $dest->getNode()->getLeftValue() + 1;
|
|
|
|
$newRight = $dest->getNode()->getLeftValue() + 2;
|
|
|
|
$newRoot = $dest->getNode()->getRootValue();
|
|
|
|
|
|
|
|
$this->shiftRLValues($newLeft, 2, $newRoot);
|
2007-07-08 16:57:52 +04:00
|
|
|
$this->record['level'] = $dest['level'] + 1;
|
2007-01-15 23:12:22 +03:00
|
|
|
$this->insertNode($newLeft, $newRight, $newRoot);
|
|
|
|
|
|
|
|
// update destination left/right values to prevent a refresh
|
|
|
|
// $dest->getNode()->setRightValue($dest->getNode()->getRightValue() + 2);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* inserts node as last child of dest record
|
|
|
|
*
|
2007-07-08 16:57:52 +04:00
|
|
|
* @return bool
|
|
|
|
* @todo Wrap in transaction
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
|
|
|
public function insertAsLastChildOf(Doctrine_Record $dest)
|
|
|
|
{
|
|
|
|
// cannot insert a node that has already has a place within the tree
|
2007-05-07 19:49:23 +04:00
|
|
|
if ($this->isValidNode())
|
2007-01-15 23:12:22 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
$newLeft = $dest->getNode()->getRightValue();
|
|
|
|
$newRight = $dest->getNode()->getRightValue() + 1;
|
|
|
|
$newRoot = $dest->getNode()->getRootValue();
|
|
|
|
|
|
|
|
$this->shiftRLValues($newLeft, 2, $newRoot);
|
2007-07-08 16:57:52 +04:00
|
|
|
$this->record['level'] = $dest['level'] + 1;
|
2007-01-15 23:12:22 +03:00
|
|
|
$this->insertNode($newLeft, $newRight, $newRoot);
|
|
|
|
|
|
|
|
// update destination left/right values to prevent a refresh
|
|
|
|
// $dest->getNode()->setRightValue($dest->getNode()->getRightValue() + 2);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-07 19:49:23 +04:00
|
|
|
/**
|
|
|
|
* Accomplishes moving of nodes between different trees.
|
2007-08-04 21:02:25 +04:00
|
|
|
* Used by the move* methods if the root values of the two nodes are different.
|
2007-05-07 19:49:23 +04:00
|
|
|
*
|
|
|
|
* @param Doctrine_Record $dest
|
|
|
|
* @param unknown_type $newLeftValue
|
|
|
|
* @param unknown_type $moveType
|
2007-07-08 16:57:52 +04:00
|
|
|
* @todo Better exception handling/wrapping
|
2007-05-07 19:49:23 +04:00
|
|
|
*/
|
|
|
|
private function _moveBetweenTrees(Doctrine_Record $dest, $newLeftValue, $moveType)
|
|
|
|
{
|
|
|
|
$conn = $this->record->getTable()->getConnection();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$conn->beginTransaction();
|
|
|
|
|
|
|
|
// Move between trees: Detach from old tree & insert into new tree
|
|
|
|
$newRoot = $dest->getNode()->getRootValue();
|
|
|
|
$oldRoot = $this->getRootValue();
|
|
|
|
$oldLft = $this->getLeftValue();
|
|
|
|
$oldRgt = $this->getRightValue();
|
2007-07-08 16:57:52 +04:00
|
|
|
$oldLevel = $this->record['level'];
|
2007-05-07 19:49:23 +04:00
|
|
|
|
|
|
|
// Prepare target tree for insertion, make room
|
|
|
|
$this->shiftRlValues($newLeftValue, $oldRgt - $oldLft - 1, $newRoot);
|
|
|
|
|
|
|
|
// Set new root id for this node
|
|
|
|
$this->setRootValue($newRoot);
|
|
|
|
$this->record->save();
|
|
|
|
|
|
|
|
// Close gap in old tree
|
|
|
|
$first = $oldRgt + 1;
|
|
|
|
$delta = $oldLft - $oldRgt - 1;
|
|
|
|
$this->shiftRLValues($first, $delta, $oldRoot);
|
|
|
|
|
|
|
|
// Insert this node as a new node
|
|
|
|
$this->setRightValue(0);
|
|
|
|
$this->setLeftValue(0);
|
|
|
|
|
|
|
|
switch ($moveType) {
|
|
|
|
case 'moveAsPrevSiblingOf':
|
|
|
|
$this->insertAsPrevSiblingOf($dest);
|
|
|
|
break;
|
|
|
|
case 'moveAsFirstChildOf':
|
|
|
|
$this->insertAsFirstChildOf($dest);
|
|
|
|
break;
|
|
|
|
case 'moveAsNextSiblingOf':
|
|
|
|
$this->insertAsNextSiblingOf($dest);
|
|
|
|
break;
|
|
|
|
case 'moveAsLastChildOf':
|
|
|
|
$this->insertAsLastChildOf($dest);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("Unknown move operation: $moveType.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$diff = $oldRgt - $oldLft;
|
|
|
|
$this->setRightValue($this->getLeftValue() + ($oldRgt - $oldLft));
|
|
|
|
$this->record->save();
|
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
$newLevel = $this->record['level'];
|
|
|
|
$levelDiff = $newLevel - $oldLevel;
|
|
|
|
|
2007-05-07 19:49:23 +04:00
|
|
|
// Relocate descendants of the node
|
|
|
|
$diff = $this->getLeftValue() - $oldLft;
|
2007-07-29 23:38:11 +04:00
|
|
|
$componentName = $this->_tree->getBaseComponent();
|
2007-10-21 13:00:40 +04:00
|
|
|
$rootColName = $this->_tree->getAttribute('rootColumnName');
|
2007-05-07 19:49:23 +04:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
// Update lft/rgt/root/level for all descendants
|
|
|
|
$q = new Doctrine_Query($conn);
|
2007-05-07 19:49:23 +04:00
|
|
|
$q = $q->update($componentName)
|
2007-07-24 15:34:00 +04:00
|
|
|
->set($componentName . '.lft', 'lft + ?', $diff)
|
|
|
|
->set($componentName . '.rgt', 'rgt + ?', $diff)
|
|
|
|
->set($componentName . '.level', 'level + ?', $levelDiff)
|
|
|
|
->set($componentName . '.' . $rootColName, '?', $newRoot)
|
2007-05-07 19:49:23 +04:00
|
|
|
->where($componentName . '.lft > ? AND ' . $componentName . '.rgt < ?',
|
|
|
|
array($oldLft, $oldRgt));
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->returnQueryWithRootId($q, $oldRoot);
|
2007-05-07 19:49:23 +04:00
|
|
|
$q->execute();
|
|
|
|
|
|
|
|
$conn->commit();
|
2007-07-08 16:57:52 +04:00
|
|
|
} catch (Exception $e) {
|
2007-05-07 19:49:23 +04:00
|
|
|
$conn->rollback();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-15 23:12:22 +03:00
|
|
|
/**
|
|
|
|
* moves node as prev sibling of dest record
|
2007-05-07 19:49:23 +04:00
|
|
|
*
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
|
|
|
public function moveAsPrevSiblingOf(Doctrine_Record $dest)
|
|
|
|
{
|
2007-05-07 19:49:23 +04:00
|
|
|
if ($dest->getNode()->getRootValue() != $this->getRootValue()) {
|
|
|
|
// Move between trees
|
|
|
|
$this->_moveBetweenTrees($dest, $dest->getNode()->getLeftValue(), __FUNCTION__);
|
|
|
|
} else {
|
|
|
|
// Move within the tree
|
2007-07-08 16:57:52 +04:00
|
|
|
$oldLevel = $this->record['level'];
|
|
|
|
$this->record['level'] = $dest['level'];
|
|
|
|
$this->updateNode($dest->getNode()->getLeftValue(), $this->record['level'] - $oldLevel);
|
2007-05-07 19:49:23 +04:00
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* moves node as next sibling of dest record
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function moveAsNextSiblingOf(Doctrine_Record $dest)
|
|
|
|
{
|
2007-05-07 19:49:23 +04:00
|
|
|
if ($dest->getNode()->getRootValue() != $this->getRootValue()) {
|
|
|
|
// Move between trees
|
|
|
|
$this->_moveBetweenTrees($dest, $dest->getNode()->getRightValue() + 1, __FUNCTION__);
|
|
|
|
} else {
|
|
|
|
// Move within tree
|
2007-07-08 16:57:52 +04:00
|
|
|
$oldLevel = $this->record['level'];
|
|
|
|
$this->record['level'] = $dest['level'];
|
|
|
|
$this->updateNode($dest->getNode()->getRightValue() + 1, $this->record['level'] - $oldLevel);
|
2007-05-07 19:49:23 +04:00
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* moves node as first child of dest record
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function moveAsFirstChildOf(Doctrine_Record $dest)
|
|
|
|
{
|
2007-05-07 19:49:23 +04:00
|
|
|
if ($dest->getNode()->getRootValue() != $this->getRootValue()) {
|
|
|
|
// Move between trees
|
|
|
|
$this->_moveBetweenTrees($dest, $dest->getNode()->getLeftValue() + 1, __FUNCTION__);
|
|
|
|
} else {
|
|
|
|
// Move within tree
|
2007-07-08 16:57:52 +04:00
|
|
|
$oldLevel = $this->record['level'];
|
|
|
|
$this->record['level'] = $dest['level'] + 1;
|
|
|
|
$this->updateNode($dest->getNode()->getLeftValue() + 1, $this->record['level'] - $oldLevel);
|
2007-05-07 19:49:23 +04:00
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* moves node as last child of dest record
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function moveAsLastChildOf(Doctrine_Record $dest)
|
|
|
|
{
|
2007-05-07 19:49:23 +04:00
|
|
|
if ($dest->getNode()->getRootValue() != $this->getRootValue()) {
|
|
|
|
// Move between trees
|
|
|
|
$this->_moveBetweenTrees($dest, $dest->getNode()->getRightValue(), __FUNCTION__);
|
|
|
|
} else {
|
|
|
|
// Move within tree
|
2007-07-08 16:57:52 +04:00
|
|
|
$oldLevel = $this->record['level'];
|
|
|
|
$this->record['level'] = $dest['level'] + 1;
|
|
|
|
$this->updateNode($dest->getNode()->getRightValue(), $this->record['level'] - $oldLevel);
|
2007-05-07 19:49:23 +04:00
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-07 22:29:52 +04:00
|
|
|
/**
|
2007-07-08 16:57:52 +04:00
|
|
|
* Makes this node a root node. Only used in multiple-root trees.
|
2007-05-07 22:29:52 +04:00
|
|
|
*
|
|
|
|
* @todo Exception handling/wrapping
|
|
|
|
*/
|
2007-05-08 23:35:31 +04:00
|
|
|
public function makeRoot($newRootId)
|
2007-05-07 22:29:52 +04:00
|
|
|
{
|
|
|
|
// TODO: throw exception instead?
|
2007-10-21 13:00:40 +04:00
|
|
|
if ($this->getLeftValue() == 1 || ! $this->_tree->getAttribute('hasManyRoots')) {
|
2007-05-07 22:29:52 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$oldRgt = $this->getRightValue();
|
|
|
|
$oldLft = $this->getLeftValue();
|
|
|
|
$oldRoot = $this->getRootValue();
|
2007-07-08 16:57:52 +04:00
|
|
|
$oldLevel = $this->record['level'];
|
2007-05-07 22:29:52 +04:00
|
|
|
|
|
|
|
try {
|
|
|
|
$conn = $this->record->getTable()->getConnection();
|
|
|
|
$conn->beginTransaction();
|
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
// Detach from old tree (close gap in old tree)
|
2007-05-07 22:29:52 +04:00
|
|
|
$first = $oldRgt + 1;
|
|
|
|
$delta = $oldLft - $oldRgt - 1;
|
|
|
|
$this->shiftRLValues($first, $delta, $this->getRootValue());
|
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
// Set new lft/rgt/root/level values for root node
|
2007-05-07 22:29:52 +04:00
|
|
|
$this->setLeftValue(1);
|
|
|
|
$this->setRightValue($oldRgt - $oldLft + 1);
|
2007-05-08 23:35:31 +04:00
|
|
|
$this->setRootValue($newRootId);
|
2007-07-08 16:57:52 +04:00
|
|
|
$this->record['level'] = 0;
|
2007-05-07 22:29:52 +04:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
// Update descendants lft/rgt/root/level values
|
2007-05-07 22:29:52 +04:00
|
|
|
$diff = 1 - $oldLft;
|
2007-05-08 23:35:31 +04:00
|
|
|
$newRoot = $newRootId;
|
2007-07-29 23:38:11 +04:00
|
|
|
$componentName = $this->_tree->getBaseComponent();
|
2007-10-21 13:00:40 +04:00
|
|
|
$rootColName = $this->_tree->getAttribute('rootColumnName');
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = new Doctrine_Query($conn);
|
2007-05-07 22:29:52 +04:00
|
|
|
$q = $q->update($componentName)
|
2007-07-24 15:34:00 +04:00
|
|
|
->set($componentName . '.lft', 'lft + ?', $diff)
|
|
|
|
->set($componentName . '.rgt', 'rgt + ?', $diff)
|
|
|
|
->set($componentName . '.level', 'level - ?', $oldLevel)
|
|
|
|
->set($componentName . '.' . $rootColName, '?', $newRoot)
|
2007-05-07 22:29:52 +04:00
|
|
|
->where($componentName . '.lft > ? AND ' . $componentName . '.rgt < ?',
|
|
|
|
array($oldLft, $oldRgt));
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->returnQueryWithRootId($q, $oldRoot);
|
2007-05-07 22:29:52 +04:00
|
|
|
$q->execute();
|
|
|
|
|
|
|
|
$conn->commit();
|
2007-07-08 16:57:52 +04:00
|
|
|
|
|
|
|
} catch (Exception $e) {
|
2007-05-07 22:29:52 +04:00
|
|
|
$conn->rollback();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* adds node as last child of record
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function addChild(Doctrine_Record $record)
|
|
|
|
{
|
|
|
|
$record->getNode()->insertAsLastChildOf($this->getRecord());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* determines if node is leaf
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isLeaf()
|
|
|
|
{
|
2007-01-16 03:12:52 +03:00
|
|
|
return (($this->getRightValue() - $this->getLeftValue()) == 1);
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* determines if node is root
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isRoot()
|
|
|
|
{
|
2007-01-16 03:12:52 +03:00
|
|
|
return ($this->getLeftValue() == 1);
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* determines if node is equal to subject node
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isEqualTo(Doctrine_Record $subj)
|
|
|
|
{
|
2007-01-16 03:12:52 +03:00
|
|
|
return (($this->getLeftValue() == $subj->getNode()->getLeftValue()) &&
|
|
|
|
($this->getRightValue() == $subj->getNode()->getRightValue()) &&
|
|
|
|
($this->getRootValue() == $subj->getNode()->getRootValue())
|
|
|
|
);
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* determines if node is child of subject node
|
|
|
|
*
|
2007-01-16 03:12:52 +03:00
|
|
|
* @return bool
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
|
|
|
public function isDescendantOf(Doctrine_Record $subj)
|
|
|
|
{
|
2007-07-08 16:57:52 +04:00
|
|
|
return (($this->getLeftValue() > $subj->getNode()->getLeftValue()) &&
|
|
|
|
($this->getRightValue() < $subj->getNode()->getRightValue()) &&
|
|
|
|
($this->getRootValue() == $subj->getNode()->getRootValue()));
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* determines if node is child of or sibling to subject node
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isDescendantOfOrEqualTo(Doctrine_Record $subj)
|
|
|
|
{
|
2007-07-08 16:57:52 +04:00
|
|
|
return (($this->getLeftValue() >= $subj->getNode()->getLeftValue()) &&
|
|
|
|
($this->getRightValue() <= $subj->getNode()->getRightValue()) &&
|
|
|
|
($this->getRootValue() == $subj->getNode()->getRootValue()));
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* determines if node is valid
|
|
|
|
*
|
2007-10-21 13:00:40 +04:00
|
|
|
* @return bool
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
2007-08-03 00:05:12 +04:00
|
|
|
public function isValidNode($record = null)
|
2007-01-15 23:12:22 +03:00
|
|
|
{
|
2007-05-17 23:52:50 +04:00
|
|
|
if ($record === null) {
|
|
|
|
return ($this->getRightValue() > $this->getLeftValue());
|
2007-09-03 18:57:18 +04:00
|
|
|
} else if ( $record instanceof Doctrine_Record ) {
|
2007-05-17 23:52:50 +04:00
|
|
|
return ($record->getNode()->getRightValue() > $record->getNode()->getLeftValue());
|
2007-08-03 00:05:12 +04:00
|
|
|
} else {
|
|
|
|
return false;
|
2007-05-17 23:52:50 +04:00
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* deletes node and it's descendants
|
2007-07-08 16:57:52 +04:00
|
|
|
* @todo Delete more efficiently. Wrap in transaction if needed.
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
// TODO: add the setting whether or not to delete descendants or relocate children
|
2007-05-08 23:35:31 +04:00
|
|
|
$oldRoot = $this->getRootValue();
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->getBaseQuery();
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-08-04 18:33:15 +04:00
|
|
|
$baseAlias = $this->_tree->getBaseAlias();
|
2007-07-29 23:38:11 +04:00
|
|
|
$componentName = $this->_tree->getBaseComponent();
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-08-04 18:33:15 +04:00
|
|
|
$q = $q->addWhere("$baseAlias.lft >= ? AND $baseAlias.rgt <= ?", array($this->getLeftValue(), $this->getRightValue()));
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-10-21 13:00:40 +04:00
|
|
|
$q = $this->_tree->returnQueryWithRootId($q, $oldRoot);
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
$coll = $q->execute();
|
|
|
|
|
|
|
|
$coll->delete();
|
|
|
|
|
|
|
|
$first = $this->getRightValue() + 1;
|
|
|
|
$delta = $this->getLeftValue() - $this->getRightValue() - 1;
|
2007-05-08 23:35:31 +04:00
|
|
|
$this->shiftRLValues($first, $delta, $oldRoot);
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sets node's left and right values and save's it
|
|
|
|
*
|
|
|
|
* @param int $destLeft node left value
|
|
|
|
* @param int $destRight node right value
|
|
|
|
*/
|
|
|
|
private function insertNode($destLeft = 0, $destRight = 0, $destRoot = 1)
|
|
|
|
{
|
|
|
|
$this->setLeftValue($destLeft);
|
|
|
|
$this->setRightValue($destRight);
|
|
|
|
$this->setRootValue($destRoot);
|
|
|
|
$this->record->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* move node's and its children to location $destLeft and updates rest of tree
|
|
|
|
*
|
|
|
|
* @param int $destLeft destination left value
|
2007-07-08 16:57:52 +04:00
|
|
|
* @todo Wrap in transaction
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
2007-07-08 16:57:52 +04:00
|
|
|
private function updateNode($destLeft, $levelDiff)
|
2007-01-15 23:12:22 +03:00
|
|
|
{
|
2007-07-29 23:38:11 +04:00
|
|
|
$componentName = $this->_tree->getBaseComponent();
|
2007-01-15 23:12:22 +03:00
|
|
|
$left = $this->getLeftValue();
|
|
|
|
$right = $this->getRightValue();
|
2007-05-07 19:49:23 +04:00
|
|
|
$rootId = $this->getRootValue();
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
$treeSize = $right - $left + 1;
|
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
// Make room in the new branch
|
2007-05-07 19:49:23 +04:00
|
|
|
$this->shiftRLValues($destLeft, $treeSize, $rootId);
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-09-03 18:57:18 +04:00
|
|
|
if ($left >= $destLeft) { // src was shifted too?
|
2007-01-15 23:12:22 +03:00
|
|
|
$left += $treeSize;
|
|
|
|
$right += $treeSize;
|
|
|
|
}
|
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
// update level for descendants
|
|
|
|
$q = new Doctrine_Query();
|
|
|
|
$q = $q->update($componentName)
|
2007-08-03 00:05:12 +04:00
|
|
|
->set($componentName . '.level', 'level + ?')
|
2007-07-08 16:57:52 +04:00
|
|
|
->where($componentName . '.lft > ? AND ' . $componentName . '.rgt < ?',
|
2007-08-03 00:05:12 +04:00
|
|
|
array($levelDiff, $left, $right));
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->returnQueryWithRootId($q, $rootId);
|
|
|
|
$q->execute();
|
|
|
|
|
2007-01-15 23:12:22 +03:00
|
|
|
// now there's enough room next to target to move the subtree
|
2007-05-07 19:49:23 +04:00
|
|
|
$this->shiftRLRange($left, $right, $destLeft - $left, $rootId);
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
// correct values after source (close gap in old tree)
|
2007-05-07 19:49:23 +04:00
|
|
|
$this->shiftRLValues($right + 1, -$treeSize, $rootId);
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
$this->record->save();
|
|
|
|
$this->record->refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* adds '$delta' to all Left and Right values that are >= '$first'. '$delta' can also be negative.
|
|
|
|
*
|
|
|
|
* @param int $first First node to be shifted
|
|
|
|
* @param int $delta Value to be shifted by, can be negative
|
|
|
|
*/
|
2007-01-16 03:12:52 +03:00
|
|
|
private function shiftRlValues($first, $delta, $rootId = 1)
|
2007-01-15 23:12:22 +03:00
|
|
|
{
|
2007-01-16 03:12:52 +03:00
|
|
|
$qLeft = new Doctrine_Query();
|
|
|
|
$qRight = new Doctrine_Query();
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
// shift left columns
|
2007-07-29 23:38:11 +04:00
|
|
|
$componentName = $this->_tree->getBaseComponent();
|
2007-04-23 14:43:09 +04:00
|
|
|
$qLeft = $qLeft->update($componentName)
|
2007-08-03 00:05:12 +04:00
|
|
|
->set($componentName . '.lft', 'lft + ?')
|
|
|
|
->where($componentName . '.lft >= ?', array($delta, $first));
|
2007-07-24 15:34:00 +04:00
|
|
|
|
2007-10-21 13:00:40 +04:00
|
|
|
$qLeft = $this->_tree->returnQueryWithRootId($qLeft, $rootId);
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
$resultLeft = $qLeft->execute();
|
|
|
|
|
|
|
|
// shift right columns
|
2007-04-23 14:43:09 +04:00
|
|
|
$resultRight = $qRight->update($componentName)
|
2007-08-03 00:05:12 +04:00
|
|
|
->set($componentName . '.rgt', 'rgt + ?')
|
|
|
|
->where($componentName . '.rgt >= ?', array($delta, $first));
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-10-21 13:00:40 +04:00
|
|
|
$qRight = $this->_tree->returnQueryWithRootId($qRight, $rootId);
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
$resultRight = $qRight->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* adds '$delta' to all Left and Right values that are >= '$first' and <= '$last'.
|
|
|
|
* '$delta' can also be negative.
|
|
|
|
*
|
|
|
|
* @param int $first First node to be shifted (L value)
|
|
|
|
* @param int $last Last node to be shifted (L value)
|
|
|
|
* @param int $delta Value to be shifted by, can be negative
|
|
|
|
*/
|
2007-01-16 03:12:52 +03:00
|
|
|
private function shiftRlRange($first, $last, $delta, $rootId = 1)
|
2007-01-15 23:12:22 +03:00
|
|
|
{
|
2007-01-16 03:12:52 +03:00
|
|
|
$qLeft = new Doctrine_Query();
|
|
|
|
$qRight = new Doctrine_Query();
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
// shift left column values
|
2007-07-29 23:38:11 +04:00
|
|
|
$componentName = $this->_tree->getBaseComponent();
|
2007-04-23 14:43:09 +04:00
|
|
|
$qLeft = $qLeft->update($componentName)
|
2007-08-03 00:05:12 +04:00
|
|
|
->set($componentName . '.lft', 'lft + ?')
|
|
|
|
->where($componentName . '.lft >= ? AND ' . $componentName . '.lft <= ?', array($delta, $first, $last));
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-10-21 13:00:40 +04:00
|
|
|
$qLeft = $this->_tree->returnQueryWithRootId($qLeft, $rootId);
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
$resultLeft = $qLeft->execute();
|
|
|
|
|
|
|
|
// shift right column values
|
2007-04-23 14:43:09 +04:00
|
|
|
$qRight = $qRight->update($componentName)
|
2007-08-03 00:05:12 +04:00
|
|
|
->set($componentName . '.rgt', 'rgt + ?')
|
|
|
|
->where($componentName . '.rgt >= ? AND ' . $componentName . '.rgt <= ?', array($delta, $first, $last));
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-10-21 13:00:40 +04:00
|
|
|
$qRight = $this->_tree->returnQueryWithRootId($qRight, $rootId);
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
$resultRight = $qRight->execute();
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-15 23:12:22 +03:00
|
|
|
/**
|
|
|
|
* gets record's left value
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getLeftValue()
|
|
|
|
{
|
|
|
|
return $this->record->get('lft');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sets record's left value
|
|
|
|
*
|
|
|
|
* @param int
|
|
|
|
*/
|
|
|
|
public function setLeftValue($lft)
|
|
|
|
{
|
|
|
|
$this->record->set('lft', $lft);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets record's right value
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getRightValue()
|
|
|
|
{
|
|
|
|
return $this->record->get('rgt');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sets record's right value
|
|
|
|
*
|
|
|
|
* @param int
|
|
|
|
*/
|
|
|
|
public function setRightValue($rgt)
|
|
|
|
{
|
|
|
|
$this->record->set('rgt', $rgt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets level (depth) of node in the tree
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getLevel()
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! isset($this->record['level'])) {
|
2007-08-04 18:33:15 +04:00
|
|
|
$baseAlias = $this->_tree->getBaseAlias();
|
2007-07-29 23:38:11 +04:00
|
|
|
$componentName = $this->_tree->getBaseComponent();
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->getBaseQuery();
|
2007-08-04 18:33:15 +04:00
|
|
|
$q = $q->addWhere("$baseAlias.lft < ? AND $baseAlias.rgt > ?", array($this->getLeftValue(), $this->getRightValue()));
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->_tree->returnQueryWithRootId($q, $this->getRootValue());
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
$coll = $q->execute();
|
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
$this->record['level'] = count($coll) ? count($coll) : 0;
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
2007-07-08 16:57:52 +04:00
|
|
|
return $this->record['level'];
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get records root id value
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getRootValue()
|
|
|
|
{
|
2007-07-08 16:57:52 +04:00
|
|
|
if ($this->_tree->getAttribute('hasManyRoots')) {
|
|
|
|
return $this->record->get($this->_tree->getAttribute('rootColumnName'));
|
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sets records root id value
|
|
|
|
*
|
|
|
|
* @param int
|
|
|
|
*/
|
|
|
|
public function setRootValue($value)
|
|
|
|
{
|
2007-07-08 16:57:52 +04:00
|
|
|
if ($this->_tree->getAttribute('hasManyRoots')) {
|
|
|
|
$this->record->set($this->_tree->getAttribute('rootColumnName'), $value);
|
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
2007-10-04 01:43:22 +04:00
|
|
|
}
|