3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2019
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
41 require_once 'Pager/Sliding.php';
44 * Class CRM_Utils_Pager
46 class CRM_Utils_Pager
extends Pager_Sliding
{
49 * Constants for static parameters of the pager
51 const ROWCOUNT
= 50, PAGE_ID
= 'crmPID', PAGE_ID_TOP
= 'crmPID', PAGE_ID_BOTTOM
= 'crmPID_B', PAGE_ROWCOUNT
= 'crmRowCount';
54 * The output of the pager. This is a name/value array with various keys
55 * that an application could use to display the pager
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
67 * @param array $params
69 * @return \CRM_Utils_Pager the newly created and initialized pager object
71 public function __construct($params) {
72 if ($params['status'] === NULL) {
73 $params['status'] = ts('Contacts %%StatusMessage%%');
76 $this->initialize($params);
78 parent
::__construct($params);
80 list($offset, $limit) = $this->getOffsetAndRowCount();
82 $end = $offset +
$limit;
83 if ($end > $params['total']) {
84 $end = $params['total'];
87 if ($params['total'] == 0) {
91 $statusMessage = ts('%1 - %2 of %3', [1 => $start, 2 => $end, 3 => $params['total']]);
93 $params['status'] = str_replace('%%StatusMessage%%', $statusMessage, $params['status']);
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),
106 'currentLocation' => $this->getCurrentLocation(),
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
112 * page variable, but a different form element for one at the bottom.
114 $this->_response
['titleTop'] = ts('Page %1 of %2', [
115 1 => '<input size="2" maxlength="4" name="' . self
::PAGE_ID
. '" type="text" value="' . $this->_response
['currentPage'] . '" />',
116 2 => $this->_response
['numPages'],
118 $this->_response
['titleBottom'] = ts('Page %1 of %2', [
119 1 => '<input size="2" maxlength="4" name="' . self
::PAGE_ID_BOTTOM
. '" type="text" value="' . $this->_response
['currentPage'] . '" />',
120 2 => $this->_response
['numPages'],
125 * Helper function to assign remaining pager options as good default
128 * @param array $params
129 * The set of options needed to initialize the parent constructor.
133 public function initialize(&$params) {
134 // set the mode for the pager to Sliding
136 $params['mode'] = 'Sliding';
138 // also set the urlVar to be a crm specific get variable.
140 $params['urlVar'] = self
::PAGE_ID
;
142 // set this to a small value, since we dont use this functionality
144 $params['delta'] = 1;
146 $params['totalItems'] = $params['total'];
147 $params['append'] = TRUE;
148 $params['separator'] = '';
149 $params['spacesBeforeSeparator'] = 1;
150 $params['spacesAfterSeparator'] = 1;
151 $params['extraVars'] = ['force' => 1];
152 $params['excludeVars'] = ['reset', 'snippet', 'section'];
154 // set previous and next text labels
155 $params['prevImg'] = ' ' . ts('< Previous');
156 $params['nextImg'] = ts('Next >') . ' ';
158 // set first and last text fragments
159 $params['firstPagePre'] = '';
160 $params['firstPageText'] = ' ' . ts('<< First');
161 $params['firstPagePost'] = '';
163 $params['lastPagePre'] = '';
164 $params['lastPageText'] = ts('Last >>') . ' ';
165 $params['lastPagePost'] = '';
167 if (isset($params['pageID'])) {
168 $params['currentPage'] = $this->getPageID($params['pageID'], $params);
171 $params['perPage'] = $this->getPageRowCount($params['rowCount']);
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)
182 * @param int $defaultPageId
183 * DefaultPageId current pageId.
185 * @param array $params
188 * new pageId to display to the user
190 public function getPageID($defaultPageId = 1, &$params) {
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
])) {
196 $currentPage = max((int ) @$_POST[self
::PAGE_ID
], 1);
198 elseif (isset($_POST[$params['buttonBottom']]) && isset($_POST[self
::PAGE_ID_BOTTOM
])) {
199 $currentPage = max((int ) @$_POST[self
::PAGE_ID_BOTTOM
], 1);
201 elseif (isset($_POST[self
::PAGE_ID
])) {
202 $currentPage = max((int ) @$_POST[self
::PAGE_ID
], 1);
204 elseif (isset($_POST[self
::PAGE_ID_BOTTOM
])) {
205 $currentPage = max((int ) @$_POST[self
::PAGE_ID_BOTTOM
]);
208 elseif (isset($_GET[self
::PAGE_ID
])) {
209 $currentPage = max((int ) @$_GET[self
::PAGE_ID
], 1);
215 * Get the number of rows to display from either a GET / POST variable
217 * @param int $defaultPageRowCount
218 * The default value if not set.
221 * the rowCount value to use
223 public function getPageRowCount($defaultPageRowCount = self
::ROWCOUNT
) {
224 // POST has higher priority than GET vars
225 if (isset($_POST[self
::PAGE_ROWCOUNT
])) {
226 $rowCount = max((int ) @$_POST[self
::PAGE_ROWCOUNT
], 1);
228 elseif (isset($_GET[self
::PAGE_ROWCOUNT
])) {
229 $rowCount = max((int ) @$_GET[self
::PAGE_ROWCOUNT
], 1);
232 $rowCount = $defaultPageRowCount;
238 * Use the pager class to get the pageId and Offset.
241 * an array of the pageID and offset
243 public function getOffsetAndRowCount() {
244 $pageId = $this->getCurrentPageID();
249 $offset = ($pageId - 1) * $this->_perPage
;
251 return [$offset, $this->_perPage
];
257 public function getCurrentLocation() {
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();
266 public function getFirstPageLink() {
267 if ($this->isFirstPage()) {
270 $href = $this->makeURL(self
::PAGE_ID
, 1);
271 return $this->formatLink($href, str_replace('%d', 1, $this->_altFirst
), $this->_firstPagePre
. $this->_firstPageText
. $this->_firstPagePost
) .
272 $this->_spacesBefore
. $this->_spacesAfter
;
278 public function getLastPageLink() {
279 if ($this->isLastPage()) {
282 $href = $this->makeURL(self
::PAGE_ID
, $this->_totalPages
);
283 return $this->formatLink($href, str_replace('%d', $this->_totalPages
, $this->_altLast
), $this->_lastPagePre
. $this->_lastPageText
. $this->_lastPagePost
);
289 public function getBackPageLink() {
290 if ($this->_currentPage
> 1) {
291 $href = $this->makeURL(self
::PAGE_ID
, $this->getPreviousPageID());
292 return $this->formatLink($href, $this->_altPrev
, $this->_prevImg
) . $this->_spacesBefore
. $this->_spacesAfter
;
300 public function getNextPageLink() {
301 if ($this->_currentPage
< $this->_totalPages
) {
302 $href = $this->makeURL(self
::PAGE_ID
, $this->getNextPageID());
303 return $this->_spacesAfter
.
304 $this->formatLink($href, $this->_altNext
, $this->_nextImg
) .
305 $this->_spacesBefore
. $this->_spacesAfter
;
311 * Build a url for pager links.
314 * @param string $value
318 public function makeURL($key, $value) {
319 $href = CRM_Utils_System
::makeURL($key, TRUE);
320 // CRM-12212 Remove alpha sort param
321 if (strpos($href, '&sortByCharacter=')) {
322 $href = preg_replace('#(.*)\&sortByCharacter=[^&]*(.*)#', '\1\2', $href);
324 return $href . $value;
328 * Output the html pager link.
329 * @param string $href
330 * @param string $title
331 * @param string $image
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);