/
ItemList.php
280 lines (238 loc) · 7.7 KB
/
ItemList.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
namespace Concrete\Core\Search\ItemList;
use Concrete\Core\Search\Column\Column;
use Concrete\Core\Search\Pagination\PaginationFactory;
use Concrete\Core\Search\StickyRequest;
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
use Pagerfanta\Exception\LessThan1CurrentPageException;
abstract class ItemList
{
protected $sortColumnParameter = 'ccm_order_by';
protected $sortDirectionParameter = 'ccm_order_by_direction';
protected $paginationPageParameter;
protected $sortBy;
protected $sortByDirection;
protected $sortBySearchColumn;
// This still checks the auto sort columns if set to true –
// we just turn it off to save processing in the attributed item list (so it doesn't have to instantiate
// all those objects if it's not necessary)
protected $enableAutomaticSorting = true;
protected $autoSortColumns = array();
protected $itemsPerPage = -1; // determined by the pagination object.
protected $debug = false;
abstract protected function executeSortBy($field, $direction = 'asc');
protected function executeSanitizedSortBy($field, $direction)
{
$this->executeSortBy($field, $direction);
}
abstract public function executeGetResults();
abstract public function getResult($mixed);
abstract public function debugStart();
abstract public function debugStop();
/**
* @return array
*/
public function getAutoSortColumns(): array
{
return $this->autoSortColumns;
}
public function debug()
{
$this->debug = true;
}
public function isDebugged()
{
return $this->debug;
}
public function sortBy($field, $direction = 'asc')
{
$this->sortBy = $field;
$this->sortByDirection = $direction;
$this->executeSortBy($field, $direction);
}
public function sortBySearchColumn(Column $column, $direction = null)
{
if ($direction != 'asc' && $direction != 'desc') {
$direction = $column->getColumnSortDirection();
}
$this->sortByDirection = $direction;
$this->sortBySearchColumn = $column;
$this->sanitizedSortBy($column->getColumnKey(), $direction);
}
public function getSearchByColumn()
{
return $this->sortBySearchColumn;
}
public function sanitizedSortBy($field, $direction = 'asc')
{
$this->sortBy = $field;
$this->sortByDirection = $direction;
$this->executeSanitizedSortBy($field, $direction);
}
/** Returns a full array of results. */
public function getResults()
{
$results = array();
$this->debugStart();
$executeResults = $this->executeGetResults();
$this->debugStop();
foreach ($executeResults as $result) {
$r = $this->getResult($result);
if ($r != null) {
$results[] = $r;
}
}
return $results;
}
public function getActiveSortColumn()
{
return $this->sortBy;
}
public function isActiveSortColumn($field)
{
return $this->sortBy == $field;
}
public function disableAutomaticSorting()
{
$this->enableAutomaticSorting = false;
}
public function getSortClassName($column)
{
$class = false;
if ($this->isActiveSortColumn($column)) {
$class = 'ccm-results-list-active-sort-';
if ($this->getActiveSortDirection() == 'desc') {
$class .= 'desc';
} else {
$class .= 'asc';
}
}
return $class;
}
public function getSortURL($column, $dir = 'asc', $url = false)
{
$uh = \Core::make("helper/url");
if ($this->isActiveSortColumn($column) && $this->getActiveSortDirection() == $dir) {
$dir = ($dir == 'asc') ? 'desc' : 'asc';
}
$args = array(
$this->getQuerySortColumnParameter() => $column,
$this->getQuerySortDirectionParameter() => $dir,
'ccm_cursor' => '',
);
$url = $uh->setVariable($args, false, $url);
return strip_tags($url);
}
/** @var \Concrete\Core\Search\Pagination\Pagination */
protected $pagination;
public function getActiveSortDirection()
{
return $this->sortByDirection;
}
public function getQuerySortColumnParameter()
{
return $this->sortColumnParameter;
}
public function getQueryPaginationPageParameter()
{
if (!isset($this->paginationPageParameter)) {
$this->loadQueryStringPagingVariable();
}
return $this->paginationPageParameter;
}
/**
* Get paging parameter from Concrete configuration
*/
protected function loadQueryStringPagingVariable()
{
$this->paginationPageParameter = \Config::get('concrete.seo.paging_string');
}
public function getQuerySortDirectionParameter()
{
return $this->sortDirectionParameter;
}
public function setItemsPerPage($itemsPerPage)
{
$this->itemsPerPage = $itemsPerPage;
}
/**
* @return int
*/
public function getItemsPerPage()
{
return $this->itemsPerPage;
}
/**
* Returns the total results in this item list.
*
* @return int
*/
abstract public function getTotalResults();
/**
* Deprecated – call the pagination factory directly.
* @deprecated
* @return \Concrete\Core\Search\Pagination\Pagination
*/
public function getPagination()
{
$factory = new PaginationFactory(\Request::getInstance());
if (method_exists($this, 'createPaginationObject')) {
$pagination = $this->createPaginationObject();
$pagination = $factory->deliverPaginationObject($this, $pagination);
} else {
$pagination = $factory->createPaginationObject($this);
}
return $pagination;
}
/**
* @param StickyRequest $request
*/
public function performAutomaticSorting(StickyRequest $request = null)
{
if ($this->enableAutomaticSorting) {
if ($request) {
$data = $request->getSearchRequest();
} else {
$data = \Request::getInstance()->query->all();
}
$direction = 'asc';
if (isset($data[$this->getQuerySortDirectionParameter()])) {
$direction = $data[$this->getQuerySortDirectionParameter()];
}
if (isset($data[$this->getQuerySortColumnParameter()])) {
$value = $data[$this->getQuerySortColumnParameter()];
if (in_array($value, $this->autoSortColumns)) {
$this->sanitizedSortBy($value, $direction);
}
}
}
}
protected function setupAutomaticSorting()
{
// Empty, meant to be extended. This separates the logic of making columns available for automatic sorting
// vs. actually automatically applying it by default within the list.
}
/**
* @deprecated
*/
public function get()
{
return $this->getResults();
}
/**
* Allow to modify the auto-pagination parameters and the auto-sorting parameters
*
* @param mixed $nameSpace Content that will be added to the parameters
*/
public function setNameSpace($nameSpace)
{
if (!isset($this->paginationPageParameter)) {
$this->loadQueryStringPagingVariable();
}
if(!is_null($nameSpace) && $nameSpace!=""){
$this->paginationPageParameter .= '_' . $nameSpace;
$this->sortColumnParameter .= '_' . $nameSpace;
$this->sortDirectionParameter .= '_' . $nameSpace;
}
}
}