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