1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System PHP 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
|
|
|
* Rebuild Tree |
43
|
|
|
* |
44
|
|
|
* Rebuild self hierarchical table |
45
|
|
|
* |
46
|
|
|
* @access public |
47
|
|
|
* |
48
|
|
|
* @param string $table Working database table |
49
|
|
|
* |
50
|
|
|
* @return int Right column value |
51
|
|
|
*/ |
52
|
|
|
protected 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 ($result = $this->qb |
58
|
|
|
->table($this->table) |
59
|
|
|
->select('id') |
60
|
|
|
->where('id_parent', $idParent) |
61
|
|
|
->orderBy('id') |
62
|
|
|
->get()) { |
63
|
|
|
|
64
|
|
|
$right = $left + 1; |
65
|
|
|
|
66
|
|
|
$i = 0; |
67
|
|
|
foreach ($result as $row) { |
68
|
|
|
if ($i == 0) { |
69
|
|
|
$this->qb |
70
|
|
|
->from($this->table) |
71
|
|
|
->where('id', $row->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', $row->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' ] = $row->id; |
89
|
|
|
|
90
|
|
|
if ($this->hasChilds($row->id)) { |
91
|
|
|
$right = $this->rebuildTree($row->id, $right, $depth + 1); |
92
|
|
|
$this->qb |
93
|
|
|
->from($this->table) |
94
|
|
|
->where('id', $row->id) |
95
|
|
|
->update($update = [ |
96
|
|
|
'record_right' => $right, |
97
|
|
|
]); |
98
|
|
|
$update[ 'id' ] = $row->id; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$i++; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $right + 1; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// ------------------------------------------------------------------------ |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Has Childs |
112
|
|
|
* |
113
|
|
|
* Check if there is a child rows |
114
|
|
|
* |
115
|
|
|
* @param string $table Working database table |
116
|
|
|
* |
117
|
|
|
* @access public |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
protected function hasChilds($idParent) |
121
|
|
|
{ |
122
|
|
|
if ($result = $this->qb |
123
|
|
|
->table($this->table) |
124
|
|
|
->select('id') |
125
|
|
|
->where('id_parent', $idParent) |
126
|
|
|
->get()) { |
127
|
|
|
return (bool)($result->count() == 0 ? false : true); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// ------------------------------------------------------------------------ |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Find Parents |
137
|
|
|
* |
138
|
|
|
* Retreive parents of a record |
139
|
|
|
* |
140
|
|
|
* @param numeric $id Record ID |
|
|
|
|
141
|
|
|
* |
142
|
|
|
* @access public |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
protected function getParents($id, &$parents = []) |
146
|
|
|
{ |
147
|
|
|
if ($result = $this->qb |
148
|
|
|
->from($this->table) |
149
|
|
|
->whereIn('id', $this->qb |
150
|
|
|
->subQuery() |
151
|
|
|
->from($this->table) |
152
|
|
|
->select('id_parent') |
153
|
|
|
->where('id', $id) |
154
|
|
|
) |
155
|
|
|
->get()) { |
156
|
|
|
if ($result->count()) { |
157
|
|
|
$parents[] = $row = $result->first(); |
158
|
|
|
|
159
|
|
|
if ($this->hasParent($row->id_parent)) { |
160
|
|
|
$this->getParents($row->id, $parents); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return array_reverse($parents); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
protected function hasParent($idParent) |
169
|
|
|
{ |
170
|
|
|
if ($result = $this->qb |
171
|
|
|
->table($this->table) |
172
|
|
|
->select('id') |
173
|
|
|
->where('id', $idParent) |
174
|
|
|
->get()) { |
175
|
|
|
return (bool)($result->count() == 0 ? false : true); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return false; |
179
|
|
|
} |
180
|
|
|
// ------------------------------------------------------------------------ |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Find Childs |
184
|
|
|
* |
185
|
|
|
* Retreive all childs |
186
|
|
|
* |
187
|
|
|
* @param numeric $idParent Parent ID |
188
|
|
|
* |
189
|
|
|
* @access public |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
protected function getChilds($idParent) |
193
|
|
|
{ |
194
|
|
|
$childs = []; |
195
|
|
|
|
196
|
|
|
if($result = $this->qb |
197
|
|
|
->table($this->table) |
198
|
|
|
->where('id_parent', $idParent) |
199
|
|
|
->get()) { |
200
|
|
|
foreach ($result as $row) { |
201
|
|
|
$childs[] = $row; |
202
|
|
|
if ($this->hasChild($row->id)) { |
|
|
|
|
203
|
|
|
$row->childs = $this->getChilds($row->id); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $childs; |
209
|
|
|
} |
210
|
|
|
// ------------------------------------------------------------------------ |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Count Childs |
214
|
|
|
* |
215
|
|
|
* Num childs of a record |
216
|
|
|
* |
217
|
|
|
* @param numeric $idParent Record Parent ID |
218
|
|
|
* |
219
|
|
|
* @access public |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
|
|
protected function getNumChilds($idParent) |
223
|
|
|
{ |
224
|
|
|
if($result = $this->qb |
225
|
|
|
->table($this->table) |
226
|
|
|
->select('id') |
227
|
|
|
->where('id_parent', $idParent) |
228
|
|
|
->get()) { |
229
|
|
|
return $result->count(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return 0; |
|
|
|
|
233
|
|
|
} |
234
|
|
|
} |