mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Make code DRYer.
This commit is contained in:
parent
844b4beaa6
commit
0750a63ed4
1 changed files with 30 additions and 72 deletions
|
@ -184,12 +184,7 @@ class TreeBehavior extends ModelBehavior {
|
||||||
$this->_addToWhitelist($Model, array($left, $right));
|
$this->_addToWhitelist($Model, array($left, $right));
|
||||||
if (!$Model->id || !$Model->exists()) {
|
if (!$Model->id || !$Model->exists()) {
|
||||||
if (array_key_exists($parent, $Model->data[$Model->alias]) && $Model->data[$Model->alias][$parent]) {
|
if (array_key_exists($parent, $Model->data[$Model->alias]) && $Model->data[$Model->alias][$parent]) {
|
||||||
$parentNode = $Model->find('first', array(
|
$parentNode = $this->_getNode($Model, $Model->data[$Model->alias][$parent]);
|
||||||
'conditions' => array($scope, $Model->escapeField() => $Model->data[$Model->alias][$parent]),
|
|
||||||
'fields' => array($Model->primaryKey, $right),
|
|
||||||
'recursive' => $recursive,
|
|
||||||
'order' => false,
|
|
||||||
));
|
|
||||||
if (!$parentNode) {
|
if (!$parentNode) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -215,24 +210,13 @@ class TreeBehavior extends ModelBehavior {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$values = $Model->find('first', array(
|
$values = $this->_getNode($Model, $Model->id);
|
||||||
'conditions' => array($scope, $Model->escapeField() => $Model->id),
|
|
||||||
'fields' => array($Model->primaryKey, $parent, $left, $right),
|
|
||||||
'order' => false,
|
|
||||||
'recursive' => $recursive)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (empty($values)) {
|
if (empty($values)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
list($node) = array_values($values);
|
list($node) = array_values($values);
|
||||||
|
|
||||||
$parentNode = $Model->find('first', array(
|
$parentNode = $this->_getNode($Model, $Model->data[$Model->alias][$parent]);
|
||||||
'conditions' => array($scope, $Model->escapeField() => $Model->data[$Model->alias][$parent]),
|
|
||||||
'fields' => array($Model->primaryKey, $left, $right),
|
|
||||||
'order' => false,
|
|
||||||
'recursive' => $recursive
|
|
||||||
));
|
|
||||||
if (!$parentNode) {
|
if (!$parentNode) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -249,6 +233,24 @@ class TreeBehavior extends ModelBehavior {
|
||||||
return true;
|
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|boolean 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
|
* Get the number of child nodes
|
||||||
*
|
*
|
||||||
|
@ -281,11 +283,7 @@ class TreeBehavior extends ModelBehavior {
|
||||||
} elseif ($Model->id === $id && isset($Model->data[$Model->alias][$left]) && isset($Model->data[$Model->alias][$right])) {
|
} elseif ($Model->id === $id && isset($Model->data[$Model->alias][$left]) && isset($Model->data[$Model->alias][$right])) {
|
||||||
$data = $Model->data[$Model->alias];
|
$data = $Model->data[$Model->alias];
|
||||||
} else {
|
} else {
|
||||||
$data = $Model->find('first', array(
|
$data = $this->_getNode($Model, $id);
|
||||||
'conditions' => array($scope, $Model->escapeField() => $id),
|
|
||||||
'order' => false,
|
|
||||||
'recursive' => $recursive
|
|
||||||
));
|
|
||||||
if (!$data) {
|
if (!$data) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -567,19 +565,9 @@ class TreeBehavior extends ModelBehavior {
|
||||||
$id = $Model->id;
|
$id = $Model->id;
|
||||||
}
|
}
|
||||||
extract($this->settings[$Model->alias]);
|
extract($this->settings[$Model->alias]);
|
||||||
list($node) = array_values($Model->find('first', array(
|
list($node) = array_values($this->_getNode($Model, $id));
|
||||||
'conditions' => array($scope, $Model->escapeField() => $id),
|
|
||||||
'fields' => array($Model->primaryKey, $left, $right, $parent),
|
|
||||||
'order' => false,
|
|
||||||
'recursive' => $recursive
|
|
||||||
)));
|
|
||||||
if ($node[$parent]) {
|
if ($node[$parent]) {
|
||||||
list($parentNode) = array_values($Model->find('first', array(
|
list($parentNode) = array_values($this->_getNode($Model, $node[$parent]));
|
||||||
'conditions' => array($scope, $Model->escapeField() => $node[$parent]),
|
|
||||||
'fields' => array($Model->primaryKey, $left, $right),
|
|
||||||
'order' => false,
|
|
||||||
'recursive' => $recursive
|
|
||||||
)));
|
|
||||||
if (($node[$right] + 1) == $parentNode[$right]) {
|
if (($node[$right] + 1) == $parentNode[$right]) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -631,19 +619,9 @@ class TreeBehavior extends ModelBehavior {
|
||||||
$id = $Model->id;
|
$id = $Model->id;
|
||||||
}
|
}
|
||||||
extract($this->settings[$Model->alias]);
|
extract($this->settings[$Model->alias]);
|
||||||
list($node) = array_values($Model->find('first', array(
|
list($node) = array_values($this->_getNode($Model, $id));
|
||||||
'conditions' => array($scope, $Model->escapeField() => $id),
|
|
||||||
'fields' => array($Model->primaryKey, $left, $right, $parent),
|
|
||||||
'order' => false,
|
|
||||||
'recursive' => $recursive
|
|
||||||
)));
|
|
||||||
if ($node[$parent]) {
|
if ($node[$parent]) {
|
||||||
list($parentNode) = array_values($Model->find('first', array(
|
list($parentNode) = array_values($this->_getNode($Model, $node[$parent]));
|
||||||
'conditions' => array($scope, $Model->escapeField() => $node[$parent]),
|
|
||||||
'fields' => array($Model->primaryKey, $left, $right),
|
|
||||||
'order' => false,
|
|
||||||
'recursive' => $recursive
|
|
||||||
)));
|
|
||||||
if (($node[$left] - 1) == $parentNode[$left]) {
|
if (($node[$left] - 1) == $parentNode[$left]) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -872,12 +850,7 @@ class TreeBehavior extends ModelBehavior {
|
||||||
}
|
}
|
||||||
extract($this->settings[$Model->alias]);
|
extract($this->settings[$Model->alias]);
|
||||||
|
|
||||||
list($node) = array_values($Model->find('first', array(
|
list($node) = array_values($this->_getNode($Model, $id));
|
||||||
'conditions' => array($scope, $Model->escapeField() => $id),
|
|
||||||
'fields' => array($Model->primaryKey, $left, $right, $parent),
|
|
||||||
'order' => false,
|
|
||||||
'recursive' => $recursive
|
|
||||||
)));
|
|
||||||
|
|
||||||
if ($node[$right] == $node[$left] + 1) {
|
if ($node[$right] == $node[$left] + 1) {
|
||||||
if ($delete) {
|
if ($delete) {
|
||||||
|
@ -886,12 +859,7 @@ class TreeBehavior extends ModelBehavior {
|
||||||
$Model->id = $id;
|
$Model->id = $id;
|
||||||
return $Model->saveField($parent, null);
|
return $Model->saveField($parent, null);
|
||||||
} elseif ($node[$parent]) {
|
} elseif ($node[$parent]) {
|
||||||
list($parentNode) = array_values($Model->find('first', array(
|
list($parentNode) = array_values($this->_getNode($Model, $node[$parent]));
|
||||||
'conditions' => array($scope, $Model->escapeField() => $node[$parent]),
|
|
||||||
'fields' => array($Model->primaryKey, $left, $right),
|
|
||||||
'order' => false,
|
|
||||||
'recursive' => $recursive
|
|
||||||
)));
|
|
||||||
} else {
|
} else {
|
||||||
$parentNode[$right] = $node[$right] + 1;
|
$parentNode[$right] = $node[$right] + 1;
|
||||||
}
|
}
|
||||||
|
@ -1016,24 +984,14 @@ class TreeBehavior extends ModelBehavior {
|
||||||
*/
|
*/
|
||||||
protected function _setParent(Model $Model, $parentId = null, $created = false) {
|
protected function _setParent(Model $Model, $parentId = null, $created = false) {
|
||||||
extract($this->settings[$Model->alias]);
|
extract($this->settings[$Model->alias]);
|
||||||
list($node) = array_values($Model->find('first', array(
|
list($node) = array_values($this->_getNode($Model, $Model->id));
|
||||||
'conditions' => array($scope, $Model->escapeField() => $Model->id),
|
|
||||||
'fields' => array($Model->primaryKey, $parent, $left, $right),
|
|
||||||
'order' => false,
|
|
||||||
'recursive' => $recursive
|
|
||||||
)));
|
|
||||||
$edge = $this->_getMax($Model, $scope, $right, $recursive, $created);
|
$edge = $this->_getMax($Model, $scope, $right, $recursive, $created);
|
||||||
|
|
||||||
if (empty($parentId)) {
|
if (empty($parentId)) {
|
||||||
$this->_sync($Model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right], $created);
|
$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);
|
$this->_sync($Model, $node[$right] - $node[$left] + 1, '-', '> ' . $node[$left], $created);
|
||||||
} else {
|
} else {
|
||||||
$values = $Model->find('first', array(
|
$values = $this->_getNode($Model, $parentId);
|
||||||
'conditions' => array($scope, $Model->escapeField() => $parentId),
|
|
||||||
'fields' => array($Model->primaryKey, $left, $right),
|
|
||||||
'order' => false,
|
|
||||||
'recursive' => $recursive
|
|
||||||
));
|
|
||||||
|
|
||||||
if ($values === false) {
|
if ($values === false) {
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Add table
Reference in a new issue