/
DooPager.php
416 lines (362 loc) · 13.8 KB
/
DooPager.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
<?php
/**
* DooPager class file.
*
* @author Leng Sheng Hong <darkredz@gmail.com>
* @link http://www.doophp.com/
* @copyright Copyright © 2009 Leng Sheng Hong
* @license http://www.doophp.com/license
*/
/**
* A helper class to help generating pagination widget when displaying a list of items.
*
* You can change every component of the pagination widget through CSS. The CSS class name for the components can be changed if needed.
*
* <p>You can send GET, POST, PUT and DELETE requests with DooRestClient.
* Method chaining is also supported by this class.</p>
* Example usage:
* <code>
* Doo::loadHelper('DooPager');
* Doo::loadModel('User');
*
* $page = $this->params['pindex'];
* $total = User::getTotal();
* $pager = new DooPager(Doo::conf()->APP_URL.'main/gallery', $total, 6, 10);
*
* #change the text of next/prev
* //$pager->prevText = 'prev';
* //$pager->nextText = 'next';
*
* #If you don't need the next/prev links
* //$pager->noNextPrev();
*
* #Do the pagination
* $pager->paginate($page);
*
* $limit = $pager->limit;
* Doo::db()->find('User', array('limit'=>$limit));
*
* echo $pager->defaultCss;
* //echo $pager->components['prev_link'];
* echo $pager->output;
* echo $pager->components['jump_menu'];
* echo $pager->components['page_size'];
* //echo $pager->components['next_link'];
* </code>
*
* @author Leng Sheng Hong <darkredz@gmail.com>
* @version $Id: DooPager.php 1000 2009-08-21 15:27:22
* @package doo.helper
* @since 1.0
*/
class DooPager{
/**
* CSS class name for the Pages links
* @var string
*/
public $pagesCss='paginate';
/**
* CSS class name for the Pages DropDown menu
* @var string
*/
public $dropDownCss = 'pagerDropDown';
/**
* CSS class name for the page sizes menu
* @var string
*/
public $pageSizeCss = 'pageSize';
/**
* CSS class name for inactive links
* @var string
*/
public $inactivePrevCss = 'inactivePrev';
/**
* CSS class name for inactive links
* @var string
*/
public $inactiveNextCss = 'inactiveNext';
/**
* CSS class name for selected current link
* @var string
*/
public $currentCss = 'currentpager';
/**
* CSS class name for next link
* @var string
*/
public $nextCss = 'next';
/**
* CSS class name for previous link
* @var string
*/
public $prevCss = 'previous';
/**
* Text for the Previous link
* @var string
*/
public $prevText = '«';
/**
* Text for the Next button link
* @var string
*/
public $nextText = '»';
/**
* Contain the list of components to be used in view. (pages, jump menu, page_size, current_page, total_page)
* @var array
*/
public $components;
/**
* Items to be displayed per page
* @var int
*/
public $itemPerPage=10;
/**
* The current page number
* @var int
*/
public $currentPage = 1;
/**
* Maximum Pager length
* @var int
*/
public $maxLength = 10;
/**
* Total items to be split in the pagination
* @var int
*/
public $totalItem;
/**
* Total pages in the pagination
* @var int
*/
public $totalPage;
/**
* The pages HTML output
* @var string
*/
public $output;
/**
* The URL prefix for all the pagination links
* @var string
*/
public $baseUrl;
//----- for SQL use -----
/**
* Position of the record to begin the pagination LIMIT query
* @var string
*/
public $low;
/**
* Position of the record to end the pagination LIMIT query
* @var string
*/
public $high;
/**
* To be use with the pagination LIMIT query LIMIT $limit
* @var string
*/
public $limit;
/**
* To display/hide the next/prev button links
* @var bool
*/
private $_noNextPrev=false;
/**
* Default css for testing
* @var string
*/
public $defaultCss = '<style>
.paginate, .currentpager, .inactivePrev, .inactiveNext, .prev, .next {
font-family: Verdana,Helvetica,Arial,sans-serif;
font-size:12px;
border:1px solid #D8D8D8;
float:left;
height:20px;
line-height:20px;
margin-right:2px;
overflow:hidden;
padding:0 6px;
text-decoration:none;
}
a:hover .paginate{
border-color:#006699;
}
.currentpager {
border-color:#006699;
background-color:#006699;
color:#fff;
font-weight:bold;
}
.inactivePrev, .inactiveNext{
color:#999;
}
</style>';
/**
* Instanciate the Pager object
*
* @param string $baseURL Base URL to be appended to the page number
* @param int $totalItems Total items to be paginate
* @param int $itemPerPage Items to be shown in one page.
* @param int $maxlength Number of links for the pager navigation
* @param string $prevText Text for the Previous button link
* @param string $nextText Text for the Next button link
*/
function __construct($baseURL='', $totalItems=120, $itemPerPage=10, $maxlength=10, $prevText='«', $nextText='»'){
$this->baseUrl = $baseURL;
$this->totalItem = $totalItems;
$this->maxLength = $maxlength;
$this->itemPerPage = $itemPerPage;
$this->prevText = $prevText;
$this->nextText = $nextText;
}
/**
* Hide next & prev links from the output
*/
public function noNextPrev(){
$this->_noNextPrev = true;
}
/**
* Set the CSS class name for the pager components
*
* @param string $pagesName
* @param string $inactiveName
* @param string $currentName
* @param string $nextName
* @param string $prevName
* @param string $dropDownName
* @param string $pageSizeName
*/
public function setCss($pagesName='paginate', $inactiveName='inactive', $currentName='current', $nextName='next', $prevName='prev', $dropDownName='pagerDropDown', $pageSizeName='pageSize'){
$this->pagesCss = $pagesName;
$this->inactiveCss = $inactiveName;
$this->currentCss = $currentName;
$this->nextCss = $nextName;
$this->prevCss = $prevName;
$this->dropDownCss = $dropDownName;
$this->pageSizeCss = $pageSizeName;
}
/**
* Paginate the list of items and prepare pager components to be use in View.
*
* @param int $page The current page number
* @param int $itemPerPage Items per page
* @return array An array of pager component, access via <strong>pages, jump_menu, page_size, current_page, total_page, next_link, prev_link</strong>
*/
public function paginate($page, $itemPerPage=0){
if($itemPerPage===0){
$itemPerPage = $this->itemPerPage;
}else{
$this->itemPerPage = $itemPerPage;
}
$this->output .='<ul class="pagination">';
$this->totalPage = ceil($this->totalItem/$itemPerPage);
$this->currentPage = (int) $page;
if($this->currentPage < 1 || !is_numeric($this->currentPage))
$this->currentPage = 1;
if($this->currentPage > $this->totalPage)
$this->currentPage = $this->totalPage;
$prev_page = $this->currentPage-1;
$next_page = $this->currentPage+1;
if($this->_noNextPrev)
$this->components['prev_link'] = ($this->currentPage != 1 && $this->totalItem >= $this->maxLength) ? "<li class=\"{$this->prevCss}\"><a href=\"{$this->baseUrl}/{$prev_page}\">{$this->prevText}</a></li> ":"<li class=\"{$this->inactivePrevCss}\"><a >{$this->prevText}</a></li> ";
else
$this->output .= ($this->currentPage != 1 && $this->totalItem >= $this->maxLength) ? "<li class=\"{$this->prevCss}\" ><a href=\"{$this->baseUrl}/{$prev_page}\">{$this->prevText}</a></li> ":"<li class=\"{$this->inactivePrevCss}\"><a >{$this->prevText}</a></li> ";
if($this->totalPage > $this->maxLength){
$midRange = $this->maxLength-2;
$start_range = $this->currentPage - floor($midRange/2);
$end_range = $this->currentPage + floor($midRange/2);
if($start_range <= 0){
$end_range += abs($start_range)+1;
$start_range = 1;
}
if($end_range > $this->totalPage){
$start_range -= $end_range-$this->totalPage;
$end_range = $this->totalPage;
}
while($end_range-$start_range+1<$this->maxLength-1){
$end_range++;
}
$modulus = (int) $this->maxLength%2==0;
$center = floor($this->maxLength/2);
if($this->currentPage > $center ){
$end_range --;
}
if($modulus==0 && $this->totalPage - $this->currentPage+1 <= $center){
while($end_range-$start_range+1<$this->maxLength-1){
$start_range--;
}
}
$range = range($start_range,$end_range);
for($i=1;$i<=$this->totalPage;$i++){
// loop through all pages. if first, last, or in range, display
if($i==1 || $i==$this->totalPage || in_array($i,$range)){
$lastDot = '';
if($modulus==1){
if($i==$this->totalPage && $this->currentPage<($this->totalPage-($this->maxLength-$center-$modulus)))
$lastDot = '...';
}else{
if($i==$this->totalPage && $this->currentPage<=($this->totalPage-($this->maxLength-$center)))
$lastDot = '...';
}
$this->output .= ($i == $this->currentPage) ? "<li class =\"{$this->currentCss}\" ><a href=\"javascript:void(0);\">$i":"<li class=\"{$this->pagesCss}\" ><a href=\"{$this->baseUrl}/$i\">$lastDot $i";
if($range[0] > 2 && $i == 1)
$this->output .= " ...</a> </li>";
else
$this->output .= '</a> </li>';
}
}
if($this->_noNextPrev)
$this->components['next_link'] = ($this->currentPage != $this->totalPage && $this->totalItem >= $this->maxLength) ? "<li class=\"{$this->nextCss}\" ><a href=\"{$this->baseUrl}/$next_page\">{$this->nextText}</a></li>\n" : "<li class=\"{$this->inactiveNextCss}\">{$this->nextText}</li>\n";
else
$this->output .= ($this->currentPage != $this->totalPage && $this->totalItem >= $this->maxLength) ? "<li class=\"{$this->nextCss}\"><a href=\"{$this->baseUrl}/$next_page\">{$this->nextText}</a>\n" : "<li class=\"{$this->inactiveNextCss}\"><a >{$this->nextText}</a></li>\n";
}
else{
for($i=1;$i<=$this->totalPage;$i++){
$this->output .= ($i == $this->currentPage) ? "<li class=\"{$this->currentCss}\" ><a href=\"javascript:void(0);\">$i</a></li> ":"<li class=\"{$this->pagesCss}\"><a href=\"{$this->baseUrl}/$i\">$i</a></li> ";
}
if($this->_noNextPrev)
$this->components['next_link'] = ($this->currentPage != $this->totalPage && $this->totalItem >= $this->maxLength) ? "<li class=\"{$this->nextCss}\" ><a href=\"{$this->baseUrl}/$next_page\">{$this->nextText}</a></li>\n" : "<li class=\"{$this->inactiveNextCss}\"><a >{$this->nextText}</a></li>\n";
else
$this->output .= ($this->currentPage != $this->totalPage && $this->totalItem >= $this->maxLength) ? "<li class=\"{$this->nextCss}\"><a href=\"{$this->baseUrl}/$next_page\">{$this->nextText}</a><li>\n" : "<li class=\"{$this->inactiveNextCss}\"><a >{$this->nextText}</a></li>\n";
}
$this->low = ($this->currentPage>1) ? ($this->currentPage-1) * $this->itemPerPage : 0;
$this->high = ($this->currentPage * $this->itemPerPage)-1;
$this->limit = " $this->low,$this->itemPerPage";
$this->components['pages'] = $this->output;
$this->components['jump_menu'] = $this->showJumpMenu();
$this->components['page_size'] = $this->showPageSize();
$this->components['current_page'] = $this->currentPage;
$this->components['total_page'] = $this->totalPage;
$this->output .='</ul>';
return $this->components;
}
/**
* Generate a Drop down menu for different page sizes.
*
* @param array $sizeArr Array of page size
* @return string Drop down menu for the page sizes
*/
public function showPageSize($sizeArr=null){
$items = '';
if($sizeArr==null)
$ipp_array = array(10,25,50,100);
foreach($ipp_array as $ipp_opt)
$items .= ($ipp_opt == $this->itemPerPage) ? "<option selected value=\"$ipp_opt\">$ipp_opt</option>\n":"<option value=\"$ipp_opt\">$ipp_opt</option>\n";
return "<select class=\"{$this->pageSizeCss}\" onchange=\"window.location='{$this->baseUrl}/1/'+this[this.selectedIndex].value; return false\">$items</select>\n";
}
/**
* Generate a Drop down menu for all page numer
*
* @param bool $withPageSize Navigate with page size
* @return string Drop down menu for all page numer
*/
public function showJumpMenu($withPageSize=false){
$option = '';
for($i=1;$i<=$this->totalPage;$i++){
$option .= ($i==$this->currentPage) ? "<option value=\"$i\" selected>$i</option>\n":"<option value=\"$i\">$i</option>\n";
}
if($withPageSize)
return "<select class=\"{$this->dropDownCss}\" onchange=\"window.location='{$this->baseUrl}/'+this[this.selectedIndex].value+'/$this->itemPerPage';return false\">$option</select>\n";
return "<select class=\"{$this->dropDownCss}\" onchange=\"window.location='{$this->baseUrl}/'+this[this.selectedIndex].value;return false\">$option</select>\n";
}
}