Author: sevein
Date: Mon Jun 25 10:11:53 2012
New Revision: 11801

Log:
Like r11794, extra nestedset/sorting functionality under qbAclPlugin

Modified:
   trunk/plugins/qbAclPlugin/lib/model/om/BaseAclGroup.php

Modified: trunk/plugins/qbAclPlugin/lib/model/om/BaseAclGroup.php
==============================================================================
--- trunk/plugins/qbAclPlugin/lib/model/om/BaseAclGroup.php     Mon Jun 25 
00:02:36 2012        (r11800)
+++ trunk/plugins/qbAclPlugin/lib/model/om/BaseAclGroup.php     Mon Jun 25 
10:11:53 2012        (r11801)
@@ -878,6 +878,11 @@
     return $aclGroupI18ns[$options['culture']];
   }
 
+  public function hasChildren()
+  {
+    return ($this->rgt - $this->lft) > 1;
+  }
+
   public function addAncestorsCriteria(Criteria $criteria)
   {
     return $criteria->add(QubitAclGroup::LFT, $this->lft, 
Criteria::LESS_THAN)->add(QubitAclGroup::RGT, $this->rgt, 
Criteria::GREATER_THAN);
@@ -1012,6 +1017,119 @@
     return $this;
   }
 
+  public function moveToPrevSiblingOf($sibling, PropelPDO $con = null)
+  {
+    if ($this->parentId != $sibling->parentId)
+    {
+      throw new PropelException('This functionality is limited to objects 
within the same level.');
+    }
+
+    $this->moveSubtreeTo($sibling->lft, $con);
+
+    return $this;
+  }
+
+  public function moveToNextSiblingOf($sibling, PropelPDO $con = null)
+  {
+    if ($this->parentId != $sibling->parentId)
+    {
+      throw new PropelException('This functionality is limited to objects 
within the same level.');
+    }
+
+    $this->moveSubtreeTo($sibling->rgt + 1, $con);
+
+    return $this;
+  }
+
+  protected function moveSubtreeTo($destLeft, PropelPDO $con = null)
+  {
+    $left  = $this->lft;
+    $right = $this->rgt;
+
+    $treeSize = $right - $left +1;
+
+    if ($con === null)
+    {
+      $con = Propel::getConnection();
+    }
+
+    $con->beginTransaction();
+
+    try
+    {
+      // make room next to the target for the subtree
+      self::shiftRLValues($treeSize, $destLeft, null, $con);
+
+      if ($left >= $destLeft) // src was shifted too?
+      {
+        $left += $treeSize;
+        $right += $treeSize;
+      }
+
+      // move the subtree to the target
+      self::shiftRLValues($destLeft - $left, $left, $right, $con);
+
+      // remove the empty room at the previous location of the subtree
+      self::shiftRLValues(-$treeSize, $right + 1, null, $con);
+
+      // update all loaded nodes
+      // self::updateLoadedNodes(null, $con);
+
+      $con->commit();
+    }
+    catch (PropelException $e)
+    {
+      $con->rollback();
+
+      throw $e;
+    }
+  }
+
+  /**
+   * Adds $delta to all L and R values that are >= $first and <= $last.
+   * '$delta' can also be negative.
+   *
+   * @param int $delta Value to be shifted by, can be negative
+   * @param int $first First node to be shifted
+   * @param int $last Last node to be shifted (optional)
+   * @param PropelPDO $con Connection to use.
+   */
+  protected function shiftRLValues($delta, $first, $last = null, PropelPDO 
$con = null)
+  {
+    if ($con === null)
+    {
+      $con = Propel::getConnection();
+    }
+
+    // Shift left column values
+    $whereCriteria = new Criteria;
+    $criterion = $whereCriteria->getNewCriterion(QubitAclGroup::LFT, $first, 
Criteria::GREATER_EQUAL);
+    if (null !== $last)
+    {
+      $criterion->addAnd($whereCriteria->getNewCriterion(QubitAclGroup::LFT, 
$last, Criteria::LESS_EQUAL));
+    }
+    $whereCriteria->add($criterion);
+
+    $valuesCriteria = new Criteria;
+    $valuesCriteria->add(QubitAclGroup::LFT, array('raw' => QubitAclGroup::LFT 
. ' + ?', 'value' => $delta), Criteria::CUSTOM_EQUAL);
+
+    BasePeer::doUpdate($whereCriteria, $valuesCriteria, $con);
+
+    // Shift right column values
+    $whereCriteria = new Criteria;
+    $criterion = $whereCriteria->getNewCriterion(QubitAclGroup::RGT, $first, 
Criteria::GREATER_EQUAL);
+    if (null !== $last)
+    {
+      $criterion->addAnd($whereCriteria->getNewCriterion(QubitAclGroup::RGT, 
$last, Criteria::LESS_EQUAL));
+    }
+    $whereCriteria->add($criterion);
+
+    $valuesCriteria = new Criteria;
+    $valuesCriteria->add(QubitAclGroup::RGT, array('raw' => QubitAclGroup::RGT 
. ' + ?', 'value' => $delta), Criteria::CUSTOM_EQUAL);
+
+    BasePeer::doUpdate($whereCriteria, $valuesCriteria, $con);
+  }
+
   public function __call($name, $args)
   {
     if ('get' == substr($name, 0, 3) || 'set' == substr($name, 0, 3))

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to