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