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->hasChilds($child->id)) { |
91
|
|
|
$right = $this->rebuildTree($child->id, $right, $depth + 1); |
92
|
|
|
$this->qb |
93
|
|
|
->from($this->table) |
94
|
|
|
->where('id', $child->id) |
95
|
|
|
->update($update = [ |
96
|
|
|
'record_right' => $right, |
97
|
|
|
]); |
98
|
|
|
$update[ 'id' ] = $child->id; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$i++; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $right + 1; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// ------------------------------------------------------------------------ |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* HierarchicalTrait::getParents |
112
|
|
|
* |
113
|
|
|
* @param int $id |
114
|
|
|
* @param string $ordering ASC|DESC |
115
|
|
|
* |
116
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result |
117
|
|
|
*/ |
118
|
|
|
public function getParents($id, $ordering = 'ASC') |
119
|
|
|
{ |
120
|
|
|
if ($parents = $this->qb |
121
|
|
|
->select($this->table . '.*') |
122
|
|
|
->from($this->table) |
123
|
|
|
->from($this->table . ' AS node') |
124
|
|
|
->whereBetween('node.record_left', [$this->table . '.record_left', $this->table . '.record_right']) |
125
|
|
|
->where([ |
126
|
|
|
'node.id' => $id, |
127
|
|
|
]) |
128
|
|
|
->orderBy($this->table . '.record_left', $ordering) |
129
|
|
|
->get()) { |
130
|
|
|
if ($parents->count()) { |
131
|
|
|
return $parents; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// ------------------------------------------------------------------------ |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* HierarchicalTrait::getParent |
142
|
|
|
* |
143
|
|
|
* @param int $idParent |
144
|
|
|
* |
145
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result\Row |
146
|
|
|
*/ |
147
|
|
|
public function getParent($idParent) |
148
|
|
|
{ |
149
|
|
|
if ($parent = $this->qb |
150
|
|
|
->select($this->table . '.*') |
151
|
|
|
->from($this->table) |
152
|
|
|
->where($this->primaryKey, $idParent) |
153
|
|
|
->get(1)) { |
154
|
|
|
if ($parent->count() == 1) { |
155
|
|
|
return $parent->first(); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// ------------------------------------------------------------------------ |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* HierarchicalTrait::getNumOfParent |
166
|
|
|
* |
167
|
|
|
* @param int $idParent |
168
|
|
|
* |
169
|
|
|
* @return int |
170
|
|
|
*/ |
171
|
|
|
public function getNumOfParent($id) |
172
|
|
|
{ |
173
|
|
|
if ($parents = $this->qb |
174
|
|
|
->table($this->table) |
175
|
|
|
->select('id') |
176
|
|
|
->where('id', $id) |
177
|
|
|
->get()) { |
178
|
|
|
return $parents->count(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return 0; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// ------------------------------------------------------------------------ |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* HierarchicalTrait::hasParent |
188
|
|
|
* |
189
|
|
|
* @param int $id |
190
|
|
|
* |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function hasParent($id, $direct = true) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
if ($numOfParents = $this->getNumOfParent($idParent, $direct)) { |
|
|
|
|
196
|
|
|
return (bool)($numOfParents == 0 ? false : true); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return false; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
// ------------------------------------------------------------------------ |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* HierarchicalTrait::getNumOfParents |
206
|
|
|
* |
207
|
|
|
* @param int $id |
208
|
|
|
* |
209
|
|
|
* @return int |
210
|
|
|
*/ |
211
|
|
|
public function getNumOfParents($id) |
212
|
|
|
{ |
213
|
|
|
if($parents = $this->getParents($id)) { |
214
|
|
|
return $parents->count(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return 0; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// ------------------------------------------------------------------------ |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* HierarchicalTrait::hasParents |
224
|
|
|
* |
225
|
|
|
* @param int $id |
226
|
|
|
* |
227
|
|
|
* @return bool |
228
|
|
|
*/ |
229
|
|
|
public function hasParents($id) |
230
|
|
|
{ |
231
|
|
|
if ($numOfParents = $this->getNumOfParents($id)) { |
232
|
|
|
return (bool)($numOfParents == 0 ? false : true); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return false; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
// ------------------------------------------------------------------------ |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* HierarchicalTrait::getChilds |
242
|
|
|
* |
243
|
|
|
* @param int $idParent |
244
|
|
|
* @param string $ordering ASC|DESC |
245
|
|
|
* |
246
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result |
247
|
|
|
*/ |
248
|
|
|
public function getChilds($idParent, $ordering = 'ASC') |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
if ($childs = $this->qb |
251
|
|
|
->select($this->table . '.*') |
252
|
|
|
->from($this->table) |
253
|
|
|
->from($this->table . ' AS node') |
254
|
|
|
->whereBetween('node.record_left', [$this->table . '.record_left', $this->table . '.record_right']) |
255
|
|
|
->where([ |
256
|
|
|
$this->table . '.id' => $id, |
|
|
|
|
257
|
|
|
]) |
258
|
|
|
->get()) { |
259
|
|
|
if ($childs->count()) { |
260
|
|
|
return $childs; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return false; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// ------------------------------------------------------------------------ |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* HierarchicalTrait::getNumOfChilds |
271
|
|
|
* |
272
|
|
|
* @param int $idParent |
273
|
|
|
* @param bool $direct |
274
|
|
|
* |
275
|
|
|
* @return int |
276
|
|
|
*/ |
277
|
|
|
public function getNumOfChilds($idParent, $direct = true) |
|
|
|
|
278
|
|
|
{ |
279
|
|
|
if($childs = $this->getChilds($idParent)) { |
280
|
|
|
return $childs->count(); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return 0; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
// ------------------------------------------------------------------------ |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* HierarchicalTrait::hasChilds |
290
|
|
|
* |
291
|
|
|
* @param int $idParent |
292
|
|
|
* |
293
|
|
|
* @return bool |
294
|
|
|
*/ |
295
|
|
|
public function hasChilds($idParent) |
296
|
|
|
{ |
297
|
|
|
if ($numOfChilds = $this->getNumOfChilds($idParent)) { |
298
|
|
|
return (bool)($numOfChilds == 0 ? false : true); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return false; |
302
|
|
|
} |
303
|
|
|
} |