1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Entitizer\Utils { |
4
|
|
|
|
5
|
|
|
use Modules\Entitizer, DB; |
6
|
|
|
|
7
|
|
|
abstract class Listview extends Collection { |
8
|
|
|
|
9
|
|
|
# Get default select query |
10
|
|
|
|
11
|
|
|
private function getDefaultSelectQuery(array $config, array $order_by, int $index, int $display) { |
12
|
|
|
|
13
|
|
|
return ("SELECT SQL_CALC_FOUND_ROWS " . $this->getSelection() . " ") . |
14
|
|
|
|
15
|
|
|
("FROM " . static::$table . " ent ") . |
16
|
|
|
|
17
|
|
|
(('' !== ($condition = $this->getCondition($config))) ? ("WHERE " . $condition . " ") : "") . |
18
|
|
|
|
19
|
|
|
("ORDER BY " . $this->getOrderBy($order_by) . " ") . |
20
|
|
|
|
21
|
|
|
(($index > 0) ? ("LIMIT " . ((($index - 1) * $display) . ", " . $display)) : ""); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
# Get nesting select query |
25
|
|
|
|
26
|
|
|
private function getNestingSelectQuery(int $parent_id, array $config, array $order_by, int $index, int $display) { |
27
|
|
|
|
28
|
|
|
return ("SELECT SQL_CALC_FOUND_ROWS " . $this->getSelection() . ", COUNT(chd.descendant) as children ") . |
29
|
|
|
|
30
|
|
|
("FROM " . static::$table . " ent ") . |
31
|
|
|
|
32
|
|
|
("LEFT JOIN " . static::$table_relations . " chd ON chd.ancestor = ent.id AND chd.depth = 1 ") . |
33
|
|
|
|
34
|
|
|
("LEFT JOIN " . static::$table_relations . " rel ON rel.descendant = ent.id AND rel.depth = 1 ") . |
35
|
|
|
|
36
|
|
|
("WHERE COALESCE(rel.ancestor, 0) = " . $parent_id . " ") . |
37
|
|
|
|
38
|
|
|
(('' !== ($condition = $this->getCondition($config))) ? ("AND " . $condition . " ") : "") . |
39
|
|
|
|
40
|
|
|
("GROUP BY ent.id ORDER BY " . $this->getOrderBy($order_by) . " ") . |
41
|
|
|
|
42
|
|
|
(($index > 0) ? ("LIMIT " . ((($index - 1) * $display) . ", " . $display)) : ""); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
# Get default count query |
46
|
|
|
|
47
|
|
|
private function getDefaultCountQuery(array $config) { |
48
|
|
|
|
49
|
|
|
return ("SELECT COUNT(ent.id) as count FROM " . static::$table . " ent ") . |
50
|
|
|
|
51
|
|
|
(('' !== ($condition = $this->getCondition($config))) ? ("WHERE " . $condition) : ""); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
# Get nesting count query |
55
|
|
|
|
56
|
|
|
private function getNestingCountQuery(int $parent_id, array $config) { |
57
|
|
|
|
58
|
|
|
return ("SELECT COUNT(ent.id) as count FROM " . static::$table . " ent ") . |
59
|
|
|
|
60
|
|
|
("LEFT JOIN " . static::$table_relations . " rel ON rel.descendant = ent.id AND rel.depth = 1 ") . |
61
|
|
|
|
62
|
|
|
("WHERE COALESCE(rel.ancestor, 0) = " . $parent_id . " ") . |
63
|
|
|
|
64
|
|
|
(('' !== ($condition = $this->getCondition($config))) ? ("AND " . $condition) : ""); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
# Select entries from DB |
68
|
|
|
|
69
|
|
|
private function select(int $parent_id = null, array $config = [], array $order_by = [], int $index = 0, int $display = 0) { |
70
|
|
|
|
71
|
|
|
if (!((null === $parent_id) || ($parent_id >= 0))) return false; |
72
|
|
|
|
73
|
|
|
if (!(($index >= 0) && ($display >= 0))) return false; |
74
|
|
|
|
75
|
|
|
# Select entities |
76
|
|
|
|
77
|
|
|
$query = ((null === $parent_id) ? $this->getDefaultSelectQuery($config, $order_by, $index, $display) : |
78
|
|
|
|
79
|
|
|
$this->getNestingSelectQuery($parent_id, $config, $order_by, $index, $display)); |
80
|
|
|
|
81
|
|
|
if (!(DB::send($query) && DB::last()->status)) return false; |
82
|
|
|
|
83
|
|
|
# Process results |
84
|
|
|
|
85
|
|
|
$items = ['list' => [], 'total' => 0]; |
86
|
|
|
|
87
|
|
|
while (null !== ($data = DB::last()->row())) { |
88
|
|
|
|
89
|
|
|
$dataset = Entitizer::dataset(static::$table, $data); |
90
|
|
|
|
91
|
|
|
$items['list'][$dataset->id]['dataset'] = $dataset; |
92
|
|
|
|
93
|
|
|
if (null !== $parent_id) $items['list'][$dataset->id]['children'] = intval($data['children']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
# Count total |
97
|
|
|
|
98
|
|
|
if (DB::send("SELECT FOUND_ROWS() as total") && (DB::last()->rows === 1)) { |
99
|
|
|
|
100
|
|
|
$items['total'] = intval(DB::last()->row()['total']); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
# ------------------------ |
104
|
|
|
|
105
|
|
|
return $items; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
# Count entries in DB |
109
|
|
|
|
110
|
|
|
private function count(int $parent_id = null, array $config = []) { |
111
|
|
|
|
112
|
|
|
if (!((null === $parent_id) || ($parent_id >= 0))) return false; |
113
|
|
|
|
114
|
|
|
# Count entities |
115
|
|
|
|
116
|
|
|
$query = ((null === $parent_id) ? $this->getDefaultCountQuery($config) : |
117
|
|
|
|
118
|
|
|
$this->getNestingCountQuery($parent_id, $config)); |
119
|
|
|
|
120
|
|
|
if (!(DB::send($query) && DB::last()->status)) return false; |
121
|
|
|
|
122
|
|
|
# ------------------------ |
123
|
|
|
|
124
|
|
|
return intval(DB::last()->row()['count']); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
# Get items |
128
|
|
|
|
129
|
|
|
public function items(array $config = [], array $order_by = [], int $index = 0, int $display = 0) { |
130
|
|
|
|
131
|
|
|
return $this->select(null, $config, $order_by, $index, $display); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
# Get items count |
135
|
|
|
|
136
|
|
|
public function itemsCount(array $config = []) { |
137
|
|
|
|
138
|
|
|
return $this->count(null, $config); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
# Get children |
142
|
|
|
|
143
|
|
|
public function children(int $parent_id = 0, array $config = [], array $order_by = [], int $index = 0, int $display = 0) { |
144
|
|
|
|
145
|
|
|
if (!static::$nesting) return false; |
146
|
|
|
|
147
|
|
|
return $this->select($parent_id, $config, $order_by, $index, $display); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
# Get children count |
151
|
|
|
|
152
|
|
|
public function childrenCount(int $parent_id = 0, array $config = []) { |
153
|
|
|
|
154
|
|
|
if (!static::$nesting) return false; |
155
|
|
|
|
156
|
|
|
return $this->count($parent_id, $config); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|