|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Framework\Models\Sql\Traits; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class HierarchicalTrait |
|
20
|
|
|
* |
|
21
|
|
|
* @package O2System\Framework\Models\Sql\Traits |
|
22
|
|
|
*/ |
|
23
|
|
|
trait HierarchicalTrait |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Parent Key Field |
|
27
|
|
|
* |
|
28
|
|
|
* @access public |
|
29
|
|
|
* @type string |
|
30
|
|
|
*/ |
|
31
|
|
|
public $parentKey = 'id_parent'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Hierarchical Enabled Flag |
|
35
|
|
|
* |
|
36
|
|
|
* @access protected |
|
37
|
|
|
* @type bool |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $hierarchical = true; |
|
40
|
|
|
|
|
41
|
|
|
// ------------------------------------------------------------------------ |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* HierarchicalTrait::rebuildTree |
|
45
|
|
|
* |
|
46
|
|
|
* @param int $idParent |
|
47
|
|
|
* @param int $left |
|
48
|
|
|
* @param int $depth |
|
49
|
|
|
* |
|
50
|
|
|
* @return int |
|
51
|
|
|
*/ |
|
52
|
|
|
public function rebuildTree($idParent = 0, $left = 1, $depth = 0) |
|
53
|
|
|
{ |
|
54
|
|
|
ini_set('xdebug.max_nesting_level', 10000); |
|
55
|
|
|
ini_set('memory_limit', '-1'); |
|
56
|
|
|
|
|
57
|
|
|
if ($childs = $this->qb |
|
58
|
|
|
->table($this->table) |
|
59
|
|
|
->select('id') |
|
60
|
|
|
->where($this->parentKey, $idParent) |
|
61
|
|
|
->orderBy('id') |
|
62
|
|
|
->get()) { |
|
63
|
|
|
|
|
64
|
|
|
$right = $left + 1; |
|
65
|
|
|
|
|
66
|
|
|
$i = 0; |
|
67
|
|
|
foreach ($childs as $child) { |
|
68
|
|
|
if ($i == 0) { |
|
69
|
|
|
$this->qb |
|
70
|
|
|
->from($this->table) |
|
71
|
|
|
->where('id', $child->id) |
|
72
|
|
|
->update($update = [ |
|
73
|
|
|
'record_left' => $left, |
|
74
|
|
|
'record_right' => $right, |
|
75
|
|
|
'record_depth' => $depth, |
|
76
|
|
|
]); |
|
77
|
|
|
} else { |
|
78
|
|
|
$this->qb |
|
79
|
|
|
->from($this->table) |
|
80
|
|
|
->where('id', $child->id) |
|
81
|
|
|
->update($update = [ |
|
82
|
|
|
'record_left' => $left = $right + 1, |
|
83
|
|
|
'record_right' => $right = $left + 1, |
|
84
|
|
|
'record_depth' => $depth, |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$update[ 'id' ] = $child->id; |
|
89
|
|
|
|
|
90
|
|
|
if ($this->qb |
|
91
|
|
|
->table($this->table) |
|
92
|
|
|
->select('id') |
|
93
|
|
|
->where($this->parentKey, $child->id) |
|
94
|
|
|
->orderBy('id') |
|
95
|
|
|
->get()) { |
|
96
|
|
|
$right = $this->rebuildTree($child->id, $right, $depth + 1); |
|
97
|
|
|
$this->qb |
|
98
|
|
|
->from($this->table) |
|
99
|
|
|
->where('id', $child->id) |
|
100
|
|
|
->update($update = [ |
|
101
|
|
|
'record_right' => $right, |
|
102
|
|
|
]); |
|
103
|
|
|
$update[ 'id' ] = $child->id; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$i++; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $right + 1; |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// ------------------------------------------------------------------------ |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* HierarchicalTrait::getParents |
|
117
|
|
|
* |
|
118
|
|
|
* @param int $id |
|
119
|
|
|
* @param string $ordering ASC|DESC |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getParents($id, $ordering = 'ASC') |
|
124
|
|
|
{ |
|
125
|
|
|
if ($parents = $this->qb |
|
126
|
|
|
->select($this->table . '.*') |
|
127
|
|
|
->from($this->table) |
|
128
|
|
|
->from($this->table . ' AS node') |
|
129
|
|
|
->whereBetween('node.record_left', [$this->table . '.record_left', $this->table . '.record_right']) |
|
130
|
|
|
->where([ |
|
131
|
|
|
'node.id' => $id, |
|
132
|
|
|
]) |
|
133
|
|
|
->orderBy($this->table . '.record_left', $ordering) |
|
134
|
|
|
->get()) { |
|
135
|
|
|
if ($parents->count()) { |
|
136
|
|
|
return $parents; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return false; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// ------------------------------------------------------------------------ |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* HierarchicalTrait::getParent |
|
147
|
|
|
* |
|
148
|
|
|
* @param int $idParent |
|
149
|
|
|
* |
|
150
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result\Row |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getParent($idParent) |
|
153
|
|
|
{ |
|
154
|
|
|
if ($parent = $this->qb |
|
155
|
|
|
->select($this->table . '.*') |
|
156
|
|
|
->from($this->table) |
|
157
|
|
|
->where($this->primaryKey, $idParent) |
|
158
|
|
|
->get(1)) { |
|
159
|
|
|
if ($parent->count() == 1) { |
|
160
|
|
|
return $parent->first(); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return false; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
// ------------------------------------------------------------------------ |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* HierarchicalTrait::getNumOfParent |
|
171
|
|
|
* |
|
172
|
|
|
* @param int $idParent |
|
173
|
|
|
* |
|
174
|
|
|
* @return int |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getNumOfParent($id) |
|
177
|
|
|
{ |
|
178
|
|
|
if ($parents = $this->qb |
|
179
|
|
|
->table($this->table) |
|
180
|
|
|
->select('id') |
|
181
|
|
|
->where('id', $id) |
|
182
|
|
|
->get()) { |
|
183
|
|
|
return $parents->count(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return 0; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
// ------------------------------------------------------------------------ |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* HierarchicalTrait::hasParent |
|
193
|
|
|
* |
|
194
|
|
|
* @param int $id |
|
195
|
|
|
* |
|
196
|
|
|
* @return bool |
|
197
|
|
|
*/ |
|
198
|
|
|
public function hasParent($id, $direct = true) |
|
|
|
|
|
|
199
|
|
|
{ |
|
200
|
|
|
if ($numOfParents = $this->getNumOfParent($idParent, $direct)) { |
|
|
|
|
|
|
201
|
|
|
return (bool)($numOfParents == 0 ? false : true); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return false; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
// ------------------------------------------------------------------------ |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* HierarchicalTrait::getNumOfParents |
|
211
|
|
|
* |
|
212
|
|
|
* @param int $id |
|
213
|
|
|
* |
|
214
|
|
|
* @return int |
|
215
|
|
|
*/ |
|
216
|
|
|
public function getNumOfParents($id) |
|
217
|
|
|
{ |
|
218
|
|
|
if($parents = $this->getParents($id)) { |
|
219
|
|
|
return $parents->count(); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
return 0; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
// ------------------------------------------------------------------------ |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* HierarchicalTrait::hasParents |
|
229
|
|
|
* |
|
230
|
|
|
* @param int $id |
|
231
|
|
|
* |
|
232
|
|
|
* @return bool |
|
233
|
|
|
*/ |
|
234
|
|
|
public function hasParents($id) |
|
235
|
|
|
{ |
|
236
|
|
|
if ($numOfParents = $this->getNumOfParents($id)) { |
|
237
|
|
|
return (bool)($numOfParents == 0 ? false : true); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return false; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
// ------------------------------------------------------------------------ |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* HierarchicalTrait::getChilds |
|
247
|
|
|
* |
|
248
|
|
|
* @param int $idParent |
|
249
|
|
|
* @param string $ordering ASC|DESC |
|
250
|
|
|
* |
|
251
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result |
|
252
|
|
|
*/ |
|
253
|
|
|
public function getChilds($idParent, $ordering = 'ASC') |
|
|
|
|
|
|
254
|
|
|
{ |
|
255
|
|
|
if ($childs = $this->qb |
|
256
|
|
|
->select($this->table . '.*') |
|
257
|
|
|
->from($this->table) |
|
258
|
|
|
->from($this->table . ' AS node') |
|
259
|
|
|
->whereBetween('node.record_left', [$this->table . '.record_left', $this->table . '.record_right']) |
|
260
|
|
|
->where([ |
|
261
|
|
|
$this->table . '.id' => $id, |
|
|
|
|
|
|
262
|
|
|
]) |
|
263
|
|
|
->get()) { |
|
264
|
|
|
if ($childs->count()) { |
|
265
|
|
|
return $childs; |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
return false; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
// ------------------------------------------------------------------------ |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* HierarchicalTrait::getNumOfChilds |
|
276
|
|
|
* |
|
277
|
|
|
* @param int $idParent |
|
278
|
|
|
* @param bool $direct |
|
279
|
|
|
* |
|
280
|
|
|
* @return int |
|
281
|
|
|
*/ |
|
282
|
|
|
public function getNumOfChilds($idParent, $direct = true) |
|
|
|
|
|
|
283
|
|
|
{ |
|
284
|
|
|
if($childs = $this->getChilds($idParent)) { |
|
285
|
|
|
return $childs->count(); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
return 0; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
// ------------------------------------------------------------------------ |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* HierarchicalTrait::hasChilds |
|
295
|
|
|
* |
|
296
|
|
|
* @param int $idParent |
|
297
|
|
|
* |
|
298
|
|
|
* @return bool |
|
299
|
|
|
*/ |
|
300
|
|
|
public function hasChilds($idParent) |
|
301
|
|
|
{ |
|
302
|
|
|
if ($numOfChilds = $this->getNumOfChilds($idParent)) { |
|
303
|
|
|
return (bool)($numOfChilds == 0 ? false : true); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
return false; |
|
307
|
|
|
} |
|
308
|
|
|
} |