Commit 484fb080 authored by zYne's avatar zYne

little refactorings

parent 4ff559ce
...@@ -281,7 +281,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int ...@@ -281,7 +281,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
$this->shiftRLValues($dest->getNode()->getRightValue() + 2, 1); $this->shiftRLValues($dest->getNode()->getRightValue() + 2, 1);
$newLeft = $dest->getNode()->getLeftValue(); $newLeft = $dest->getNode()->getLeftValue();
$newRight = $dest->getNode()->getRightValue() + 2; $newRight = $dest->getNode()->getRightValue() + 2;
$newRoot = $dest->getNode()->getRootValue(); $newRoot = $dest->getNode()->getRootValue();
$this->insertNode($newLeft, $newRight, $newRoot); $this->insertNode($newLeft, $newRight, $newRoot);
...@@ -437,7 +437,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int ...@@ -437,7 +437,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
*/ */
public function isLeaf() public function isLeaf()
{ {
return (($this->getRightValue()-$this->getLeftValue())==1); return (($this->getRightValue() - $this->getLeftValue()) == 1);
} }
/** /**
...@@ -447,7 +447,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int ...@@ -447,7 +447,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
*/ */
public function isRoot() public function isRoot()
{ {
return ($this->getLeftValue()==1); return ($this->getLeftValue() == 1);
} }
/** /**
...@@ -457,17 +457,20 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int ...@@ -457,17 +457,20 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
*/ */
public function isEqualTo(Doctrine_Record $subj) public function isEqualTo(Doctrine_Record $subj)
{ {
return (($this->getLeftValue()==$subj->getNode()->getLeftValue()) and ($this->getRightValue()==$subj->getNode()->getRightValue()) and ($this->getRootValue() == $subj->getNode()->getRootValue())); return (($this->getLeftValue() == $subj->getNode()->getLeftValue()) &&
($this->getRightValue() == $subj->getNode()->getRightValue()) &&
($this->getRootValue() == $subj->getNode()->getRootValue())
);
} }
/** /**
* determines if node is child of subject node * determines if node is child of subject node
* *
* @return bool * @return bool
*/ */
public function isDescendantOf(Doctrine_Record $subj) public function isDescendantOf(Doctrine_Record $subj)
{ {
return (($this->getLeftValue()>$subj->getNode()->getLeftValue()) and ($this->getRightValue()<$subj->getNode()->getRightValue()) and ($this->getRootValue() == $subj->getNode()->getRootValue())); return (($this->getLeftValue()>$subj->getNode()->getLeftValue()) && ($this->getRightValue()<$subj->getNode()->getRightValue()) && ($this->getRootValue() == $subj->getNode()->getRootValue()));
} }
/** /**
...@@ -477,7 +480,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int ...@@ -477,7 +480,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
*/ */
public function isDescendantOfOrEqualTo(Doctrine_Record $subj) public function isDescendantOfOrEqualTo(Doctrine_Record $subj)
{ {
return (($this->getLeftValue()>=$subj->getNode()->getLeftValue()) and ($this->getRightValue()<=$subj->getNode()->getRightValue()) and ($this->getRootValue() == $subj->getNode()->getRootValue())); return (($this->getLeftValue()>=$subj->getNode()->getLeftValue()) && ($this->getRightValue()<=$subj->getNode()->getRightValue()) && ($this->getRootValue() == $subj->getNode()->getRootValue()));
} }
/** /**
...@@ -502,7 +505,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int ...@@ -502,7 +505,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
$componentName = $this->record->getTable()->getComponentName(); $componentName = $this->record->getTable()->getComponentName();
$q = $q->where("$componentName.lft >= ? AND $componentName.rgt <= ?", array($this->getLeftValue(), $this->getRightValue())); $q = $q->where($componentName. '.lft >= ? AND ' . $componentName . '.rgt <= ?', array($this->getLeftValue(), $this->getRightValue()));
$q = $this->record->getTable()->getTree()->returnQueryWithRootId($q, $this->getRootValue()); $q = $this->record->getTable()->getTree()->returnQueryWithRootId($q, $this->getRootValue());
...@@ -566,28 +569,28 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int ...@@ -566,28 +569,28 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
* @param int $first First node to be shifted * @param int $first First node to be shifted
* @param int $delta Value to be shifted by, can be negative * @param int $delta Value to be shifted by, can be negative
*/ */
private function shiftRLValues($first, $delta, $root_id = 1) private function shiftRlValues($first, $delta, $rootId = 1)
{ {
$qLeft = $this->record->getTable()->createQuery(); $qLeft = new Doctrine_Query();
$qRight = $this->record->getTable()->createQuery(); $qRight = new Doctrine_Query();
// TODO: Wrap in transaction // TODO: Wrap in transaction
// shift left columns // shift left columns
$qLeft = $qLeft->update($this->record->getTable()->getComponentName()) $qLeft = $qLeft->update($this->record->getTable()->getComponentName())
->set('lft', "lft + $delta") ->set('lft', 'lft + ' . $delta)
->where('lft >= ?', $first); ->where('lft >= ?', $first);
$qLeft = $this->record->getTable()->getTree()->returnQueryWithRootId($qLeft, $root_id); $qLeft = $this->record->getTable()->getTree()->returnQueryWithRootId($qLeft, $rootId);
$resultLeft = $qLeft->execute(); $resultLeft = $qLeft->execute();
// shift right columns // shift right columns
$resultRight = $qRight->update($this->record->getTable()->getComponentName()) $resultRight = $qRight->update($this->record->getTable()->getComponentName())
->set('rgt', "rgt + $delta") ->set('rgt', 'rgt + ' . $delta)
->where('rgt >= ?', $first); ->where('rgt >= ?', $first);
$qRight = $this->record->getTable()->getTree()->returnQueryWithRootId($qRight, $root_id); $qRight = $this->record->getTable()->getTree()->returnQueryWithRootId($qRight, $rootId);
$resultRight = $qRight->execute(); $resultRight = $qRight->execute();
} }
...@@ -600,28 +603,28 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int ...@@ -600,28 +603,28 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
* @param int $last Last 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 * @param int $delta Value to be shifted by, can be negative
*/ */
private function shiftRLRange($first, $last, $delta, $root_id = 1) private function shiftRlRange($first, $last, $delta, $rootId = 1)
{ {
$qLeft = $this->record->getTable()->createQuery(); $qLeft = new Doctrine_Query();
$qRight = $this->record->getTable()->createQuery(); $qRight = new Doctrine_Query();
// TODO : Wrap in transaction // TODO : Wrap in transaction
// shift left column values // shift left column values
$qLeft = $qLeft->update($this->record->getTable()->getComponentName()) $qLeft = $qLeft->update($this->record->getTable()->getComponentName())
->set('lft', "lft + $delta") ->set('lft', 'lft + ' . $delta)
->where('lft >= ? AND lft <= ?', array($first, $last)); ->where('lft >= ? AND lft <= ?', array($first, $last));
$qLeft = $this->record->getTable()->getTree()->returnQueryWithRootId($qLeft, $root_id); $qLeft = $this->record->getTable()->getTree()->returnQueryWithRootId($qLeft, $rootId);
$resultLeft = $qLeft->execute(); $resultLeft = $qLeft->execute();
// shift right column values // shift right column values
$qRight = $qRight->update($this->record->getTable()->getComponentName()) $qRight = $qRight->update($this->record->getTable()->getComponentName())
->set('rgt', "rgt + $delta") ->set('rgt', 'rgt + ' . $delta)
->where('rgt >= ? AND rgt <= ?', array($first, $last)); ->where('rgt >= ? AND rgt <= ?', array($first, $last));
$qRight = $this->record->getTable()->getTree()->returnQueryWithRootId($qRight, $root_id); $qRight = $this->record->getTable()->getTree()->returnQueryWithRootId($qRight, $rootId);
$resultRight = $qRight->execute(); $resultRight = $qRight->execute();
} }
......
...@@ -53,12 +53,12 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int ...@@ -53,12 +53,12 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
*/ */
public function setTableDefinition() public function setTableDefinition()
{ {
if ($this->getAttribute('has_many_roots')) { if ($root = $this->getAttribute('root_column_name')) {
$this->table->setColumn($this->getAttribute('root_column_name'),"integer",11); $this->table->setColumn($root, 'integer', 11);
} }
$this->table->setColumn("lft","integer",11); $this->table->setColumn('lft', 'integer', 11);
$this->table->setColumn("rgt","integer",11); $this->table->setColumn('rgt', 'integer', 11);
} }
/** /**
...@@ -73,7 +73,7 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int ...@@ -73,7 +73,7 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
} }
// if tree is many roots, then get next root id // if tree is many roots, then get next root id
if ($this->getAttribute('has_many_roots')) { if($root = $this->getAttribute('root_column_name')) {
$record->getNode()->setRootValue($this->getNextRootId()); $record->getNode()->setRootValue($this->getNextRootId());
} }
...@@ -207,7 +207,7 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int ...@@ -207,7 +207,7 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
public function getMaxRootId() public function getMaxRootId()
{ {
$component = $this->table->getComponentName(); $component = $this->table->getComponentName();
$column = $this->getAttribute('root_column_name'); $column = $this->getAttribute('root_column_name');
// cannot get this dql to work, cannot retrieve result using $coll[0]->max // cannot get this dql to work, cannot retrieve result using $coll[0]->max
//$dql = "SELECT MAX(c.$column) FROM $component c"; //$dql = "SELECT MAX(c.$column) FROM $component c";
...@@ -232,8 +232,8 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int ...@@ -232,8 +232,8 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
*/ */
public function returnQueryWithRootId($query, $rootId = 1) public function returnQueryWithRootId($query, $rootId = 1)
{ {
if($this->getAttribute('has_many_roots')) { if ($root = $this->getAttribute('root_column_name')) {
$query->addWhere($this->getAttribute('root_column_name') . ' = ?', $rootId); $query->addWhere($root . ' = ?', $rootId);
} }
return $query; return $query;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment