This repository has been archived by the owner on May 14, 2019. It is now read-only.
forked from bermi/akelos
/
pagination.php
444 lines (383 loc) · 18.2 KB
/
pagination.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
# This file is part of the Akelos Framework
# (Copyright) 2004-2010 Bermi Ferrer bermi a t bermilabs com
# See LICENSE and CREDITS for details
require_once(AK_CONTRIB_DIR.DS.'pear'.DS.'HTML'.DS.'Pager'.DS.'Pager.php');
require_once(AK_CONTRIB_DIR.DS.'pear'.DS.'HTML'.DS.'Pager'.DS.'Common.php');
/**
* AkPager and AkPaginator are a fork of Pear::Pager
* coded by Lorenzo Alberton <at quipo.it> and
* Richard Heyes <mailto:richard at phpguru.org>,
*/
class AkPager extends Pager_Common
{
// {{{ AkPager()
/**
* Constructor
*
* @param mixed $options An associative array of option names
* and their values
* @access public
*/
public function init($options = array()) {
//set default AkPager options
$this->_delta = 2;
$this->_prevImg = '«';
$this->_nextImg = '»';
$this->_separator = '|';
$this->_spacesBeforeSeparator = 3;
$this->_spacesAfterSeparator = 3;
$this->_curPageSpanPre = '<b><u>';
$this->_curPageSpanPost = '</u></b>';
//set custom options
$err = $this->_setOptions($options);
if ($err !== PAGER_OK) {
return $this->raiseError($this->errorMessage($err), $err);
}
$this->_generatePageData();
$this->_setFirstLastText();
if ($this->_totalPages > (2 * $this->_delta + 1)) {
$this->links .= $this->_printFirstPage();
}
$this->links .= $this->_getBackLink();
$this->links .= $this->_getPageLinks();
$this->links .= $this->_getNextLink();
$this->linkTags .= $this->_getFirstLinkTag();
$this->linkTags .= $this->_getPrevLinkTag();
$this->linkTags .= $this->_getNextLinkTag();
$this->linkTags .= $this->_getLastLinkTag();
if ($this->_totalPages > (2 * $this->_delta + 1)) {
$this->links .= $this->_printLastPage();
}
}
// }}}
// {{{ getPageIdByOffset()
/**
* "Overload" PEAR::Pager method. VOID. Not needed here...
* @param integer $index Offset to get pageID for
* @deprecated
* @access public
*/
public function getPageIdByOffset($index=null) { }
// }}}
// {{{ getPageRangeByPageId()
/**
* Given a PageId, it returns the limits of the range of pages displayed.
* While getOffsetByPageId() returns the offset of the data within the
* current page, this method returns the offsets of the page numbers interval.
* E.g., if you have pageId=5 and delta=2, it will return (3, 7).
* PageID of 9 would give you (4, 8).
* If the method is called without parameter, pageID is set to currentPage#.
*
* @param integer PageID to get offsets for
* @return array First and last offsets
* @access public
*/
public function getPageRangeByPageId($pageid = null) {
$pageid = isset($pageid) ? (int)$pageid : $this->_currentPage;
if (!isset($this->_pageData)) {
$this->_generatePageData();
}
if (isset($this->_pageData[$pageid]) || is_null($this->_itemData)) {
if ($this->_expanded) {
$min_surplus = ($pageid <= $this->_delta) ? ($this->_delta - $pageid + 1) : 0;
$max_surplus = ($pageid >= ($this->_totalPages - $this->_delta)) ?
($pageid - ($this->_totalPages - $this->_delta)) : 0;
} else {
$min_surplus = $max_surplus = 0;
}
return array(
max($pageid - $this->_delta - $max_surplus, 1),
min($pageid + $this->_delta + $min_surplus, $this->_totalPages)
);
}
return array(0, 0);
}
// }}}
// {{{ getLinks()
/**
* Returns back/next/first/last and page links,
* both as ordered and associative array.
*
* @param integer $pageID Optional pageID. If specified, links
* for that page are provided instead of current one.
* @return array back/pages/next/first/last/all links
* @access public
*/
public function getLinks($pageID=null) {
if ($pageID != null) {
$_sav = $this->_currentPage;
$this->_currentPage = $pageID;
$this->links = '';
if ($this->_totalPages > (2 * $this->_delta + 1)) {
$this->links .= $this->_printFirstPage();
}
$this->links .= $this->_getBackLink();
$this->links .= $this->_getPageLinks();
$this->links .= $this->_getNextLink();
if ($this->_totalPages > (2 * $this->_delta + 1)) {
$this->links .= $this->_printLastPage();
}
}
$back = str_replace(' ', '', $this->_getBackLink());
$next = str_replace(' ', '', $this->_getNextLink());
$pages = $this->_getPageLinks();
$first = $this->_printFirstPage();
$last = $this->_printLastPage();
$all = $this->links;
$linkTags = $this->linkTags;
if ($pageID != null) {
$this->_currentPage = $_sav;
}
return array(
$back,
$pages,
trim($next),
$first,
$last,
$all,
$linkTags,
'back' => $back,
'pages' => $pages,
'next' => $next,
'first' => $first,
'last' => $last,
'all' => $all,
'linktags' => $linkTags
);
}
// }}}
// {{{ _getPageLinks()
/**
* Returns pages link
*
* @return string Links
* @access private
*/
public function _getPageLinks($url = '') {
//legacy setting... the preferred way to set an option now
//is adding it to the constuctor
if (!empty($url)) {
$this->_path = $url;
}
//If there's only one page, don't display links
if ($this->_clearIfVoid && ($this->_totalPages < 2)) {
return '';
}
$links = '';
if ($this->_totalPages > (2 * $this->_delta + 1)) {
if ($this->_expanded) {
if (($this->_totalPages - $this->_delta) <= $this->_currentPage) {
$expansion_before = $this->_currentPage - ($this->_totalPages - $this->_delta);
} else {
$expansion_before = 0;
}
for ($i = $this->_currentPage - $this->_delta - $expansion_before; $expansion_before; $expansion_before--, $i++) {
$print_separator_flag = ($i != $this->_currentPage + $this->_delta); // && ($i != $this->_totalPages - 1)
$this->range[$i] = false;
$this->_linkData[$this->_urlVar] = $i;
$links .= $this->_renderLink($this->_altPage.' '.$i, $i)
. $this->_spacesBefore
. ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
}
}
$expansion_after = 0;
for ($i = $this->_currentPage - $this->_delta; ($i <= $this->_currentPage + $this->_delta) && ($i <= $this->_totalPages); $i++) {
if ($i < 1) {
++$expansion_after;
continue;
}
// check when to print separator
$print_separator_flag = (($i != $this->_currentPage + $this->_delta) && ($i != $this->_totalPages));
if ($i == $this->_currentPage) {
$this->range[$i] = true;
$links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
} else {
$this->range[$i] = false;
$this->_linkData[$this->_urlVar] = $i;
$links .= $this->_renderLink($this->_altPage.' '.$i, $i);
}
$links .= $this->_spacesBefore
. ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
}
if ($this->_expanded && $expansion_after) {
$links .= $this->_separator . $this->_spacesAfter;
for ($i = $this->_currentPage + $this->_delta +1; $expansion_after; $expansion_after--, $i++) {
$print_separator_flag = ($expansion_after != 1);
$this->range[$i] = false;
$this->_linkData[$this->_urlVar] = $i;
$links .= $this->_renderLink($this->_altPage.' '.$i, $i)
. $this->_spacesBefore
. ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
}
}
} else {
//if $this->_totalPages <= (2*Delta+1) show them all
for ($i=1; $i<=$this->_totalPages; $i++) {
if ($i != $this->_currentPage) {
$this->range[$i] = false;
$this->_linkData[$this->_urlVar] = $i;
$links .= $this->_renderLink($this->_altPage.' '.$i, $i);
} else {
$this->range[$i] = true;
$links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
}
$links .= $this->_spacesBefore
. (($i != $this->_totalPages) ? $this->_separator.$this->_spacesAfter : '');
}
}
return $links;
}
// }}}
/**
* Renders a link using the appropriate method
*
* @param altText Alternative text for this link (title property)
* @param linkText Text contained by this link
* @return string The link in string form
* @access private
*/
public function _renderLink($altText, $linkText) {
$href = $this->controller->url_helper->modify_current_url($this->_linkData);
if ($this->_httpMethod == 'GET') {
return sprintf('<a href="%s"%s title="%s">%s</a>',
$href,
empty($this->_classString) ? '' : ' '.$this->_classString,
$altText,
$linkText
);
}
if ($this->_httpMethod == 'POST') {
return sprintf("<a href='javascript:void(0)' onClick='%s'%s title='%s'>%s</a>",
$this->_generateFormOnClick($this->_url, $this->_linkData),
empty($this->_classString) ? '' : ' '.$this->_classString,
$altText,
$linkText
);
}
return '';
}
}
/**
* A class representing a paginator for an Active Record collection.
*/
class AkPaginator
{
/**
* Creates a new AkPaginator on the given +controller+ for a set of items
* of size +item_count+ and having +items_per_page+ items per page.
* Raises an error if items_per_page is out of bounds (i.e., less
* than or equal to zero). The page GET parameter for links defaults to
* "page" and can be overridden with +page_parameter+.
*/
public function &AkPaginator(&$controller, $item_count, $items_per_page, $current_page=1) {
static $paginator;
if($items_per_page <= 0){
trigger_error(Ak::t('must have at least one item per page'),E_USER_WARNING);
}
if(empty($current_page)){
$current_page = 1;
$controller->params[$controller->_pagination_options['parameter']] = 1;
}
$this->controller = $controller;
$controller_name = $controller->Request->getController();
$this->item_count = !empty($item_count) ? $item_count : 0;
$this->items_per_page = $items_per_page;
$this->pages = ceil($item_count/$items_per_page);
if(!isset($paginator[$controller_name.'_paginator'])){
$pager_options = array(
'totalItems'=>$item_count,// Number of items to page (used only if itemData is not provided).
'perPage'=>$items_per_page,//Number of items to display on each page.
'currentPage'=>$current_page,//Initial page number (if you want to show page #2 by default, set currentPage to 2)
'delta'=>4,// Number of page numbers to display before and after the current one.
'mode'=>'Sliding',// "Jumping" or "Sliding" -window - It determines pager behaviour.
'httpMethod'=>'GET',// Specifies the HTTP method to use. Valid values are 'GET' or 'POST'.
//'formID'=>'',//Specifies which HTML form to use in POST mode.
'importQuery'=>true,//if true (default behaviour), variables and values are imported from
//'extraVars'=>'',//additional URL vars to be added to the querystring.
//'excludeVars'=>'',//URL vars to be excluded from the querystring.
// the submitted data (query string) and used in the generated links, otherwise they're ignored completely
'expanded'=>true,// if TRUE, window size is always 2*delta+1
'linkClass'=> 'paginationLink',//Name of CSS class used for link styling.
'curPageLinkClassName'=>'paginationCurrent',//Name of CSS class used for current page link.
'urlVar'=>$controller->_pagination_options['parameter'],//Name of URL var used to indicate the page number. Default value is "pageID".
'path'=> '',//Complete path to the page (without the page name).
'fileName'=>'?'.$controller->_pagination_options['parameter'].'=%d',//name of the page, with a "%d" if append == TRUE.
'append'=>false,//If TRUE pageID is appended as GET value to the URL. If FALSE it is embedded in the URL according to fileName specs.
'altFirst'=>Ak::t('first page'),//Alt text to display on the link of the first page. Default value is "first page";
//if you want a string with the page number, use "%d" as a placeholder (for instance "page %d")
'altPrev'=>Ak::t('previous page'),// Alt text to display on the link of the previous page. Default value is "previous page";
'altNext'=>Ak::t('next page'),//Alt text to display on the link of the next page. Default value is "next page";
'altLast'=>Ak::t('last page'),//Alt text to display on the link of the last page. Default value is "last page";
//if you want a string with the page number, use "%d" as a placeholder (for instance "page %d")
'altPage'=>Ak::t('page').' ',//Alt text to display before the page number. Default value is "page ".
'prevImg'=>'<span class="paginationPrevious">'.Ak::t('previous')."</span>",//Something to display instead of "<<". It can be text such as "<< PREV" or an <img/> as well.
'nextImg'=>'<span class="paginationNext">'.Ak::t('next').'</span>',//Something to display instead of ">>". It can be text such as "NEXT >>" or an <img/> as well.
'separator'=>' ',// What to use to separate numbers. It can be an <img/>, a comma, an hyphen, or whatever.
'spacesBeforeSeparator'=>0,//Number of spaces before the separator.
'spacesAfterSeparator'=>0,//Number of spaces after the separator.
'firstPagePre'=>' ',//String used before first page number. It can be an <img/>, a "{", an empty string, or whatever.
'firstPageText'=>'<span class="paginationFirst">'.Ak::t('first').'</span>',//String used in place of first page number.
'firstPagePost'=>' ',//String used after first page number. It can be an <img/>, a "}", an empty string, or whatever.
'lastPagePre'=>' ',//Similar to firstPagePre, but used for last page number.
'lastPageText'=>'<span class="paginationLast">'.Ak::t('last').'</span>',//Similar to firstPageText, but used for last page number.
'lastPagePost'=>' ',//Similar to firstPagePost, but used for last page number.
'clearIfVoid'=>true,//if there's only one page, don't display pager links (returns an empty string).
'useSessions'=>true,//if TRUE, number of items to display per page is stored in the $_SESSION[$_sessionVar] var.
'closeSession'=>false,//if TRUE, the session is closed just after R/W.
'sessionVar'=>$controller_name.'_paginator',//Name of the session var for perPage value. A value different from default can be useful when using more than one Pager istance in the page.
'showAllText'=>Ak::t('show all')// Text to be used for the 'show all' option in the select box generated by getPerPageSelectBox()
);
$paginator[$controller_name.'_paginator'] = new AkPager();
$paginator[$controller_name.'_paginator']->controller = $controller;
$paginator[$controller_name.'_paginator']->init($pager_options);
}
$this->paginator = $paginator[$controller_name.'_paginator'];
$this->controller->paginator = $this->paginator;
$this->links = $this->links();
//$paginator[$controller_name.'_paginator']->links = 'Hola';
return $paginator[$controller_name.'_paginator'];
}
public function &getController() {
return $this->controller;
}
public function getItemCount() {
return $this->item_count;
}
public function getItemsPerPage() {
return $this->items_per_page;
}
public function getOffset() {
return Ak::first($this->paginator->getOffsetByPageId($this->getCurrent()))-1;
}
public function getCurrent() {
return $this->paginator->getCurrentPageID();
}
public function getCurrentPage() {
return $this->paginator->getCurrentPageID();
}
public function getFirstPage() {
return 1;
}
public function getFirst() {
return 1;
}
public function getLast() {
return $this->paginator->getLastPage();
}
public function pageCount() {
return $this->paginator->numPages();
}
public function lenght() {
return $this->pageCount();
}
/**
* Returns true if this paginator contains the page of index +number+.
*/
public function hasPageNumber($number) {
return $number >= 1 && $number <= $this->pageCount();
}
public function links() {
return $this->paginator->links;
}
}