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