| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * @package CRM |
| 30 | * @copyright CiviCRM LLC (c) 2004-2019 |
| 31 | */ |
| 32 | |
| 33 | /** |
| 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 |
| 39 | */ |
| 40 | |
| 41 | require_once 'Pager/Sliding.php'; |
| 42 | |
| 43 | /** |
| 44 | * Class CRM_Utils_Pager |
| 45 | */ |
| 46 | class CRM_Utils_Pager extends Pager_Sliding { |
| 47 | |
| 48 | /** |
| 49 | * Constants for static parameters of the pager |
| 50 | */ |
| 51 | const ROWCOUNT = 50, PAGE_ID = 'crmPID', PAGE_ID_TOP = 'crmPID', PAGE_ID_BOTTOM = 'crmPID_B', PAGE_ROWCOUNT = 'crmRowCount'; |
| 52 | |
| 53 | /** |
| 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 |
| 56 | * |
| 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 | * |
| 67 | * @param array $params |
| 68 | * |
| 69 | * @return \CRM_Utils_Pager the newly created and initialized pager object |
| 70 | */ |
| 71 | public function __construct($params) { |
| 72 | if ($params['status'] === NULL) { |
| 73 | $params['status'] = ts('Contacts %%StatusMessage%%'); |
| 74 | } |
| 75 | |
| 76 | $this->initialize($params); |
| 77 | |
| 78 | parent::__construct($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), |
| 106 | 'currentLocation' => $this->getCurrentLocation(), |
| 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 |
| 112 | * page variable, but a different form element for one at the bottom. |
| 113 | */ |
| 114 | $this->_response['titleTop'] = ts('Page %1 of %2', array( |
| 115 | 1 => '<input size="2" maxlength="4" name="' . self::PAGE_ID . '" type="text" value="' . $this->_response['currentPage'] . '" />', |
| 116 | 2 => $this->_response['numPages'], |
| 117 | )); |
| 118 | $this->_response['titleBottom'] = ts('Page %1 of %2', array( |
| 119 | 1 => '<input size="2" maxlength="4" name="' . self::PAGE_ID_BOTTOM . '" type="text" value="' . $this->_response['currentPage'] . '" />', |
| 120 | 2 => $this->_response['numPages'], |
| 121 | )); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Helper function to assign remaining pager options as good default |
| 126 | * values. |
| 127 | * |
| 128 | * @param array $params |
| 129 | * The set of options needed to initialize the parent constructor. |
| 130 | * |
| 131 | * @return array |
| 132 | */ |
| 133 | public function initialize(&$params) { |
| 134 | // set the mode for the pager to Sliding |
| 135 | |
| 136 | $params['mode'] = 'Sliding'; |
| 137 | |
| 138 | // also set the urlVar to be a crm specific get variable. |
| 139 | |
| 140 | $params['urlVar'] = self::PAGE_ID; |
| 141 | |
| 142 | // set this to a small value, since we dont use this functionality |
| 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('< Previous'); |
| 156 | $params['nextImg'] = ts('Next >') . ' '; |
| 157 | |
| 158 | // set first and last text fragments |
| 159 | $params['firstPagePre'] = ''; |
| 160 | $params['firstPageText'] = ' ' . ts('<< First'); |
| 161 | $params['firstPagePost'] = ''; |
| 162 | |
| 163 | $params['lastPagePre'] = ''; |
| 164 | $params['lastPageText'] = ts('Last >>') . ' '; |
| 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 | * |
| 182 | * @param int $defaultPageId |
| 183 | * DefaultPageId current pageId. |
| 184 | * |
| 185 | * @param array $params |
| 186 | * |
| 187 | * @return int |
| 188 | * new pageId to display to the user |
| 189 | */ |
| 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); |
| 197 | } |
| 198 | elseif (isset($_POST[$params['buttonBottom']]) && isset($_POST[self::PAGE_ID_BOTTOM])) { |
| 199 | $currentPage = max((int ) @$_POST[self::PAGE_ID_BOTTOM], 1); |
| 200 | } |
| 201 | elseif (isset($_POST[self::PAGE_ID])) { |
| 202 | $currentPage = max((int ) @$_POST[self::PAGE_ID], 1); |
| 203 | } |
| 204 | elseif (isset($_POST[self::PAGE_ID_BOTTOM])) { |
| 205 | $currentPage = max((int ) @$_POST[self::PAGE_ID_BOTTOM]); |
| 206 | } |
| 207 | } |
| 208 | elseif (isset($_GET[self::PAGE_ID])) { |
| 209 | $currentPage = max((int ) @$_GET[self::PAGE_ID], 1); |
| 210 | } |
| 211 | return $currentPage; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Get the number of rows to display from either a GET / POST variable |
| 216 | * |
| 217 | * @param int $defaultPageRowCount |
| 218 | * The default value if not set. |
| 219 | * |
| 220 | * @return int |
| 221 | * the rowCount value to use |
| 222 | */ |
| 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); |
| 227 | } |
| 228 | elseif (isset($_GET[self::PAGE_ROWCOUNT])) { |
| 229 | $rowCount = max((int ) @$_GET[self::PAGE_ROWCOUNT], 1); |
| 230 | } |
| 231 | else { |
| 232 | $rowCount = $defaultPageRowCount; |
| 233 | } |
| 234 | return $rowCount; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Use the pager class to get the pageId and Offset. |
| 239 | * |
| 240 | * @return array |
| 241 | * an array of the pageID and offset |
| 242 | */ |
| 243 | public function getOffsetAndRowCount() { |
| 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 | |
| 254 | /** |
| 255 | * @return string |
| 256 | */ |
| 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(); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @return string |
| 265 | */ |
| 266 | public function getFirstPageLink() { |
| 267 | if ($this->isFirstPage()) { |
| 268 | return ''; |
| 269 | } |
| 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; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @return string |
| 277 | */ |
| 278 | public function getLastPageLink() { |
| 279 | if ($this->isLastPage()) { |
| 280 | return ''; |
| 281 | } |
| 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); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * @return string |
| 288 | */ |
| 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; |
| 293 | } |
| 294 | return ''; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * @return string |
| 299 | */ |
| 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; |
| 306 | } |
| 307 | return ''; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Build a url for pager links. |
| 312 | * |
| 313 | * @param string $key |
| 314 | * @param string $value |
| 315 | * |
| 316 | * @return string |
| 317 | */ |
| 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); |
| 323 | } |
| 324 | return $href . $value; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Output the html pager link. |
| 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 | } |
| 337 | |
| 338 | } |