2007-02-09 08:13:13 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* Tree behavior class.
|
|
|
|
*
|
|
|
|
* Enables a model object to act as a node-based tree.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright (c) 2006, Cake Software Foundation, Inc.
|
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
|
|
|
* @package cake
|
2007-02-09 18:18:01 +00:00
|
|
|
* @subpackage cake.cake.libs.model.behaviors
|
|
|
|
* @since CakePHP v 1.2.0.4487
|
2007-02-09 08:13:13 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
2007-02-09 18:18:01 +00:00
|
|
|
/**
|
|
|
|
* Short description for file
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model.behaviors
|
|
|
|
*/
|
2007-02-09 08:13:13 +00:00
|
|
|
class TreeBehavior extends ModelBehavior {
|
|
|
|
|
|
|
|
function setup(&$model, $config = array()) {
|
2007-12-08 06:08:03 +00:00
|
|
|
$settings = array_merge(array(
|
2007-10-27 20:32:19 +00:00
|
|
|
'parent' => 'parent_id',
|
|
|
|
'left' => 'lft',
|
|
|
|
'right' => 'rght',
|
|
|
|
'scope' => '1 = 1',
|
|
|
|
'enabled' => true,
|
|
|
|
'type' => 'nested',
|
|
|
|
'__parentChange' => false
|
2007-12-08 06:08:03 +00:00
|
|
|
), (array)$config);
|
2007-02-09 08:13:13 +00:00
|
|
|
|
|
|
|
/*if (in_array($settings['scope'], $model->getAssociated('belongsTo'))) {
|
|
|
|
$data = $model->getAssociated($settings['scope']);
|
|
|
|
$parent =& $model->{$data['className']};
|
|
|
|
$settings['scope'] = $model->escapeField($data['foreignKey']) . ' = ' . $parent->escapeField($parent->primaryKey, $settings['scope']);
|
2007-10-27 20:32:19 +00:00
|
|
|
}*/
|
2007-10-27 01:32:17 +00:00
|
|
|
$this->settings[$model->alias] = $settings;
|
2007-02-09 08:13:13 +00:00
|
|
|
}
|
2007-12-25 09:00:05 +00:00
|
|
|
/**
|
|
|
|
* Change the Tree behavior on the fly
|
|
|
|
*
|
|
|
|
* @param object $model
|
|
|
|
* @param mixed $scope
|
|
|
|
*/
|
|
|
|
function setScope(&$model, $scope) {
|
|
|
|
$this->settings[$model->name]['scope'] = $scope;
|
|
|
|
}
|
2007-02-09 08:13:13 +00:00
|
|
|
/**
|
2007-06-22 05:01:39 +00:00
|
|
|
* After save method. Called after all saves
|
|
|
|
*
|
|
|
|
* Overriden to transparently manage setting the lft and rght fields if and only if the parent field is included in the
|
|
|
|
* parameters to be saved.
|
2007-02-09 08:13:13 +00:00
|
|
|
*
|
2007-06-22 05:01:39 +00:00
|
|
|
* @param AppModel $model
|
2007-10-22 16:54:36 +00:00
|
|
|
* @param boolean $created indicates whether the node just saved was created or updated
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean true on success, false on failure
|
2007-02-09 08:13:13 +00:00
|
|
|
*/
|
2007-06-22 05:01:39 +00:00
|
|
|
function afterSave(&$model, $created) {
|
2007-10-27 01:32:17 +00:00
|
|
|
extract($this->settings[$model->alias]);
|
2007-08-11 15:04:23 +00:00
|
|
|
if (!$enabled) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-06-22 05:01:39 +00:00
|
|
|
if ($created) {
|
2007-10-27 01:32:17 +00:00
|
|
|
if ((isset($model->data[$model->alias][$parent])) && $model->data[$model->alias][$parent]) {
|
|
|
|
return $this->_setParent($model, $model->data[$model->alias][$parent]);
|
2007-02-09 08:13:13 +00:00
|
|
|
}
|
2007-10-21 14:18:46 +00:00
|
|
|
} elseif ($__parentChange) {
|
2007-10-27 01:32:17 +00:00
|
|
|
$this->settings[$model->alias]['__parentChange'] = false;
|
|
|
|
return $this->_setParent($model, $model->data[$model->alias][$parent]);
|
2007-02-09 08:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-22 05:01:39 +00:00
|
|
|
/**
|
|
|
|
* Before delete method. Called before all deletes
|
|
|
|
*
|
|
|
|
* Will delete the current node and all children using the deleteAll method and sync the table
|
|
|
|
*
|
|
|
|
* @param AppModel $model
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean true to continue, false to abort the delete
|
2007-06-22 05:01:39 +00:00
|
|
|
*/
|
|
|
|
function beforeDelete(&$model) {
|
2007-10-27 01:32:17 +00:00
|
|
|
extract($this->settings[$model->alias]);
|
2007-10-17 18:10:23 +00:00
|
|
|
|
2007-08-11 15:04:23 +00:00
|
|
|
if (!$enabled) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-12-08 08:09:46 +00:00
|
|
|
list($name, $data) = array($model->alias, $model->read());
|
|
|
|
$data = $data[$name];
|
2007-02-09 08:13:13 +00:00
|
|
|
|
2007-06-22 05:01:39 +00:00
|
|
|
if (!$data[$right] || !$data[$left]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$diff = $data[$right] - $data[$left] + 1;
|
2007-10-17 18:10:23 +00:00
|
|
|
|
2007-08-11 15:04:23 +00:00
|
|
|
if ($diff > 2) {
|
2008-01-01 01:18:32 +00:00
|
|
|
$constraint = $scope . ' AND ' . $model->escapeField($left) . ' BETWEEN ' . ($data[$left] + 1) . ' AND ' . ($data[$right] - 1);
|
2007-08-11 15:04:23 +00:00
|
|
|
$model->deleteAll($constraint);
|
|
|
|
}
|
|
|
|
$this->__sync($model, $diff, '-', '> ' . $data[$right]);
|
2007-09-24 23:49:54 +00:00
|
|
|
return true;
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Before save method. Called before all saves
|
|
|
|
*
|
|
|
|
* Overriden to transparently manage setting the lft and rght fields if and only if the parent field is included in the
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @since 1.2
|
|
|
|
* @param AppModel $model
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean true to continue, false to abort the save
|
2007-06-22 05:01:39 +00:00
|
|
|
*/
|
|
|
|
function beforeSave(&$model) {
|
2007-10-27 01:32:17 +00:00
|
|
|
extract($this->settings[$model->alias]);
|
2007-10-17 18:10:23 +00:00
|
|
|
|
2007-08-11 15:04:23 +00:00
|
|
|
if (!$enabled) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-10-27 01:32:17 +00:00
|
|
|
if (isset($model->data[$model->alias][$model->primaryKey])) {
|
|
|
|
if ($model->data[$model->alias][$model->primaryKey]) {
|
2007-06-22 05:01:39 +00:00
|
|
|
if (!$model->id) {
|
2007-10-27 01:32:17 +00:00
|
|
|
$model->id = $model->data[$model->alias][$model->primaryKey];
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
2007-02-09 08:13:13 +00:00
|
|
|
}
|
2007-12-08 08:09:46 +00:00
|
|
|
unset($model->data[$model->alias][$model->primaryKey]);
|
2007-02-09 08:13:13 +00:00
|
|
|
}
|
|
|
|
|
2007-06-22 05:01:39 +00:00
|
|
|
if (!$model->id) {
|
2007-10-27 01:32:17 +00:00
|
|
|
if ((!isset($model->data[$model->alias][$parent])) || (!$model->data[$model->alias][$parent])) {
|
2007-06-22 05:01:39 +00:00
|
|
|
$edge = $this->__getMax($model, $scope, $right);
|
2007-12-08 08:09:46 +00:00
|
|
|
$model->data[$model->alias][$left] = $edge + 1;
|
|
|
|
$model->data[$model->alias][$right] = $edge + 2;
|
|
|
|
$this->_addToWhitelist($model, array($left, $right));
|
2007-06-22 05:01:39 +00:00
|
|
|
} else {
|
2007-12-08 08:09:46 +00:00
|
|
|
$parentNode = $model->find('first', array(
|
|
|
|
'conditions' => array($scope, $model->escapeField() => $model->data[$model->alias][$parent]),
|
|
|
|
'fields' => array($model->primaryKey), 'recursive' => -1
|
|
|
|
));
|
2007-10-17 18:10:23 +00:00
|
|
|
|
2007-06-22 05:01:39 +00:00
|
|
|
if (!$parentNode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-10-27 01:32:17 +00:00
|
|
|
} elseif (isset($model->data[$model->alias][$parent])) {
|
|
|
|
if ($model->data[$model->alias][$parent] != $model->field($parent)) {
|
|
|
|
$this->settings[$model->alias]['__parentChange'] = true;
|
2007-10-21 14:18:46 +00:00
|
|
|
}
|
2007-10-27 01:32:17 +00:00
|
|
|
if (!$model->data[$model->alias][$parent]) {
|
2007-12-08 08:09:46 +00:00
|
|
|
$model->data[$model->alias][$parent] = null;
|
|
|
|
$this->_addToWhitelist($model, $parent);
|
2007-06-22 05:01:39 +00:00
|
|
|
} else {
|
2007-12-08 08:09:46 +00:00
|
|
|
list($node) = array_values($model->find('first', array(
|
|
|
|
'conditions' => array($scope,$model->escapeField() => $model->id),
|
|
|
|
'fields' => array($model->primaryKey, $parent, $left, $right ), 'recursive' => -1)
|
|
|
|
));
|
2007-06-22 05:01:39 +00:00
|
|
|
|
2007-12-08 08:09:46 +00:00
|
|
|
$parentNode = $model->find('first', array(
|
|
|
|
'conditions' => array($scope, $model->escapeField() => $model->data[$model->alias][$parent]),
|
|
|
|
'fields' => array($model->primaryKey, $left, $right), 'recursive' => -1
|
|
|
|
));
|
2007-06-22 05:01:39 +00:00
|
|
|
if (!$parentNode) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
list($parentNode) = array_values($parentNode);
|
|
|
|
if (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) {
|
|
|
|
return false;
|
2007-08-11 15:04:23 +00:00
|
|
|
} elseif ($node[$model->primaryKey] == $parentNode[$model->primaryKey]) {
|
2007-06-22 05:01:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param AppModel $model
|
|
|
|
* @param mixed $id The ID of the record to read or false to read all top level nodes
|
2007-10-22 16:54:36 +00:00
|
|
|
* @param boolean $direct whether to count direct, or all, children
|
|
|
|
* @return integer number of child nodes
|
2007-06-22 05:01:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function childcount(&$model, $id = null, $direct = false) {
|
|
|
|
if ($id === null && $model->id) {
|
|
|
|
$id = $model->id;
|
|
|
|
} elseif (!$id) {
|
|
|
|
$id = null;
|
|
|
|
}
|
2007-10-27 01:32:17 +00:00
|
|
|
extract($this->settings[$model->alias]);
|
2007-02-09 08:13:13 +00:00
|
|
|
|
2007-06-22 05:01:39 +00:00
|
|
|
if ($direct) {
|
2007-12-02 23:50:40 +00:00
|
|
|
return $model->find('count', array('conditions' => array($scope, $model->escapeField($parent) => $id)));
|
2007-06-22 05:01:39 +00:00
|
|
|
} else {
|
|
|
|
if ($id === null) {
|
2007-12-02 23:50:40 +00:00
|
|
|
return $model->find('count', array('conditions' => $scope));
|
2007-12-08 08:09:46 +00:00
|
|
|
} elseif (!empty ($model->data)) {
|
2007-10-27 01:32:17 +00:00
|
|
|
$data = $model->data[$model->alias];
|
2007-06-22 05:01:39 +00:00
|
|
|
} else {
|
2007-12-08 08:09:46 +00:00
|
|
|
list($data) = array_values($model->find('first', array('conditions' => array($scope, $model->escapeField() => $id), 'recursive' => -1)));
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
|
|
|
return ($data[$right] - $data[$left] - 1) / 2;
|
|
|
|
}
|
2007-02-09 08:13:13 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get the child nodes of the current model
|
|
|
|
*
|
2007-06-22 05:01:39 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param AppModel $model
|
|
|
|
* @param mixed $id The ID of the record to read
|
2007-10-22 16:54:36 +00:00
|
|
|
* @param boolean $direct whether to return only the direct, or all, children
|
2007-06-22 05:01:39 +00:00
|
|
|
* @param mixed $fields Either a single string of a field name, or an array of field names
|
|
|
|
* @param string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC") defaults to the tree order
|
2007-10-22 16:11:12 +00:00
|
|
|
* @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
|
2007-06-22 05:01:39 +00:00
|
|
|
* @return array Array of child nodes
|
|
|
|
* @access public
|
2007-02-09 08:13:13 +00:00
|
|
|
*/
|
2007-06-22 05:01:39 +00:00
|
|
|
function children(&$model, $id = null, $direct = false, $fields = null, $order = null, $limit = null, $page = 1, $recursive = -1) {
|
|
|
|
if ($id === null && $model->id) {
|
|
|
|
$id = $model->id;
|
|
|
|
} elseif (!$id) {
|
|
|
|
$id = null;
|
|
|
|
}
|
2007-10-27 01:32:17 +00:00
|
|
|
$name = $model->alias;
|
2007-02-09 08:13:13 +00:00
|
|
|
extract($this->settings[$name]);
|
2007-06-22 05:01:39 +00:00
|
|
|
|
|
|
|
if (!$order) {
|
2007-10-27 01:32:17 +00:00
|
|
|
$order = $model->alias . '.' . $left . ' asc';
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
|
|
|
if ($direct) {
|
2007-12-08 08:09:46 +00:00
|
|
|
$conditions = array($scope, $model->escapeField($parent) => $id);
|
|
|
|
return $model->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));
|
2007-06-22 05:01:39 +00:00
|
|
|
} else {
|
|
|
|
if (!$id) {
|
|
|
|
$constraint = $scope;
|
|
|
|
} else {
|
2007-12-08 08:09:46 +00:00
|
|
|
@list($item) = array_values($model->find('first', array('conditions' => array($scope, $model->escapeField() => $id), 'fields' => array($left, $right), 'recursive' => -1)));
|
2007-10-17 18:10:23 +00:00
|
|
|
$constraint = array($scope, $model->escapeField($right) => '< ' . $item[$right], $model->escapeField($left) => '> ' . $item[$left]);
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
2007-12-02 23:50:40 +00:00
|
|
|
return $model->find('all', array('conditions' => $constraint, 'fields' => $fields, 'order' => $order, 'limit' => $limit, 'page' => $page, 'recursive' => $recursive));
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
2007-02-09 08:13:13 +00:00
|
|
|
}
|
|
|
|
/**
|
2007-10-19 21:31:09 +00:00
|
|
|
* A convenience method for returning a hierarchical array used for HTML select boxes
|
2007-06-22 05:01:39 +00:00
|
|
|
*
|
|
|
|
* @param AppModel $model
|
|
|
|
* @param mixed $conditions SQL conditions as a string or as an array('field' =>'value',...)
|
|
|
|
* @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"
|
2007-10-19 21:31:09 +00:00
|
|
|
* @param string $spacer The character or characters which will be repeated
|
2007-10-22 16:11:12 +00:00
|
|
|
* @param integer $recursive The number of levels deep to fetch associated records
|
2007-06-22 05:01:39 +00:00
|
|
|
* @return array An associative array of records, where the id is the key, and the display field is the value
|
|
|
|
* @access public
|
2007-02-09 08:13:13 +00:00
|
|
|
*/
|
2007-10-19 21:31:09 +00:00
|
|
|
function generatetreelist(&$model, $conditions = null, $keyPath = null, $valuePath = null, $spacer = '_', $recursive = -1) {
|
2007-10-27 01:32:17 +00:00
|
|
|
extract($this->settings[$model->alias]);
|
2007-08-11 15:04:23 +00:00
|
|
|
|
2007-10-19 21:31:09 +00:00
|
|
|
if ($keyPath == null && $valuePath == null && $model->hasField($model->displayField)) {
|
|
|
|
$fields = array($model->primaryKey, $model->displayField, $left, $right);
|
|
|
|
} else {
|
|
|
|
$fields = null;
|
|
|
|
}
|
2007-08-11 15:04:23 +00:00
|
|
|
|
2007-10-19 21:31:09 +00:00
|
|
|
if ($keyPath == null) {
|
2007-10-27 01:32:17 +00:00
|
|
|
$keyPath = '{n}.' . $model->alias . '.' . $model->primaryKey;
|
2007-10-19 21:31:09 +00:00
|
|
|
}
|
2007-06-22 05:01:39 +00:00
|
|
|
|
2007-10-19 21:31:09 +00:00
|
|
|
if ($valuePath == null) {
|
2007-10-27 01:32:17 +00:00
|
|
|
$valuePath = array('{0}{1}', '{n}.tree_prefix', '{n}.' . $model->alias . '.' . $model->displayField);
|
2007-10-19 21:31:09 +00:00
|
|
|
|
|
|
|
} elseif (is_string($valuePath)) {
|
|
|
|
$valuePath = array('{0}{1}', '{n}.tree_prefix', $valuePath);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$valuePath[0] = '{' . (count($valuePath) - 1) . '}' . $valuePath[0];
|
|
|
|
$valuePath[] = '{n}.tree_prefix';
|
|
|
|
}
|
2007-12-08 08:09:46 +00:00
|
|
|
$order = $left;
|
|
|
|
$results = $model->find('all', compact('conditions', 'fields', 'order', 'recursive'));
|
2007-10-19 21:31:09 +00:00
|
|
|
$stack = array();
|
|
|
|
|
|
|
|
foreach ($results as $i => $result) {
|
2007-12-08 08:09:46 +00:00
|
|
|
while ($stack && ($stack[count($stack) - 1] < $result[$model->alias][$right])) {
|
2007-10-19 21:31:09 +00:00
|
|
|
array_pop($stack);
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
2007-10-19 21:31:09 +00:00
|
|
|
$results[$i]['tree_prefix'] = str_repeat($spacer,count($stack));
|
2007-10-27 01:32:17 +00:00
|
|
|
$stack[] = $result[$model->alias][$right];
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
2007-12-29 16:20:30 +00:00
|
|
|
if (empty($results)) {
|
|
|
|
return array();
|
|
|
|
}
|
2007-10-19 21:31:09 +00:00
|
|
|
return Set::combine($results, $keyPath, $valuePath);
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get the parent node
|
|
|
|
*
|
|
|
|
* reads the parent id and returns this node
|
|
|
|
*
|
|
|
|
* @param AppModel $model
|
|
|
|
* @param mixed $id The ID of the record to read
|
2007-10-22 16:11:12 +00:00
|
|
|
* @param integer $recursive The number of levels deep to fetch associated records
|
2007-06-22 05:01:39 +00:00
|
|
|
* @return array Array of data for the parent node
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getparentnode(&$model, $id = null, $fields = null, $recursive = -1) {
|
|
|
|
if (empty ($id)) {
|
|
|
|
$id = $model->id;
|
|
|
|
}
|
2007-10-27 01:32:17 +00:00
|
|
|
extract($this->settings[$model->alias]);
|
2007-06-22 05:01:39 +00:00
|
|
|
$parentId = $model->read($parent, $id);
|
|
|
|
|
|
|
|
if ($parentId) {
|
2007-10-27 01:32:17 +00:00
|
|
|
$parentId = $parentId[$model->alias][$parent];
|
2007-12-02 23:50:40 +00:00
|
|
|
$parent = $model->find('first', array('conditions' => array($model->escapeField() => $parentId), 'fields' => $fields, 'recursive' => $recursive));
|
2007-06-23 04:16:33 +00:00
|
|
|
|
2007-06-22 05:01:39 +00:00
|
|
|
return $parent;
|
2007-02-09 08:13:13 +00:00
|
|
|
} else {
|
2007-06-22 05:01:39 +00:00
|
|
|
return false;
|
2007-02-09 08:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
2007-06-22 05:01:39 +00:00
|
|
|
* Get the path to the given node
|
2007-02-09 08:13:13 +00:00
|
|
|
*
|
2007-06-22 05:01:39 +00:00
|
|
|
* @param AppModel $model
|
|
|
|
* @param mixed $id The ID of the record to read
|
|
|
|
* @param mixed $fields Either a single string of a field name, or an array of field names
|
2007-10-22 16:11:12 +00:00
|
|
|
* @param integer $recursive The number of levels deep to fetch associated records
|
2007-06-22 05:01:39 +00:00
|
|
|
* @return array Array of nodes from top most parent to current node
|
|
|
|
* @access public
|
2007-02-09 08:13:13 +00:00
|
|
|
*/
|
2007-06-22 05:01:39 +00:00
|
|
|
function getpath(&$model, $id = null, $fields = null, $recursive = -1) {
|
|
|
|
if (empty ($id)) {
|
2007-02-09 08:13:13 +00:00
|
|
|
$id = $model->id;
|
|
|
|
}
|
2007-10-27 01:32:17 +00:00
|
|
|
extract($this->settings[$model->alias]);
|
2007-12-02 23:50:40 +00:00
|
|
|
@list($item) = array_values($model->find('first', array('conditions' => array($model->escapeField() => $id), 'fields' => array($left, $right), 'recursive' => -1)));
|
2007-06-22 05:01:39 +00:00
|
|
|
|
|
|
|
if (empty ($item)) {
|
2007-02-09 08:13:13 +00:00
|
|
|
return null;
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
|
|
|
|
2007-12-25 10:37:08 +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
|
|
|
|
));
|
2007-06-22 05:01:39 +00:00
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @param AppModel $model
|
|
|
|
* @param mixed $id The ID of the record to move
|
2007-08-11 15:04:23 +00:00
|
|
|
* @param mixed $number how many places to move the node or true to move to last position
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean true on success, false on failure
|
2007-06-22 05:01:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2007-08-11 15:04:23 +00:00
|
|
|
function movedown(&$model, $id = null, $number = 1) {
|
2007-06-22 05:01:39 +00:00
|
|
|
if (empty ($id)) {
|
|
|
|
$id = $model->id;
|
|
|
|
}
|
2007-10-27 01:32:17 +00:00
|
|
|
extract($this->settings[$model->alias]);
|
2007-12-25 10:37:08 +00:00
|
|
|
list($node) = array_values($model->find('first', array(
|
|
|
|
'conditions' => array($scope, $model->escapeField() => $id),
|
|
|
|
'fields' => array($model->primaryKey, $left, $right, $parent), 'recursive' => -1
|
|
|
|
)));
|
2007-06-22 05:01:39 +00:00
|
|
|
if ($node[$parent]) {
|
2007-12-25 10:37:08 +00:00
|
|
|
list($parentNode) = array_values($model->find('first', array(
|
|
|
|
'conditions' => array($scope, $model->escapeField() => $node[$parent]),
|
|
|
|
'fields' => array($model->primaryKey, $left, $right), 'recursive' => -1
|
|
|
|
)));
|
2007-06-22 05:01:39 +00:00
|
|
|
if (($node[$right] + 1) == $parentNode[$right]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-12-02 23:50:40 +00:00
|
|
|
$nextNode = $model->find('first', array('conditions' => array($scope, $model->escapeField($left) => ($node[$right] + 1)),
|
|
|
|
'fields' => array($model->primaryKey, $left, $right), 'recursive' => -1));
|
2007-06-22 05:01:39 +00:00
|
|
|
if ($nextNode) {
|
|
|
|
list($nextNode)= array_values($nextNode);
|
2007-02-09 08:13:13 +00:00
|
|
|
} else {
|
2007-06-22 05:01:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$edge = $this->__getMax($model, $scope, $right);
|
2007-08-11 15:04:23 +00:00
|
|
|
$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);
|
|
|
|
if (is_int($number)) {
|
|
|
|
$number--;
|
2007-02-09 08:13:13 +00:00
|
|
|
}
|
2007-08-11 15:04:23 +00:00
|
|
|
if ($number) {
|
|
|
|
$this->moveDown($model, $id, $number);
|
|
|
|
}
|
|
|
|
return true;
|
2007-02-09 08:13:13 +00:00
|
|
|
}
|
|
|
|
/**
|
2007-06-22 05:01:39 +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
|
2007-02-09 08:13:13 +00:00
|
|
|
*
|
2007-06-22 05:01:39 +00:00
|
|
|
* @param AppModel $model
|
|
|
|
* @param mixed $id The ID of the record to move
|
2007-08-11 15:04:23 +00:00
|
|
|
* @param mixed $number how many places to move the node, or true to move to first position
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean true on success, false on failure
|
2007-06-22 05:01:39 +00:00
|
|
|
* @access public
|
2007-02-09 08:13:13 +00:00
|
|
|
*/
|
2007-08-11 15:04:23 +00:00
|
|
|
function moveup(&$model, $id = null, $number = 1) {
|
2007-06-22 05:01:39 +00:00
|
|
|
if (empty ($id)) {
|
|
|
|
$id = $model->id;
|
|
|
|
}
|
2007-10-27 01:32:17 +00:00
|
|
|
extract($this->settings[$model->alias]);
|
2007-12-25 10:37:08 +00:00
|
|
|
list($node) = array_values($model->find('first', array(
|
|
|
|
'conditions' => array($scope, $model->escapeField() => $id),
|
|
|
|
'fields' => array($model->primaryKey, $left, $right, $parent ), 'recursive' => -1
|
|
|
|
)));
|
2007-06-22 05:01:39 +00:00
|
|
|
if ($node[$parent]) {
|
2007-12-25 10:37:08 +00:00
|
|
|
list($parentNode) = array_values($model->find('first', array(
|
|
|
|
'conditions' => array($scope, $model->escapeField() => $node[$parent]),
|
|
|
|
'fields' => array($model->primaryKey, $left, $right), 'recursive' => -1
|
|
|
|
)));
|
2007-06-22 05:01:39 +00:00
|
|
|
if (($node[$left] - 1) == $parentNode[$left]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-12-02 23:50:40 +00:00
|
|
|
$previousNode = $model->find('first', array('conditions' => array($scope, $model->escapeField($right) => ($node[$left] - 1)),
|
|
|
|
'fields' => array($model->primaryKey, $left, $right), 'recursive' => -1));
|
2007-06-22 05:01:39 +00:00
|
|
|
if ($previousNode) {
|
|
|
|
list($previousNode) = array_values($previousNode);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$edge = $this->__getMax($model, $scope, $right);
|
2007-08-11 15:04:23 +00:00
|
|
|
$this->__sync($model, $edge - $previousNode[$left] +1, '+', 'BETWEEN ' . $previousNode[$left] . ' AND ' . $previousNode[$right]);
|
|
|
|
$this->__sync($model, $node[$left] - $previousNode[$left], '-', 'BETWEEN ' .$node[$left] . ' AND ' . $node[$right]);
|
|
|
|
$this->__sync($model, $edge - $previousNode[$left] - ($node[$right] - $node[$left]), '-', '> ' . $edge);
|
|
|
|
if (is_int($number)) {
|
|
|
|
$number--;
|
|
|
|
}
|
|
|
|
if ($number) {
|
|
|
|
$this->moveUp($model, $id, $number);
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
2007-08-11 15:04:23 +00:00
|
|
|
return true;
|
2007-06-22 05:01:39 +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.
|
|
|
|
*
|
|
|
|
* @todo Could be written to be faster, *maybe*. Ideally using a subquery and putting all the logic burden on the DB.
|
|
|
|
* @param AppModel $model
|
|
|
|
* @param string $mode parent or tree
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean true on success, false on failure
|
2007-06-22 05:01:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function recover(&$model, $mode = 'parent') {
|
2007-10-27 01:32:17 +00:00
|
|
|
extract($this->settings[$model->alias]);
|
2007-02-09 08:13:13 +00:00
|
|
|
$model->recursive = -1;
|
2007-06-22 05:01:39 +00:00
|
|
|
if ($mode == 'parent') {
|
|
|
|
$count = 1;
|
2007-12-02 23:50:40 +00:00
|
|
|
foreach ($model->find('all', array('conditions' => $scope, 'fields' => array($model->primaryKey), 'order' => $left)) as $array) {
|
2007-10-27 01:32:17 +00:00
|
|
|
$model->{$model->primaryKey} = $array[$model->alias][$model->primaryKey];
|
2007-06-22 05:01:39 +00:00
|
|
|
$lft = $count++;
|
|
|
|
$rght = $count++;
|
|
|
|
$model->save(array($left => $lft,$right => $rght));
|
|
|
|
}
|
2007-12-02 23:50:40 +00:00
|
|
|
foreach ($model->find('all', array('conditions' => $scope, 'fields' => array($model->primaryKey, $parent), 'order' => $left)) as $array) {
|
2007-06-22 05:01:39 +00:00
|
|
|
$model->create();
|
2007-10-27 01:32:17 +00:00
|
|
|
$model->id = $array[$model->alias][$model->primaryKey];
|
|
|
|
$this->_setParent($model, $array[$model->alias][$parent], true);
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
2007-12-02 23:50:40 +00:00
|
|
|
foreach ($model->find('all', array('conditions' => $scope, 'fields' => array($model->primaryKey, $parent), 'order' => $left)) as $array) {
|
2007-10-27 01:32:17 +00:00
|
|
|
$path = $this->getpath($model, $array[$model->alias][$model->primaryKey]);
|
2007-06-22 05:01:39 +00:00
|
|
|
if ($path == null || count($path) < 2) {
|
|
|
|
$parentId = null;
|
|
|
|
} else {
|
2007-10-27 01:32:17 +00:00
|
|
|
$parentId = $path[count($path) - 2][$model->alias][$model->primaryKey];
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
2007-12-25 10:37:08 +00:00
|
|
|
$model->updateAll(array($parent => $parentId), array($model->escapeField() => $array[$model->alias][$model->primaryKey]));
|
2007-06-22 05:01:39 +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.
|
|
|
|
*
|
|
|
|
* @param AppModel $model
|
|
|
|
* @param mixed $id The ID of the record to remove
|
2007-10-22 16:54:36 +00:00
|
|
|
* @param boolean $delete whether to delete the node after reparenting children (if any)
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean true on success, false on failure
|
2007-06-22 05:01:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2007-08-11 15:04:23 +00:00
|
|
|
function removefromtree(&$model, $id = null, $delete = false) {
|
2007-06-22 05:01:39 +00:00
|
|
|
if (empty ($id)) {
|
|
|
|
$id = $model->id;
|
|
|
|
}
|
2007-10-27 01:32:17 +00:00
|
|
|
extract($this->settings[$model->alias]);
|
2007-12-02 23:50:40 +00:00
|
|
|
list($node) = array_values($model->find('first', array('conditions' => array($scope, $model->escapeField() => $id),
|
|
|
|
'fields' => array($model->primaryKey, $left, $right, $parent), 'recursive' => -1))
|
2007-08-11 15:04:23 +00:00
|
|
|
);
|
2007-06-22 05:01:39 +00:00
|
|
|
if ($node[$right] == $node[$left] + 1) {
|
2007-08-11 15:04:23 +00:00
|
|
|
if ($delete) {
|
|
|
|
$model->delete();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2007-06-22 05:01:39 +00:00
|
|
|
} elseif ($node[$parent]) {
|
2007-12-02 23:50:40 +00:00
|
|
|
list($parentNode) = array_values($model->find('first', array('conditions' => array($scope, $model->escapeField() => $node[$parent]),
|
|
|
|
'fields' => array($model->primaryKey, $left, $right), 'recursive' => -1))
|
2007-08-11 15:04:23 +00:00
|
|
|
);
|
2007-06-22 05:01:39 +00:00
|
|
|
} else {
|
|
|
|
$parentNode[$right]= $node[$right] + 1;
|
|
|
|
}
|
|
|
|
$model->updateAll(array($parent => $node[$parent]), array($parent => $node[$model->primaryKey]));
|
2007-08-11 15:04:23 +00:00
|
|
|
$this->__sync($model, 1, '-', 'BETWEEN ' . ($node[$left] + 1) . ' AND ' . ($node[$right] - 1));
|
|
|
|
$this->__sync($model, 2, '-', '> ' . ($node[$right]));
|
2007-06-22 05:01:39 +00:00
|
|
|
$model->id = $id;
|
2007-08-11 15:04:23 +00:00
|
|
|
|
2007-06-22 05:01:39 +00:00
|
|
|
if ($delete) {
|
2007-12-25 10:37:08 +00:00
|
|
|
$model->updateAll(
|
|
|
|
array($model->escapeField($left) => null, $model->escapeField($right) => null, $model->escapeField($parent) => null),
|
|
|
|
array($model->escapeField() => $id)
|
|
|
|
);
|
2007-06-22 05:01:39 +00:00
|
|
|
return $model->delete($id);
|
|
|
|
} else {
|
|
|
|
$edge = $this->__getMax($model, $scope, $right);
|
|
|
|
if ($node[$right] == $edge) {
|
|
|
|
$edge = $edge - 2;
|
|
|
|
}
|
|
|
|
$model->id = $id;
|
|
|
|
return $model->save(array($left => $edge + 1, $right => $edge + 2, $parent => null));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Backward compatible method
|
|
|
|
*
|
|
|
|
* Returns true if the change is successful.
|
|
|
|
*
|
|
|
|
* @param AppModel $model
|
|
|
|
* @param mixed $parentId The ID to set as the parent of the current node.
|
|
|
|
* @return true on success
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function setparent(&$model, $parentId = null , $created = null) {
|
2007-10-27 01:32:17 +00:00
|
|
|
extract($this->settings[$model->alias]);
|
2007-10-20 05:55:37 +00:00
|
|
|
if ($created === false && $parentId == $model->field($parent)) {
|
2007-06-22 05:01:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return $model->saveField($parent, $parentId);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Check if the current tree is valid.
|
|
|
|
*
|
|
|
|
* Returns true if the tree is valid otherwise an array of (type, incorrect left/right index, message)
|
|
|
|
*
|
|
|
|
* @param AppModel $model
|
|
|
|
* @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)
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function verify(&$model) {
|
2007-10-27 01:32:17 +00:00
|
|
|
extract($this->settings[$model->alias]);
|
2007-12-02 23:50:40 +00:00
|
|
|
if (!$model->find('count', array('conditions' => $scope))) {
|
2007-06-22 05:01:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$min = $this->__getMin($model, $scope, $left);
|
|
|
|
$edge = $this->__getMax($model, $scope, $right);
|
|
|
|
$errors = array();
|
2007-02-09 08:13:13 +00:00
|
|
|
|
2007-06-22 05:01:39 +00:00
|
|
|
for ($i = $min; $i <= $edge; $i++) {
|
2007-12-02 23:50:40 +00:00
|
|
|
$count = $model->find('count', array('conditions' => array($scope, 'OR' => array($model->escapeField($left) => $i, $model->escapeField($right) => $i))));
|
2007-06-22 05:01:39 +00:00
|
|
|
if ($count != 1) {
|
|
|
|
if ($count == 0) {
|
|
|
|
$errors[] = array('index', $i, 'missing');
|
|
|
|
} else {
|
|
|
|
$errors[] = array('index', $i, 'duplicate');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-02 23:50:40 +00:00
|
|
|
$count = $model->find('count', array('conditions' => array($scope, $model->escapeField($right) => '< ' . $model->escapeField($left))));
|
2007-06-22 05:01:39 +00:00
|
|
|
if ($count != 0) {
|
2007-12-02 23:50:40 +00:00
|
|
|
$node = $model->find('first', array('conditions' => array($scope, $model->escapeField($right) => '< ' . $model->escapeField($left))));
|
2007-06-22 05:01:39 +00:00
|
|
|
$errors[] = array('node', $node[$model->primaryKey], 'left greater than right.');
|
2007-02-09 08:13:13 +00:00
|
|
|
}
|
|
|
|
|
2007-08-11 15:04:23 +00:00
|
|
|
$model->bindModel(array('belongsTo' => array('VerifyParent' => array(
|
2007-10-27 01:32:17 +00:00
|
|
|
'className' => $model->alias,
|
2007-08-11 15:04:23 +00:00
|
|
|
'foreignKey' => $parent,
|
|
|
|
'fields' => array($model->primaryKey, $left, $right, $parent)
|
|
|
|
))));
|
2007-12-08 08:09:46 +00:00
|
|
|
|
2007-12-02 23:50:40 +00:00
|
|
|
foreach ($model->find('all', array('conditions' => $scope, 'recursive' => 1)) as $instance) {
|
2007-10-27 01:32:17 +00:00
|
|
|
if ($instance[$model->alias][$parent]) {
|
2007-06-22 05:01:39 +00:00
|
|
|
if (!$instance['VerifyParent'][$model->primaryKey]) {
|
2007-10-27 01:32:17 +00:00
|
|
|
$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],
|
2007-08-11 15:04:23 +00:00
|
|
|
'left less than parent (node ' . $instance['VerifyParent'][$model->primaryKey] . ').');
|
2007-10-27 01:32:17 +00:00
|
|
|
} elseif ($instance[$model->alias][$right] > $instance['VerifyParent'][$right]) {
|
|
|
|
$errors[] = array('node', $instance[$model->alias][$model->primaryKey],
|
2007-08-11 15:04:23 +00:00
|
|
|
'right greater than parent (node ' . $instance['VerifyParent'][$model->primaryKey] . ').');
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
2007-12-02 23:50:40 +00:00
|
|
|
} elseif ($model->find('count', array('conditions' => array($scope, $left . '< ' . $instance[$model->alias][$left], $right . '> ' . $instance[$model->alias][$right])))) {
|
2007-10-27 01:32:17 +00:00
|
|
|
$errors[] = array('node', $instance[$model->alias][$model->primaryKey], 'The parent field is blank, but has a parent');
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
2007-08-11 15:04:23 +00:00
|
|
|
|
2007-06-22 05:01:39 +00:00
|
|
|
if ($errors) {
|
|
|
|
return $errors;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2007-02-09 08:13:13 +00:00
|
|
|
}
|
2007-06-22 05:01:39 +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
|
|
|
|
*
|
|
|
|
* @param AppModel $model
|
|
|
|
* @param mixed $parentId
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean true on success, false on failure
|
2007-06-22 05:01:39 +00:00
|
|
|
* @access protected
|
|
|
|
*/
|
2007-10-21 14:18:46 +00:00
|
|
|
function _setParent(&$model, $parentId = null) {
|
2007-10-27 01:32:17 +00:00
|
|
|
extract($this->settings[$model->alias]);
|
2007-12-02 23:50:40 +00:00
|
|
|
list($node) = array_values($model->find('first', array('conditions' => array($scope, $model->escapeField() => $model->id),
|
|
|
|
'fields' => array($model->primaryKey, $parent, $left, $right), 'recursive' => -1)));
|
2007-06-22 05:01:39 +00:00
|
|
|
$edge = $this->__getMax($model, $scope, $right);
|
2007-02-09 08:13:13 +00:00
|
|
|
|
2007-06-22 05:01:39 +00:00
|
|
|
if (empty ($parentId)) {
|
2007-08-11 15:04:23 +00:00
|
|
|
$this->__sync($model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right]);
|
|
|
|
$this->__sync($model, $node[$right] - $node[$left] + 1, '-', '> ' . $node[$left]);
|
2007-06-22 05:01:39 +00:00
|
|
|
} else {
|
2007-12-02 23:50:40 +00:00
|
|
|
list($parentNode)= array_values($model->find('first', array('conditions' => array($scope, $model->escapeField() => $parentId),
|
|
|
|
'fields' => array($model->primaryKey, $left, $right), 'recursive' => -1)));
|
2007-10-17 18:10:23 +00:00
|
|
|
|
2007-06-22 05:01:39 +00:00
|
|
|
if (empty ($parentNode)) {
|
|
|
|
return false;
|
2007-10-20 05:55:37 +00:00
|
|
|
|
|
|
|
} elseif (($model->id == $parentId)) {
|
2007-06-22 05:01:39 +00:00
|
|
|
return false;
|
2007-10-20 05:55:37 +00:00
|
|
|
|
|
|
|
} elseif (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) {
|
2007-06-22 05:01:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-10-17 18:10:23 +00:00
|
|
|
|
2007-06-22 05:01:39 +00:00
|
|
|
if (empty ($node[$left]) && empty ($node[$right])) {
|
2007-08-11 15:04:23 +00:00
|
|
|
$this->__sync($model, 2, '+', '>= ' . $parentNode[$right]);
|
2007-06-22 05:01:39 +00:00
|
|
|
$model->save(array($left => $parentNode[$right], $right => $parentNode[$right] + 1, $parent => $parentId), false);
|
|
|
|
} else {
|
2007-08-11 15:04:23 +00:00
|
|
|
$this->__sync($model, $edge - $node[$left] +1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right]);
|
2007-06-22 05:01:39 +00:00
|
|
|
$diff = $node[$right] - $node[$left] + 1;
|
|
|
|
|
|
|
|
if ($node[$left] > $parentNode[$left]) {
|
|
|
|
if ($node[$right] < $parentNode[$right]) {
|
2007-08-11 15:04:23 +00:00
|
|
|
$this->__sync($model, $diff, '-', 'BETWEEN ' . $node[$right] . ' AND ' . ($parentNode[$right] - 1));
|
|
|
|
$this->__sync($model, $edge - $parentNode[$right] + $diff + 1, '-', '> ' . $edge);
|
2007-06-22 05:01:39 +00:00
|
|
|
} else {
|
2007-08-11 15:04:23 +00:00
|
|
|
$this->__sync($model, $diff, '+', 'BETWEEN ' . $parentNode[$right] . ' AND ' . $node[$right]);
|
|
|
|
$this->__sync($model, $edge - $parentNode[$right] + 1, '-', '> ' . $edge);
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
2007-08-11 15:04:23 +00:00
|
|
|
$this->__sync($model, $diff, '-', 'BETWEEN ' . $node[$right] . ' AND ' . ($parentNode[$right] - 1));
|
|
|
|
$this->__sync($model, $edge - $parentNode[$right] + $diff + 1, '-', '> ' . $edge);
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* get the maximum index value in the table.
|
|
|
|
*
|
|
|
|
* @param AppModel $model
|
|
|
|
* @param string $scope
|
|
|
|
* @param string $right
|
|
|
|
* @return int
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __getMax($model, $scope, $right) {
|
2007-12-02 23:50:40 +00:00
|
|
|
list($edge) = array_values($model->find('first', array('conditions' => $scope, 'fields' => 'MAX(' . $right . ') AS ' . $right, 'recursive' => -1)));
|
2007-06-22 05:01:39 +00:00
|
|
|
return ife(empty ($edge[$right]), 0, $edge[$right]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* get the minimum index value in the table.
|
|
|
|
*
|
|
|
|
* @param AppModel $model
|
|
|
|
* @param string $scope
|
|
|
|
* @param string $right
|
|
|
|
* @return int
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __getMin($model, $scope, $left) {
|
2007-12-02 23:50:40 +00:00
|
|
|
list($edge) = array_values($model->find('first', array('conditions' => $scope, 'fields' => 'MIN(' . $left . ') AS ' . $left, 'recursive' => -1)));
|
2007-08-11 15:04:23 +00:00
|
|
|
return ife(empty ($edge[$left]), 0, $edge[$left]);
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Table sync method.
|
|
|
|
*
|
|
|
|
* Handles table sync operations, Taking account of the behavior scope.
|
|
|
|
*
|
|
|
|
* @param AppModel $model
|
2007-10-22 16:11:12 +00:00
|
|
|
* @param integer $shift
|
2007-06-22 05:01:39 +00:00
|
|
|
* @param string $direction
|
|
|
|
* @param array $conditions
|
|
|
|
* @param string $field
|
2007-08-11 15:04:23 +00:00
|
|
|
* @access private
|
2007-06-22 05:01:39 +00:00
|
|
|
*/
|
2007-08-11 15:04:23 +00:00
|
|
|
function __sync(&$model, $shift, $dir = '+', $conditions = array(), $field = 'both') {
|
2007-10-27 01:32:17 +00:00
|
|
|
extract($this->settings[$model->alias]);
|
2007-06-22 05:01:39 +00:00
|
|
|
if ($field == 'both') {
|
2007-08-11 15:04:23 +00:00
|
|
|
$this->__sync($model, $shift, $dir, $conditions, $left);
|
|
|
|
$field = $right;
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
|
|
|
if (is_string($conditions)) {
|
2007-12-25 10:37:08 +00:00
|
|
|
$conditions = array($model->escapeField($field) => $conditions);
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
2007-08-11 15:04:23 +00:00
|
|
|
if ($scope != '1 = 1' && $scope) {
|
2007-06-22 05:01:39 +00:00
|
|
|
if (is_string($scope)) {
|
|
|
|
$conditions[]= $scope;
|
|
|
|
} else {
|
2007-12-08 06:08:03 +00:00
|
|
|
$conditions= array_merge($conditions, $scope);
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-25 10:37:08 +00:00
|
|
|
$model->updateAll(array($model->escapeField($field) => $model->escapeField($field) . ' ' . $dir . ' ' . $shift), $conditions);
|
2007-06-22 05:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
2007-10-28 04:18:18 +00:00
|
|
|
?>
|