Merge pull request #5903 from cakephp/tree-optimization

Reduced code nesting.
This commit is contained in:
Mark Story 2015-02-15 10:35:43 -05:00
commit 10b6ba7dc2

View file

@ -184,65 +184,73 @@ class TreeBehavior extends ModelBehavior {
$this->_addToWhitelist($Model, array($left, $right));
if (!$Model->id || !$Model->exists()) {
if (array_key_exists($parent, $Model->data[$Model->alias]) && $Model->data[$Model->alias][$parent]) {
$parentNode = $Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $Model->data[$Model->alias][$parent]),
'fields' => array($Model->primaryKey, $right),
'recursive' => $recursive,
'order' => false,
));
$parentNode = $this->_getNode($Model, $Model->data[$Model->alias][$parent]);
if (!$parentNode) {
return false;
}
list($parentNode) = array_values($parentNode);
$Model->data[$Model->alias][$left] = 0;
$Model->data[$Model->alias][$right] = 0;
} else {
$edge = $this->_getMax($Model, $scope, $right, $recursive);
$Model->data[$Model->alias][$left] = $edge + 1;
$Model->data[$Model->alias][$right] = $edge + 2;
return true;
}
} elseif (array_key_exists($parent, $Model->data[$Model->alias])) {
$edge = $this->_getMax($Model, $scope, $right, $recursive);
$Model->data[$Model->alias][$left] = $edge + 1;
$Model->data[$Model->alias][$right] = $edge + 2;
return true;
}
if (array_key_exists($parent, $Model->data[$Model->alias])) {
if ($Model->data[$Model->alias][$parent] != $Model->field($parent)) {
$this->settings[$Model->alias]['__parentChange'] = true;
}
if (!$Model->data[$Model->alias][$parent]) {
$Model->data[$Model->alias][$parent] = null;
$this->_addToWhitelist($Model, $parent);
} else {
$values = $Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $Model->id),
'fields' => array($Model->primaryKey, $parent, $left, $right),
'order' => false,
'recursive' => $recursive)
);
return true;
}
if (empty($values)) {
return false;
}
list($node) = array_values($values);
$values = $this->_getNode($Model, $Model->id);
if (empty($values)) {
return false;
}
list($node) = array_values($values);
$parentNode = $Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $Model->data[$Model->alias][$parent]),
'fields' => array($Model->primaryKey, $left, $right),
'order' => false,
'recursive' => $recursive
));
if (!$parentNode) {
return false;
}
list($parentNode) = array_values($parentNode);
$parentNode = $this->_getNode($Model, $Model->data[$Model->alias][$parent]);
if (!$parentNode) {
return false;
}
list($parentNode) = array_values($parentNode);
if (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) {
return false;
}
if ($node[$Model->primaryKey] === $parentNode[$Model->primaryKey]) {
return false;
}
if (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) {
return false;
}
if ($node[$Model->primaryKey] === $parentNode[$Model->primaryKey]) {
return false;
}
}
return true;
}
/**
* Returns a single node from the tree from its primary key
*
* @param Model $Model Model using this behavior
* @param int|string $id The ID of the record to read
* @return array|bool The record read or false
*/
protected function _getNode(Model $Model, $id) {
$settings = $this->settings[$Model->alias];
return $Model->find('first', array(
'conditions' => array($Model->escapeField() => $id),
'fields' => array($Model->primaryKey, $settings['parent'], $settings['left'], $settings['right']),
'recursive' => $settings['recursive'],
'order' => false,
));
}
/**
* Get the number of child nodes
*
@ -275,11 +283,7 @@ class TreeBehavior extends ModelBehavior {
} elseif ($Model->id === $id && isset($Model->data[$Model->alias][$left]) && isset($Model->data[$Model->alias][$right])) {
$data = $Model->data[$Model->alias];
} else {
$data = $Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $id),
'order' => false,
'recursive' => $recursive
));
$data = $this->_getNode($Model, $id);
if (!$data) {
return 0;
}
@ -561,19 +565,9 @@ class TreeBehavior extends ModelBehavior {
$id = $Model->id;
}
extract($this->settings[$Model->alias]);
list($node) = array_values($Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $id),
'fields' => array($Model->primaryKey, $left, $right, $parent),
'order' => false,
'recursive' => $recursive
)));
list($node) = array_values($this->_getNode($Model, $id));
if ($node[$parent]) {
list($parentNode) = array_values($Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $node[$parent]),
'fields' => array($Model->primaryKey, $left, $right),
'order' => false,
'recursive' => $recursive
)));
list($parentNode) = array_values($this->_getNode($Model, $node[$parent]));
if (($node[$right] + 1) == $parentNode[$right]) {
return false;
}
@ -625,19 +619,9 @@ class TreeBehavior extends ModelBehavior {
$id = $Model->id;
}
extract($this->settings[$Model->alias]);
list($node) = array_values($Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $id),
'fields' => array($Model->primaryKey, $left, $right, $parent),
'order' => false,
'recursive' => $recursive
)));
list($node) = array_values($this->_getNode($Model, $id));
if ($node[$parent]) {
list($parentNode) = array_values($Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $node[$parent]),
'fields' => array($Model->primaryKey, $left, $right),
'order' => false,
'recursive' => $recursive
)));
list($parentNode) = array_values($this->_getNode($Model, $node[$parent]));
if (($node[$left] - 1) == $parentNode[$left]) {
return false;
}
@ -866,12 +850,7 @@ class TreeBehavior extends ModelBehavior {
}
extract($this->settings[$Model->alias]);
list($node) = array_values($Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $id),
'fields' => array($Model->primaryKey, $left, $right, $parent),
'order' => false,
'recursive' => $recursive
)));
list($node) = array_values($this->_getNode($Model, $id));
if ($node[$right] == $node[$left] + 1) {
if ($delete) {
@ -880,12 +859,7 @@ class TreeBehavior extends ModelBehavior {
$Model->id = $id;
return $Model->saveField($parent, null);
} elseif ($node[$parent]) {
list($parentNode) = array_values($Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $node[$parent]),
'fields' => array($Model->primaryKey, $left, $right),
'order' => false,
'recursive' => $recursive
)));
list($parentNode) = array_values($this->_getNode($Model, $node[$parent]));
} else {
$parentNode[$right] = $node[$right] + 1;
}
@ -1010,24 +984,14 @@ class TreeBehavior extends ModelBehavior {
*/
protected function _setParent(Model $Model, $parentId = null, $created = false) {
extract($this->settings[$Model->alias]);
list($node) = array_values($Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $Model->id),
'fields' => array($Model->primaryKey, $parent, $left, $right),
'order' => false,
'recursive' => $recursive
)));
list($node) = array_values($this->_getNode($Model, $Model->id));
$edge = $this->_getMax($Model, $scope, $right, $recursive, $created);
if (empty($parentId)) {
$this->_sync($Model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right], $created);
$this->_sync($Model, $node[$right] - $node[$left] + 1, '-', '> ' . $node[$left], $created);
} else {
$values = $Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $parentId),
'fields' => array($Model->primaryKey, $left, $right),
'order' => false,
'recursive' => $recursive
));
$values = $this->_getNode($Model, $parentId);
if ($values === false) {
return false;