Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
6a488035 | 13 | * @package CRM |
ca5cec67 | 14 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
15 | */ |
16 | ||
17 | /** | |
6a488035 TO |
18 | * This class extends the PEAR pager object by substituting standard default pager arguments |
19 | * We also extract the pageId from either the GET variables or the POST variable (since we | |
20 | * use a POST to jump to a specific page). At some point we should evaluate if we want | |
21 | * to use Pager_Jumping instead. We've changed the format to allow navigation by jumping | |
22 | * to a page and also First, Prev CURRENT Next Last | |
6a488035 TO |
23 | */ |
24 | ||
25 | require_once 'Pager/Sliding.php'; | |
5bc392e6 EM |
26 | |
27 | /** | |
28 | * Class CRM_Utils_Pager | |
29 | */ | |
6a488035 TO |
30 | class CRM_Utils_Pager extends Pager_Sliding { |
31 | ||
32 | /** | |
100fef9d | 33 | * Constants for static parameters of the pager |
6a488035 | 34 | */ |
7da04cde | 35 | const ROWCOUNT = 50, PAGE_ID = 'crmPID', PAGE_ID_TOP = 'crmPID', PAGE_ID_BOTTOM = 'crmPID_B', PAGE_ROWCOUNT = 'crmRowCount'; |
6a488035 TO |
36 | |
37 | /** | |
100fef9d | 38 | * The output of the pager. This is a name/value array with various keys |
6a488035 | 39 | * that an application could use to display the pager |
50bfb460 | 40 | * |
6a488035 TO |
41 | * @var array |
42 | */ | |
43 | public $_response; | |
44 | ||
45 | /** | |
46 | * The pager constructor. Takes a few values, and then assigns a lot of defaults | |
47 | * to the PEAR pager class | |
48 | * We have embedded some html in this class. Need to figure out how to export this | |
49 | * to the top level at some point in time | |
50 | * | |
c490a46a | 51 | * @param array $params |
f4aaa82a | 52 | * |
e0b82b44 | 53 | * @return \CRM_Utils_Pager the newly created and initialized pager object |
6a488035 | 54 | */ |
00be9182 | 55 | public function __construct($params) { |
6a488035 TO |
56 | if ($params['status'] === NULL) { |
57 | $params['status'] = ts('Contacts %%StatusMessage%%'); | |
58 | } | |
59 | ||
60 | $this->initialize($params); | |
61 | ||
6ef04c72 | 62 | parent::__construct($params); |
6a488035 TO |
63 | |
64 | list($offset, $limit) = $this->getOffsetAndRowCount(); | |
65 | $start = $offset + 1; | |
66 | $end = $offset + $limit; | |
67 | if ($end > $params['total']) { | |
68 | $end = $params['total']; | |
69 | } | |
70 | ||
71 | if ($params['total'] == 0) { | |
72 | $statusMessage = ''; | |
73 | } | |
74 | else { | |
be2fb01f | 75 | $statusMessage = ts('%1 - %2 of %3', [1 => $start, 2 => $end, 3 => $params['total']]); |
6a488035 TO |
76 | } |
77 | $params['status'] = str_replace('%%StatusMessage%%', $statusMessage, $params['status']); | |
78 | ||
be2fb01f | 79 | $this->_response = [ |
6a488035 TO |
80 | 'first' => $this->getFirstPageLink(), |
81 | 'back' => $this->getBackPageLink(), | |
82 | 'next' => $this->getNextPageLink(), | |
83 | 'last' => $this->getLastPageLink(), | |
84 | 'currentPage' => $this->getCurrentPageID(), | |
85 | 'numPages' => $this->numPages(), | |
86 | 'csvString' => CRM_Utils_Array::value('csvString', $params), | |
87 | 'status' => CRM_Utils_Array::value('status', $params), | |
88 | 'buttonTop' => CRM_Utils_Array::value('buttonTop', $params), | |
89 | 'buttonBottom' => CRM_Utils_Array::value('buttonBottom', $params), | |
fe010227 | 90 | 'currentLocation' => $this->getCurrentLocation(), |
be2fb01f | 91 | ]; |
6a488035 TO |
92 | |
93 | /** | |
94 | * A page cannot have two variables with the same form name. Hence in the | |
95 | * pager display, we have a form submission at the top with the normal | |
50bfb460 | 96 | * page variable, but a different form element for one at the bottom. |
6a488035 | 97 | */ |
be2fb01f | 98 | $this->_response['titleTop'] = ts('Page %1 of %2', [ |
6714d8d2 SL |
99 | 1 => '<input size="2" maxlength="4" name="' . self::PAGE_ID . '" type="text" value="' . $this->_response['currentPage'] . '" />', |
100 | 2 => $this->_response['numPages'], | |
101 | ]); | |
be2fb01f | 102 | $this->_response['titleBottom'] = ts('Page %1 of %2', [ |
6714d8d2 SL |
103 | 1 => '<input size="2" maxlength="4" name="' . self::PAGE_ID_BOTTOM . '" type="text" value="' . $this->_response['currentPage'] . '" />', |
104 | 2 => $this->_response['numPages'], | |
105 | ]); | |
6a488035 TO |
106 | } |
107 | ||
108 | /** | |
100fef9d | 109 | * Helper function to assign remaining pager options as good default |
4d429319 | 110 | * values. |
6a488035 | 111 | * |
77855840 | 112 | * @param array $params |
b8c71ffa | 113 | * The set of options needed to initialize the parent constructor. |
6a488035 | 114 | * |
b8c71ffa | 115 | * @return array |
6a488035 | 116 | */ |
00be9182 | 117 | public function initialize(&$params) { |
50bfb460 | 118 | // set the mode for the pager to Sliding |
6a488035 TO |
119 | |
120 | $params['mode'] = 'Sliding'; | |
121 | ||
50bfb460 | 122 | // also set the urlVar to be a crm specific get variable. |
6a488035 TO |
123 | |
124 | $params['urlVar'] = self::PAGE_ID; | |
125 | ||
50bfb460 | 126 | // set this to a small value, since we dont use this functionality |
6a488035 TO |
127 | |
128 | $params['delta'] = 1; | |
129 | ||
130 | $params['totalItems'] = $params['total']; | |
131 | $params['append'] = TRUE; | |
132 | $params['separator'] = ''; | |
133 | $params['spacesBeforeSeparator'] = 1; | |
134 | $params['spacesAfterSeparator'] = 1; | |
be2fb01f CW |
135 | $params['extraVars'] = ['force' => 1]; |
136 | $params['excludeVars'] = ['reset', 'snippet', 'section']; | |
6a488035 TO |
137 | |
138 | // set previous and next text labels | |
139 | $params['prevImg'] = ' ' . ts('< Previous'); | |
140 | $params['nextImg'] = ts('Next >') . ' '; | |
141 | ||
6a488035 TO |
142 | // set first and last text fragments |
143 | $params['firstPagePre'] = ''; | |
144 | $params['firstPageText'] = ' ' . ts('<< First'); | |
145 | $params['firstPagePost'] = ''; | |
146 | ||
147 | $params['lastPagePre'] = ''; | |
148 | $params['lastPageText'] = ts('Last >>') . ' '; | |
149 | $params['lastPagePost'] = ''; | |
150 | ||
151 | if (isset($params['pageID'])) { | |
152 | $params['currentPage'] = $this->getPageID($params['pageID'], $params); | |
153 | } | |
154 | ||
155 | $params['perPage'] = $this->getPageRowCount($params['rowCount']); | |
156 | ||
157 | return $params; | |
158 | } | |
159 | ||
160 | /** | |
161 | * Figure out the current page number based on value of | |
162 | * GET / POST variables. Hierarchy rules are followed, | |
163 | * POST over-rides a GET, a POST at the top overrides | |
164 | * a POST at the bottom (of the page) | |
165 | * | |
77855840 TO |
166 | * @param int $defaultPageId |
167 | * DefaultPageId current pageId. | |
f4aaa82a | 168 | * |
c490a46a | 169 | * @param array $params |
6a488035 | 170 | * |
a6c01b45 CW |
171 | * @return int |
172 | * new pageId to display to the user | |
6a488035 | 173 | */ |
00be9182 | 174 | public function getPageID($defaultPageId = 1, &$params) { |
6a488035 TO |
175 | // POST has higher priority than GET vars |
176 | // else if a value is set that has higher priority and finally the GET var | |
177 | $currentPage = $defaultPageId; | |
178 | if (!empty($_POST)) { | |
179 | if (isset($_POST[CRM_Utils_Array::value('buttonTop', $params)]) && isset($_POST[self::PAGE_ID])) { | |
e7292422 | 180 | $currentPage = max((int ) @$_POST[self::PAGE_ID], 1); |
6a488035 TO |
181 | } |
182 | elseif (isset($_POST[$params['buttonBottom']]) && isset($_POST[self::PAGE_ID_BOTTOM])) { | |
e7292422 | 183 | $currentPage = max((int ) @$_POST[self::PAGE_ID_BOTTOM], 1); |
6a488035 TO |
184 | } |
185 | elseif (isset($_POST[self::PAGE_ID])) { | |
e7292422 | 186 | $currentPage = max((int ) @$_POST[self::PAGE_ID], 1); |
6a488035 TO |
187 | } |
188 | elseif (isset($_POST[self::PAGE_ID_BOTTOM])) { | |
e7292422 | 189 | $currentPage = max((int ) @$_POST[self::PAGE_ID_BOTTOM]); |
6a488035 TO |
190 | } |
191 | } | |
192 | elseif (isset($_GET[self::PAGE_ID])) { | |
e7292422 | 193 | $currentPage = max((int ) @$_GET[self::PAGE_ID], 1); |
6a488035 TO |
194 | } |
195 | return $currentPage; | |
196 | } | |
197 | ||
198 | /** | |
199 | * Get the number of rows to display from either a GET / POST variable | |
200 | * | |
77855840 TO |
201 | * @param int $defaultPageRowCount |
202 | * The default value if not set. | |
6a488035 | 203 | * |
a6c01b45 CW |
204 | * @return int |
205 | * the rowCount value to use | |
6a488035 | 206 | */ |
00be9182 | 207 | public function getPageRowCount($defaultPageRowCount = self::ROWCOUNT) { |
6a488035 TO |
208 | // POST has higher priority than GET vars |
209 | if (isset($_POST[self::PAGE_ROWCOUNT])) { | |
e7292422 | 210 | $rowCount = max((int ) @$_POST[self::PAGE_ROWCOUNT], 1); |
6a488035 TO |
211 | } |
212 | elseif (isset($_GET[self::PAGE_ROWCOUNT])) { | |
e7292422 | 213 | $rowCount = max((int ) @$_GET[self::PAGE_ROWCOUNT], 1); |
6a488035 TO |
214 | } |
215 | else { | |
216 | $rowCount = $defaultPageRowCount; | |
217 | } | |
218 | return $rowCount; | |
219 | } | |
220 | ||
221 | /** | |
fe482240 | 222 | * Use the pager class to get the pageId and Offset. |
6a488035 | 223 | * |
a6c01b45 CW |
224 | * @return array |
225 | * an array of the pageID and offset | |
6a488035 | 226 | */ |
00be9182 | 227 | public function getOffsetAndRowCount() { |
6a488035 TO |
228 | $pageId = $this->getCurrentPageID(); |
229 | if (!$pageId) { | |
230 | $pageId = 1; | |
231 | } | |
232 | ||
233 | $offset = ($pageId - 1) * $this->_perPage; | |
234 | ||
be2fb01f | 235 | return [$offset, $this->_perPage]; |
6a488035 TO |
236 | } |
237 | ||
5bc392e6 EM |
238 | /** |
239 | * @return string | |
240 | */ | |
00be9182 | 241 | public function getCurrentLocation() { |
fe010227 CW |
242 | $config = CRM_Core_Config::singleton(); |
243 | $path = CRM_Utils_Array::value($config->userFrameworkURLVar, $_GET); | |
244 | return CRM_Utils_System::url($path, CRM_Utils_System::getLinksUrl(self::PAGE_ID, FALSE, TRUE), FALSE, NULL, FALSE) . $this->getCurrentPageID(); | |
6a488035 TO |
245 | } |
246 | ||
5bc392e6 EM |
247 | /** |
248 | * @return string | |
249 | */ | |
00be9182 | 250 | public function getFirstPageLink() { |
6a488035 TO |
251 | if ($this->isFirstPage()) { |
252 | return ''; | |
253 | } | |
10824c72 | 254 | $href = $this->makeURL(self::PAGE_ID, 1); |
c5d647c1 | 255 | return $this->formatLink($href, str_replace('%d', 1, $this->_altFirst), $this->_firstPagePre . $this->_firstPageText . $this->_firstPagePost) . |
353ffa53 | 256 | $this->_spacesBefore . $this->_spacesAfter; |
6a488035 TO |
257 | } |
258 | ||
5bc392e6 EM |
259 | /** |
260 | * @return string | |
261 | */ | |
00be9182 | 262 | public function getLastPageLink() { |
6a488035 TO |
263 | if ($this->isLastPage()) { |
264 | return ''; | |
265 | } | |
10824c72 | 266 | $href = $this->makeURL(self::PAGE_ID, $this->_totalPages); |
c5d647c1 | 267 | return $this->formatLink($href, str_replace('%d', $this->_totalPages, $this->_altLast), $this->_lastPagePre . $this->_lastPageText . $this->_lastPagePost); |
6a488035 TO |
268 | } |
269 | ||
5bc392e6 EM |
270 | /** |
271 | * @return string | |
272 | */ | |
00be9182 | 273 | public function getBackPageLink() { |
6a488035 | 274 | if ($this->_currentPage > 1) { |
10824c72 | 275 | $href = $this->makeURL(self::PAGE_ID, $this->getPreviousPageID()); |
c5d647c1 | 276 | return $this->formatLink($href, $this->_altPrev, $this->_prevImg) . $this->_spacesBefore . $this->_spacesAfter; |
6a488035 TO |
277 | } |
278 | return ''; | |
279 | } | |
280 | ||
5bc392e6 EM |
281 | /** |
282 | * @return string | |
283 | */ | |
00be9182 | 284 | public function getNextPageLink() { |
6a488035 | 285 | if ($this->_currentPage < $this->_totalPages) { |
10824c72 | 286 | $href = $this->makeURL(self::PAGE_ID, $this->getNextPageID()); |
c5d647c1 | 287 | return $this->_spacesAfter . |
353ffa53 TO |
288 | $this->formatLink($href, $this->_altNext, $this->_nextImg) . |
289 | $this->_spacesBefore . $this->_spacesAfter; | |
6a488035 TO |
290 | } |
291 | return ''; | |
292 | } | |
10824c72 CW |
293 | |
294 | /** | |
fe482240 | 295 | * Build a url for pager links. |
54957108 | 296 | * |
297 | * @param string $key | |
298 | * @param string $value | |
299 | * | |
300 | * @return string | |
10824c72 | 301 | */ |
00be9182 | 302 | public function makeURL($key, $value) { |
e6add384 | 303 | $href = CRM_Utils_System::makeURL($key, TRUE); |
10824c72 CW |
304 | // CRM-12212 Remove alpha sort param |
305 | if (strpos($href, '&sortByCharacter=')) { | |
306 | $href = preg_replace('#(.*)\&sortByCharacter=[^&]*(.*)#', '\1\2', $href); | |
307 | } | |
308 | return $href . $value; | |
309 | } | |
c5d647c1 CW |
310 | |
311 | /** | |
fe482240 | 312 | * Output the html pager link. |
c5d647c1 CW |
313 | * @param string $href |
314 | * @param string $title | |
315 | * @param string $image | |
316 | * @return string | |
317 | */ | |
318 | private function formatLink($href, $title, $image) { | |
319 | return sprintf('<a class="crm-pager-link action-item crm-hover-button" href="%s" title="%s">%s</a>', $href, $title, $image); | |
320 | } | |
96025800 | 321 | |
6a488035 | 322 | } |