|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Cadmium\System\Modules\Entitizer |
|
5
|
|
|
* @author Anton Romanov |
|
6
|
|
|
* @copyright Copyright (c) 2015-2017, Anton Romanov |
|
7
|
|
|
* @link http://cadmium-cms.com |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Modules\Entitizer\Utils { |
|
11
|
|
|
|
|
12
|
|
|
use Modules\Entitizer, DB; |
|
13
|
|
|
|
|
14
|
|
|
abstract class Listview extends Collection { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Get the basic select query |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
private function getBasicSelectQuery(array $config, array $order_by, int $index, int $display) : string { |
|
21
|
|
|
|
|
22
|
|
|
return ("SELECT SQL_CALC_FOUND_ROWS " . $this->getSelection() . " ") . |
|
23
|
|
|
|
|
24
|
|
|
("FROM " . static::$table . " ent ") . |
|
25
|
|
|
|
|
26
|
|
|
(('' !== ($condition = $this->getCondition($config))) ? ("WHERE " . $condition . " ") : "") . |
|
27
|
|
|
|
|
28
|
|
|
("ORDER BY " . $this->getOrderBy($order_by) . " ") . |
|
29
|
|
|
|
|
30
|
|
|
(($index > 0) ? ("LIMIT " . ((($index - 1) * $display) . ", " . $display)) : ""); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get the nesting select query |
|
35
|
|
|
*/ |
|
36
|
|
|
|
|
37
|
|
|
private function getNestingSelectQuery(int $parent_id, array $config, array $order_by, int $index, int $display) : string { |
|
38
|
|
|
|
|
39
|
|
|
return ("SELECT SQL_CALC_FOUND_ROWS " . $this->getSelection() . ", COUNT(chd.descendant) as children ") . |
|
40
|
|
|
|
|
41
|
|
|
("FROM " . static::$table . " ent ") . |
|
42
|
|
|
|
|
43
|
|
|
("LEFT JOIN " . static::$table_relations . " chd ON chd.ancestor = ent.id AND chd.depth = 1 ") . |
|
44
|
|
|
|
|
45
|
|
|
("LEFT JOIN " . static::$table_relations . " rel ON rel.descendant = ent.id AND rel.depth = 1 ") . |
|
46
|
|
|
|
|
47
|
|
|
("WHERE COALESCE(rel.ancestor, 0) = " . $parent_id . " ") . |
|
48
|
|
|
|
|
49
|
|
|
(('' !== ($condition = $this->getCondition($config))) ? ("AND " . $condition . " ") : "") . |
|
50
|
|
|
|
|
51
|
|
|
("GROUP BY ent.id ORDER BY " . $this->getOrderBy($order_by) . " ") . |
|
52
|
|
|
|
|
53
|
|
|
(($index > 0) ? ("LIMIT " . ((($index - 1) * $display) . ", " . $display)) : ""); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get the basic count query |
|
58
|
|
|
*/ |
|
59
|
|
|
|
|
60
|
|
|
private function getBasicCountQuery(array $config) : string { |
|
61
|
|
|
|
|
62
|
|
|
return ("SELECT COUNT(ent.id) as count FROM " . static::$table . " ent ") . |
|
63
|
|
|
|
|
64
|
|
|
(('' !== ($condition = $this->getCondition($config))) ? ("WHERE " . $condition) : ""); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the nesting count query |
|
69
|
|
|
*/ |
|
70
|
|
|
|
|
71
|
|
|
private function getNestingCountQuery(int $parent_id, array $config) : string { |
|
72
|
|
|
|
|
73
|
|
|
return ("SELECT COUNT(ent.id) as count FROM " . static::$table . " ent ") . |
|
74
|
|
|
|
|
75
|
|
|
("LEFT JOIN " . static::$table_relations . " rel ON rel.descendant = ent.id AND rel.depth = 1 ") . |
|
76
|
|
|
|
|
77
|
|
|
("WHERE COALESCE(rel.ancestor, 0) = " . $parent_id . " ") . |
|
78
|
|
|
|
|
79
|
|
|
(('' !== ($condition = $this->getCondition($config))) ? ("AND " . $condition) : ""); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Select entries from DB |
|
84
|
|
|
* |
|
85
|
|
|
* @return array|false : the array of entities or false on failure |
|
86
|
|
|
*/ |
|
87
|
|
|
|
|
88
|
|
|
private function select(int $parent_id = null, array $config = [], array $order_by = [], int $index = 0, int $display = 0) { |
|
89
|
|
|
|
|
90
|
|
|
if (!((null === $parent_id) || ($parent_id >= 0))) return false; |
|
91
|
|
|
|
|
92
|
|
|
if (!(($index >= 0) && ($display >= 0))) return false; |
|
93
|
|
|
|
|
94
|
|
|
# Select entities |
|
95
|
|
|
|
|
96
|
|
|
$query = ((null === $parent_id) ? $this->getBasicSelectQuery($config, $order_by, $index, $display) : |
|
97
|
|
|
|
|
98
|
|
|
$this->getNestingSelectQuery($parent_id, $config, $order_by, $index, $display)); |
|
99
|
|
|
|
|
100
|
|
|
if (!(DB::send($query) && DB::getLast()->status)) return false; |
|
101
|
|
|
|
|
102
|
|
|
# Process results |
|
103
|
|
|
|
|
104
|
|
|
$items = ['list' => [], 'total' => 0]; |
|
105
|
|
|
|
|
106
|
|
|
while (null !== ($data = DB::getLast()->getRow())) { |
|
107
|
|
|
|
|
108
|
|
|
$dataset = Entitizer::getDataset(static::$table, $data); |
|
109
|
|
|
|
|
110
|
|
|
$items['list'][$dataset->id]['dataset'] = $dataset; |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
if (null !== $parent_id) $items['list'][$dataset->id]['children'] = intval($data['children']); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
# Count total |
|
116
|
|
|
|
|
117
|
|
|
if (DB::send("SELECT FOUND_ROWS() as total") && (DB::getLast()->rows === 1)) { |
|
118
|
|
|
|
|
119
|
|
|
$items['total'] = intval(DB::getLast()->getRow()['total']); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
# ------------------------ |
|
123
|
|
|
|
|
124
|
|
|
return $items; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Count entries in DB |
|
129
|
|
|
* |
|
130
|
|
|
* @return int|false : the number of entities or false on failure |
|
131
|
|
|
*/ |
|
132
|
|
|
|
|
133
|
|
|
private function count(int $parent_id = null, array $config = []) { |
|
134
|
|
|
|
|
135
|
|
|
if (!((null === $parent_id) || ($parent_id >= 0))) return false; |
|
136
|
|
|
|
|
137
|
|
|
# Count entities |
|
138
|
|
|
|
|
139
|
|
|
$query = ((null === $parent_id) ? $this->getBasicCountQuery($config) : |
|
140
|
|
|
|
|
141
|
|
|
$this->getNestingCountQuery($parent_id, $config)); |
|
142
|
|
|
|
|
143
|
|
|
if (!(DB::send($query) && DB::getLast()->status)) return false; |
|
144
|
|
|
|
|
145
|
|
|
# ------------------------ |
|
146
|
|
|
|
|
147
|
|
|
return intval(DB::getLast()->getRow()['count']); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get the list of items |
|
152
|
|
|
* |
|
153
|
|
|
* @param $config an array of filtering options |
|
154
|
|
|
* @param $order_by an array where each key is a field name and each value is a sorting direction (ASC or DESC) |
|
155
|
|
|
* @param $index a page index |
|
156
|
|
|
* @param $display a number of results per page |
|
157
|
|
|
* |
|
158
|
|
|
* @return array|false : the array of entities or false on failure |
|
159
|
|
|
*/ |
|
160
|
|
|
|
|
161
|
|
|
public function getItems(array $config = [], array $order_by = [], int $index = 0, int $display = 0) { |
|
162
|
|
|
|
|
163
|
|
|
return $this->select(null, $config, $order_by, $index, $display); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get the items count |
|
168
|
|
|
* |
|
169
|
|
|
* @param $config : an array of filtering options |
|
170
|
|
|
* |
|
171
|
|
|
* @return int|false : the number of entities or false on failure |
|
172
|
|
|
*/ |
|
173
|
|
|
|
|
174
|
|
|
public function getItemsCount(array $config = []) { |
|
175
|
|
|
|
|
176
|
|
|
return $this->count(null, $config); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Get the list of children items |
|
181
|
|
|
* |
|
182
|
|
|
* @param $parent_id an id of a parent entity |
|
183
|
|
|
* @param $config an array of filtering options |
|
184
|
|
|
* @param $order_by an array where each key is a field name and each value is a sorting direction (ASC or DESC) |
|
185
|
|
|
* @param $index a page index |
|
186
|
|
|
* @param $display a number of results per page |
|
187
|
|
|
* |
|
188
|
|
|
* @return array|false : the array of entities or false on failure |
|
189
|
|
|
*/ |
|
190
|
|
|
|
|
191
|
|
|
public function getChildren(int $parent_id = 0, array $config = [], array $order_by = [], int $index = 0, int $display = 0) { |
|
192
|
|
|
|
|
193
|
|
|
if (!static::$nesting) return false; |
|
194
|
|
|
|
|
195
|
|
|
return $this->select($parent_id, $config, $order_by, $index, $display); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Get the children items count |
|
200
|
|
|
* |
|
201
|
|
|
* @param $parent_id an id of a parent entity |
|
202
|
|
|
* @param $config an array of filtering options |
|
203
|
|
|
* |
|
204
|
|
|
* @return int|false : the number of entities or false on failure |
|
205
|
|
|
*/ |
|
206
|
|
|
|
|
207
|
|
|
public function getChildrenCount(int $parent_id = 0, array $config = []) { |
|
208
|
|
|
|
|
209
|
|
|
if (!static::$nesting) return false; |
|
210
|
|
|
|
|
211
|
|
|
return $this->count($parent_id, $config); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.