Skip to content

Commit

Permalink
Implemented left and right fields quoting in generated plain text que…
Browse files Browse the repository at this point in the history
…ries.
  • Loading branch information
skie committed Jan 12, 2016
1 parent f3a990d commit d1372b8
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions src/ORM/Behavior/TreeBehavior.php
Expand Up @@ -20,6 +20,7 @@
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Query;
use Cake\ORM\Table;
use InvalidArgumentException;
use RuntimeException;

Expand Down Expand Up @@ -75,6 +76,23 @@ class TreeBehavior extends Behavior
'recoverOrder' => null
];

/**
* {@inheritDoc}
*/
public function __construct(Table $table, array $config = [])
{
parent::__construct($table, $config);
$connection = $table->connection();
if (!is_null($connection)) {
$driver = $connection->driver();
$this->config('leftField', $driver->quoteIdentifier($this->config('left')));
$this->config('rightField', $driver->quoteIdentifier($this->config('right')));
} else {
$this->config('leftField', $this->config('left'));
$this->config('rightField', $this->config('right'));
}
}

/**
* Before save listener.
* Transparently manages setting the lft and rght fields if the parent field is
Expand Down Expand Up @@ -212,8 +230,8 @@ public function beforeDelete(Event $event, EntityInterface $entity)

if ($diff > 2) {
$this->_table->deleteAll([
"{$config['left']} >=" => $left + 1,
"{$config['left']} <=" => $right - 1
"{$config['leftField']} >=" => $left + 1,
"{$config['leftField']} <=" => $right - 1
]);
}

Expand Down Expand Up @@ -328,9 +346,9 @@ protected function _unmarkInternalTree()
$config = $this->config();
$query = $this->_table->query();
$this->_table->updateAll([
$query->newExpr()->add("{$config['left']} = {$config['left']} * -1"),
$query->newExpr()->add("{$config['right']} = {$config['right']} * -1"),
], [$config['left'] . ' <' => 0]);
$query->newExpr()->add("{$config['leftField']} = {$config['leftField']} * -1"),
$query->newExpr()->add("{$config['rightField']} = {$config['rightField']} * -1"),
], [$config['leftField'] . ' <' => 0]);
}

/**
Expand Down Expand Up @@ -590,24 +608,24 @@ public function moveUp(EntityInterface $node, $number = 1)
protected function _moveUp($node, $number)
{
$config = $this->config();
list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];
list($parent, $left, $right, $leftField, $rightField) = [$config['parent'], $config['left'], $config['right'], $config['leftField'], $config['rightField']];
list($nodeParent, $nodeLeft, $nodeRight) = array_values($node->extract([$parent, $left, $right]));

$targetNode = null;
if ($number !== true) {
$targetNode = $this->_scope($this->_table->find())
->select([$left, $right])
->where(["$parent IS" => $nodeParent, "$right <" => $nodeLeft])
->order([$left => 'DESC'])
->where(["$parent IS" => $nodeParent, "$rightField <" => $nodeLeft])
->order([$leftField => 'DESC'])
->offset($number - 1)
->limit(1)
->first();
}
if (!$targetNode) {
$targetNode = $this->_scope($this->_table->find())
->select([$left, $right])
->where(["$parent IS" => $nodeParent, "$right <" => $nodeLeft])
->order([$left => 'ASC'])
->where(["$parent IS" => $nodeParent, "$rightField <" => $nodeLeft])
->order([$leftField => 'ASC'])
->limit(1)
->first();

Expand Down Expand Up @@ -671,25 +689,25 @@ public function moveDown(EntityInterface $node, $number = 1)
protected function _moveDown($node, $number)
{
$config = $this->config();
list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];
list($parent, $left, $right, $leftField, $rightField) = [$config['parent'], $config['left'], $config['right'], $config['leftField'], $config['rightField']];

list($nodeParent, $nodeLeft, $nodeRight) = array_values($node->extract([$parent, $left, $right]));

$targetNode = null;
if ($number !== true) {
$targetNode = $this->_scope($this->_table->find())
->select([$left, $right])
->where(["$parent IS" => $nodeParent, "$left >" => $nodeRight])
->order([$left => 'ASC'])
->where(["$parent IS" => $nodeParent, "$leftField >" => $nodeRight])
->order([$leftField => 'ASC'])
->offset($number - 1)
->limit(1)
->first();
}
if (!$targetNode) {
$targetNode = $this->_scope($this->_table->find())
->select([$left, $right])
->where(["$parent IS" => $nodeParent, "$left >" => $nodeRight])
->order([$left => 'DESC'])
->where(["$parent IS" => $nodeParent, "$leftField >" => $nodeRight])
->order([$leftField => 'DESC'])
->limit(1)
->first();

Expand Down Expand Up @@ -845,7 +863,7 @@ protected function _sync($shift, $dir, $conditions, $mark = false)
{
$config = $this->config();

foreach ([$config['left'], $config['right']] as $field) {
foreach ([$config['leftField'], $config['rightField']] as $field) {
$query = $this->_scope($this->_table->query());

$mark = $mark ? '*-1' : '';
Expand Down

0 comments on commit d1372b8

Please sign in to comment.