1
|
|
|
<?php |
2
|
|
|
namespace StefanoTree\NestedSet\Adapter; |
3
|
|
|
|
4
|
|
|
use StefanoTree\NestedSet\NodeInfo; |
5
|
|
|
use StefanoTree\NestedSet\Options; |
6
|
|
|
use Zend\Db; |
7
|
|
|
use Zend\Db\Adapter\Adapter as DbAdapter; |
8
|
|
|
|
9
|
|
|
class Zend2 |
10
|
|
|
implements AdapterInterface |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
private $options; |
13
|
|
|
|
14
|
|
|
private $dbAdapter; |
15
|
|
|
|
16
|
|
|
private $defaultDbSelect = null; |
17
|
|
|
|
18
|
|
|
public function __construct(Options $options, DbAdapter $dbAdapter) |
19
|
|
|
{ |
20
|
|
|
$this->options = $options; |
21
|
|
|
$this->dbAdapter = $dbAdapter; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return Options |
26
|
|
|
*/ |
27
|
|
|
private function getOptions() |
28
|
|
|
{ |
29
|
|
|
return $this->options; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return DbAdapter |
34
|
|
|
*/ |
35
|
|
|
private function getDbAdapter() |
36
|
|
|
{ |
37
|
|
|
return $this->dbAdapter; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Data cannot contain keys like idColumnName, levelColumnName, ... |
42
|
|
|
* |
43
|
|
|
* @param array $data |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
private function cleanData(array $data) |
47
|
|
|
{ |
48
|
|
|
$options = $this->getOptions(); |
49
|
|
|
|
50
|
|
|
$disallowedDataKeys = array( |
51
|
|
|
$options->getIdColumnName(), |
52
|
|
|
$options->getLeftColumnName(), |
53
|
|
|
$options->getRightColumnName(), |
54
|
|
|
$options->getLevelColumnName(), |
55
|
|
|
$options->getParentIdColumnName(), |
56
|
|
|
$options->getScopeColumnName(), |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
return array_diff_key($data, array_flip($disallowedDataKeys)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return base db select without any join, etc. |
64
|
|
|
* @return Db\Sql\Select |
65
|
|
|
*/ |
66
|
|
|
public function getBlankDbSelect() |
67
|
|
|
{ |
68
|
|
|
return new Db\Sql\Select($this->getOptions()->getTableName()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Db\Sql\Select $dbSelect |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function setDefaultDbSelect(Db\Sql\Select $dbSelect) |
76
|
|
|
{ |
77
|
|
|
$this->defaultDbSelect = $dbSelect; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Return default db select |
82
|
|
|
* @return Db\Sql\Select |
83
|
|
|
*/ |
84
|
|
|
public function getDefaultDbSelect() |
85
|
|
|
{ |
86
|
|
|
if (null === $this->defaultDbSelect) { |
87
|
|
|
$this->defaultDbSelect = $this->getBlankDbSelect(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$dbSelect = clone $this->defaultDbSelect; |
91
|
|
|
|
92
|
|
|
return $dbSelect; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function lockTree($scope) |
96
|
|
|
{ |
97
|
|
|
$options = $this->getOptions(); |
98
|
|
|
|
99
|
|
|
$dbAdapter = $this->getDbAdapter(); |
100
|
|
|
|
101
|
|
|
$select = $this->getBlankDbSelect(); |
102
|
|
|
$select->columns(array( |
103
|
|
|
'i' => $options->getIdColumnName(), |
104
|
|
|
)); |
105
|
|
|
|
106
|
|
|
if ($options->getScopeColumnName()) { |
107
|
|
|
$select->where(array( |
108
|
|
|
$options->getScopeColumnName() => $scope, |
109
|
|
|
)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$sql = $select->getSqlString($dbAdapter->getPlatform()) . ' FOR UPDATE'; |
113
|
|
|
|
114
|
|
|
$dbAdapter->query($sql, DbAdapter::QUERY_MODE_EXECUTE); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function beginTransaction() |
118
|
|
|
{ |
119
|
|
|
$this->getDbAdapter() |
120
|
|
|
->getDriver() |
121
|
|
|
->getConnection() |
122
|
|
|
->beginTransaction(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function commitTransaction() |
126
|
|
|
{ |
127
|
|
|
$this->getDbAdapter() |
128
|
|
|
->getDriver() |
129
|
|
|
->getConnection() |
130
|
|
|
->commit(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function rollbackTransaction() |
134
|
|
|
{ |
135
|
|
|
$this->getDbAdapter() |
136
|
|
|
->getDriver() |
137
|
|
|
->getConnection() |
138
|
|
|
->rollback(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function update($nodeId, array $data) |
142
|
|
|
{ |
143
|
|
|
$options = $this->getOptions(); |
144
|
|
|
|
145
|
|
|
$dbAdapter = $this->getDbAdapter(); |
146
|
|
|
|
147
|
|
|
$data = $this->cleanData($data); |
148
|
|
|
|
149
|
|
|
$update = new Db\Sql\Update($options->getTableName()); |
150
|
|
|
$update->set($data) |
151
|
|
|
->where(array( |
152
|
|
|
$options->getIdColumnName() => $nodeId, |
153
|
|
|
)); |
154
|
|
|
|
155
|
|
|
$dbAdapter->query($update->getSqlString($dbAdapter->getPlatform()), |
156
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function insert(NodeInfo $nodeInfo, array $data) |
160
|
|
|
{ |
161
|
|
|
$options = $this->getOptions(); |
162
|
|
|
|
163
|
|
|
$dbAdapter = $this->getDbAdapter(); |
164
|
|
|
|
165
|
|
|
$data[$options->getParentIdColumnName()] = $nodeInfo->getParentId(); |
166
|
|
|
$data[$options->getLevelColumnName()] = $nodeInfo->getLevel(); |
167
|
|
|
$data[$options->getLeftColumnName()] = $nodeInfo->getLeft(); |
168
|
|
|
$data[$options->getRightColumnName()] = $nodeInfo->getRight(); |
169
|
|
|
|
170
|
|
|
if ($options->getScopeColumnName()) { |
171
|
|
|
$data[$options->getScopeColumnName()] = $nodeInfo->getScope(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$insert = new Db\Sql\Insert($options->getTableName()); |
175
|
|
|
$insert->values($data); |
176
|
|
|
$dbAdapter->query($insert->getSqlString($dbAdapter->getPlatform()), |
177
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
178
|
|
|
|
179
|
|
|
$lastGeneratedValue = $dbAdapter->getDriver() |
180
|
|
|
->getLastGeneratedValue($options->getSequenceName()); |
|
|
|
|
181
|
|
|
|
182
|
|
|
return $lastGeneratedValue; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function delete($leftIndex, $rightIndex, $scope = null) |
186
|
|
|
{ |
187
|
|
|
$options = $this->getOptions(); |
188
|
|
|
|
189
|
|
|
$dbAdapter = $this->getDbAdapter(); |
190
|
|
|
|
191
|
|
|
$delete = new Db\Sql\Delete($options->getTableName()); |
192
|
|
|
$delete->where |
193
|
|
|
->greaterThanOrEqualTo($options->getLeftColumnName(), $leftIndex) |
194
|
|
|
->AND |
195
|
|
|
->lessThanOrEqualTo($options->getRightColumnName(), $rightIndex); |
196
|
|
|
|
197
|
|
|
if ($options->getScopeColumnName()) { |
198
|
|
|
$delete->where |
199
|
|
|
->AND |
200
|
|
|
->equalTo($options->getScopeColumnName(), $scope); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$dbAdapter->query($delete->getSqlString($dbAdapter->getPlatform()), |
204
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function moveLeftIndexes($fromIndex, $shift, $scope = null) |
208
|
|
|
{ |
209
|
|
|
$options = $this->getOptions(); |
210
|
|
|
|
211
|
|
|
if (0 == $shift) { |
212
|
|
|
return null; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$dbAdapter = $this->getDbAdapter(); |
216
|
|
|
$dbPlatform = $dbAdapter->getPlatform(); |
217
|
|
|
|
218
|
|
|
$sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName()) |
219
|
|
|
. ' SET ' |
220
|
|
|
. $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' = ' |
221
|
|
|
. $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' + :shift' |
222
|
|
|
. ' WHERE ' |
223
|
|
|
. $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' > :fromIndex'; |
224
|
|
|
|
225
|
|
|
if ($options->getScopeColumnName()) { |
226
|
|
|
$sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$binds = array( |
230
|
|
|
':shift' => $shift, |
231
|
|
|
':fromIndex' => $fromIndex, |
232
|
|
|
); |
233
|
|
|
|
234
|
|
|
$dbAdapter->query($sql) |
|
|
|
|
235
|
|
|
->execute($binds); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function moveRightIndexes($fromIndex, $shift, $scope = null) |
239
|
|
|
{ |
240
|
|
|
$options = $this->getOptions(); |
241
|
|
|
|
242
|
|
|
if (0 == $shift) { |
243
|
|
|
return null; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$dbAdapter = $this->getDbAdapter(); |
247
|
|
|
$dbPlatform = $dbAdapter->getPlatform(); |
248
|
|
|
|
249
|
|
|
$sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName()) |
250
|
|
|
. ' SET ' |
251
|
|
|
. $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' = ' |
252
|
|
|
. $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' + :shift' |
253
|
|
|
. ' WHERE ' |
254
|
|
|
. $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' > :fromIndex'; |
255
|
|
|
|
256
|
|
|
if ($options->getScopeColumnName()) { |
257
|
|
|
$sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$binds = array( |
261
|
|
|
':shift' => $shift, |
262
|
|
|
':fromIndex' => $fromIndex, |
263
|
|
|
); |
264
|
|
|
|
265
|
|
|
$dbAdapter->query($sql) |
|
|
|
|
266
|
|
|
->execute($binds); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function updateParentId($nodeId, $newParentId) |
270
|
|
|
{ |
271
|
|
|
$options = $this->getOptions(); |
272
|
|
|
|
273
|
|
|
$dbAdapter = $this->getDbAdapter(); |
274
|
|
|
|
275
|
|
|
$update = new Db\Sql\Update($options->getTableName()); |
276
|
|
|
$update->set(array( |
277
|
|
|
$options->getParentIdColumnName() => $newParentId, |
278
|
|
|
)) |
279
|
|
|
->where(array( |
280
|
|
|
$options->getIdColumnName() => $nodeId, |
281
|
|
|
)); |
282
|
|
|
|
283
|
|
|
$dbAdapter->query($update->getSqlString($dbAdapter->getPlatform()), |
284
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public function updateLevels($leftIndexFrom, $rightIndexTo, $shift, $scope = null) |
288
|
|
|
{ |
289
|
|
|
$options = $this->getOptions(); |
290
|
|
|
|
291
|
|
|
if (0 == $shift) { |
292
|
|
|
return null; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$dbAdapter = $this->getDbAdapter(); |
296
|
|
|
$dbPlatform = $dbAdapter->getPlatform(); |
297
|
|
|
|
298
|
|
|
$sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName()) |
299
|
|
|
. ' SET ' |
300
|
|
|
. $dbPlatform->quoteIdentifier($options->getLevelColumnName()) . ' = ' |
301
|
|
|
. $dbPlatform->quoteIdentifier($options->getLevelColumnName()) . ' + :shift' |
302
|
|
|
. ' WHERE ' |
303
|
|
|
. $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' >= :leftFrom' |
304
|
|
|
. ' AND ' . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' <= :rightTo'; |
305
|
|
|
|
306
|
|
|
if ($options->getScopeColumnName()) { |
307
|
|
|
$sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
$binds = array( |
311
|
|
|
':shift' => $shift, |
312
|
|
|
':leftFrom' => $leftIndexFrom, |
313
|
|
|
':rightTo' => $rightIndexTo, |
314
|
|
|
); |
315
|
|
|
|
316
|
|
|
$dbAdapter->query($sql) |
|
|
|
|
317
|
|
|
->execute($binds); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
public function moveBranch($leftIndexFrom, $rightIndexTo, $shift, $scope = null) |
321
|
|
|
{ |
322
|
|
|
if (0 == $shift) { |
323
|
|
|
return null; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
$options = $this->getOptions(); |
327
|
|
|
|
328
|
|
|
$dbAdapter = $this->getDbAdapter(); |
329
|
|
|
$dbPlatform = $dbAdapter->getPlatform(); |
330
|
|
|
|
331
|
|
|
$sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName()) |
332
|
|
|
. ' SET ' |
333
|
|
|
. $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' = ' |
334
|
|
|
. $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' + :shift, ' |
335
|
|
|
. $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' = ' |
336
|
|
|
. $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' + :shift' |
337
|
|
|
. ' WHERE ' |
338
|
|
|
. $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' >= :leftFrom' |
339
|
|
|
. ' AND ' . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' <= :rightTo'; |
340
|
|
|
|
341
|
|
|
if ($options->getScopeColumnName()) { |
342
|
|
|
$sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
$binds = array( |
346
|
|
|
':shift' => $shift, |
347
|
|
|
':leftFrom' => $leftIndexFrom, |
348
|
|
|
':rightTo' => $rightIndexTo, |
349
|
|
|
); |
350
|
|
|
|
351
|
|
|
$dbAdapter->query($sql) |
|
|
|
|
352
|
|
|
->execute($binds); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
public function getRoots($scope = null) |
356
|
|
|
{ |
357
|
|
|
$options = $this->getOptions(); |
358
|
|
|
|
359
|
|
|
$dbAdapter = $this->getDbAdapter(); |
360
|
|
|
|
361
|
|
|
$select = $this->getBlankDbSelect(); |
362
|
|
|
$select->where |
363
|
|
|
->equalTo($options->getParentIdColumnName(), 0); |
364
|
|
|
|
365
|
|
|
if (null != $scope && $options->getScopeColumnName()) { |
366
|
|
|
$select->where |
367
|
|
|
->equalTo($options->getScopeColumnName(), $scope); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
$result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()), |
371
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
372
|
|
|
|
373
|
|
|
return $result->toArray(); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
public function getRoot($scope = null) |
377
|
|
|
{ |
378
|
|
|
$roots = $this->getRoots($scope); |
379
|
|
|
|
380
|
|
|
return (0 < count($roots)) ? $roots[0] : array(); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
public function getNode($nodeId) |
384
|
|
|
{ |
385
|
|
|
$options = $this->getOptions(); |
386
|
|
|
|
387
|
|
|
$nodeId = (int) $nodeId; |
388
|
|
|
|
389
|
|
|
$dbAdapter = $this->getDbAdapter(); |
390
|
|
|
|
391
|
|
|
$select = $this->getDefaultDbSelect() |
392
|
|
|
->where(array($options->getIdColumnName() => $nodeId)); |
393
|
|
|
|
394
|
|
|
$result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()), |
395
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
396
|
|
|
|
397
|
|
|
$array = $result->toArray(); |
398
|
|
|
|
399
|
|
|
return (0 < count($array)) ? $array[0] : null; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* @param array $data |
404
|
|
|
* @return NodeInfo |
405
|
|
|
*/ |
406
|
|
|
private function _buildNodeInfoObject(array $data) |
407
|
|
|
{ |
408
|
|
|
$options = $this->getOptions(); |
409
|
|
|
|
410
|
|
|
$id = $data[$options->getIdColumnName()]; |
411
|
|
|
$parentId = $data[$options->getParentIdColumnName()]; |
412
|
|
|
$level = $data[$options->getLevelColumnName()]; |
413
|
|
|
$left = $data[$options->getLeftColumnName()]; |
414
|
|
|
$right = $data[$options->getRightColumnName()]; |
415
|
|
|
|
416
|
|
|
if (isset($data[$options->getScopeColumnName()])) { |
417
|
|
|
$scope = $data[$options->getScopeColumnName()]; |
418
|
|
|
} else { |
419
|
|
|
$scope = null; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
return new NodeInfo($id, $parentId, $level, $left, $right, $scope); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
public function getNodeInfo($nodeId) |
426
|
|
|
{ |
427
|
|
|
$options = $this->getOptions(); |
428
|
|
|
|
429
|
|
|
$nodeId = (int) $nodeId; |
430
|
|
|
|
431
|
|
|
$dbAdapter = $this->getDbAdapter(); |
432
|
|
|
|
433
|
|
|
$select = $this->getBlankDbSelect() |
434
|
|
|
->where(array($options->getIdColumnName() => $nodeId)); |
435
|
|
|
|
436
|
|
|
$result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()), |
437
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
438
|
|
|
|
439
|
|
|
$array = $result->toArray(); |
440
|
|
|
|
441
|
|
|
$result = ($array) ? $this->_buildNodeInfoObject($array[0]) : null; |
442
|
|
|
|
443
|
|
|
return $result; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
public function getChildrenNodeInfo($parentNodeId) |
447
|
|
|
{ |
448
|
|
|
$dbAdapter = $this->getDbAdapter(); |
449
|
|
|
$options = $this->getOptions(); |
450
|
|
|
|
451
|
|
|
$columns = array( |
452
|
|
|
$options->getIdColumnName(), |
453
|
|
|
$options->getLeftColumnName(), |
454
|
|
|
$options->getRightColumnName(), |
455
|
|
|
$options->getParentIdColumnName(), |
456
|
|
|
$options->getLevelColumnName(), |
457
|
|
|
); |
458
|
|
|
|
459
|
|
|
if ($options->getScopeColumnName()) { |
460
|
|
|
$columns[] = $options->getScopeColumnName(); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
$select = $this->getBlankDbSelect(); |
464
|
|
|
$select->columns($columns); |
465
|
|
|
$select->order($options->getLeftColumnName()); |
466
|
|
|
$select->where(array( |
467
|
|
|
$options->getParentIdColumnName() => $parentNodeId |
468
|
|
|
)); |
469
|
|
|
|
470
|
|
|
$data = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()), |
471
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
472
|
|
|
|
473
|
|
|
$result = array(); |
474
|
|
|
|
475
|
|
|
foreach ($data->toArray() as $nodeData) { |
476
|
|
|
$result[] = $this->_buildNodeInfoObject($nodeData); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
return $result; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
public function updateNodeMetadata(NodeInfo $nodeInfo) |
483
|
|
|
{ |
484
|
|
|
$dbAdapter = $this->getDbAdapter(); |
485
|
|
|
$options = $this->getOptions(); |
486
|
|
|
|
487
|
|
|
$update = new Db\Sql\Update($options->getTableName()); |
488
|
|
|
|
489
|
|
|
$update->set(array( |
490
|
|
|
$options->getRightColumnName() => $nodeInfo->getRight(), |
491
|
|
|
$options->getLeftColumnName() => $nodeInfo->getLeft(), |
492
|
|
|
$options->getLevelColumnName() => $nodeInfo->getLevel(), |
493
|
|
|
)); |
494
|
|
|
|
495
|
|
|
$update->where(array( |
496
|
|
|
$options->getIdColumnName() => $nodeInfo->getId(), |
497
|
|
|
)); |
498
|
|
|
|
499
|
|
|
$dbAdapter->query($update->getSqlString($dbAdapter->getPlatform()), |
500
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
|
504
|
|
|
public function getPath($nodeId, $startLevel = 0, $excludeLastNode = false) |
505
|
|
|
{ |
506
|
|
|
$options = $this->getOptions(); |
507
|
|
|
|
508
|
|
|
$startLevel = (int) $startLevel; |
509
|
|
|
|
510
|
|
|
// node does not exist |
511
|
|
|
$nodeInfo = $this->getNodeInfo($nodeId); |
512
|
|
|
if (!$nodeInfo) { |
513
|
|
|
return array(); |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
$dbAdapter = $this->getDbAdapter(); |
517
|
|
|
|
518
|
|
|
$select = $this->getDefaultDbSelect(); |
519
|
|
|
|
520
|
|
|
if ($options->getScopeColumnName()) { |
521
|
|
|
$select->where |
522
|
|
|
->equalTo($options->getScopeColumnName(), $nodeInfo->getScope()); |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
$select->where |
526
|
|
|
->lessThanOrEqualTo($options->getLeftColumnName(), $nodeInfo->getLeft()) |
527
|
|
|
->AND |
528
|
|
|
->greaterThanOrEqualTo($options->getRightColumnName(), $nodeInfo->getRight()); |
529
|
|
|
|
530
|
|
|
$select->order($options->getLeftColumnName() . ' ASC'); |
531
|
|
|
|
532
|
|
|
if (0 < $startLevel) { |
533
|
|
|
$select->where |
534
|
|
|
->greaterThanOrEqualTo($options->getLevelColumnName(), $startLevel); |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
if (true == $excludeLastNode) { |
|
|
|
|
538
|
|
|
$select->where |
539
|
|
|
->lessThan($options->getLevelColumnName(), $nodeInfo->getLevel()); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
$result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()), |
543
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
544
|
|
|
|
545
|
|
|
return $result->toArray(); |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
public function getDescendants($nodeId = 1, $startLevel = 0, $levels = null, $excludeBranch = null) |
549
|
|
|
{ |
550
|
|
|
$options = $this->getOptions(); |
551
|
|
|
|
552
|
|
|
if (!$nodeInfo = $this->getNodeInfo($nodeId)) { |
553
|
|
|
return array(); |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
$dbAdapter = $this->getDbAdapter(); |
557
|
|
|
$select = $this->getDefaultDbSelect(); |
558
|
|
|
$select->order($options->getLeftColumnName() . ' ASC'); |
559
|
|
|
|
560
|
|
|
if ($options->getScopeColumnName()) { |
561
|
|
|
$select->where |
562
|
|
|
->equalTo($options->getScopeColumnName(), $nodeInfo->getScope()); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
if (0 != $startLevel) { |
566
|
|
|
$level = $nodeInfo->getLevel() + (int) $startLevel; |
567
|
|
|
$select->where |
568
|
|
|
->greaterThanOrEqualTo($options->getLevelColumnName(), $level); |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
if (null != $levels) { |
|
|
|
|
572
|
|
|
$endLevel = $nodeInfo->getLevel() + (int) $startLevel + abs($levels); |
573
|
|
|
$select->where |
574
|
|
|
->lessThan($options->getLevelColumnName(), $endLevel); |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
if (null != $excludeBranch && null != ($excludeNodeInfo = $this->getNodeInfo($excludeBranch))) { |
|
|
|
|
578
|
|
|
$select->where |
579
|
|
|
->NEST |
580
|
|
|
->between($options->getLeftColumnName(), |
581
|
|
|
$nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1) |
582
|
|
|
->OR |
583
|
|
|
->between($options->getLeftColumnName(), |
584
|
|
|
$excludeNodeInfo->getRight() + 1, $nodeInfo->getRight()) |
585
|
|
|
->UNNEST |
586
|
|
|
->AND |
587
|
|
|
->NEST |
588
|
|
|
->between($options->getRightColumnName(), |
589
|
|
|
$excludeNodeInfo->getRight() + 1, $nodeInfo->getRight()) |
590
|
|
|
->OR |
591
|
|
|
->between($options->getRightColumnName(), |
592
|
|
|
$nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1) |
593
|
|
|
->UNNEST; |
594
|
|
|
} else { |
595
|
|
|
$select->where |
596
|
|
|
->greaterThanOrEqualTo($options->getLeftColumnName(), $nodeInfo->getLeft()) |
597
|
|
|
->AND |
598
|
|
|
->lessThanOrEqualTo($options->getRightColumnName(), $nodeInfo->getRight()); |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
$result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()), |
602
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
603
|
|
|
|
604
|
|
|
$resultArray = $result->toArray(); |
605
|
|
|
|
606
|
|
|
return (0 < count($resultArray)) ? $resultArray : array(); |
607
|
|
|
} |
608
|
|
|
} |
609
|
|
|
|