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
|
|
|
protected 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() |
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
|
|
|
$sql = $select->getSqlString($dbAdapter->getPlatform()) . ' FOR UPDATE'; |
107
|
|
|
|
108
|
|
|
$dbAdapter->query($sql, DbAdapter::QUERY_MODE_EXECUTE); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function beginTransaction() |
112
|
|
|
{ |
113
|
|
|
$this->getDbAdapter() |
114
|
|
|
->getDriver() |
115
|
|
|
->getConnection() |
116
|
|
|
->beginTransaction(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function commitTransaction() |
120
|
|
|
{ |
121
|
|
|
$this->getDbAdapter() |
122
|
|
|
->getDriver() |
123
|
|
|
->getConnection() |
124
|
|
|
->commit(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function rollbackTransaction() |
128
|
|
|
{ |
129
|
|
|
$this->getDbAdapter() |
130
|
|
|
->getDriver() |
131
|
|
|
->getConnection() |
132
|
|
|
->rollback(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function update($nodeId, array $data) |
136
|
|
|
{ |
137
|
|
|
$options = $this->getOptions(); |
138
|
|
|
|
139
|
|
|
$dbAdapter = $this->getDbAdapter(); |
140
|
|
|
|
141
|
|
|
$data = $this->cleanData($data); |
142
|
|
|
|
143
|
|
|
$update = new Db\Sql\Update($options->getTableName()); |
144
|
|
|
$update->set($data) |
145
|
|
|
->where(array( |
146
|
|
|
$options->getIdColumnName() => $nodeId, |
147
|
|
|
)); |
148
|
|
|
|
149
|
|
|
$dbAdapter->query($update->getSqlString($dbAdapter->getPlatform()), |
150
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function insert(NodeInfo $nodeInfo, array $data) |
154
|
|
|
{ |
155
|
|
|
$options = $this->getOptions(); |
156
|
|
|
|
157
|
|
|
$dbAdapter = $this->getDbAdapter(); |
158
|
|
|
|
159
|
|
|
$data[$options->getParentIdColumnName()] = $nodeInfo->getParentId(); |
160
|
|
|
$data[$options->getLevelColumnName()] = $nodeInfo->getLevel(); |
161
|
|
|
$data[$options->getLeftColumnName()] = $nodeInfo->getLeft(); |
162
|
|
|
$data[$options->getRightColumnName()] = $nodeInfo->getRight(); |
163
|
|
|
|
164
|
|
|
if ($options->getScopeColumnName()) { |
165
|
|
|
$data[$options->getScopeColumnName()] = $nodeInfo->getScope(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$insert = new Db\Sql\Insert($options->getTableName()); |
169
|
|
|
$insert->values($data); |
170
|
|
|
$dbAdapter->query($insert->getSqlString($dbAdapter->getPlatform()), |
171
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
172
|
|
|
|
173
|
|
|
$lastGeneratedValue = $dbAdapter->getDriver() |
174
|
|
|
->getLastGeneratedValue($options->getSequenceName()); |
|
|
|
|
175
|
|
|
|
176
|
|
|
return $lastGeneratedValue; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function delete($nodeId) |
180
|
|
|
{ |
181
|
|
|
$options = $this->getOptions(); |
182
|
|
|
|
183
|
|
|
$dbAdapter = $this->getDbAdapter(); |
184
|
|
|
|
185
|
|
|
$delete = new Db\Sql\Delete($options->getTableName()); |
186
|
|
|
$delete->where |
187
|
|
|
->equalTo($options->getIdColumnName(), $nodeId); |
188
|
|
|
|
189
|
|
|
$dbAdapter->query($delete->getSqlString($dbAdapter->getPlatform()), |
190
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function moveLeftIndexes($fromIndex, $shift, $scope = null) |
194
|
|
|
{ |
195
|
|
|
$options = $this->getOptions(); |
196
|
|
|
|
197
|
|
|
if (0 == $shift) { |
198
|
|
|
return null; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$dbAdapter = $this->getDbAdapter(); |
202
|
|
|
$dbPlatform = $dbAdapter->getPlatform(); |
203
|
|
|
|
204
|
|
|
$sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName()) |
205
|
|
|
. ' SET ' |
206
|
|
|
. $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' = ' |
207
|
|
|
. $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' + :shift' |
208
|
|
|
. ' WHERE ' |
209
|
|
|
. $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' > :fromIndex'; |
210
|
|
|
|
211
|
|
|
if ($options->getScopeColumnName()) { |
212
|
|
|
$sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$binds = array( |
216
|
|
|
':shift' => $shift, |
217
|
|
|
':fromIndex' => $fromIndex, |
218
|
|
|
); |
219
|
|
|
|
220
|
|
|
$dbAdapter->query($sql) |
|
|
|
|
221
|
|
|
->execute($binds); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function moveRightIndexes($fromIndex, $shift, $scope = null) |
225
|
|
|
{ |
226
|
|
|
$options = $this->getOptions(); |
227
|
|
|
|
228
|
|
|
if (0 == $shift) { |
229
|
|
|
return null; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$dbAdapter = $this->getDbAdapter(); |
233
|
|
|
$dbPlatform = $dbAdapter->getPlatform(); |
234
|
|
|
|
235
|
|
|
$sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName()) |
236
|
|
|
. ' SET ' |
237
|
|
|
. $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' = ' |
238
|
|
|
. $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' + :shift' |
239
|
|
|
. ' WHERE ' |
240
|
|
|
. $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' > :fromIndex'; |
241
|
|
|
|
242
|
|
|
if ($options->getScopeColumnName()) { |
243
|
|
|
$sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$binds = array( |
247
|
|
|
':shift' => $shift, |
248
|
|
|
':fromIndex' => $fromIndex, |
249
|
|
|
); |
250
|
|
|
|
251
|
|
|
$dbAdapter->query($sql) |
|
|
|
|
252
|
|
|
->execute($binds); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function updateParentId($nodeId, $newParentId) |
256
|
|
|
{ |
257
|
|
|
$options = $this->getOptions(); |
258
|
|
|
|
259
|
|
|
$dbAdapter = $this->getDbAdapter(); |
260
|
|
|
|
261
|
|
|
$update = new Db\Sql\Update($options->getTableName()); |
262
|
|
|
$update->set(array( |
263
|
|
|
$options->getParentIdColumnName() => $newParentId, |
264
|
|
|
)) |
265
|
|
|
->where(array( |
266
|
|
|
$options->getIdColumnName() => $nodeId, |
267
|
|
|
)); |
268
|
|
|
|
269
|
|
|
$dbAdapter->query($update->getSqlString($dbAdapter->getPlatform()), |
270
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
public function updateLevels($leftIndexFrom, $rightIndexTo, $shift, $scope = null) |
274
|
|
|
{ |
275
|
|
|
$options = $this->getOptions(); |
276
|
|
|
|
277
|
|
|
if (0 == $shift) { |
278
|
|
|
return null; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$dbAdapter = $this->getDbAdapter(); |
282
|
|
|
$dbPlatform = $dbAdapter->getPlatform(); |
283
|
|
|
|
284
|
|
|
$sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName()) |
285
|
|
|
. ' SET ' |
286
|
|
|
. $dbPlatform->quoteIdentifier($options->getLevelColumnName()) . ' = ' |
287
|
|
|
. $dbPlatform->quoteIdentifier($options->getLevelColumnName()) . ' + :shift' |
288
|
|
|
. ' WHERE ' |
289
|
|
|
. $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' >= :leftFrom' |
290
|
|
|
. ' AND ' . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' <= :rightTo'; |
291
|
|
|
|
292
|
|
|
if ($options->getScopeColumnName()) { |
293
|
|
|
$sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
$binds = array( |
297
|
|
|
':shift' => $shift, |
298
|
|
|
':leftFrom' => $leftIndexFrom, |
299
|
|
|
':rightTo' => $rightIndexTo, |
300
|
|
|
); |
301
|
|
|
|
302
|
|
|
$dbAdapter->query($sql) |
|
|
|
|
303
|
|
|
->execute($binds); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function moveBranch($leftIndexFrom, $rightIndexTo, $shift, $scope = null) |
307
|
|
|
{ |
308
|
|
|
if (0 == $shift) { |
309
|
|
|
return null; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
$options = $this->getOptions(); |
313
|
|
|
|
314
|
|
|
$dbAdapter = $this->getDbAdapter(); |
315
|
|
|
$dbPlatform = $dbAdapter->getPlatform(); |
316
|
|
|
|
317
|
|
|
$sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName()) |
318
|
|
|
. ' SET ' |
319
|
|
|
. $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' = ' |
320
|
|
|
. $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' + :shift, ' |
321
|
|
|
. $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' = ' |
322
|
|
|
. $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' + :shift' |
323
|
|
|
. ' WHERE ' |
324
|
|
|
. $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' >= :leftFrom' |
325
|
|
|
. ' AND ' . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' <= :rightTo'; |
326
|
|
|
|
327
|
|
|
if ($options->getScopeColumnName()) { |
328
|
|
|
$sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
$binds = array( |
332
|
|
|
':shift' => $shift, |
333
|
|
|
':leftFrom' => $leftIndexFrom, |
334
|
|
|
':rightTo' => $rightIndexTo, |
335
|
|
|
); |
336
|
|
|
|
337
|
|
|
$dbAdapter->query($sql) |
|
|
|
|
338
|
|
|
->execute($binds); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
public function getRoots($scope = null) |
342
|
|
|
{ |
343
|
|
|
$options = $this->getOptions(); |
344
|
|
|
|
345
|
|
|
$dbAdapter = $this->getDbAdapter(); |
346
|
|
|
|
347
|
|
|
$select = $this->getBlankDbSelect(); |
348
|
|
|
$select->where |
349
|
|
|
->isNull($options->getParentIdColumnName()); |
350
|
|
|
$select->order($options->getIdColumnName()); |
351
|
|
|
|
352
|
|
|
if (null != $scope && $options->getScopeColumnName()) { |
353
|
|
|
$select->where |
354
|
|
|
->equalTo($options->getScopeColumnName(), $scope); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
$result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()), |
358
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
359
|
|
|
|
360
|
|
|
return $result->toArray(); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
public function getRoot($scope = null) |
364
|
|
|
{ |
365
|
|
|
$roots = $this->getRoots($scope); |
366
|
|
|
|
367
|
|
|
return (0 < count($roots)) ? $roots[0] : array(); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
public function getNode($nodeId) |
371
|
|
|
{ |
372
|
|
|
$options = $this->getOptions(); |
373
|
|
|
|
374
|
|
|
$nodeId = (int) $nodeId; |
375
|
|
|
|
376
|
|
|
$dbAdapter = $this->getDbAdapter(); |
377
|
|
|
|
378
|
|
|
$select = $this->getDefaultDbSelect() |
379
|
|
|
->where(array($options->getIdColumnName() => $nodeId)); |
380
|
|
|
|
381
|
|
|
$result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()), |
382
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
383
|
|
|
|
384
|
|
|
$array = $result->toArray(); |
385
|
|
|
|
386
|
|
|
return (0 < count($array)) ? $array[0] : null; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @param array $data |
391
|
|
|
* @return NodeInfo |
392
|
|
|
*/ |
393
|
|
|
private function _buildNodeInfoObject(array $data) |
394
|
|
|
{ |
395
|
|
|
$options = $this->getOptions(); |
396
|
|
|
|
397
|
|
|
$id = $data[$options->getIdColumnName()]; |
398
|
|
|
$parentId = $data[$options->getParentIdColumnName()]; |
399
|
|
|
$level = $data[$options->getLevelColumnName()]; |
400
|
|
|
$left = $data[$options->getLeftColumnName()]; |
401
|
|
|
$right = $data[$options->getRightColumnName()]; |
402
|
|
|
|
403
|
|
|
if (isset($data[$options->getScopeColumnName()])) { |
404
|
|
|
$scope = $data[$options->getScopeColumnName()]; |
405
|
|
|
} else { |
406
|
|
|
$scope = null; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
return new NodeInfo($id, $parentId, $level, $left, $right, $scope); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
public function getNodeInfo($nodeId) |
413
|
|
|
{ |
414
|
|
|
$options = $this->getOptions(); |
415
|
|
|
|
416
|
|
|
$nodeId = (int) $nodeId; |
417
|
|
|
|
418
|
|
|
$dbAdapter = $this->getDbAdapter(); |
419
|
|
|
|
420
|
|
|
$select = $this->getBlankDbSelect() |
421
|
|
|
->where(array($options->getIdColumnName() => $nodeId)); |
422
|
|
|
|
423
|
|
|
$result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()), |
424
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
425
|
|
|
|
426
|
|
|
$array = $result->toArray(); |
427
|
|
|
|
428
|
|
|
$result = ($array) ? $this->_buildNodeInfoObject($array[0]) : null; |
429
|
|
|
|
430
|
|
|
return $result; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
public function getChildrenNodeInfo($parentNodeId) |
434
|
|
|
{ |
435
|
|
|
$dbAdapter = $this->getDbAdapter(); |
436
|
|
|
$options = $this->getOptions(); |
437
|
|
|
|
438
|
|
|
$columns = array( |
439
|
|
|
$options->getIdColumnName(), |
440
|
|
|
$options->getLeftColumnName(), |
441
|
|
|
$options->getRightColumnName(), |
442
|
|
|
$options->getParentIdColumnName(), |
443
|
|
|
$options->getLevelColumnName(), |
444
|
|
|
); |
445
|
|
|
|
446
|
|
|
if ($options->getScopeColumnName()) { |
447
|
|
|
$columns[] = $options->getScopeColumnName(); |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
$select = $this->getBlankDbSelect(); |
451
|
|
|
$select->columns($columns); |
452
|
|
|
$select->order($options->getLeftColumnName()); |
453
|
|
|
$select->where(array( |
454
|
|
|
$options->getParentIdColumnName() => $parentNodeId |
455
|
|
|
)); |
456
|
|
|
|
457
|
|
|
$data = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()), |
458
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
459
|
|
|
|
460
|
|
|
$result = array(); |
461
|
|
|
|
462
|
|
|
foreach ($data->toArray() as $nodeData) { |
463
|
|
|
$result[] = $this->_buildNodeInfoObject($nodeData); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
return $result; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
public function updateNodeMetadata(NodeInfo $nodeInfo) |
470
|
|
|
{ |
471
|
|
|
$dbAdapter = $this->getDbAdapter(); |
472
|
|
|
$options = $this->getOptions(); |
473
|
|
|
|
474
|
|
|
$update = new Db\Sql\Update($options->getTableName()); |
475
|
|
|
|
476
|
|
|
$update->set(array( |
477
|
|
|
$options->getRightColumnName() => $nodeInfo->getRight(), |
478
|
|
|
$options->getLeftColumnName() => $nodeInfo->getLeft(), |
479
|
|
|
$options->getLevelColumnName() => $nodeInfo->getLevel(), |
480
|
|
|
)); |
481
|
|
|
|
482
|
|
|
$update->where(array( |
483
|
|
|
$options->getIdColumnName() => $nodeInfo->getId(), |
484
|
|
|
)); |
485
|
|
|
|
486
|
|
|
$dbAdapter->query($update->getSqlString($dbAdapter->getPlatform()), |
487
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
public function getPath($nodeId, $startLevel = 0, $excludeLastNode = false) |
491
|
|
|
{ |
492
|
|
|
$options = $this->getOptions(); |
493
|
|
|
|
494
|
|
|
$startLevel = (int) $startLevel; |
495
|
|
|
|
496
|
|
|
// node does not exist |
497
|
|
|
$nodeInfo = $this->getNodeInfo($nodeId); |
498
|
|
|
if (!$nodeInfo) { |
499
|
|
|
return array(); |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
$dbAdapter = $this->getDbAdapter(); |
503
|
|
|
|
504
|
|
|
$select = $this->getDefaultDbSelect(); |
505
|
|
|
|
506
|
|
|
if ($options->getScopeColumnName()) { |
507
|
|
|
$select->where |
508
|
|
|
->equalTo($options->getScopeColumnName(), $nodeInfo->getScope()); |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
$select->where |
512
|
|
|
->lessThanOrEqualTo($options->getLeftColumnName(), $nodeInfo->getLeft()) |
513
|
|
|
->AND |
514
|
|
|
->greaterThanOrEqualTo($options->getRightColumnName(), $nodeInfo->getRight()); |
515
|
|
|
|
516
|
|
|
$select->order($options->getLeftColumnName() . ' ASC'); |
517
|
|
|
|
518
|
|
|
if (0 < $startLevel) { |
519
|
|
|
$select->where |
520
|
|
|
->greaterThanOrEqualTo($options->getLevelColumnName(), $startLevel); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
if (true == $excludeLastNode) { |
|
|
|
|
524
|
|
|
$select->where |
525
|
|
|
->lessThan($options->getLevelColumnName(), $nodeInfo->getLevel()); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
$result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()), |
529
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
530
|
|
|
|
531
|
|
|
return $result->toArray(); |
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
public function getDescendants($nodeId = 1, $startLevel = 0, $levels = null, $excludeBranch = null) |
535
|
|
|
{ |
536
|
|
|
$options = $this->getOptions(); |
537
|
|
|
|
538
|
|
|
if (!$nodeInfo = $this->getNodeInfo($nodeId)) { |
539
|
|
|
return array(); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
$dbAdapter = $this->getDbAdapter(); |
543
|
|
|
$select = $this->getDefaultDbSelect(); |
544
|
|
|
$select->order($options->getLeftColumnName() . ' ASC'); |
545
|
|
|
|
546
|
|
|
if ($options->getScopeColumnName()) { |
547
|
|
|
$select->where |
548
|
|
|
->equalTo($options->getScopeColumnName(), $nodeInfo->getScope()); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
if (0 != $startLevel) { |
552
|
|
|
$level = $nodeInfo->getLevel() + (int) $startLevel; |
553
|
|
|
$select->where |
554
|
|
|
->greaterThanOrEqualTo($options->getLevelColumnName(), $level); |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
if (null != $levels) { |
|
|
|
|
558
|
|
|
$endLevel = $nodeInfo->getLevel() + (int) $startLevel + abs($levels); |
559
|
|
|
$select->where |
560
|
|
|
->lessThan($options->getLevelColumnName(), $endLevel); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
if (null != $excludeBranch && null != ($excludeNodeInfo = $this->getNodeInfo($excludeBranch))) { |
|
|
|
|
564
|
|
|
$select->where |
565
|
|
|
->NEST |
566
|
|
|
->between($options->getLeftColumnName(), |
567
|
|
|
$nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1) |
568
|
|
|
->OR |
569
|
|
|
->between($options->getLeftColumnName(), |
570
|
|
|
$excludeNodeInfo->getRight() + 1, $nodeInfo->getRight()) |
571
|
|
|
->UNNEST |
572
|
|
|
->AND |
573
|
|
|
->NEST |
574
|
|
|
->between($options->getRightColumnName(), |
575
|
|
|
$excludeNodeInfo->getRight() + 1, $nodeInfo->getRight()) |
576
|
|
|
->OR |
577
|
|
|
->between($options->getRightColumnName(), |
578
|
|
|
$nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1) |
579
|
|
|
->UNNEST; |
580
|
|
|
} else { |
581
|
|
|
$select->where |
582
|
|
|
->greaterThanOrEqualTo($options->getLeftColumnName(), $nodeInfo->getLeft()) |
583
|
|
|
->AND |
584
|
|
|
->lessThanOrEqualTo($options->getRightColumnName(), $nodeInfo->getRight()); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
$result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()), |
588
|
|
|
DbAdapter::QUERY_MODE_EXECUTE); |
589
|
|
|
|
590
|
|
|
$resultArray = $result->toArray(); |
591
|
|
|
|
592
|
|
|
return (0 < count($resultArray)) ? $resultArray : array(); |
593
|
|
|
} |
594
|
|
|
} |
595
|
|
|
|