-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResultList.php
241 lines (208 loc) · 6.6 KB
/
ResultList.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
<?php
namespace plugins\riResultList;
/**
* A (result) list that handles lists spanning multiple pages.
*
* <p>A result list operates on any given set of results. Results do not have to have
* any specific properties, type specific code is delegated to filters and sorters.</p>
*
* <p>Results are obtained via the <code>ZMResultSource</code> object. This defers the actual
* query to the latest possible moment. Methods may trigger the query if they depend
* on results; a good example for that is, for example, <code>getNumberOfResults()</code>.</p>
*
* @author DerManoMann <[email protected]>
* @package org.zenmagick.mvc.resultlist
*/
class ResultList extends \Zepluf\Bundle\StoreBundle\Object {
protected $resultSource_;
protected $sorters_;
protected $filters_;
protected $page_;
protected $pagination_;
protected $number_of_results_ = false;
protected $results_;
function __construct() {
$this->resultSource_ = null;
$this->sorters_ = array();
$this->filters_ = array();
$this->number_of_results_ = false;
$this->page_ = 1;
$this->pagination_ = 15;
$this->results_ = null;
}
/**
* Destruct instance.
*/
function __destruct() {
parent::__destruct();
}
/**
* Set a source for results.
*
* <p>The advantage of using a result source is that alternative implementations are free
* to ignore these, modify them or replace them as needed. Providing results via
* the constructor means that the resources used to build that list might be wasted.</p>
*
* @param ZMResultSource resultSource A result source.
*/
public function setResultSource($source) {
$this->resultSource_ = $source;
$this->resultSource_->setResultList($this);
$this->results_ = null;
}
/**
* Checks if there are results available.
*
* @return boolean <code>true</code> if results are available, <code>false</code> if not.
*/
public function hasResults() {
return 0 < count($this->getResults());
}
/**
* Count all results.
*
* @return int The total number if results.
*/
public function getNumberOfResults() {
if($this->number_of_results_ === false)
$this->number_of_results_ = $this->resultSource_->getTotalNumberOfResults();
return $this->number_of_results_;
}
public function getNumberOfCurrentResults() {
return count($this->$results);
}
/**
* Get the page number (1-based).
*
* @return int The page number.
*/
public function getPageNumber() {
return $this->page_;
}
/**
* Set the page number (1-based).
*
* @param int page The page number.
*/
public function setPageNumber($page) {
$this->page_ = (0 < $page ? $page : 1);
$this->results_ = null;
}
/**
* Get the configured pagination.
*
* @return int The number of results per page.
*/
public function getPagination() {
return $this->pagination_;
}
public function addSorter($sorter){
$this->sorters_[] = $sorter;
}
public function getSorters(){
return $this->sorters_;
}
public function getSortersString(){
return count($this->sorters_) > 0 ? implode(" , ", $this->sorters_) : '';
}
public function addFilter($filter){
$this->filters_[] = $filter;
}
public function addFilters($filters, $map){
foreach ($filters as $key => $value){
if(isset($map[$key])){
$filter = $map[$key]['field'] . $map[$key]['condition'] . (isset($map[$key]['bind']) ? $map[$key]['bind']($value) : $value);
$this->addFilter($filter);
}
}
}
public function getFilters(){
return $this->filters_;
}
public function getFiltersString(){
return count($this->filters_) > 0 ? implode(" AND ", $this->filters_) : '';
}
public function getLimit(){
$start = $this->getPageNumber() == 1 ? 0 : (($this->getPageNumber() -1 ) * $this->getPagination());
return array($start, $this->getPagination());
}
/**
* Set the configured pagination.
*
* @param int pagination The number of results per page.
*/
public function setPagination($pagination) {
$this->pagination_ = $pagination;
$this->results_ = null;
}
/**
* Get the calculated number of pages.
*
* @return int The number of pages; will return <em>0</em> if no results available.
*/
public function getNumberOfPages() {
if (0 == $this->pagination_) {
return 1;
}
return (int)ceil($this->getNumberOfResults() / $this->pagination_);
}
/**
* Check if a previous page is available.
*
* @return boolean <code>true</code> if a previous page is available, <code>false</code> if not.
*/
public function hasPreviousPage() {
return 1 < $this->page_;
}
/**
* Check if a next page is available.
*
* @return boolean <code>true</code> if a next page is available, <code>false</code> if not.
*/
public function hasNextPage() {
return $this->page_ < $this->getNumberOfPages();
}
/**
* Get the previous page number.
*
* @return int The previous page number; (default: 1)
*/
public function getPreviousPageNumber() {
return $this->hasPreviousPage() ? ($this->page_ - 1) : 1;
}
/**
* Get the next page number.
*
* @return int The next page number.
*/
public function getNextPageNumber() {
return $this->hasNextPage() ? ($this->page_ + 1) : $this->getNumberOfPages();
}
/**
* Get the results for the current page.
*
* @return array List of results for the current page.
*/
public function getResults() {
$this->$results_ = $this->resultSource_->getResults();
return $this->$results_;
}
public function buildBaseQuery($query){
$where = $this->getFiltersString();
if(!empty($where))
$query .= " WHERE ".$where;
return $query;
}
public function buildPaginationQuery($query){
$query = $this->buildBaseQuery($query);
// put in sorters
$sorter = $this->getSortersString();
if(!empty($sorter))
$query .= " ORDER BY ".$sorter;
// put in the limit
list($start, $duration) = $this->getLimit();
if($duration > 0)
$query .= " Limit $start, $duration";
return $query;
}
}