5
* Enables a model object to act as a node-based tree.
9
* CakePHP : Rapid Development Framework (http://cakephp.org)
10
* Copyright 2006-2010, Cake Software Foundation, Inc.
12
* Licensed under The MIT License
13
* Redistributions of files must retain the above copyright notice.
15
* @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
16
* @link http://cakephp.org CakePHP Project
18
* @subpackage cake.cake.libs.model.behaviors
19
* @since CakePHP v 1.2.0.4487
20
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
26
* Enables a model object to act as a node-based tree. Using Modified Preorder Tree Traversal
28
* @see http://en.wikipedia.org/wiki/Tree_traversal
30
* @subpackage cake.cake.libs.model.behaviors
31
* @link http://book.cakephp.org/view/1339/Tree
33
class TreeBehavior extends ModelBehavior {
40
var $errors = array();
48
var $_defaults = array(
49
'parent' => 'parent_id', 'left' => 'lft', 'right' => 'rght',
50
'scope' => '1 = 1', 'type' => 'nested', '__parentChange' => false, 'recursive' => -1
54
* Initiate Tree behavior
56
* @param object $Model instance of model
57
* @param array $config array of configuration settings.
61
function setup(&$Model, $config = array()) {
62
if (!is_array($config)) {
63
$config = array('type' => $config);
65
$settings = array_merge($this->_defaults, $config);
67
if (in_array($settings['scope'], $Model->getAssociated('belongsTo'))) {
68
$data = $Model->getAssociated($settings['scope']);
69
$parent =& $Model->{$settings['scope']};
70
$settings['scope'] = $Model->alias . '.' . $data['foreignKey'] . ' = ' . $parent->alias . '.' . $parent->primaryKey;
71
$settings['recursive'] = 0;
73
$this->settings[$Model->alias] = $settings;
77
* After save method. Called after all saves
79
* Overriden to transparently manage setting the lft and rght fields if and only if the parent field is included in the
80
* parameters to be saved.
82
* @param AppModel $Model Model instance.
83
* @param boolean $created indicates whether the node just saved was created or updated
84
* @return boolean true on success, false on failure
87
function afterSave(&$Model, $created) {
88
extract($this->settings[$Model->alias]);
90
if ((isset($Model->data[$Model->alias][$parent])) && $Model->data[$Model->alias][$parent]) {
91
return $this->_setParent($Model, $Model->data[$Model->alias][$parent], $created);
93
} elseif ($__parentChange) {
94
$this->settings[$Model->alias]['__parentChange'] = false;
95
return $this->_setParent($Model, $Model->data[$Model->alias][$parent]);
100
* Before delete method. Called before all deletes
102
* Will delete the current node and all children using the deleteAll method and sync the table
104
* @param AppModel $Model Model instance
105
* @return boolean true to continue, false to abort the delete
108
function beforeDelete(&$Model) {
109
extract($this->settings[$Model->alias]);
110
list($name, $data) = array($Model->alias, $Model->read());
111
$data = $data[$name];
113
if (!$data[$right] || !$data[$left]) {
116
$diff = $data[$right] - $data[$left] + 1;
119
if (is_string($scope)) {
120
$scope = array($scope);
122
$scope[]["{$Model->alias}.{$left} BETWEEN ? AND ?"] = array($data[$left] + 1, $data[$right] - 1);
123
$Model->deleteAll($scope);
125
$this->__sync($Model, $diff, '-', '> ' . $data[$right]);
130
* Before save method. Called before all saves
132
* Overriden to transparently manage setting the lft and rght fields if and only if the parent field is included in the
133
* parameters to be saved. For newly created nodes with NO parent the left and right field values are set directly by
134
* this method bypassing the setParent logic.
137
* @param AppModel $Model Model instance
138
* @return boolean true to continue, false to abort the save
141
function beforeSave(&$Model) {
142
extract($this->settings[$Model->alias]);
144
$this->_addToWhitelist($Model, array($left, $right));
146
if (array_key_exists($parent, $Model->data[$Model->alias]) && $Model->data[$Model->alias][$parent]) {
147
$parentNode = $Model->find('first', array(
148
'conditions' => array($scope, $Model->escapeField() => $Model->data[$Model->alias][$parent]),
149
'fields' => array($Model->primaryKey, $right), 'recursive' => $recursive
154
list($parentNode) = array_values($parentNode);
155
$Model->data[$Model->alias][$left] = 0; //$parentNode[$right];
156
$Model->data[$Model->alias][$right] = 0; //$parentNode[$right] + 1;
158
$edge = $this->__getMax($Model, $scope, $right, $recursive);
159
$Model->data[$Model->alias][$left] = $edge + 1;
160
$Model->data[$Model->alias][$right] = $edge + 2;
162
} elseif (array_key_exists($parent, $Model->data[$Model->alias])) {
163
if ($Model->data[$Model->alias][$parent] != $Model->field($parent)) {
164
$this->settings[$Model->alias]['__parentChange'] = true;
166
if (!$Model->data[$Model->alias][$parent]) {
167
$Model->data[$Model->alias][$parent] = null;
168
$this->_addToWhitelist($Model, $parent);
170
$values = $Model->find('first', array(
171
'conditions' => array($scope,$Model->escapeField() => $Model->id),
172
'fields' => array($Model->primaryKey, $parent, $left, $right ), 'recursive' => $recursive)
175
if ($values === false) {
178
list($node) = array_values($values);
180
$parentNode = $Model->find('first', array(
181
'conditions' => array($scope, $Model->escapeField() => $Model->data[$Model->alias][$parent]),
182
'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive
187
list($parentNode) = array_values($parentNode);
189
if (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) {
191
} elseif ($node[$Model->primaryKey] == $parentNode[$Model->primaryKey]) {
200
* Get the number of child nodes
202
* If the direct parameter is set to true, only the direct children are counted (based upon the parent_id field)
203
* If false is passed for the id parameter, all top level nodes are counted, or all nodes are counted.
205
* @param AppModel $Model Model instance
206
* @param mixed $id The ID of the record to read or false to read all top level nodes
207
* @param boolean $direct whether to count direct, or all, children
208
* @return integer number of child nodes
210
* @link http://book.cakephp.org/view/1347/Counting-children
212
function childcount(&$Model, $id = null, $direct = false) {
214
extract (array_merge(array('id' => null), $id));
216
if ($id === null && $Model->id) {
221
extract($this->settings[$Model->alias]);
224
return $Model->find('count', array('conditions' => array($scope, $Model->escapeField($parent) => $id)));
228
return $Model->find('count', array('conditions' => $scope));
229
} elseif (isset($Model->data[$Model->alias][$left]) && isset($Model->data[$Model->alias][$right])) {
230
$data = $Model->data[$Model->alias];
232
$data = $Model->find('first', array('conditions' => array($scope, $Model->escapeField() => $id), 'recursive' => $recursive));
236
$data = $data[$Model->alias];
238
return ($data[$right] - $data[$left] - 1) / 2;
242
* Get the child nodes of the current model
244
* If the direct parameter is set to true, only the direct children are returned (based upon the parent_id field)
245
* If false is passed for the id parameter, top level, or all (depending on direct parameter appropriate) are counted.
247
* @param AppModel $Model Model instance
248
* @param mixed $id The ID of the record to read
249
* @param boolean $direct whether to return only the direct, or all, children
250
* @param mixed $fields Either a single string of a field name, or an array of field names
251
* @param string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC") defaults to the tree order
252
* @param integer $limit SQL LIMIT clause, for calculating items per page.
253
* @param integer $page Page number, for accessing paged data
254
* @param integer $recursive The number of levels deep to fetch associated records
255
* @return array Array of child nodes
257
* @link http://book.cakephp.org/view/1346/Children
259
function children(&$Model, $id = null, $direct = false, $fields = null, $order = null, $limit = null, $page = 1, $recursive = null) {
261
extract (array_merge(array('id' => null), $id));
263
$overrideRecursive = $recursive;
265
if ($id === null && $Model->id) {
270
$name = $Model->alias;
271
extract($this->settings[$Model->alias]);
273
if (!is_null($overrideRecursive)) {
274
$recursive = $overrideRecursive;
277
$order = $Model->alias . '.' . $left . ' asc';
280
$conditions = array($scope, $Model->escapeField($parent) => $id);
281
return $Model->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));
285
$conditions = $scope;
287
$result = array_values((array)$Model->find('first', array(
288
'conditions' => array($scope, $Model->escapeField() => $id),
289
'fields' => array($left, $right),
290
'recursive' => $recursive
293
if (empty($result) || !isset($result[0])) {
296
$conditions = array($scope,
297
$Model->escapeField($right) . ' <' => $result[0][$right],
298
$Model->escapeField($left) . ' >' => $result[0][$left]
301
return $Model->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));
305
* A convenience method for returning a hierarchical array used for HTML select boxes
307
* @param AppModel $Model Model instance
308
* @param mixed $conditions SQL conditions as a string or as an array('field' =>'value',...)
309
* @param string $keyPath A string path to the key, i.e. "{n}.Post.id"
310
* @param string $valuePath A string path to the value, i.e. "{n}.Post.title"
311
* @param string $spacer The character or characters which will be repeated
312
* @param integer $recursive The number of levels deep to fetch associated records
313
* @return array An associative array of records, where the id is the key, and the display field is the value
315
* @link http://book.cakephp.org/view/1348/generatetreelist
317
function generatetreelist(&$Model, $conditions = null, $keyPath = null, $valuePath = null, $spacer = '_', $recursive = null) {
318
$overrideRecursive = $recursive;
319
extract($this->settings[$Model->alias]);
320
if (!is_null($overrideRecursive)) {
321
$recursive = $overrideRecursive;
324
if ($keyPath == null && $valuePath == null && $Model->hasField($Model->displayField)) {
325
$fields = array($Model->primaryKey, $Model->displayField, $left, $right);
330
if ($keyPath == null) {
331
$keyPath = '{n}.' . $Model->alias . '.' . $Model->primaryKey;
334
if ($valuePath == null) {
335
$valuePath = array('{0}{1}', '{n}.tree_prefix', '{n}.' . $Model->alias . '.' . $Model->displayField);
337
} elseif (is_string($valuePath)) {
338
$valuePath = array('{0}{1}', '{n}.tree_prefix', $valuePath);
341
$valuePath[0] = '{' . (count($valuePath) - 1) . '}' . $valuePath[0];
342
$valuePath[] = '{n}.tree_prefix';
344
$order = $Model->alias . '.' . $left . ' asc';
345
$results = $Model->find('all', compact('conditions', 'fields', 'order', 'recursive'));
348
foreach ($results as $i => $result) {
349
while ($stack && ($stack[count($stack) - 1] < $result[$Model->alias][$right])) {
352
$results[$i]['tree_prefix'] = str_repeat($spacer,count($stack));
353
$stack[] = $result[$Model->alias][$right];
355
if (empty($results)) {
358
return Set::combine($results, $keyPath, $valuePath);
362
* Get the parent node
364
* reads the parent id and returns this node
366
* @param AppModel $Model Model instance
367
* @param mixed $id The ID of the record to read
368
* @param integer $recursive The number of levels deep to fetch associated records
369
* @return array Array of data for the parent node
371
* @link http://book.cakephp.org/view/1349/getparentnode
373
function getparentnode(&$Model, $id = null, $fields = null, $recursive = null) {
375
extract (array_merge(array('id' => null), $id));
377
$overrideRecursive = $recursive;
381
extract($this->settings[$Model->alias]);
382
if (!is_null($overrideRecursive)) {
383
$recursive = $overrideRecursive;
385
$parentId = $Model->find('first', array('conditions' => array($Model->primaryKey => $id), 'fields' => array($parent), 'recursive' => -1));
388
$parentId = $parentId[$Model->alias][$parent];
389
$parent = $Model->find('first', array('conditions' => array($Model->escapeField() => $parentId), 'fields' => $fields, 'recursive' => $recursive));
397
* Get the path to the given node
399
* @param AppModel $Model Model instance
400
* @param mixed $id The ID of the record to read
401
* @param mixed $fields Either a single string of a field name, or an array of field names
402
* @param integer $recursive The number of levels deep to fetch associated records
403
* @return array Array of nodes from top most parent to current node
405
* @link http://book.cakephp.org/view/1350/getpath
407
function getpath(&$Model, $id = null, $fields = null, $recursive = null) {
409
extract (array_merge(array('id' => null), $id));
411
$overrideRecursive = $recursive;
415
extract($this->settings[$Model->alias]);
416
if (!is_null($overrideRecursive)) {
417
$recursive = $overrideRecursive;
419
$result = $Model->find('first', array('conditions' => array($Model->escapeField() => $id), 'fields' => array($left, $right), 'recursive' => $recursive));
421
$result = array_values($result);
426
$results = $Model->find('all', array(
427
'conditions' => array($scope, $Model->escapeField($left) . ' <=' => $item[$left], $Model->escapeField($right) . ' >=' => $item[$right]),
428
'fields' => $fields, 'order' => array($Model->escapeField($left) => 'asc'), 'recursive' => $recursive
434
* Reorder the node without changing the parent.
436
* If the node is the last child, or is a top level node with no subsequent node this method will return false
438
* @param AppModel $Model Model instance
439
* @param mixed $id The ID of the record to move
440
* @param mixed $number how many places to move the node or true to move to last position
441
* @return boolean true on success, false on failure
443
* @link http://book.cakephp.org/view/1352/moveDown
445
function movedown(&$Model, $id = null, $number = 1) {
447
extract (array_merge(array('id' => null), $id));
455
extract($this->settings[$Model->alias]);
456
list($node) = array_values($Model->find('first', array(
457
'conditions' => array($scope, $Model->escapeField() => $id),
458
'fields' => array($Model->primaryKey, $left, $right, $parent), 'recursive' => $recursive
460
if ($node[$parent]) {
461
list($parentNode) = array_values($Model->find('first', array(
462
'conditions' => array($scope, $Model->escapeField() => $node[$parent]),
463
'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive
465
if (($node[$right] + 1) == $parentNode[$right]) {
469
$nextNode = $Model->find('first', array(
470
'conditions' => array($scope, $Model->escapeField($left) => ($node[$right] + 1)),
471
'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive)
474
list($nextNode) = array_values($nextNode);
478
$edge = $this->__getMax($Model, $scope, $right, $recursive);
479
$this->__sync($Model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right]);
480
$this->__sync($Model, $nextNode[$left] - $node[$left], '-', 'BETWEEN ' . $nextNode[$left] . ' AND ' . $nextNode[$right]);
481
$this->__sync($Model, $edge - $node[$left] - ($nextNode[$right] - $nextNode[$left]), '-', '> ' . $edge);
483
if (is_int($number)) {
487
$this->moveDown($Model, $id, $number);
493
* Reorder the node without changing the parent.
495
* If the node is the first child, or is a top level node with no previous node this method will return false
497
* @param AppModel $Model Model instance
498
* @param mixed $id The ID of the record to move
499
* @param mixed $number how many places to move the node, or true to move to first position
500
* @return boolean true on success, false on failure
502
* @link http://book.cakephp.org/view/1353/moveUp
504
function moveup(&$Model, $id = null, $number = 1) {
506
extract (array_merge(array('id' => null), $id));
514
extract($this->settings[$Model->alias]);
515
list($node) = array_values($Model->find('first', array(
516
'conditions' => array($scope, $Model->escapeField() => $id),
517
'fields' => array($Model->primaryKey, $left, $right, $parent ), 'recursive' => $recursive
519
if ($node[$parent]) {
520
list($parentNode) = array_values($Model->find('first', array(
521
'conditions' => array($scope, $Model->escapeField() => $node[$parent]),
522
'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive
524
if (($node[$left] - 1) == $parentNode[$left]) {
528
$previousNode = $Model->find('first', array(
529
'conditions' => array($scope, $Model->escapeField($right) => ($node[$left] - 1)),
530
'fields' => array($Model->primaryKey, $left, $right),
531
'recursive' => $recursive
535
list($previousNode) = array_values($previousNode);
539
$edge = $this->__getMax($Model, $scope, $right, $recursive);
540
$this->__sync($Model, $edge - $previousNode[$left] +1, '+', 'BETWEEN ' . $previousNode[$left] . ' AND ' . $previousNode[$right]);
541
$this->__sync($Model, $node[$left] - $previousNode[$left], '-', 'BETWEEN ' .$node[$left] . ' AND ' . $node[$right]);
542
$this->__sync($Model, $edge - $previousNode[$left] - ($node[$right] - $node[$left]), '-', '> ' . $edge);
543
if (is_int($number)) {
547
$this->moveUp($Model, $id, $number);
553
* Recover a corrupted tree
555
* The mode parameter is used to specify the source of info that is valid/correct. The opposite source of data
556
* will be populated based upon that source of info. E.g. if the MPTT fields are corrupt or empty, with the $mode
557
* 'parent' the values of the parent_id field will be used to populate the left and right fields. The missingParentAction
558
* parameter only applies to "parent" mode and determines what to do if the parent field contains an id that is not present.
560
* @todo Could be written to be faster, *maybe*. Ideally using a subquery and putting all the logic burden on the DB.
561
* @param AppModel $Model Model instance
562
* @param string $mode parent or tree
563
* @param mixed $missingParentAction 'return' to do nothing and return, 'delete' to
564
* delete, or the id of the parent to set as the parent_id
565
* @return boolean true on success, false on failure
567
* @link http://book.cakephp.org/view/1628/Recover
569
function recover(&$Model, $mode = 'parent', $missingParentAction = null) {
570
if (is_array($mode)) {
571
extract (array_merge(array('mode' => 'parent'), $mode));
573
extract($this->settings[$Model->alias]);
574
$Model->recursive = $recursive;
575
if ($mode == 'parent') {
576
$Model->bindModel(array('belongsTo' => array('VerifyParent' => array(
577
'className' => $Model->alias,
578
'foreignKey' => $parent,
579
'fields' => array($Model->primaryKey, $left, $right, $parent),
581
$missingParents = $Model->find('list', array(
583
'conditions' => array($scope, array(
584
'NOT' => array($Model->escapeField($parent) => null), $Model->VerifyParent->escapeField() => null
587
$Model->unbindModel(array('belongsTo' => array('VerifyParent')));
588
if ($missingParents) {
589
if ($missingParentAction == 'return') {
590
foreach ($missingParents as $id => $display) {
591
$this->errors[] = 'cannot find the parent for ' . $Model->alias . ' with id ' . $id . '(' . $display . ')';
595
} elseif ($missingParentAction == 'delete') {
596
$Model->deleteAll(array($Model->primaryKey => array_flip($missingParents)));
598
$Model->updateAll(array($parent => $missingParentAction), array($Model->escapeField($Model->primaryKey) => array_flip($missingParents)));
602
foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey), 'order' => $left)) as $array) {
603
$Model->id = $array[$Model->alias][$Model->primaryKey];
606
$Model->save(array($left => $lft, $right => $rght), array('callbacks' => false));
608
foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey, $parent), 'order' => $left)) as $array) {
610
$Model->id = $array[$Model->alias][$Model->primaryKey];
611
$this->_setParent($Model, $array[$Model->alias][$parent]);
614
$db =& ConnectionManager::getDataSource($Model->useDbConfig);
615
foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey, $parent), 'order' => $left)) as $array) {
616
$path = $this->getpath($Model, $array[$Model->alias][$Model->primaryKey]);
617
if ($path == null || count($path) < 2) {
620
$parentId = $path[count($path) - 2][$Model->alias][$Model->primaryKey];
622
$Model->updateAll(array($parent => $db->value($parentId, $parent)), array($Model->escapeField() => $array[$Model->alias][$Model->primaryKey]));
631
* Reorders the nodes (and child nodes) of the tree according to the field and direction specified in the parameters.
632
* This method does not change the parent of any node.
634
* Requires a valid tree, by default it verifies the tree before beginning.
638
* - 'id' id of record to use as top node for reordering
639
* - 'field' Which field to use in reordeing defaults to displayField
640
* - 'order' Direction to order either DESC or ASC (defaults to ASC)
641
* - 'verify' Whether or not to verify the tree before reorder. defaults to true.
643
* @param AppModel $Model Model instance
644
* @param array $options array of options to use in reordering.
645
* @return boolean true on success, false on failure
646
* @link http://book.cakephp.org/view/1355/reorder
647
* @link http://book.cakephp.org/view/1629/Reorder
649
function reorder(&$Model, $options = array()) {
650
$options = array_merge(array('id' => null, 'field' => $Model->displayField, 'order' => 'ASC', 'verify' => true), $options);
652
if ($verify && !$this->verify($Model)) {
656
extract($this->settings[$Model->alias]);
657
$fields = array($Model->primaryKey, $field, $left, $right);
658
$sort = $field . ' ' . $order;
659
$nodes = $this->children($Model, $id, true, $fields, $sort, null, null, $recursive);
661
$cacheQueries = $Model->cacheQueries;
662
$Model->cacheQueries = false;
664
foreach ($nodes as $node) {
665
$id = $node[$Model->alias][$Model->primaryKey];
666
$this->moveDown($Model, $id, true);
667
if ($node[$Model->alias][$left] != $node[$Model->alias][$right] - 1) {
668
$this->reorder($Model, compact('id', 'field', 'order', 'verify'));
672
$Model->cacheQueries = $cacheQueries;
677
* Remove the current node from the tree, and reparent all children up one level.
679
* If the parameter delete is false, the node will become a new top level node. Otherwise the node will be deleted
680
* after the children are reparented.
682
* @param AppModel $Model Model instance
683
* @param mixed $id The ID of the record to remove
684
* @param boolean $delete whether to delete the node after reparenting children (if any)
685
* @return boolean true on success, false on failure
687
* @link http://book.cakephp.org/view/1354/removeFromTree
689
function removefromtree(&$Model, $id = null, $delete = false) {
691
extract (array_merge(array('id' => null), $id));
693
extract($this->settings[$Model->alias]);
695
list($node) = array_values($Model->find('first', array(
696
'conditions' => array($scope, $Model->escapeField() => $id),
697
'fields' => array($Model->primaryKey, $left, $right, $parent),
698
'recursive' => $recursive
701
if ($node[$right] == $node[$left] + 1) {
703
return $Model->delete($id);
706
return $Model->saveField($parent, null);
708
} elseif ($node[$parent]) {
709
list($parentNode) = array_values($Model->find('first', array(
710
'conditions' => array($scope, $Model->escapeField() => $node[$parent]),
711
'fields' => array($Model->primaryKey, $left, $right),
712
'recursive' => $recursive
715
$parentNode[$right] = $node[$right] + 1;
718
$db =& ConnectionManager::getDataSource($Model->useDbConfig);
720
array($parent => $db->value($node[$parent], $parent)),
721
array($Model->escapeField($parent) => $node[$Model->primaryKey])
723
$this->__sync($Model, 1, '-', 'BETWEEN ' . ($node[$left] + 1) . ' AND ' . ($node[$right] - 1));
724
$this->__sync($Model, 2, '-', '> ' . ($node[$right]));
730
$Model->escapeField($left) => 0,
731
$Model->escapeField($right) => 0,
732
$Model->escapeField($parent) => null
734
array($Model->escapeField() => $id)
736
return $Model->delete($id);
738
$edge = $this->__getMax($Model, $scope, $right, $recursive);
739
if ($node[$right] == $edge) {
744
array($left => $edge + 1, $right => $edge + 2, $parent => null),
745
array('callbacks' => false)
751
* Check if the current tree is valid.
753
* Returns true if the tree is valid otherwise an array of (type, incorrect left/right index, message)
755
* @param AppModel $Model Model instance
756
* @return mixed true if the tree is valid or empty, otherwise an array of (error type [index, node],
757
* [incorrect left/right index,node id], message)
759
* @link http://book.cakephp.org/view/1630/Verify
761
function verify(&$Model) {
762
extract($this->settings[$Model->alias]);
763
if (!$Model->find('count', array('conditions' => $scope))) {
766
$min = $this->__getMin($Model, $scope, $left, $recursive);
767
$edge = $this->__getMax($Model, $scope, $right, $recursive);
770
for ($i = $min; $i <= $edge; $i++) {
771
$count = $Model->find('count', array('conditions' => array(
772
$scope, 'OR' => array($Model->escapeField($left) => $i, $Model->escapeField($right) => $i)
776
$errors[] = array('index', $i, 'missing');
778
$errors[] = array('index', $i, 'duplicate');
782
$node = $Model->find('first', array('conditions' => array($scope, $Model->escapeField($right) . '< ' . $Model->escapeField($left)), 'recursive' => 0));
784
$errors[] = array('node', $node[$Model->alias][$Model->primaryKey], 'left greater than right.');
787
$Model->bindModel(array('belongsTo' => array('VerifyParent' => array(
788
'className' => $Model->alias,
789
'foreignKey' => $parent,
790
'fields' => array($Model->primaryKey, $left, $right, $parent)
793
foreach ($Model->find('all', array('conditions' => $scope, 'recursive' => 0)) as $instance) {
794
if (is_null($instance[$Model->alias][$left]) || is_null($instance[$Model->alias][$right])) {
795
$errors[] = array('node', $instance[$Model->alias][$Model->primaryKey],
796
'has invalid left or right values');
797
} elseif ($instance[$Model->alias][$left] == $instance[$Model->alias][$right]) {
798
$errors[] = array('node', $instance[$Model->alias][$Model->primaryKey],
799
'left and right values identical');
800
} elseif ($instance[$Model->alias][$parent]) {
801
if (!$instance['VerifyParent'][$Model->primaryKey]) {
802
$errors[] = array('node', $instance[$Model->alias][$Model->primaryKey],
803
'The parent node ' . $instance[$Model->alias][$parent] . ' doesn\'t exist');
804
} elseif ($instance[$Model->alias][$left] < $instance['VerifyParent'][$left]) {
805
$errors[] = array('node', $instance[$Model->alias][$Model->primaryKey],
806
'left less than parent (node ' . $instance['VerifyParent'][$Model->primaryKey] . ').');
807
} elseif ($instance[$Model->alias][$right] > $instance['VerifyParent'][$right]) {
808
$errors[] = array('node', $instance[$Model->alias][$Model->primaryKey],
809
'right greater than parent (node ' . $instance['VerifyParent'][$Model->primaryKey] . ').');
811
} elseif ($Model->find('count', array('conditions' => array($scope, $Model->escapeField($left) . ' <' => $instance[$Model->alias][$left], $Model->escapeField($right) . ' >' => $instance[$Model->alias][$right]), 'recursive' => 0))) {
812
$errors[] = array('node', $instance[$Model->alias][$Model->primaryKey], 'The parent field is blank, but has a parent');
822
* Sets the parent of the given node
824
* The force parameter is used to override the "don't change the parent to the current parent" logic in the event
825
* of recovering a corrupted table, or creating new nodes. Otherwise it should always be false. In reality this
826
* method could be private, since calling save with parent_id set also calls setParent
828
* @param AppModel $Model Model instance
829
* @param mixed $parentId
830
* @return boolean true on success, false on failure
833
function _setParent(&$Model, $parentId = null, $created = false) {
834
extract($this->settings[$Model->alias]);
835
list($node) = array_values($Model->find('first', array(
836
'conditions' => array($scope, $Model->escapeField() => $Model->id),
837
'fields' => array($Model->primaryKey, $parent, $left, $right),
838
'recursive' => $recursive
840
$edge = $this->__getMax($Model, $scope, $right, $recursive, $created);
842
if (empty ($parentId)) {
843
$this->__sync($Model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right], $created);
844
$this->__sync($Model, $node[$right] - $node[$left] + 1, '-', '> ' . $node[$left], $created);
846
$values = $Model->find('first', array(
847
'conditions' => array($scope, $Model->escapeField() => $parentId),
848
'fields' => array($Model->primaryKey, $left, $right),
849
'recursive' => $recursive
852
if ($values === false) {
855
$parentNode = array_values($values);
857
if (empty($parentNode) || empty($parentNode[0])) {
860
$parentNode = $parentNode[0];
862
if (($Model->id == $parentId)) {
865
} elseif (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) {
868
if (empty ($node[$left]) && empty ($node[$right])) {
869
$this->__sync($Model, 2, '+', '>= ' . $parentNode[$right], $created);
870
$result = $Model->save(
871
array($left => $parentNode[$right], $right => $parentNode[$right] + 1, $parent => $parentId),
872
array('validate' => false, 'callbacks' => false)
874
$Model->data = $result;
876
$this->__sync($Model, $edge - $node[$left] +1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right], $created);
877
$diff = $node[$right] - $node[$left] + 1;
879
if ($node[$left] > $parentNode[$left]) {
880
if ($node[$right] < $parentNode[$right]) {
881
$this->__sync($Model, $diff, '-', 'BETWEEN ' . $node[$right] . ' AND ' . ($parentNode[$right] - 1), $created);
882
$this->__sync($Model, $edge - $parentNode[$right] + $diff + 1, '-', '> ' . $edge, $created);
884
$this->__sync($Model, $diff, '+', 'BETWEEN ' . $parentNode[$right] . ' AND ' . $node[$right], $created);
885
$this->__sync($Model, $edge - $parentNode[$right] + 1, '-', '> ' . $edge, $created);
888
$this->__sync($Model, $diff, '-', 'BETWEEN ' . $node[$right] . ' AND ' . ($parentNode[$right] - 1), $created);
889
$this->__sync($Model, $edge - $parentNode[$right] + $diff + 1, '-', '> ' . $edge, $created);
897
* get the maximum index value in the table.
899
* @param AppModel $Model
900
* @param string $scope
901
* @param string $right
905
function __getMax($Model, $scope, $right, $recursive = -1, $created = false) {
906
$db =& ConnectionManager::getDataSource($Model->useDbConfig);
908
if (is_string($scope)) {
909
$scope .= " AND {$Model->alias}.{$Model->primaryKey} <> ";
910
$scope .= $db->value($Model->id, $Model->getColumnType($Model->primaryKey));
912
$scope['NOT'][$Model->alias . '.' . $Model->primaryKey] = $Model->id;
915
$name = $Model->alias . '.' . $right;
916
list($edge) = array_values($Model->find('first', array(
917
'conditions' => $scope,
918
'fields' => $db->calculate($Model, 'max', array($name, $right)),
919
'recursive' => $recursive
921
return (empty($edge[$right])) ? 0 : $edge[$right];
925
* get the minimum index value in the table.
927
* @param AppModel $Model
928
* @param string $scope
929
* @param string $right
933
function __getMin($Model, $scope, $left, $recursive = -1) {
934
$db =& ConnectionManager::getDataSource($Model->useDbConfig);
935
$name = $Model->alias . '.' . $left;
936
list($edge) = array_values($Model->find('first', array(
937
'conditions' => $scope,
938
'fields' => $db->calculate($Model, 'min', array($name, $left)),
939
'recursive' => $recursive
941
return (empty($edge[$left])) ? 0 : $edge[$left];
947
* Handles table sync operations, Taking account of the behavior scope.
949
* @param AppModel $Model
950
* @param integer $shift
951
* @param string $direction
952
* @param array $conditions
953
* @param string $field
956
function __sync(&$Model, $shift, $dir = '+', $conditions = array(), $created = false, $field = 'both') {
957
$ModelRecursive = $Model->recursive;
958
extract($this->settings[$Model->alias]);
959
$Model->recursive = $recursive;
961
if ($field == 'both') {
962
$this->__sync($Model, $shift, $dir, $conditions, $created, $left);
965
if (is_string($conditions)) {
966
$conditions = array("{$Model->alias}.{$field} {$conditions}");
968
if (($scope != '1 = 1' && $scope !== true) && $scope) {
969
$conditions[] = $scope;
972
$conditions['NOT'][$Model->alias . '.' . $Model->primaryKey] = $Model->id;
974
$Model->updateAll(array($Model->alias . '.' . $field => $Model->escapeField($field) . ' ' . $dir . ' ' . $shift), $conditions);
975
$Model->recursive = $ModelRecursive;