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 AdjacencyTrait |
20
|
|
|
* |
21
|
|
|
* @package O2System\Framework\Models\Sql\Traits |
22
|
|
|
*/ |
23
|
|
|
trait AdjacencyTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* AdjacencyTrait::$parentKey |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public $parentKey = 'id_parent'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* AdjacencyTrait::$adjacency |
34
|
|
|
* |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $adjacency = true; |
38
|
|
|
|
39
|
|
|
// ------------------------------------------------------------------------ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* AdjacencyTrait::getParent |
43
|
|
|
* |
44
|
|
|
* @param int $id |
45
|
|
|
* |
46
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result\Row |
47
|
|
|
*/ |
48
|
|
|
public function getParent($id) |
49
|
|
|
{ |
50
|
|
|
if ($parent = $this->qb |
51
|
|
|
->from($this->table) |
52
|
|
|
->where($this->parentKey, $id) |
53
|
|
|
->get(1)) { |
54
|
|
|
if ($parent->count() == 1) { |
55
|
|
|
return $parent; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// ------------------------------------------------------------------------ |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* AdjacencyTrait::getNumOfParent |
66
|
|
|
* |
67
|
|
|
* @param int $id |
68
|
|
|
* |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
|
|
public function getNumOfParent($id) |
72
|
|
|
{ |
73
|
|
|
if ($parents = $this->qb |
74
|
|
|
->table($this->table) |
75
|
|
|
->select('id') |
76
|
|
|
->where('id', $id) |
77
|
|
|
->get(1)) { |
78
|
|
|
return $parents->count(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return 0; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// ------------------------------------------------------------------------ |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* AdjacencyTrait::hasParent |
88
|
|
|
* |
89
|
|
|
* @param int $id |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function hasParent($id) |
94
|
|
|
{ |
95
|
|
|
if (($numParents = $this->getNumOfParent($id)) > 0) { |
|
|
|
|
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// ------------------------------------------------------------------------ |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* AdjacencyTrait::getChilds |
106
|
|
|
* |
107
|
|
|
* @param int $idParent |
108
|
|
|
* |
109
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result |
110
|
|
|
*/ |
111
|
|
|
public function getChilds($idParent) |
112
|
|
|
{ |
113
|
|
|
if ($childs = $this->qb |
114
|
|
|
->from($this->table) |
115
|
|
|
->where($this->parentKey, $idParent) |
116
|
|
|
->get()) { |
117
|
|
|
if ($childs->count() > 0) { |
118
|
|
|
return $childs; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// ------------------------------------------------------------------------ |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* AdjacencyTrait::getNumChilds |
129
|
|
|
* |
130
|
|
|
* @param int $idParent |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function getNumOfChilds($idParent) |
135
|
|
|
{ |
136
|
|
|
if ($childs = $this->getChilds($idParent)) { |
137
|
|
|
return $childs->count(); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return 0; |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// ------------------------------------------------------------------------ |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* AdjacencyTrait::hasChilds |
147
|
|
|
* |
148
|
|
|
* @param int $idParent |
149
|
|
|
* |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public function hasChilds($idParent) |
153
|
|
|
{ |
154
|
|
|
if ($numOfChilds = $this->getNumOfChilds($idParent)) { |
155
|
|
|
return (bool)($numOfChilds == 0 ? false : true); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
} |