initialize($params);
$this->Pager_Sliding($params);
list($offset, $limit) = $this->getOffsetAndRowCount();
$start = $offset + 1;
$end = $offset + $limit;
if ($end > $params['total']) {
$end = $params['total'];
}
if ($params['total'] == 0) {
$statusMessage = '';
}
else {
$statusMessage = ts('%1 - %2 of %3', array(1 => $start, 2 => $end, 3 => $params['total']));
}
$params['status'] = str_replace('%%StatusMessage%%', $statusMessage, $params['status']);
$this->_response = array(
'first' => $this->getFirstPageLink(),
'back' => $this->getBackPageLink(),
'next' => $this->getNextPageLink(),
'last' => $this->getLastPageLink(),
'currentPage' => $this->getCurrentPageID(),
'numPages' => $this->numPages(),
'csvString' => CRM_Utils_Array::value('csvString', $params),
'status' => CRM_Utils_Array::value('status', $params),
'buttonTop' => CRM_Utils_Array::value('buttonTop', $params),
'buttonBottom' => CRM_Utils_Array::value('buttonBottom', $params),
'twentyfive' => $this->getPerPageLink(25),
'fifty' => $this->getPerPageLink(50),
'onehundred' => $this->getPerPageLink(100),
);
/**
* A page cannot have two variables with the same form name. Hence in the
* pager display, we have a form submission at the top with the normal
* page variable, but a different form element for one at the bottom
*
*/
$this->_response['titleTop'] = ts('Page %1 of %2', array(1 => '', 2 => $this->_response['numPages']));
$this->_response['titleBottom'] = ts('Page %1 of %2', array(1 => '', 2 => $this->_response['numPages']));
}
/**
* helper function to assign remaining pager options as good default
* values
*
* @param array $params the set of options needed to initialize the parent
* constructor
*
* @access public
*
* @return void
*
*/
function initialize(&$params) {
/* set the mode for the pager to Sliding */
$params['mode'] = 'Sliding';
/* also set the urlVar to be a crm specific get variable */
$params['urlVar'] = self::PAGE_ID;
/* set this to a small value, since we dont use this functionality */
$params['delta'] = 1;
$params['totalItems'] = $params['total'];
$params['append'] = TRUE;
$params['separator'] = '';
$params['spacesBeforeSeparator'] = 1;
$params['spacesAfterSeparator'] = 1;
$params['extraVars'] = array('force' => 1);
$params['excludeVars'] = array('reset', 'snippet', 'section');
// set previous and next text labels
$params['prevImg'] = ' ' . ts('< Previous');
$params['nextImg'] = ts('Next >') . ' ';
// set first and last text fragments
$params['firstPagePre'] = '';
$params['firstPageText'] = ' ' . ts('<< First');
$params['firstPagePost'] = '';
$params['lastPagePre'] = '';
$params['lastPageText'] = ts('Last >>') . ' ';
$params['lastPagePost'] = '';
if (isset($params['pageID'])) {
$params['currentPage'] = $this->getPageID($params['pageID'], $params);
}
$params['perPage'] = $this->getPageRowCount($params['rowCount']);
return $params;
}
/**
* Figure out the current page number based on value of
* GET / POST variables. Hierarchy rules are followed,
* POST over-rides a GET, a POST at the top overrides
* a POST at the bottom (of the page)
*
* @param int defaultPageId current pageId
*
* @return int new pageId to display to the user
* @access public
*
*/
function getPageID($defaultPageId = 1, &$params) {
// POST has higher priority than GET vars
// else if a value is set that has higher priority and finally the GET var
$currentPage = $defaultPageId;
if (!empty($_POST)) {
if (isset($_POST[CRM_Utils_Array::value('buttonTop', $params)]) && isset($_POST[self::PAGE_ID])) {
$currentPage = max((int )@$_POST[self::PAGE_ID], 1);
}
elseif (isset($_POST[$params['buttonBottom']]) && isset($_POST[self::PAGE_ID_BOTTOM])) {
$currentPage = max((int )@$_POST[self::PAGE_ID_BOTTOM], 1);
}
elseif (isset($_POST[self::PAGE_ID])) {
$currentPage = max((int )@$_POST[self::PAGE_ID], 1);
}
elseif (isset($_POST[self::PAGE_ID_BOTTOM])) {
$currentPage = max((int )@$_POST[self::PAGE_ID_BOTTOM]);
}
}
elseif (isset($_GET[self::PAGE_ID])) {
$currentPage = max((int )@$_GET[self::PAGE_ID], 1);
}
return $currentPage;
}
/**
* Get the number of rows to display from either a GET / POST variable
*
* @param int $defaultPageRowCount the default value if not set
*
* @return int the rowCount value to use
* @access public
*
*/
function getPageRowCount($defaultPageRowCount = self::ROWCOUNT) {
// POST has higher priority than GET vars
if (isset($_POST[self::PAGE_ROWCOUNT])) {
$rowCount = max((int )@$_POST[self::PAGE_ROWCOUNT], 1);
}
elseif (isset($_GET[self::PAGE_ROWCOUNT])) {
$rowCount = max((int )@$_GET[self::PAGE_ROWCOUNT], 1);
}
else {
$rowCount = $defaultPageRowCount;
}
return $rowCount;
}
/**
* Use the pager class to get the pageId and Offset
*
* @param void
*
* @return array: an array of the pageID and offset
*
* @access public
*
*/
function getOffsetAndRowCount() {
$pageId = $this->getCurrentPageID();
if (!$pageId) {
$pageId = 1;
}
$offset = ($pageId - 1) * $this->_perPage;
return array($offset, $this->_perPage);
}
/**
* given a number create a link that will display the number of
* rows as specified by that link
*
* @param int $perPage the number of rows
*
* @return string the link
* @access void
*/
function getPerPageLink($perPage) {
if ($perPage != $this->_perPage) {
$href = $this->makeURL(self::PAGE_ROWCOUNT, $perPage);
$link = sprintf('%s',
$href,
$this->_classString,
$perPage
) . $this->_spacesBefore . $this->_spacesAfter;
}
else {
$link = $this->_spacesBefore . $perPage . $this->_spacesAfter;
}
return $link;
}
function getFirstPageLink() {
if ($this->isFirstPage()) {
return '';
}
$href = $this->makeURL(self::PAGE_ID, 1);
return sprintf('%s',
$href,
str_replace('%d', 1, $this->_altFirst),
$this->_firstPagePre . $this->_firstPageText . $this->_firstPagePost
) . $this->_spacesBefore . $this->_spacesAfter;
}
function getLastPageLink() {
if ($this->isLastPage()) {
return '';
}
$href = $this->makeURL(self::PAGE_ID, $this->_totalPages);
return sprintf('%s',
$href,
str_replace('%d', $this->_totalPages, $this->_altLast),
$this->_lastPagePre . $this->_lastPageText . $this->_lastPagePost
);
}
function getBackPageLink() {
if ($this->_currentPage > 1) {
$href = $this->makeURL(self::PAGE_ID, $this->getPreviousPageID());
return sprintf('%s',
$href,
$this->_altPrev, $this->_prevImg
) . $this->_spacesBefore . $this->_spacesAfter;
}
return '';
}
function getNextPageLink() {
if ($this->_currentPage < $this->_totalPages) {
$href = $this->makeURL(self::PAGE_ID, $this->getNextPageID());
return $this->_spacesAfter . sprintf('%s',
$href,
$this->_altNext, $this->_nextImg
) . $this->_spacesBefore . $this->_spacesAfter;
}
return '';
}
/**
* Build a url for pager links
*/
function makeURL($key, $value) {
$href = CRM_Utils_System::makeURL($key);
// CRM-12212 Remove alpha sort param
if (strpos($href, '&sortByCharacter=')) {
$href = preg_replace('#(.*)\&sortByCharacter=[^&]*(.*)#', '\1\2', $href);
}
return $href . $value;
}
}