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