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