/
TreeBehavior.php
408 lines (342 loc) · 10.8 KB
/
TreeBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Model\Behavior;
use ArrayObject;
use Cake\Collection\Collection;
use Cake\Database\Expression\QueryExpression;
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
class TreeBehavior extends Behavior {
/**
* Table instance
*
* @var \Cake\ORM\Table
*/
protected $_table;
/**
* Default config
*
* These are merged with user-provided configuration when the behavior is used.
*
* @var array
*/
protected static $_defaultConfig = [
'implementedFinders' => [
'path' => 'findPath',
'children' => 'findChildren',
],
'parent' => 'parent_id',
'left' => 'lft',
'right' => 'rght',
'scope' => null
];
/**
* Constructor
*
* @param Table $table The table this behavior is attached to.
* @param array $config The config for this behavior.
*/
public function __construct(Table $table, array $config = []) {
parent::__construct($table, $config);
$this->_table = $table;
}
public function findPath($query, $options) {
if (empty($options['for'])) {
throw new \InvalidArgumentException("The 'for' key is required for find('path')");
}
$config = $this->config();
list($left, $right) = [$config['left'], $config['right']];
$node = $this->_table->get($options['for'], ['fields' => [$left, $right]]);
return $this->_scope($query)
->where([
"$left <=" => $node->get($left),
"$right >=" => $node->get($right)
]);
}
/**
* Get the number of child nodes.
*
* @param integer|string $id The ID of the record to read
* @param boolean $direct whether to count direct, or all, children
* @return integer Number of child nodes.
*/
public function childCount($id, $direct = false) {
$config = $this->config();
list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];
if ($direct) {
$count = $this->_table->find()
->where([$parent => $id])
->count();
return $count;
}
$node = $this->_table->get($id, [$this->_table->primaryKey() => $id]);
return ($node->{$right} - $node->{$left} - 1) / 2;
}
/**
* Get the child nodes of the current model
*
* Available options are:
*
* - for: The ID of the record to read.
* - direct: Boolean, whether to return only the direct (true), or all (false), children. default to false (all children).
*
* If the direct option 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 array $options Array of options as described above
* @return \Cake\ORM\Query
* @throws \Cake\ORM\Error\RecordNotFoundException When node was not found
* @throws \InvalidArgumentException When the 'for' key is not passed in $options
*/
public function findChildren($query, $options) {
$config = $this->config();
$options += ['for' => null, 'direct' => false];
list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];
list($for, $direct) = [$options['for'], $options['direct']];
$primaryKey = $this->_table->primaryKey();
if (empty($for)) {
throw new \InvalidArgumentException("The 'for' key is required for find('children')");
}
if ($query->clause('order') === null) {
$query->order([$left => 'ASC']);
}
if ($direct) {
return $this->_scope($query)->where([$parent => $for]);
}
$node = $this->_scope($this->_table->find())
->select([$right, $left])
->where([$primaryKey => $for])
->first();
if (!$node) {
throw new \Cake\ORM\Error\RecordNotFoundException("Node \"{$for}\ was not found in the tree.");
}
return $this->_scope($query)
->where([
"{$right} <" => $node->{$right},
"{$left} >" => $node->{$left}
]);
}
/**
* 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
*
* @param integer|string $id The ID of the record to move
* @param integer|boolean $number How many places to move the node, or true to move to first position
* @throws \Cake\ORM\Error\RecordNotFoundException When node was not found
* @return boolean true on success, false on failure
*/
public function moveUp($id, $number = 1) {
$config = $this->config();
list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];
$primaryKey = $this->_table->primaryKey();
if (!$number) {
return false;
}
$node = $this->_scope($this->_table->find())
->select([$parent, $left, $right])
->where([$primaryKey => $id])
->first();
if (!$node) {
throw new \Cake\ORM\Error\RecordNotFoundException("Node \"{$id}\" was not found in the tree.");
}
if ($node->{$parent}) {
$parentNode = $this->_table->get($node->{$parent}, ['fields' => [$left, $right]]);
if (($node->{$left} - 1) == $parentNode->{$left}) {
return false;
}
}
$previousNode = $this->_scope($this->_table->find())
->select([$left, $right])
->where([$right => ($node->{$left} - 1)])
->first();
if (!$previousNode) {
return false;
}
$edge = $this->_getMax();
$this->_sync($edge - $previousNode->{$left} + 1, '+', "BETWEEN {$previousNode->{$left}} AND {$previousNode->{$right}}");
$this->_sync($node->{$left} - $previousNode->{$left}, '-', "BETWEEN {$node->{$left}} AND {$node->{$right}}");
$this->_sync($edge - $previousNode->{$left} - ($node->{$right} - $node->{$left}), '-', "> {$edge}");
if (is_int($number)) {
$number--;
}
if ($number) {
$this->moveUp($id, $number);
}
return true;
}
/**
* 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 integer|string $id The ID of the record to move
* @param integer|boolean $number How many places to move the node or true to move to last position
* @throws \Cake\ORM\Error\RecordNotFoundException When node was not found
* @return boolean true on success, false on failure
*/
public function moveDown($id, $number = 1) {
$config = $this->config();
list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];
$primaryKey = $this->_table->primaryKey();
if (!$number) {
return false;
}
$node = $this->_scope($this->_table->find())
->select([$parent, $left, $right])
->where([$primaryKey => $id])
->first();
if (!$node) {
throw new \Cake\ORM\Error\RecordNotFoundException("Node \"{$id}\" was not found in the tree.");
}
if ($node->{$parent}) {
$parentNode = $this->_table->get($node->{$parent}, ['fields' => [$left, $right]]);
if (($node->{$right} + 1) == $parentNode->{$right}) {
return false;
}
}
$nextNode = $this->_scope($this->_table->find())
->select([$left, $right])
->where([$left => $node->{$right} + 1])
->first();
if (!$nextNode) {
return false;
}
$edge = $this->_getMax();
$this->_sync($edge - $node->{$left} + 1, '+', "BETWEEN {$node->{$left}} AND {$node->{$right}}");
$this->_sync($nextNode->{$left} - $node->{$left}, '-', "BETWEEN {$nextNode->{$left}} AND {$nextNode->{$right}}");
$this->_sync($edge - $node->{$left} - ($nextNode->{$right} - $nextNode->{$left}), '-', "> {$edge}");
if (is_int($number)) {
$number--;
}
if ($number) {
$this->moveDown($id, $number);
}
return true;
}
/**
* Recovers the lft and right column values out of the hirearchy defined by the
* parent column.
*
* @return void
*/
public function recover() {
$this->_table->connection()->transactional(function() {
$this->_recoverTree();
});
}
/**
* Recursive method used to recover a single level of the tree
*
* @param integer $counter The Last left column value that was assigned
* @param mixed $parentId the parent id of the level to be recovered
* @return integer Ne next value to use for the left column
*/
protected function _recoverTree($counter = 0, $parentId = null) {
$config = $this->config();
list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];
$pk = (array)$this->_table->primaryKey();
$query = $this->_scope($this->_table->query())
->select($pk)
->where(function($exp) use ($parentId, $parent) {
return $parentId === null ? $exp->isNull($parent) : $exp->eq($parent, $parentId);
})
->order($pk)
->hydrate(false)
->bufferResults(false);
$leftCounter = $counter;
foreach ($query as $row) {
$counter++;
$counter = $this->_recoverTree($counter, $row[$pk[0]]);
}
if ($parentId === null) {
return $counter;
}
$this->_table->updateAll(
[$left => $leftCounter, $right => $counter + 1],
[$pk[0] => $parentId]
);
return $counter + 1;
}
/**
* Get the maximum index value in the table.
*
* @return integer
*/
protected function _getMax() {
return $this->_getMaxOrMin('max');
}
/**
* Get the minimum index value in the table.
*
* @return integer
*/
protected function _getMin() {
return $this->_getMaxOrMin('min');
}
/**
* Get the maximum|minimum index value in the table.
*
* @param string $maxOrMin Either 'max' or 'min'
* @return integer
*/
protected function _getMaxOrMin($maxOrMin = 'max') {
extract($this->config());
$LorR = $maxOrMin === 'max' ? $right : $left;
$DorA = $maxOrMin === 'max' ? 'DESC' : 'ASC';
$edge = $this->_scope($this->_table->find())
->select([$LorR])
->order([$LorR => $DorA])
->first();
if (empty($edge->{$LorR})) {
return 0;
}
return $edge->{$LorR};
}
protected function _sync($shift, $dir = '+', $conditions = null, $field = 'both') {
extract($this->config());
if ($field === 'both') {
$this->_sync($shift, $dir, $conditions, $left);
$field = $right;
}
// updateAll + scope
$exp = new QueryExpression();
$exp->add("{$field} = ({$field} {$dir} {$shift})");
$query = $this->_scope($this->_table->query());
$query->update()
->set($exp);
if ($conditions) {
$conditions = "{$field} {$conditions}";
$query->where($conditions);
}
$statement = $query->execute();
$success = $statement->rowCount() > 0;
return $success;
}
protected function _scope($query) {
$config = $this->config();
if (empty($config['scope'])) {
return $query;
} elseif (is_array($config['scope'])) {
return $query->where($config['scope']);
} elseif (is_callable($config['scope'])) {
return $config['scope']($query);
}
return $query;
}
}