4774a6b93e39ff0f91487c5598f9434b74b760b5
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
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
46 require_once 'Pager/Sliding.php';
49 * Class CRM_Utils_Pager
51 class CRM_Utils_Pager
extends Pager_Sliding
{
54 * Constants for static parameters of the pager
56 const ROWCOUNT
= 50, PAGE_ID
= 'crmPID', PAGE_ID_TOP
= 'crmPID', PAGE_ID_BOTTOM
= 'crmPID_B', PAGE_ROWCOUNT
= 'crmRowCount';
59 * The output of the pager. This is a name/value array with various keys
60 * that an application could use to display the pager
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
71 * @param array $params
73 * @internal param \total $int the total count of items to be displayed
74 * @internal param \currentPage $int the page currently being displayed
75 * @internal param \status $string the status message to be displayed. It embeds a token
76 * %%statusMessage%% that will be replaced with which items
77 * are currently being displayed
78 * @internal param \csvString $string the title of the link to be displayed for the export
79 * @internal param \perPage $int the number of items displayed per page
81 * @return \CRM_Utils_Pager the newly created and initialized pager object
84 public function __construct($params) {
85 if ($params['status'] === NULL) {
86 $params['status'] = ts('Contacts %%StatusMessage%%');
89 $this->initialize($params);
91 $this->Pager_Sliding($params);
93 list($offset, $limit) = $this->getOffsetAndRowCount();
95 $end = $offset +
$limit;
96 if ($end > $params['total']) {
97 $end = $params['total'];
100 if ($params['total'] == 0) {
104 $statusMessage = ts('%1 - %2 of %3', array(1 => $start, 2 => $end, 3 => $params['total']));
106 $params['status'] = str_replace('%%StatusMessage%%', $statusMessage, $params['status']);
108 $this->_response
= array(
109 'first' => $this->getFirstPageLink(),
110 'back' => $this->getBackPageLink(),
111 'next' => $this->getNextPageLink(),
112 'last' => $this->getLastPageLink(),
113 'currentPage' => $this->getCurrentPageID(),
114 'numPages' => $this->numPages(),
115 'csvString' => CRM_Utils_Array
::value('csvString', $params),
116 'status' => CRM_Utils_Array
::value('status', $params),
117 'buttonTop' => CRM_Utils_Array
::value('buttonTop', $params),
118 'buttonBottom' => CRM_Utils_Array
::value('buttonBottom', $params),
119 'currentLocation' => $this->getCurrentLocation(),
123 * A page cannot have two variables with the same form name. Hence in the
124 * pager display, we have a form submission at the top with the normal
125 * page variable, but a different form element for one at the bottom
128 $this->_response
['titleTop'] = ts('Page %1 of %2', array(1 => '<input size="2" maxlength="3" name="' . self
::PAGE_ID
. '" type="text" value="' . $this->_response
['currentPage'] . '" />', 2 => $this->_response
['numPages']));
129 $this->_response
['titleBottom'] = ts('Page %1 of %2', array(1 => '<input size="2" maxlength="3" name="' . self
::PAGE_ID_BOTTOM
. '" type="text" value="' . $this->_response
['currentPage'] . '" />', 2 => $this->_response
['numPages']));
133 * Helper function to assign remaining pager options as good default
136 * @param array $params
137 * The set of options needed to initialize the parent.
144 public function initialize(&$params) {
145 /* set the mode for the pager to Sliding */
147 $params['mode'] = 'Sliding';
149 /* also set the urlVar to be a crm specific get variable */
151 $params['urlVar'] = self
::PAGE_ID
;
153 /* set this to a small value, since we dont use this functionality */
155 $params['delta'] = 1;
157 $params['totalItems'] = $params['total'];
158 $params['append'] = TRUE;
159 $params['separator'] = '';
160 $params['spacesBeforeSeparator'] = 1;
161 $params['spacesAfterSeparator'] = 1;
162 $params['extraVars'] = array('force' => 1);
163 $params['excludeVars'] = array('reset', 'snippet', 'section');
165 // set previous and next text labels
166 $params['prevImg'] = ' ' . ts('< Previous');
167 $params['nextImg'] = ts('Next >') . ' ';
169 // set first and last text fragments
170 $params['firstPagePre'] = '';
171 $params['firstPageText'] = ' ' . ts('<< First');
172 $params['firstPagePost'] = '';
174 $params['lastPagePre'] = '';
175 $params['lastPageText'] = ts('Last >>') . ' ';
176 $params['lastPagePost'] = '';
178 if (isset($params['pageID'])) {
179 $params['currentPage'] = $this->getPageID($params['pageID'], $params);
182 $params['perPage'] = $this->getPageRowCount($params['rowCount']);
188 * Figure out the current page number based on value of
189 * GET / POST variables. Hierarchy rules are followed,
190 * POST over-rides a GET, a POST at the top overrides
191 * a POST at the bottom (of the page)
193 * @param int $defaultPageId
194 * DefaultPageId current pageId.
196 * @param array $params
199 * new pageId to display to the user
201 public function getPageID($defaultPageId = 1, &$params) {
202 // POST has higher priority than GET vars
203 // else if a value is set that has higher priority and finally the GET var
204 $currentPage = $defaultPageId;
205 if (!empty($_POST)) {
206 if (isset($_POST[CRM_Utils_Array
::value('buttonTop', $params)]) && isset($_POST[self
::PAGE_ID
])) {
207 $currentPage = max((int ) @$_POST[self
::PAGE_ID
], 1);
209 elseif (isset($_POST[$params['buttonBottom']]) && isset($_POST[self
::PAGE_ID_BOTTOM
])) {
210 $currentPage = max((int ) @$_POST[self
::PAGE_ID_BOTTOM
], 1);
212 elseif (isset($_POST[self
::PAGE_ID
])) {
213 $currentPage = max((int ) @$_POST[self
::PAGE_ID
], 1);
215 elseif (isset($_POST[self
::PAGE_ID_BOTTOM
])) {
216 $currentPage = max((int ) @$_POST[self
::PAGE_ID_BOTTOM
]);
219 elseif (isset($_GET[self
::PAGE_ID
])) {
220 $currentPage = max((int ) @$_GET[self
::PAGE_ID
], 1);
226 * Get the number of rows to display from either a GET / POST variable
228 * @param int $defaultPageRowCount
229 * The default value if not set.
232 * the rowCount value to use
235 public function getPageRowCount($defaultPageRowCount = self
::ROWCOUNT
) {
236 // POST has higher priority than GET vars
237 if (isset($_POST[self
::PAGE_ROWCOUNT
])) {
238 $rowCount = max((int ) @$_POST[self
::PAGE_ROWCOUNT
], 1);
240 elseif (isset($_GET[self
::PAGE_ROWCOUNT
])) {
241 $rowCount = max((int ) @$_GET[self
::PAGE_ROWCOUNT
], 1);
244 $rowCount = $defaultPageRowCount;
250 * Use the pager class to get the pageId and Offset
255 * an array of the pageID and offset
259 public function getOffsetAndRowCount() {
260 $pageId = $this->getCurrentPageID();
265 $offset = ($pageId - 1) * $this->_perPage
;
267 return array($offset, $this->_perPage
);
273 public function getCurrentLocation() {
274 $config = CRM_Core_Config
::singleton();
275 $path = CRM_Utils_Array
::value($config->userFrameworkURLVar
, $_GET);
276 return CRM_Utils_System
::url($path, CRM_Utils_System
::getLinksUrl(self
::PAGE_ID
, FALSE, TRUE), FALSE, NULL, FALSE) . $this->getCurrentPageID();
282 public function getFirstPageLink() {
283 if ($this->isFirstPage()) {
286 $href = $this->makeURL(self
::PAGE_ID
, 1);
287 return $this->formatLink($href, str_replace('%d', 1, $this->_altFirst
), $this->_firstPagePre
. $this->_firstPageText
. $this->_firstPagePost
) .
288 $this->_spacesBefore
. $this->_spacesAfter
;
294 public function getLastPageLink() {
295 if ($this->isLastPage()) {
298 $href = $this->makeURL(self
::PAGE_ID
, $this->_totalPages
);
299 return $this->formatLink($href, str_replace('%d', $this->_totalPages
, $this->_altLast
), $this->_lastPagePre
. $this->_lastPageText
. $this->_lastPagePost
);
305 public function getBackPageLink() {
306 if ($this->_currentPage
> 1) {
307 $href = $this->makeURL(self
::PAGE_ID
, $this->getPreviousPageID());
308 return $this->formatLink($href, $this->_altPrev
, $this->_prevImg
) . $this->_spacesBefore
. $this->_spacesAfter
;
316 public function getNextPageLink() {
317 if ($this->_currentPage
< $this->_totalPages
) {
318 $href = $this->makeURL(self
::PAGE_ID
, $this->getNextPageID());
319 return $this->_spacesAfter
.
320 $this->formatLink($href, $this->_altNext
, $this->_nextImg
) .
321 $this->_spacesBefore
. $this->_spacesAfter
;
327 * Build a url for pager links
329 public function makeURL($key, $value) {
330 $href = CRM_Utils_System
::makeURL($key, TRUE);
331 // CRM-12212 Remove alpha sort param
332 if (strpos($href, '&sortByCharacter=')) {
333 $href = preg_replace('#(.*)\&sortByCharacter=[^&]*(.*)#', '\1\2', $href);
335 return $href . $value;
339 * Output the html pager link
340 * @param string $href
341 * @param string $title
342 * @param string $image
345 private function formatLink($href, $title, $image) {
346 return sprintf('<a class="crm-pager-link action-item crm-hover-button" href="%s" title="%s">%s</a>', $href, $title, $image);