2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Tree behavior class.
|
|
|
|
*
|
|
|
|
* Enables a model object to act as a node-based tree.
|
|
|
|
*
|
2010-01-26 22:15:15 +00:00
|
|
|
* CakePHP : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 12:28:17 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model.Behavior
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP v 1.2.0.4487
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2012-07-21 18:29:49 +00:00
|
|
|
App::uses('ModelBehavior', 'Model');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-08 17:12:30 +00:00
|
|
|
* Tree Behavior.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-03-08 17:12:30 +00:00
|
|
|
* Enables a model object to act as a node-based tree. Using Modified Preorder Tree Traversal
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2009-03-08 17:12:30 +00:00
|
|
|
* @see http://en.wikipedia.org/wiki/Tree_traversal
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model.Behavior
|
2011-10-15 17:04:31 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class TreeBehavior extends ModelBehavior {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-25 16:49:56 +00:00
|
|
|
/**
|
|
|
|
* Errors
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $errors = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-25 16:49:56 +00:00
|
|
|
/**
|
|
|
|
* Defaults
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_defaults = array(
|
2008-05-30 11:40:08 +00:00
|
|
|
'parent' => 'parent_id', 'left' => 'lft', 'right' => 'rght',
|
|
|
|
'scope' => '1 = 1', 'type' => 'nested', '__parentChange' => false, 'recursive' => -1
|
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-03-21 02:36:48 +00:00
|
|
|
/**
|
|
|
|
* Used to preserve state between delete callbacks.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-12-20 01:48:51 +00:00
|
|
|
protected $_deletedRow = array();
|
2012-03-21 02:36:48 +00:00
|
|
|
|
2008-09-25 16:49:56 +00:00
|
|
|
/**
|
|
|
|
* Initiate Tree behavior
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model using this behavior of model
|
2009-03-08 17:12:30 +00:00
|
|
|
* @param array $config array of configuration settings.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function setup(Model $Model, $config = array()) {
|
2011-10-16 10:06:41 +00:00
|
|
|
if (isset($config[0])) {
|
|
|
|
$config['type'] = $config[0];
|
|
|
|
unset($config[0]);
|
|
|
|
}
|
2014-04-07 23:25:14 +00:00
|
|
|
$settings = $config + $this->_defaults;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-01-14 23:21:53 +00:00
|
|
|
if (in_array($settings['scope'], $Model->getAssociated('belongsTo'))) {
|
|
|
|
$data = $Model->getAssociated($settings['scope']);
|
2012-07-21 19:07:51 +00:00
|
|
|
$Parent = $Model->{$settings['scope']};
|
|
|
|
$settings['scope'] = $Model->escapeField($data['foreignKey']) . ' = ' . $Parent->escapeField();
|
2008-05-30 11:40:08 +00:00
|
|
|
$settings['recursive'] = 0;
|
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
$this->settings[$Model->alias] = $settings;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* After save method. Called after all saves
|
|
|
|
*
|
2011-12-02 05:58:09 +00:00
|
|
|
* Overridden to transparently manage setting the lft and rght fields if and only if the parent field is included in the
|
2008-05-30 11:40:08 +00:00
|
|
|
* parameters to be saved.
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param boolean $created indicates whether the node just saved was created or updated
|
2013-09-10 00:20:22 +00:00
|
|
|
* @param array $options Options passed from Model::save().
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean true on success, false on failure
|
|
|
|
*/
|
2013-09-10 00:20:22 +00:00
|
|
|
public function afterSave(Model $Model, $created, $options = array()) {
|
2009-01-14 23:21:53 +00:00
|
|
|
extract($this->settings[$Model->alias]);
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($created) {
|
2009-01-14 23:21:53 +00:00
|
|
|
if ((isset($Model->data[$Model->alias][$parent])) && $Model->data[$Model->alias][$parent]) {
|
|
|
|
return $this->_setParent($Model, $Model->data[$Model->alias][$parent], $created);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-03-04 19:18:04 +00:00
|
|
|
} elseif ($this->settings[$Model->alias]['__parentChange']) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$this->settings[$Model->alias]['__parentChange'] = false;
|
|
|
|
return $this->_setParent($Model, $Model->data[$Model->alias][$parent]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-01-19 22:20:15 +00:00
|
|
|
/**
|
|
|
|
* Runs before a find() operation
|
|
|
|
*
|
2013-07-05 12:15:18 +00:00
|
|
|
* @param Model $Model Model using the behavior
|
2012-01-19 22:20:15 +00:00
|
|
|
* @param array $query Query parameters as set by cake
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function beforeFind(Model $Model, $query) {
|
2012-11-13 10:18:17 +00:00
|
|
|
if ($Model->findQueryType === 'threaded' && !isset($query['parent'])) {
|
2012-01-19 22:20:15 +00:00
|
|
|
$query['parent'] = $this->settings[$Model->alias]['parent'];
|
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-03-21 02:36:48 +00:00
|
|
|
* Stores the record about to be deleted.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-03-21 02:36:48 +00:00
|
|
|
* This is used to delete child nodes in the afterDelete.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior.
|
|
|
|
* @param boolean $cascade If true records that depend on this record will also be deleted
|
2012-03-21 02:36:48 +00:00
|
|
|
* @return boolean
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function beforeDelete(Model $Model, $cascade = true) {
|
2009-01-14 23:21:53 +00:00
|
|
|
extract($this->settings[$Model->alias]);
|
2012-07-14 19:54:07 +00:00
|
|
|
$data = $Model->find('first', array(
|
2012-07-21 19:07:51 +00:00
|
|
|
'conditions' => array($Model->escapeField($Model->primaryKey) => $Model->id),
|
|
|
|
'fields' => array($Model->escapeField($left), $Model->escapeField($right)),
|
2012-07-14 19:54:07 +00:00
|
|
|
'recursive' => -1));
|
|
|
|
if ($data) {
|
2012-12-20 01:48:51 +00:00
|
|
|
$this->_deletedRow[$Model->alias] = current($data);
|
2012-07-14 19:54:07 +00:00
|
|
|
}
|
2012-03-21 02:36:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* After delete method.
|
|
|
|
*
|
|
|
|
* Will delete the current node and all children using the deleteAll method and sync the table
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
2012-03-21 02:36:48 +00:00
|
|
|
* @return boolean true to continue, false to abort the delete
|
|
|
|
*/
|
|
|
|
public function afterDelete(Model $Model) {
|
|
|
|
extract($this->settings[$Model->alias]);
|
2012-12-20 01:48:51 +00:00
|
|
|
$data = $this->_deletedRow[$Model->alias];
|
|
|
|
$this->_deletedRow[$Model->alias] = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!$data[$right] || !$data[$left]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$diff = $data[$right] - $data[$left] + 1;
|
|
|
|
|
|
|
|
if ($diff > 2) {
|
|
|
|
if (is_string($scope)) {
|
|
|
|
$scope = array($scope);
|
|
|
|
}
|
2012-07-22 00:40:49 +00:00
|
|
|
$scope[][$Model->escapeField($left) . " BETWEEN ? AND ?"] = array($data[$left] + 1, $data[$right] - 1);
|
2009-01-14 23:21:53 +00:00
|
|
|
$Model->deleteAll($scope);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_sync($Model, $diff, '-', '> ' . $data[$right]);
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Before save method. Called before all saves
|
|
|
|
*
|
2011-12-02 05:58:09 +00:00
|
|
|
* Overridden to transparently manage setting the lft and rght fields if and only if the parent field is included in the
|
2008-05-30 11:40:08 +00:00
|
|
|
* parameters to be saved. For newly created nodes with NO parent the left and right field values are set directly by
|
|
|
|
* this method bypassing the setParent logic.
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
2013-09-05 13:38:29 +00:00
|
|
|
* @param array $options Options passed from Model::save().
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean true to continue, false to abort the save
|
2013-09-05 13:38:29 +00:00
|
|
|
* @see Model::save()
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-09-05 13:38:29 +00:00
|
|
|
public function beforeSave(Model $Model, $options = array()) {
|
2009-01-14 23:21:53 +00:00
|
|
|
extract($this->settings[$Model->alias]);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-01-14 23:21:53 +00:00
|
|
|
$this->_addToWhitelist($Model, array($left, $right));
|
2012-10-10 11:51:16 +00:00
|
|
|
if (!$Model->id || !$Model->exists()) {
|
2009-01-14 23:21:53 +00:00
|
|
|
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
|
2008-05-30 11:40:08 +00:00
|
|
|
));
|
|
|
|
if (!$parentNode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
list($parentNode) = array_values($parentNode);
|
2012-03-04 19:18:04 +00:00
|
|
|
$Model->data[$Model->alias][$left] = 0;
|
|
|
|
$Model->data[$Model->alias][$right] = 0;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-08-20 04:43:34 +00:00
|
|
|
$edge = $this->_getMax($Model, $scope, $right, $recursive);
|
2009-01-14 23:21:53 +00:00
|
|
|
$Model->data[$Model->alias][$left] = $edge + 1;
|
|
|
|
$Model->data[$Model->alias][$right] = $edge + 2;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
} elseif (array_key_exists($parent, $Model->data[$Model->alias])) {
|
|
|
|
if ($Model->data[$Model->alias][$parent] != $Model->field($parent)) {
|
|
|
|
$this->settings[$Model->alias]['__parentChange'] = true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
if (!$Model->data[$Model->alias][$parent]) {
|
|
|
|
$Model->data[$Model->alias][$parent] = null;
|
|
|
|
$this->_addToWhitelist($Model, $parent);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-02-05 18:18:46 +00:00
|
|
|
$values = $Model->find('first', array(
|
2011-12-16 06:52:07 +00:00
|
|
|
'conditions' => array($scope, $Model->escapeField() => $Model->id),
|
|
|
|
'fields' => array($Model->primaryKey, $parent, $left, $right), 'recursive' => $recursive)
|
2010-02-05 18:18:46 +00:00
|
|
|
);
|
|
|
|
|
2013-12-27 05:37:39 +00:00
|
|
|
if (empty($values)) {
|
2010-02-05 18:18:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
list($node) = array_values($values);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-01-14 23:21:53 +00:00
|
|
|
$parentNode = $Model->find('first', array(
|
|
|
|
'conditions' => array($scope, $Model->escapeField() => $Model->data[$Model->alias][$parent]),
|
|
|
|
'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive
|
2008-05-30 11:40:08 +00:00
|
|
|
));
|
|
|
|
if (!$parentNode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
list($parentNode) = array_values($parentNode);
|
|
|
|
|
|
|
|
if (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) {
|
|
|
|
return false;
|
2014-04-29 12:19:33 +00:00
|
|
|
}
|
|
|
|
if ($node[$Model->primaryKey] === $parentNode[$Model->primaryKey]) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Get the number of child nodes
|
|
|
|
*
|
|
|
|
* If the direct parameter is set to true, only the direct children are counted (based upon the parent_id field)
|
|
|
|
* If false is passed for the id parameter, all top level nodes are counted, or all nodes are counted.
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param integer|string|boolean $id The ID of the record to read or false to read all top level nodes
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param boolean $direct whether to count direct, or all, children
|
|
|
|
* @return integer number of child nodes
|
2011-10-15 17:04:31 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::childCount
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function childCount(Model $Model, $id = null, $direct = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($id)) {
|
2012-05-13 09:37:50 +00:00
|
|
|
extract(array_merge(array('id' => null), $id));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
if ($id === null && $Model->id) {
|
|
|
|
$id = $Model->id;
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (!$id) {
|
|
|
|
$id = null;
|
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
extract($this->settings[$Model->alias]);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($direct) {
|
2009-01-14 23:21:53 +00:00
|
|
|
return $Model->find('count', array('conditions' => array($scope, $Model->escapeField($parent) => $id)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($id === null) {
|
2009-01-14 23:21:53 +00:00
|
|
|
return $Model->find('count', array('conditions' => $scope));
|
2011-07-30 19:37:46 +00:00
|
|
|
} elseif ($Model->id === $id && isset($Model->data[$Model->alias][$left]) && isset($Model->data[$Model->alias][$right])) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$data = $Model->data[$Model->alias];
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-01-15 00:50:01 +00:00
|
|
|
$data = $Model->find('first', array('conditions' => array($scope, $Model->escapeField() => $id), 'recursive' => $recursive));
|
|
|
|
if (!$data) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$data = $data[$Model->alias];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return ($data[$right] - $data[$left] - 1) / 2;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Get the child nodes of the current model
|
|
|
|
*
|
|
|
|
* If the direct parameter is set to true, only the direct children are returned (based upon the parent_id field)
|
|
|
|
* If false is passed for the id parameter, top level, or all (depending on direct parameter appropriate) are counted.
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param integer|string $id The ID of the record to read
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param boolean $direct whether to return only the direct, or all, children
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $fields Either a single string of a field name, or an array of field names
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC") defaults to the tree order
|
|
|
|
* @param integer $limit SQL LIMIT clause, for calculating items per page.
|
|
|
|
* @param integer $page Page number, for accessing paged data
|
|
|
|
* @param integer $recursive The number of levels deep to fetch associated records
|
|
|
|
* @return array Array of child nodes
|
2011-10-15 17:04:31 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::children
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function children(Model $Model, $id = null, $direct = false, $fields = null, $order = null, $limit = null, $page = 1, $recursive = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($id)) {
|
2012-05-13 09:37:50 +00:00
|
|
|
extract(array_merge(array('id' => null), $id));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$overrideRecursive = $recursive;
|
2008-05-31 12:36:38 +00:00
|
|
|
|
2009-01-14 23:21:53 +00:00
|
|
|
if ($id === null && $Model->id) {
|
|
|
|
$id = $Model->id;
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (!$id) {
|
|
|
|
$id = null;
|
|
|
|
}
|
2012-02-17 20:10:43 +00:00
|
|
|
|
2009-01-14 23:21:53 +00:00
|
|
|
extract($this->settings[$Model->alias]);
|
2008-05-31 12:36:38 +00:00
|
|
|
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($overrideRecursive !== null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$recursive = $overrideRecursive;
|
|
|
|
}
|
|
|
|
if (!$order) {
|
2012-07-22 00:40:49 +00:00
|
|
|
$order = $Model->escapeField($left) . " asc";
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($direct) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$conditions = array($scope, $Model->escapeField($parent) => $id);
|
|
|
|
return $Model->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-06-20 20:17:23 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$id) {
|
2008-06-20 20:17:23 +00:00
|
|
|
$conditions = $scope;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-08-24 11:14:46 +00:00
|
|
|
$result = array_values((array)$Model->find('first', array(
|
2009-01-14 23:21:53 +00:00
|
|
|
'conditions' => array($scope, $Model->escapeField() => $id),
|
2008-06-20 20:17:23 +00:00
|
|
|
'fields' => array($left, $right),
|
|
|
|
'recursive' => $recursive
|
|
|
|
)));
|
|
|
|
|
|
|
|
if (empty($result) || !isset($result[0])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return array();
|
|
|
|
}
|
2008-06-20 20:17:23 +00:00
|
|
|
$conditions = array($scope,
|
2009-01-14 23:21:53 +00:00
|
|
|
$Model->escapeField($right) . ' <' => $result[0][$right],
|
|
|
|
$Model->escapeField($left) . ' >' => $result[0][$left]
|
2008-06-20 20:17:23 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
return $Model->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* A convenience method for returning a hierarchical array used for HTML select boxes
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $conditions SQL conditions as a string or as an array('field' =>'value',...)
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $keyPath A string path to the key, i.e. "{n}.Post.id"
|
|
|
|
* @param string $valuePath A string path to the value, i.e. "{n}.Post.title"
|
|
|
|
* @param string $spacer The character or characters which will be repeated
|
|
|
|
* @param integer $recursive The number of levels deep to fetch associated records
|
|
|
|
* @return array An associative array of records, where the id is the key, and the display field is the value
|
2011-10-15 17:04:31 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::generateTreeList
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function generateTreeList(Model $Model, $conditions = null, $keyPath = null, $valuePath = null, $spacer = '_', $recursive = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$overrideRecursive = $recursive;
|
2009-01-14 23:21:53 +00:00
|
|
|
extract($this->settings[$Model->alias]);
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($overrideRecursive !== null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$recursive = $overrideRecursive;
|
|
|
|
}
|
|
|
|
|
2012-09-14 17:26:30 +00:00
|
|
|
$fields = null;
|
|
|
|
if (!$keyPath && !$valuePath && $Model->hasField($Model->displayField)) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$fields = array($Model->primaryKey, $Model->displayField, $left, $right);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2012-09-14 17:26:30 +00:00
|
|
|
if (!$keyPath) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$keyPath = '{n}.' . $Model->alias . '.' . $Model->primaryKey;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2012-09-14 17:26:30 +00:00
|
|
|
if (!$valuePath) {
|
2012-03-11 01:57:18 +00:00
|
|
|
$valuePath = array('%s%s', '{n}.tree_prefix', '{n}.' . $Model->alias . '.' . $Model->displayField);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
} elseif (is_string($valuePath)) {
|
2012-03-11 01:57:18 +00:00
|
|
|
$valuePath = array('%s%s', '{n}.tree_prefix', $valuePath);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
} else {
|
2012-07-18 01:06:41 +00:00
|
|
|
array_unshift($valuePath, '%s' . $valuePath[0], '{n}.tree_prefix');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-09-01 08:02:26 +00:00
|
|
|
|
|
|
|
$conditions = (array)$conditions;
|
|
|
|
if ($scope) {
|
|
|
|
$conditions[] = $scope;
|
|
|
|
}
|
|
|
|
|
2012-07-22 00:40:49 +00:00
|
|
|
$order = $Model->escapeField($left) . " asc";
|
2009-01-14 23:21:53 +00:00
|
|
|
$results = $Model->find('all', compact('conditions', 'fields', 'order', 'recursive'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$stack = array();
|
|
|
|
|
|
|
|
foreach ($results as $i => $result) {
|
2012-06-01 01:14:34 +00:00
|
|
|
$count = count($stack);
|
|
|
|
while ($stack && ($stack[$count - 1] < $result[$Model->alias][$right])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
array_pop($stack);
|
2012-06-01 01:14:34 +00:00
|
|
|
$count--;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-06-01 01:14:34 +00:00
|
|
|
$results[$i]['tree_prefix'] = str_repeat($spacer, $count);
|
2009-01-14 23:21:53 +00:00
|
|
|
$stack[] = $result[$Model->alias][$right];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (empty($results)) {
|
|
|
|
return array();
|
|
|
|
}
|
2012-03-11 01:57:18 +00:00
|
|
|
return Hash::combine($results, $keyPath, $valuePath);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Get the parent node
|
|
|
|
*
|
|
|
|
* reads the parent id and returns this node
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param integer|string $id The ID of the record to read
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param string|array $fields Fields to get
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param integer $recursive The number of levels deep to fetch associated records
|
2011-07-30 22:38:57 +00:00
|
|
|
* @return array|boolean Array of data for the parent node
|
2011-10-15 17:04:31 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::getParentNode
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function getParentNode(Model $Model, $id = null, $fields = null, $recursive = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($id)) {
|
2012-05-13 09:37:50 +00:00
|
|
|
extract(array_merge(array('id' => null), $id));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$overrideRecursive = $recursive;
|
2013-02-04 08:26:50 +00:00
|
|
|
if (empty($id)) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$id = $Model->id;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
extract($this->settings[$Model->alias]);
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($overrideRecursive !== null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$recursive = $overrideRecursive;
|
|
|
|
}
|
2010-04-12 17:27:31 +00:00
|
|
|
$parentId = $Model->find('first', array('conditions' => array($Model->primaryKey => $id), 'fields' => array($parent), 'recursive' => -1));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($parentId) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$parentId = $parentId[$Model->alias][$parent];
|
|
|
|
$parent = $Model->find('first', array('conditions' => array($Model->escapeField() => $parentId), 'fields' => $fields, 'recursive' => $recursive));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
return $parent;
|
|
|
|
}
|
2008-09-08 20:33:48 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Get the path to the given node
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param integer|string $id The ID of the record to read
|
|
|
|
* @param string|array $fields Either a single string of a field name, or an array of field names
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param integer $recursive The number of levels deep to fetch associated records
|
|
|
|
* @return array Array of nodes from top most parent to current node
|
2011-10-15 17:04:31 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::getPath
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function getPath(Model $Model, $id = null, $fields = null, $recursive = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($id)) {
|
2012-05-13 09:37:50 +00:00
|
|
|
extract(array_merge(array('id' => null), $id));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$overrideRecursive = $recursive;
|
2013-02-04 08:26:50 +00:00
|
|
|
if (empty($id)) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$id = $Model->id;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
extract($this->settings[$Model->alias]);
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($overrideRecursive !== null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$recursive = $overrideRecursive;
|
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
$result = $Model->find('first', array('conditions' => array($Model->escapeField() => $id), 'fields' => array($left, $right), 'recursive' => $recursive));
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($result) {
|
|
|
|
$result = array_values($result);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$item = $result[0];
|
2009-01-14 23:21:53 +00:00
|
|
|
$results = $Model->find('all', array(
|
|
|
|
'conditions' => array($scope, $Model->escapeField($left) . ' <=' => $item[$left], $Model->escapeField($right) . ' >=' => $item[$right]),
|
|
|
|
'fields' => $fields, 'order' => array($Model->escapeField($left) => 'asc'), 'recursive' => $recursive
|
2008-05-30 11:40:08 +00:00
|
|
|
));
|
|
|
|
return $results;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Reorder the node without changing the parent.
|
|
|
|
*
|
|
|
|
* If the node is the last child, or is a top level node with no subsequent node this method will return false
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param integer|string $id The ID of the record to move
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param integer|boolean $number how many places to move the node or true to move to last position
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean true on success, false on failure
|
2011-10-15 17:04:31 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::moveDown
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function moveDown(Model $Model, $id = null, $number = 1) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($id)) {
|
2012-05-13 09:37:50 +00:00
|
|
|
extract(array_merge(array('id' => null), $id));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!$number) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-04 08:26:50 +00:00
|
|
|
if (empty($id)) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$id = $Model->id;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
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), 'recursive' => $recursive
|
2008-05-30 11:40:08 +00:00
|
|
|
)));
|
|
|
|
if ($node[$parent]) {
|
2009-01-14 23:21:53 +00:00
|
|
|
list($parentNode) = array_values($Model->find('first', array(
|
|
|
|
'conditions' => array($scope, $Model->escapeField() => $node[$parent]),
|
|
|
|
'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive
|
2008-05-30 11:40:08 +00:00
|
|
|
)));
|
|
|
|
if (($node[$right] + 1) == $parentNode[$right]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
$nextNode = $Model->find('first', array(
|
|
|
|
'conditions' => array($scope, $Model->escapeField($left) => ($node[$right] + 1)),
|
|
|
|
'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive)
|
2008-05-31 12:36:38 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($nextNode) {
|
2009-10-21 04:22:36 +00:00
|
|
|
list($nextNode) = array_values($nextNode);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-20 04:43:34 +00:00
|
|
|
$edge = $this->_getMax($Model, $scope, $right, $recursive);
|
|
|
|
$this->_sync($Model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right]);
|
|
|
|
$this->_sync($Model, $nextNode[$left] - $node[$left], '-', 'BETWEEN ' . $nextNode[$left] . ' AND ' . $nextNode[$right]);
|
|
|
|
$this->_sync($Model, $edge - $node[$left] - ($nextNode[$right] - $nextNode[$left]), '-', '> ' . $edge);
|
2008-06-20 20:17:23 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_int($number)) {
|
|
|
|
$number--;
|
|
|
|
}
|
|
|
|
if ($number) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$this->moveDown($Model, $id, $number);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Reorder the node without changing the parent.
|
|
|
|
*
|
|
|
|
* If the node is the first child, or is a top level node with no previous node this method will return false
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param integer|string $id The ID of the record to move
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param integer|boolean $number how many places to move the node, or true to move to first position
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean true on success, false on failure
|
2011-10-15 17:04:31 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::moveUp
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function moveUp(Model $Model, $id = null, $number = 1) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($id)) {
|
2012-05-13 09:37:50 +00:00
|
|
|
extract(array_merge(array('id' => null), $id));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!$number) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-04 08:26:50 +00:00
|
|
|
if (empty($id)) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$id = $Model->id;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
extract($this->settings[$Model->alias]);
|
|
|
|
list($node) = array_values($Model->find('first', array(
|
|
|
|
'conditions' => array($scope, $Model->escapeField() => $id),
|
2011-12-16 06:52:07 +00:00
|
|
|
'fields' => array($Model->primaryKey, $left, $right, $parent), 'recursive' => $recursive
|
2008-05-30 11:40:08 +00:00
|
|
|
)));
|
|
|
|
if ($node[$parent]) {
|
2009-01-14 23:21:53 +00:00
|
|
|
list($parentNode) = array_values($Model->find('first', array(
|
|
|
|
'conditions' => array($scope, $Model->escapeField() => $node[$parent]),
|
|
|
|
'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive
|
2008-05-30 11:40:08 +00:00
|
|
|
)));
|
|
|
|
if (($node[$left] - 1) == $parentNode[$left]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
$previousNode = $Model->find('first', array(
|
|
|
|
'conditions' => array($scope, $Model->escapeField($right) => ($node[$left] - 1)),
|
|
|
|
'fields' => array($Model->primaryKey, $left, $right),
|
2008-05-30 11:40:08 +00:00
|
|
|
'recursive' => $recursive
|
|
|
|
));
|
|
|
|
|
|
|
|
if ($previousNode) {
|
|
|
|
list($previousNode) = array_values($previousNode);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-20 04:43:34 +00:00
|
|
|
$edge = $this->_getMax($Model, $scope, $right, $recursive);
|
2012-03-04 19:18:04 +00:00
|
|
|
$this->_sync($Model, $edge - $previousNode[$left] + 1, '+', 'BETWEEN ' . $previousNode[$left] . ' AND ' . $previousNode[$right]);
|
2011-12-16 06:52:07 +00:00
|
|
|
$this->_sync($Model, $node[$left] - $previousNode[$left], '-', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right]);
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_sync($Model, $edge - $previousNode[$left] - ($node[$right] - $node[$left]), '-', '> ' . $edge);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_int($number)) {
|
|
|
|
$number--;
|
|
|
|
}
|
|
|
|
if ($number) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$this->moveUp($Model, $id, $number);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Recover a corrupted tree
|
|
|
|
*
|
|
|
|
* The mode parameter is used to specify the source of info that is valid/correct. The opposite source of data
|
|
|
|
* will be populated based upon that source of info. E.g. if the MPTT fields are corrupt or empty, with the $mode
|
|
|
|
* 'parent' the values of the parent_id field will be used to populate the left and right fields. The missingParentAction
|
|
|
|
* parameter only applies to "parent" mode and determines what to do if the parent field contains an id that is not present.
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $mode parent or tree
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|integer $missingParentAction 'return' to do nothing and return, 'delete' to
|
2008-05-30 11:40:08 +00:00
|
|
|
* delete, or the id of the parent to set as the parent_id
|
|
|
|
* @return boolean true on success, false on failure
|
2011-10-15 17:04:31 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::recover
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function recover(Model $Model, $mode = 'parent', $missingParentAction = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($mode)) {
|
2012-05-13 09:37:50 +00:00
|
|
|
extract(array_merge(array('mode' => 'parent'), $mode));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
extract($this->settings[$Model->alias]);
|
|
|
|
$Model->recursive = $recursive;
|
2012-11-13 10:18:17 +00:00
|
|
|
if ($mode === 'parent') {
|
2009-01-14 23:21:53 +00:00
|
|
|
$Model->bindModel(array('belongsTo' => array('VerifyParent' => array(
|
2011-11-30 01:42:44 +00:00
|
|
|
'className' => $Model->name,
|
2008-05-30 11:40:08 +00:00
|
|
|
'foreignKey' => $parent,
|
2009-01-14 23:21:53 +00:00
|
|
|
'fields' => array($Model->primaryKey, $left, $right, $parent),
|
2008-05-30 11:40:08 +00:00
|
|
|
))));
|
2009-01-14 23:21:53 +00:00
|
|
|
$missingParents = $Model->find('list', array(
|
2008-05-30 11:40:08 +00:00
|
|
|
'recursive' => 0,
|
|
|
|
'conditions' => array($scope, array(
|
2009-01-14 23:21:53 +00:00
|
|
|
'NOT' => array($Model->escapeField($parent) => null), $Model->VerifyParent->escapeField() => null
|
2008-05-30 11:40:08 +00:00
|
|
|
))
|
|
|
|
));
|
2009-01-14 23:21:53 +00:00
|
|
|
$Model->unbindModel(array('belongsTo' => array('VerifyParent')));
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($missingParents) {
|
2012-11-13 10:18:17 +00:00
|
|
|
if ($missingParentAction === 'return') {
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($missingParents as $id => $display) {
|
2012-11-14 11:36:55 +00:00
|
|
|
$this->errors[] = 'cannot find the parent for ' . $Model->alias . ' with id ' . $id . '(' . $display . ')';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
2012-11-13 10:18:17 +00:00
|
|
|
} elseif ($missingParentAction === 'delete') {
|
2012-11-13 20:12:46 +00:00
|
|
|
$Model->deleteAll(array($Model->escapeField($Model->primaryKey) => array_flip($missingParents)), false);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2012-11-13 20:12:46 +00:00
|
|
|
$Model->updateAll(array($Model->escapeField($parent) => $missingParentAction), array($Model->escapeField($Model->primaryKey) => array_flip($missingParents)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-24 16:58:30 +00:00
|
|
|
|
|
|
|
$this->_recoverByParentId($Model);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-10-17 23:28:03 +00:00
|
|
|
$db = ConnectionManager::getDataSource($Model->useDbConfig);
|
2009-01-14 23:21:53 +00:00
|
|
|
foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey, $parent), 'order' => $left)) as $array) {
|
2010-07-24 18:02:49 +00:00
|
|
|
$path = $this->getPath($Model, $array[$Model->alias][$Model->primaryKey]);
|
2012-09-14 17:26:30 +00:00
|
|
|
$parentId = null;
|
|
|
|
if (count($path) > 1) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$parentId = $path[count($path) - 2][$Model->alias][$Model->primaryKey];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-01-15 00:50:01 +00:00
|
|
|
$Model->updateAll(array($parent => $db->value($parentId, $parent)), array($Model->escapeField() => $array[$Model->alias][$Model->primaryKey]));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2013-03-24 16:58:30 +00:00
|
|
|
/**
|
|
|
|
* _recoverByParentId
|
|
|
|
*
|
|
|
|
* Recursive helper function used by recover
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model instance.
|
|
|
|
* @param integer $counter Counter
|
|
|
|
* @param mixed $parentId Parent record Id
|
2013-03-24 16:58:30 +00:00
|
|
|
* @return integer $counter
|
|
|
|
*/
|
|
|
|
protected function _recoverByParentId(Model $Model, $counter = 1, $parentId = null) {
|
|
|
|
$params = array(
|
|
|
|
'conditions' => array(
|
|
|
|
$this->settings[$Model->alias]['parent'] => $parentId
|
|
|
|
),
|
|
|
|
'fields' => array($Model->primaryKey),
|
|
|
|
'page' => 1,
|
|
|
|
'limit' => 100,
|
|
|
|
'order' => array($Model->primaryKey)
|
|
|
|
);
|
|
|
|
|
|
|
|
$scope = $this->settings[$Model->alias]['scope'];
|
|
|
|
if ($scope && ($scope !== '1 = 1' && $scope !== true)) {
|
2013-09-18 19:09:19 +00:00
|
|
|
$params['conditions'][] = $scope;
|
2013-03-24 16:58:30 +00:00
|
|
|
}
|
|
|
|
|
2013-03-25 08:46:02 +00:00
|
|
|
$children = $Model->find('all', $params);
|
|
|
|
$hasChildren = (bool)$children;
|
|
|
|
|
2013-10-11 23:27:00 +00:00
|
|
|
if ($parentId !== null) {
|
2013-03-31 20:46:52 +00:00
|
|
|
if ($hasChildren) {
|
2013-03-25 08:46:02 +00:00
|
|
|
$Model->updateAll(
|
|
|
|
array($this->settings[$Model->alias]['left'] => $counter),
|
|
|
|
array($Model->escapeField() => $parentId)
|
|
|
|
);
|
|
|
|
$counter++;
|
|
|
|
} else {
|
|
|
|
$Model->updateAll(
|
|
|
|
array(
|
|
|
|
$this->settings[$Model->alias]['left'] => $counter,
|
|
|
|
$this->settings[$Model->alias]['right'] => $counter + 1
|
|
|
|
),
|
|
|
|
array($Model->escapeField() => $parentId)
|
|
|
|
);
|
|
|
|
$counter += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while ($children) {
|
|
|
|
foreach ($children as $row) {
|
2013-03-24 16:58:30 +00:00
|
|
|
$counter = $this->_recoverByParentId($Model, $counter, $row[$Model->alias][$Model->primaryKey]);
|
|
|
|
}
|
2013-03-25 08:46:02 +00:00
|
|
|
|
|
|
|
if (count($children) !== $params['limit']) {
|
2013-03-24 16:58:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-03-25 08:46:02 +00:00
|
|
|
$params['page']++;
|
|
|
|
$children = $Model->find('all', $params);
|
2013-03-24 16:58:30 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 23:27:00 +00:00
|
|
|
if ($parentId !== null && $hasChildren) {
|
2013-03-24 16:58:30 +00:00
|
|
|
$Model->updateAll(
|
|
|
|
array($this->settings[$Model->alias]['right'] => $counter),
|
|
|
|
array($Model->escapeField() => $parentId)
|
|
|
|
);
|
|
|
|
$counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $counter;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Reorder method.
|
|
|
|
*
|
|
|
|
* Reorders the nodes (and child nodes) of the tree according to the field and direction specified in the parameters.
|
|
|
|
* This method does not change the parent of any node.
|
|
|
|
*
|
|
|
|
* Requires a valid tree, by default it verifies the tree before beginning.
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2009-03-08 17:12:30 +00:00
|
|
|
* Options:
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2009-03-08 17:12:30 +00:00
|
|
|
* - 'id' id of record to use as top node for reordering
|
2011-12-02 05:58:09 +00:00
|
|
|
* - 'field' Which field to use in reordering defaults to displayField
|
2009-03-08 17:12:30 +00:00
|
|
|
* - 'order' Direction to order either DESC or ASC (defaults to ASC)
|
|
|
|
* - 'verify' Whether or not to verify the tree before reorder. defaults to true.
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
2009-03-08 17:12:30 +00:00
|
|
|
* @param array $options array of options to use in reordering.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean true on success, false on failure
|
2011-10-15 17:04:31 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::reorder
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function reorder(Model $Model, $options = array()) {
|
2014-04-07 23:25:14 +00:00
|
|
|
$options += array('id' => null, 'field' => $Model->displayField, 'order' => 'ASC', 'verify' => true);
|
2008-05-30 11:40:08 +00:00
|
|
|
extract($options);
|
2009-01-14 23:21:53 +00:00
|
|
|
if ($verify && !$this->verify($Model)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$verify = false;
|
2009-01-14 23:21:53 +00:00
|
|
|
extract($this->settings[$Model->alias]);
|
|
|
|
$fields = array($Model->primaryKey, $field, $left, $right);
|
2008-05-30 11:40:08 +00:00
|
|
|
$sort = $field . ' ' . $order;
|
2009-01-14 23:21:53 +00:00
|
|
|
$nodes = $this->children($Model, $id, true, $fields, $sort, null, null, $recursive);
|
2008-06-20 20:17:23 +00:00
|
|
|
|
2009-10-21 04:22:36 +00:00
|
|
|
$cacheQueries = $Model->cacheQueries;
|
|
|
|
$Model->cacheQueries = false;
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($nodes) {
|
|
|
|
foreach ($nodes as $node) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$id = $node[$Model->alias][$Model->primaryKey];
|
|
|
|
$this->moveDown($Model, $id, true);
|
|
|
|
if ($node[$Model->alias][$left] != $node[$Model->alias][$right] - 1) {
|
|
|
|
$this->reorder($Model, compact('id', 'field', 'order', 'verify'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-21 04:22:36 +00:00
|
|
|
$Model->cacheQueries = $cacheQueries;
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Remove the current node from the tree, and reparent all children up one level.
|
|
|
|
*
|
|
|
|
* If the parameter delete is false, the node will become a new top level node. Otherwise the node will be deleted
|
|
|
|
* after the children are reparented.
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param integer|string $id The ID of the record to remove
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param boolean $delete whether to delete the node after reparenting children (if any)
|
|
|
|
* @return boolean true on success, false on failure
|
2011-10-15 17:04:31 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::removeFromTree
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function removeFromTree(Model $Model, $id = null, $delete = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($id)) {
|
2012-05-13 09:37:50 +00:00
|
|
|
extract(array_merge(array('id' => null), $id));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
extract($this->settings[$Model->alias]);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-01-14 23:21:53 +00:00
|
|
|
list($node) = array_values($Model->find('first', array(
|
|
|
|
'conditions' => array($scope, $Model->escapeField() => $id),
|
|
|
|
'fields' => array($Model->primaryKey, $left, $right, $parent),
|
2008-05-30 11:40:08 +00:00
|
|
|
'recursive' => $recursive
|
|
|
|
)));
|
|
|
|
|
|
|
|
if ($node[$right] == $node[$left] + 1) {
|
|
|
|
if ($delete) {
|
2009-01-14 23:21:53 +00:00
|
|
|
return $Model->delete($id);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-08-12 17:06:34 +00:00
|
|
|
$Model->id = $id;
|
|
|
|
return $Model->saveField($parent, null);
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif ($node[$parent]) {
|
2009-01-14 23:21:53 +00:00
|
|
|
list($parentNode) = array_values($Model->find('first', array(
|
|
|
|
'conditions' => array($scope, $Model->escapeField() => $node[$parent]),
|
|
|
|
'fields' => array($Model->primaryKey, $left, $right),
|
2008-05-30 11:40:08 +00:00
|
|
|
'recursive' => $recursive
|
|
|
|
)));
|
|
|
|
} else {
|
|
|
|
$parentNode[$right] = $node[$right] + 1;
|
|
|
|
}
|
|
|
|
|
2010-10-17 23:28:03 +00:00
|
|
|
$db = ConnectionManager::getDataSource($Model->useDbConfig);
|
2010-07-31 18:29:00 +00:00
|
|
|
$Model->updateAll(
|
2011-05-28 20:38:46 +00:00
|
|
|
array($parent => $db->value($node[$parent], $parent)),
|
2010-07-31 18:29:00 +00:00
|
|
|
array($Model->escapeField($parent) => $node[$Model->primaryKey])
|
|
|
|
);
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_sync($Model, 1, '-', 'BETWEEN ' . ($node[$left] + 1) . ' AND ' . ($node[$right] - 1));
|
|
|
|
$this->_sync($Model, 2, '-', '> ' . ($node[$right]));
|
2009-01-14 23:21:53 +00:00
|
|
|
$Model->id = $id;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($delete) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$Model->updateAll(
|
2008-05-30 11:40:08 +00:00
|
|
|
array(
|
2009-01-14 23:21:53 +00:00
|
|
|
$Model->escapeField($left) => 0,
|
|
|
|
$Model->escapeField($right) => 0,
|
|
|
|
$Model->escapeField($parent) => null
|
2008-05-30 11:40:08 +00:00
|
|
|
),
|
2009-01-14 23:21:53 +00:00
|
|
|
array($Model->escapeField() => $id)
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
2009-01-14 23:21:53 +00:00
|
|
|
return $Model->delete($id);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-07-02 22:52:48 +00:00
|
|
|
$edge = $this->_getMax($Model, $scope, $right, $recursive);
|
|
|
|
if ($node[$right] == $edge) {
|
|
|
|
$edge = $edge - 2;
|
|
|
|
}
|
|
|
|
$Model->id = $id;
|
|
|
|
return $Model->save(
|
|
|
|
array($left => $edge + 1, $right => $edge + 2, $parent => null),
|
|
|
|
array('callbacks' => false, 'validate' => false)
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Check if the current tree is valid.
|
|
|
|
*
|
|
|
|
* Returns true if the tree is valid otherwise an array of (type, incorrect left/right index, message)
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed true if the tree is valid or empty, otherwise an array of (error type [index, node],
|
|
|
|
* [incorrect left/right index,node id], message)
|
2011-10-15 17:04:31 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::verify
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function verify(Model $Model) {
|
2009-01-14 23:21:53 +00:00
|
|
|
extract($this->settings[$Model->alias]);
|
|
|
|
if (!$Model->find('count', array('conditions' => $scope))) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-08-20 04:43:34 +00:00
|
|
|
$min = $this->_getMin($Model, $scope, $left, $recursive);
|
|
|
|
$edge = $this->_getMax($Model, $scope, $right, $recursive);
|
2012-03-04 19:18:04 +00:00
|
|
|
$errors = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
for ($i = $min; $i <= $edge; $i++) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$count = $Model->find('count', array('conditions' => array(
|
|
|
|
$scope, 'OR' => array($Model->escapeField($left) => $i, $Model->escapeField($right) => $i)
|
2008-05-30 11:40:08 +00:00
|
|
|
)));
|
|
|
|
if ($count != 1) {
|
2012-09-14 17:42:25 +00:00
|
|
|
if (!$count) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$errors[] = array('index', $i, 'missing');
|
|
|
|
} else {
|
|
|
|
$errors[] = array('index', $i, 'duplicate');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
$node = $Model->find('first', array('conditions' => array($scope, $Model->escapeField($right) . '< ' . $Model->escapeField($left)), 'recursive' => 0));
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($node) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$errors[] = array('node', $node[$Model->alias][$Model->primaryKey], 'left greater than right.');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-01-14 23:21:53 +00:00
|
|
|
$Model->bindModel(array('belongsTo' => array('VerifyParent' => array(
|
2011-11-30 01:42:44 +00:00
|
|
|
'className' => $Model->name,
|
2008-05-30 11:40:08 +00:00
|
|
|
'foreignKey' => $parent,
|
2009-01-14 23:21:53 +00:00
|
|
|
'fields' => array($Model->primaryKey, $left, $right, $parent)
|
2008-05-30 11:40:08 +00:00
|
|
|
))));
|
|
|
|
|
2009-01-14 23:21:53 +00:00
|
|
|
foreach ($Model->find('all', array('conditions' => $scope, 'recursive' => 0)) as $instance) {
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($instance[$Model->alias][$left] === null || $instance[$Model->alias][$right] === null) {
|
2009-01-14 23:21:53 +00:00
|
|
|
$errors[] = array('node', $instance[$Model->alias][$Model->primaryKey],
|
2008-05-30 11:40:08 +00:00
|
|
|
'has invalid left or right values');
|
2009-01-14 23:21:53 +00:00
|
|
|
} elseif ($instance[$Model->alias][$left] == $instance[$Model->alias][$right]) {
|
|
|
|
$errors[] = array('node', $instance[$Model->alias][$Model->primaryKey],
|
2008-06-22 12:27:54 +00:00
|
|
|
'left and right values identical');
|
2009-01-14 23:21:53 +00:00
|
|
|
} elseif ($instance[$Model->alias][$parent]) {
|
|
|
|
if (!$instance['VerifyParent'][$Model->primaryKey]) {
|
|
|
|
$errors[] = array('node', $instance[$Model->alias][$Model->primaryKey],
|
|
|
|
'The parent node ' . $instance[$Model->alias][$parent] . ' doesn\'t exist');
|
|
|
|
} elseif ($instance[$Model->alias][$left] < $instance['VerifyParent'][$left]) {
|
|
|
|
$errors[] = array('node', $instance[$Model->alias][$Model->primaryKey],
|
|
|
|
'left less than parent (node ' . $instance['VerifyParent'][$Model->primaryKey] . ').');
|
|
|
|
} elseif ($instance[$Model->alias][$right] > $instance['VerifyParent'][$right]) {
|
|
|
|
$errors[] = array('node', $instance[$Model->alias][$Model->primaryKey],
|
|
|
|
'right greater than parent (node ' . $instance['VerifyParent'][$Model->primaryKey] . ').');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-01-14 23:21:53 +00:00
|
|
|
} elseif ($Model->find('count', array('conditions' => array($scope, $Model->escapeField($left) . ' <' => $instance[$Model->alias][$left], $Model->escapeField($right) . ' >' => $instance[$Model->alias][$right]), 'recursive' => 0))) {
|
|
|
|
$errors[] = array('node', $instance[$Model->alias][$Model->primaryKey], 'The parent field is blank, but has a parent');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($errors) {
|
|
|
|
return $errors;
|
|
|
|
}
|
2008-09-08 20:33:48 +00:00
|
|
|
return true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sets the parent of the given node
|
|
|
|
*
|
|
|
|
* The force parameter is used to override the "don't change the parent to the current parent" logic in the event
|
|
|
|
* of recovering a corrupted table, or creating new nodes. Otherwise it should always be false. In reality this
|
|
|
|
* method could be private, since calling save with parent_id set also calls setParent
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model using this behavior
|
|
|
|
* @param integer|string $parentId Parent record Id
|
|
|
|
* @param boolean $created True if newly created record else false.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean true on success, false on failure
|
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
protected function _setParent(Model $Model, $parentId = null, $created = false) {
|
2009-01-14 23:21:53 +00:00
|
|
|
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),
|
2008-06-20 20:17:23 +00:00
|
|
|
'recursive' => $recursive
|
|
|
|
)));
|
2011-08-20 04:43:34 +00:00
|
|
|
$edge = $this->_getMax($Model, $scope, $right, $recursive, $created);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2013-02-04 08:26:50 +00:00
|
|
|
if (empty($parentId)) {
|
2011-08-20 04:43:34 +00:00
|
|
|
$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);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-02-02 16:45:57 +00:00
|
|
|
$values = $Model->find('first', array(
|
2009-01-14 23:21:53 +00:00
|
|
|
'conditions' => array($scope, $Model->escapeField() => $parentId),
|
|
|
|
'fields' => array($Model->primaryKey, $left, $right),
|
2008-06-20 20:17:23 +00:00
|
|
|
'recursive' => $recursive
|
2010-02-02 16:45:57 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
if ($values === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$parentNode = array_values($values);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (empty($parentNode) || empty($parentNode[0])) {
|
|
|
|
return false;
|
2008-06-15 17:51:25 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$parentNode = $parentNode[0];
|
|
|
|
|
2014-04-29 12:19:33 +00:00
|
|
|
if (($Model->id === $parentId)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
} elseif (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-17 22:13:36 +00:00
|
|
|
if (empty($node[$left]) && empty($node[$right])) {
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_sync($Model, 2, '+', '>= ' . $parentNode[$right], $created);
|
2009-01-14 23:21:53 +00:00
|
|
|
$result = $Model->save(
|
2008-06-20 20:17:23 +00:00
|
|
|
array($left => $parentNode[$right], $right => $parentNode[$right] + 1, $parent => $parentId),
|
|
|
|
array('validate' => false, 'callbacks' => false)
|
|
|
|
);
|
2009-01-14 23:21:53 +00:00
|
|
|
$Model->data = $result;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2012-03-04 19:18:04 +00:00
|
|
|
$this->_sync($Model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right], $created);
|
2008-05-30 11:40:08 +00:00
|
|
|
$diff = $node[$right] - $node[$left] + 1;
|
|
|
|
|
|
|
|
if ($node[$left] > $parentNode[$left]) {
|
|
|
|
if ($node[$right] < $parentNode[$right]) {
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_sync($Model, $diff, '-', 'BETWEEN ' . $node[$right] . ' AND ' . ($parentNode[$right] - 1), $created);
|
|
|
|
$this->_sync($Model, $edge - $parentNode[$right] + $diff + 1, '-', '> ' . $edge, $created);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_sync($Model, $diff, '+', 'BETWEEN ' . $parentNode[$right] . ' AND ' . $node[$right], $created);
|
|
|
|
$this->_sync($Model, $edge - $parentNode[$right] + 1, '-', '> ' . $edge, $created);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_sync($Model, $diff, '-', 'BETWEEN ' . $node[$right] . ' AND ' . ($parentNode[$right] - 1), $created);
|
|
|
|
$this->_sync($Model, $edge - $parentNode[$right] + $diff + 1, '-', '> ' . $edge, $created);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* get the maximum index value in the table.
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model Instance.
|
|
|
|
* @param string $scope Scoping conditions.
|
|
|
|
* @param string $right Right value
|
|
|
|
* @param integer $recursive Recursive find value.
|
|
|
|
* @param boolean $created Whether it's a new record.
|
2011-07-30 22:38:57 +00:00
|
|
|
* @return integer
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
protected function _getMax(Model $Model, $scope, $right, $recursive = -1, $created = false) {
|
2010-10-17 23:28:03 +00:00
|
|
|
$db = ConnectionManager::getDataSource($Model->useDbConfig);
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($created) {
|
|
|
|
if (is_string($scope)) {
|
2012-07-22 00:40:49 +00:00
|
|
|
$scope .= " AND " . $Model->escapeField() . " <> ";
|
2009-01-14 23:21:53 +00:00
|
|
|
$scope .= $db->value($Model->id, $Model->getColumnType($Model->primaryKey));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-01-14 23:21:53 +00:00
|
|
|
$scope['NOT'][$Model->alias . '.' . $Model->primaryKey] = $Model->id;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-21 19:07:51 +00:00
|
|
|
$name = $Model->escapeField($right);
|
2009-01-14 23:21:53 +00:00
|
|
|
list($edge) = array_values($Model->find('first', array(
|
2008-05-30 11:40:08 +00:00
|
|
|
'conditions' => $scope,
|
2009-05-26 17:17:57 +00:00
|
|
|
'fields' => $db->calculate($Model, 'max', array($name, $right)),
|
2012-12-25 01:00:07 +00:00
|
|
|
'recursive' => $recursive,
|
|
|
|
'callbacks' => false
|
2008-05-30 11:40:08 +00:00
|
|
|
)));
|
2008-07-30 20:34:01 +00:00
|
|
|
return (empty($edge[$right])) ? 0 : $edge[$right];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* get the minimum index value in the table.
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model instance.
|
|
|
|
* @param string $scope Scoping conditions.
|
|
|
|
* @param string $left Left value.
|
|
|
|
* @param integer $recursive Recurursive find value.
|
2011-07-30 22:38:57 +00:00
|
|
|
* @return integer
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
protected function _getMin(Model $Model, $scope, $left, $recursive = -1) {
|
2010-10-17 23:28:03 +00:00
|
|
|
$db = ConnectionManager::getDataSource($Model->useDbConfig);
|
2012-07-21 19:07:51 +00:00
|
|
|
$name = $Model->escapeField($left);
|
2009-01-14 23:21:53 +00:00
|
|
|
list($edge) = array_values($Model->find('first', array(
|
2008-06-20 20:17:23 +00:00
|
|
|
'conditions' => $scope,
|
2009-05-26 17:17:57 +00:00
|
|
|
'fields' => $db->calculate($Model, 'min', array($name, $left)),
|
2012-12-25 01:00:07 +00:00
|
|
|
'recursive' => $recursive,
|
|
|
|
'callbacks' => false
|
2008-05-30 11:40:08 +00:00
|
|
|
)));
|
2008-07-30 20:34:01 +00:00
|
|
|
return (empty($edge[$left])) ? 0 : $edge[$left];
|
2008-06-20 20:17:23 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Table sync method.
|
|
|
|
*
|
|
|
|
* Handles table sync operations, Taking account of the behavior scope.
|
|
|
|
*
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param Model $Model Model instance.
|
|
|
|
* @param integer $shift Shift by.
|
|
|
|
* @param string $dir Direction.
|
|
|
|
* @param array $conditions Conditions.
|
2014-06-11 03:31:42 +00:00
|
|
|
* @param boolean $created Whether it's a new record.
|
2014-06-09 19:08:44 +00:00
|
|
|
* @param string $field Field type.
|
2011-07-30 22:38:57 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
protected function _sync(Model $Model, $shift, $dir = '+', $conditions = array(), $created = false, $field = 'both') {
|
2009-01-14 23:21:53 +00:00
|
|
|
$ModelRecursive = $Model->recursive;
|
|
|
|
extract($this->settings[$Model->alias]);
|
|
|
|
$Model->recursive = $recursive;
|
2008-05-31 12:36:38 +00:00
|
|
|
|
2012-11-13 10:18:17 +00:00
|
|
|
if ($field === 'both') {
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_sync($Model, $shift, $dir, $conditions, $created, $left);
|
2008-05-30 11:40:08 +00:00
|
|
|
$field = $right;
|
|
|
|
}
|
|
|
|
if (is_string($conditions)) {
|
2012-07-22 00:40:49 +00:00
|
|
|
$conditions = array($Model->escapeField($field) . " {$conditions}");
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-11-13 10:18:17 +00:00
|
|
|
if (($scope !== '1 = 1' && $scope !== true) && $scope) {
|
2008-05-31 12:36:38 +00:00
|
|
|
$conditions[] = $scope;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($created) {
|
2012-07-21 19:07:51 +00:00
|
|
|
$conditions['NOT'][$Model->escapeField()] = $Model->id;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-07-21 19:07:51 +00:00
|
|
|
$Model->updateAll(array($Model->escapeField($field) => $Model->escapeField($field) . ' ' . $dir . ' ' . $shift), $conditions);
|
2009-01-14 23:21:53 +00:00
|
|
|
$Model->recursive = $ModelRecursive;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-03-04 19:18:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|