/
DatabaseItemList.php
245 lines (214 loc) · 7.21 KB
/
DatabaseItemList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
namespace Concrete\Core\Legacy;
class DatabaseItemList extends ItemList
{
protected $query = '';
protected $userQuery = '';
protected $debug = false;
protected $filters = array();
protected $sortByString = '';
protected $groupByString = '';
protected $havingString = '';
protected $autoSortColumns = array();
protected $userPostQuery = '';
protected $attributeClass = '';
public function getTotal()
{
if ($this->total == -1) {
$db = Loader::db();
$arr = $this->executeBase(); // returns an associated array of query/placeholder values
$r = $db->Execute($arr);
$this->total = $r->rowCount();
}
return $this->total;
}
public function debug($dbg = true)
{
$this->debug = $dbg;
}
protected function setQuery($query)
{
$this->query = $query . ' ';
}
protected function getQuery()
{
return $this->query;
}
public function addToQuery($query)
{
$this->userQuery .= $query . ' ';
}
protected function setupAutoSort()
{
if (count($this->autoSortColumns) > 0) {
$req = $this->getSearchRequest();
if (
isset($req[$this->queryStringSortVariable])
&&
in_array($req[$this->queryStringSortVariable], $this->autoSortColumns)
) {
$this->sortBy($req[$this->queryStringSortVariable], $req[$this->queryStringSortDirectionVariable]);
}
}
}
protected function executeBase()
{
$db = Loader::db();
$q = $this->query . $this->userQuery . ' where 1=1 ';
foreach ($this->filters as $f) {
$column = $f[0];
$comp = $f[2];
$value = $f[1];
// if there is NO column, then we have a free text filter that we just add on
if ($column == false || $column == '') {
$q .= 'and ' . $f[1] . ' ';
} else {
if (is_array($value)) {
if (count($value) > 0) {
switch ($comp) {
case '=':
$comp = 'in';
break;
case '!=':
$comp = 'not in';
break;
}
$q .= 'and ' . $column . ' ' . $comp . ' (';
for ($i = 0; $i < count($value); ++$i) {
if ($i > 0) {
$q .= ',';
}
$q .= $db->quote($value[$i]);
}
$q .= ') ';
} else {
$q .= 'and 1 = 2 ';
}
} else {
$comp = (is_null($value) && stripos($comp, 'is') === false) ? (($comp == '!=' || $comp == '<>') ? 'IS NOT' : 'IS') : $comp;
$q .= 'and ' . $column . ' ' . $comp . ' ' . $db->quote($value) . ' ';
}
}
}
if ($this->userPostQuery != '') {
$q .= ' ' . $this->userPostQuery . ' ';
}
if ($this->groupByString != '') {
$q .= 'group by ' . $this->groupByString . ' ';
}
if ($this->havingString != '') {
$q .= 'having ' . $this->havingString . ' ';
}
return $q;
}
protected function setupSortByString()
{
if ($this->sortByString == '' && $this->sortBy != '') {
$this->sortByString = $this->sortBy . ' ' . $this->sortByDirection;
}
}
protected function setupAttributeSort()
{
if ($this->attributeClass && is_callable(array($this->attributeClass, 'getList'))) {
$l = call_user_func(array($this->attributeClass, 'getList'));
foreach ($l as $ak) {
$this->autoSortColumns[] = 'ak_' . $ak->getAttributeKeyHandle();
}
if ($this->sortBy != '' && in_array('ak_' . $this->sortBy, $this->autoSortColumns)) {
$this->sortBy = 'ak_' . $this->sortBy;
}
}
}
/**
* Returns an array of whatever objects extends this class (e.g. PageList returns a list of pages).
*/
public function get($itemsToGet = 0, $offset = 0)
{
$q = $this->executeBase();
// handle order by
$this->setupAttributeSort();
$this->setupAutoSort();
$this->setupSortByString();
if ($this->sortByString != '') {
$q .= 'order by ' . $this->sortByString . ' ';
}
if ($this->itemsPerPage > 0 && (intval($itemsToGet) || intval($offset))) {
$q .= 'limit ' . $offset . ',' . $itemsToGet . ' ';
}
$db = Loader::db();
$resp = $db->GetAll($q);
$this->start = $offset;
return $resp;
}
/**
* Adds a filter to this item list.
*/
public function filter($column, $value, $comparison = '=')
{
$foundFilterIndex = -1;
if ($column) {
foreach ($this->filters as $key => $info) {
if ($info[0] == $column) {
$foundFilterIndex = $key;
break;
}
}
}
if ($foundFilterIndex > -1) {
$this->filters[$foundFilterIndex] = array($column, $value, $comparison);
} else {
$this->filters[] = array($column, $value, $comparison);
}
}
public function getSearchResultsClass($field)
{
if ($field instanceof AttributeKey) {
$field = 'ak_' . $field->getAttributeKeyHandle();
}
return parent::getSearchResultsClass($field);
}
public function sortBy($key, $dir = 'asc')
{
if ($key instanceof AttributeKey) {
$key = 'ak_' . $key->getAttributeKeyHandle();
}
parent::sortBy($key, $dir);
}
public function groupBy($key)
{
if ($key instanceof AttributeKey) {
$key = 'ak_' . $key->getAttributeKeyHandle();
}
$this->groupByString = $key;
}
public function having($column, $value, $comparison = '=')
{
if ($column == false) {
$this->havingString = $value;
} else {
$this->havingString = $column . ' ' . $comparison . ' ' . $value;
}
}
public function getSortByURL($column, $dir = 'asc', $baseURL = false, $additionalVars = array())
{
if ($column instanceof AttributeKey) {
$column = 'ak_' . $column->getAttributeKeyHandle();
}
return parent::getSortByURL($column, $dir, $baseURL, $additionalVars);
}
protected function setupAttributeFilters($join)
{
$i = 1;
$this->addToQuery($join);
foreach ($this->attributeFilters as $caf) {
$this->filter($caf[0], $caf[1], $caf[2]);
}
}
public function filterByAttribute($column, $value, $comparison = '=')
{
if (is_array($column)) {
$column = $column[key($column)] . '_' . key($column);
}
$this->attributeFilters[] = array('ak_' . $column, $value, $comparison);
}
}