1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 13 14 15 16
17 class
18 {
19 20 21 22
23 public $limitstart = null;
24
25 26 27 28
29 public $limit = null;
30
31 32 33 34
35 public $total = null;
36
37 38 39 40
41 public $prefix = null;
42
43 44 45 46
47 public $pagesStart;
48
49 50 51 52
53 public $pagesStop;
54
55 56 57 58
59 public $pagesCurrent;
60
61 62 63 64
65 public $pagesTotal;
66
67 68 69 70
71 protected $viewall = false;
72
73 74 75 76 77 78 79
80 protected $additionalUrlParams = array();
81
82 83 84 85
86 protected $app = null;
87
88 89 90 91 92 93
94 protected $data;
95
96 97 98 99 100 101 102 103 104 105 106
107 public function __construct($total, $limitstart, $limit, $prefix = '', JApplicationCms $app = null)
108 {
109
110 $this->total = (int) $total;
111 $this->limitstart = (int) max($limitstart, 0);
112 $this->limit = (int) max($limit, 0);
113 $this->prefix = $prefix;
114 $this->app = $app ?: JFactory::getApplication();
115
116 if ($this->limit > $this->total)
117 {
118 $this->limitstart = 0;
119 }
120
121 if (!$this->limit)
122 {
123 $this->limit = $total;
124 $this->limitstart = 0;
125 }
126
127 128 129 130
131 if ($this->limitstart > $this->total - $this->limit)
132 {
133 $this->limitstart = max(0, (int) (ceil($this->total / $this->limit) - 1) * $this->limit);
134 }
135
136
137 if ($this->limit > 0)
138 {
139 $this->pagesTotal = ceil($this->total / $this->limit);
140 $this->pagesCurrent = ceil(($this->limitstart + 1) / $this->limit);
141 }
142
143
144 $displayedPages = 10;
145 $this->pagesStart = $this->pagesCurrent - ($displayedPages / 2);
146
147 if ($this->pagesStart < 1)
148 {
149 $this->pagesStart = 1;
150 }
151
152 if ($this->pagesStart + $displayedPages > $this->pagesTotal)
153 {
154 $this->pagesStop = $this->pagesTotal;
155
156 if ($this->pagesTotal < $displayedPages)
157 {
158 $this->pagesStart = 1;
159 }
160 else
161 {
162 $this->pagesStart = $this->pagesTotal - $displayedPages + 1;
163 }
164 }
165 else
166 {
167 $this->pagesStop = $this->pagesStart + $displayedPages - 1;
168 }
169
170
171 if ($limit === 0)
172 {
173 $this->viewall = true;
174 }
175 }
176
177 178 179 180 181 182 183 184 185 186 187
188 public function setAdditionalUrlParam($key, $value)
189 {
190
191 $result = isset($this->additionalUrlParams[$key]) ? $this->additionalUrlParams[$key] : null;
192
193
194 if ($value === null)
195 {
196 unset($this->additionalUrlParams[$key]);
197 }
198 else
199 {
200 $this->additionalUrlParams[$key] = $value;
201 }
202
203 return $result;
204 }
205
206 207 208 209 210 211 212 213 214 215
216 public function getAdditionalUrlParam($key)
217 {
218 $result = isset($this->additionalUrlParams[$key]) ? $this->additionalUrlParams[$key] : null;
219
220 return $result;
221 }
222
223 224 225 226 227 228 229 230 231
232 public function getRowOffset($index)
233 {
234 return $index + 1 + $this->limitstart;
235 }
236
237 238 239 240 241 242 243
244 public function getData()
245 {
246 if (!$this->data)
247 {
248 $this->data = $this->_buildDataObject();
249 }
250
251 return $this->data;
252 }
253
254 255 256 257 258 259 260
261 public function getPagesCounter()
262 {
263 $html = null;
264
265 if ($this->pagesTotal > 1)
266 {
267 $html .= JText::sprintf('JLIB_HTML_PAGE_CURRENT_OF_TOTAL', $this->pagesCurrent, $this->pagesTotal);
268 }
269
270 return $html;
271 }
272
273 274 275 276 277 278 279
280 public function getResultsCounter()
281 {
282 $html = null;
283 $fromResult = $this->limitstart + 1;
284
285
286 if ($this->limitstart + $this->limit < $this->total)
287 {
288 $toResult = $this->limitstart + $this->limit;
289 }
290 else
291 {
292 $toResult = $this->total;
293 }
294
295
296 if ($this->total > 0)
297 {
298 $msg = JText::sprintf('JLIB_HTML_RESULTS_OF', $fromResult, $toResult, $this->total);
299 $html .= "\n" . $msg;
300 }
301 else
302 {
303 $html .= "\n" . JText::_('JLIB_HTML_NO_RECORDS_FOUND');
304 }
305
306 return $html;
307 }
308
309 310 311 312 313 314 315
316 public function getPagesLinks()
317 {
318
319 $data = $this->_buildDataObject();
320
321 $list = array();
322 $list['prefix'] = $this->prefix;
323
324 $itemOverride = false;
325 $listOverride = false;
326
327 $chromePath = JPATH_THEMES . '/' . $this->app->getTemplate() . '/html/pagination.php';
328
329 if (file_exists($chromePath))
330 {
331 include_once $chromePath;
332
333 334 335
336 if (function_exists('pagination_item_active') && function_exists('pagination_item_inactive'))
337 {
338 JLog::add(
339 'pagination_item_active and pagination_item_inactive are deprecated. Use the layout joomla.pagination.link instead.',
340 JLog::WARNING,
341 'deprecated'
342 );
343
344 $itemOverride = true;
345 }
346
347 348 349 350
351 if (function_exists('pagination_list_render'))
352 {
353 JLog::add('pagination_list_render is deprecated. Use the layout joomla.pagination.list instead.', JLog::WARNING, 'deprecated');
354 $listOverride = true;
355 }
356 }
357
358
359 if ($data->all->base !== null)
360 {
361 $list['all']['active'] = true;
362 $list['all']['data'] = $itemOverride ? pagination_item_active($data->all) : $this->_item_active($data->all);
363 }
364 else
365 {
366 $list['all']['active'] = false;
367 $list['all']['data'] = $itemOverride ? pagination_item_inactive($data->all) : $this->_item_inactive($data->all);
368 }
369
370 if ($data->start->base !== null)
371 {
372 $list['start']['active'] = true;
373 $list['start']['data'] = $itemOverride ? pagination_item_active($data->start) : $this->_item_active($data->start);
374 }
375 else
376 {
377 $list['start']['active'] = false;
378 $list['start']['data'] = $itemOverride ? pagination_item_inactive($data->start) : $this->_item_inactive($data->start);
379 }
380
381 if ($data->previous->base !== null)
382 {
383 $list['previous']['active'] = true;
384 $list['previous']['data'] = $itemOverride ? pagination_item_active($data->previous) : $this->_item_active($data->previous);
385 }
386 else
387 {
388 $list['previous']['active'] = false;
389 $list['previous']['data'] = $itemOverride ? pagination_item_inactive($data->previous) : $this->_item_inactive($data->previous);
390 }
391
392
393 $list['pages'] = array();
394
395 foreach ($data->pages as $i => $page)
396 {
397 if ($page->base !== null)
398 {
399 $list['pages'][$i]['active'] = true;
400 $list['pages'][$i]['data'] = $itemOverride ? pagination_item_active($page) : $this->_item_active($page);
401 }
402 else
403 {
404 $list['pages'][$i]['active'] = false;
405 $list['pages'][$i]['data'] = $itemOverride ? pagination_item_inactive($page) : $this->_item_inactive($page);
406 }
407 }
408
409 if ($data->next->base !== null)
410 {
411 $list['next']['active'] = true;
412 $list['next']['data'] = $itemOverride ? pagination_item_active($data->next) : $this->_item_active($data->next);
413 }
414 else
415 {
416 $list['next']['active'] = false;
417 $list['next']['data'] = $itemOverride ? pagination_item_inactive($data->next) : $this->_item_inactive($data->next);
418 }
419
420 if ($data->end->base !== null)
421 {
422 $list['end']['active'] = true;
423 $list['end']['data'] = $itemOverride ? pagination_item_active($data->end) : $this->_item_active($data->end);
424 }
425 else
426 {
427 $list['end']['active'] = false;
428 $list['end']['data'] = $itemOverride ? pagination_item_inactive($data->end) : $this->_item_inactive($data->end);
429 }
430
431 if ($this->total > $this->limit)
432 {
433 return $listOverride ? pagination_list_render($list) : $this->_list_render($list);
434 }
435 else
436 {
437 return '';
438 }
439 }
440
441 442 443 444 445 446 447 448 449 450
451 public function ($layoutId = 'joomla.pagination.links', $options = array())
452 {
453
454 $layoutId = $layoutId === null ? 'joomla.pagination.links' : $layoutId;
455
456 $list = array(
457 'prefix' => $this->prefix,
458 'limit' => $this->limit,
459 'limitstart' => $this->limitstart,
460 'total' => $this->total,
461 'limitfield' => $this->getLimitBox(),
462 'pagescounter' => $this->getPagesCounter(),
463 'pages' => $this->getPaginationPages(),
464 'pagesTotal' => $this->pagesTotal,
465 );
466
467 return JLayoutHelper::render($layoutId, array('list' => $list, 'options' => $options));
468 }
469
470 471 472 473 474 475 476
477 public function ()
478 {
479 $list = array();
480
481 if ($this->total > $this->limit)
482 {
483
484 $data = $this->_buildDataObject();
485
486
487 $list['all']['active'] = $data->all->base !== null;
488 $list['all']['data'] = $data->all;
489
490
491 $list['start']['active'] = $data->start->base !== null;
492 $list['start']['data'] = $data->start;
493
494
495 $list['previous']['active'] = $data->previous->base !== null;
496 $list['previous']['data'] = $data->previous;
497
498
499 $list['pages'] = array();
500
501 foreach ($data->pages as $i => $page)
502 {
503 $list['pages'][$i]['active'] = $page->base !== null;
504 $list['pages'][$i]['data'] = $page;
505 }
506
507 $list['next']['active'] = $data->next->base !== null;
508 $list['next']['data'] = $data->next;
509
510 $list['end']['active'] = $data->end->base !== null;
511 $list['end']['data'] = $data->end;
512 }
513
514 return $list;
515 }
516
517 518 519 520 521 522 523
524 public function ()
525 {
526
527 $chromePath = JPATH_THEMES . '/' . $this->app->getTemplate() . '/html/pagination.php';
528
529 if (file_exists($chromePath))
530 {
531 include_once $chromePath;
532
533 if (function_exists('pagination_list_footer'))
534 {
535 JLog::add('pagination_list_footer is deprecated. Use the layout joomla.pagination.links instead.', JLog::WARNING, 'deprecated');
536
537 $list = array(
538 'prefix' => $this->prefix,
539 'limit' => $this->limit,
540 'limitstart' => $this->limitstart,
541 'total' => $this->total,
542 'limitfield' => $this->getLimitBox(),
543 'pagescounter' => $this->getPagesCounter(),
544 'pageslinks' => $this->getPagesLinks(),
545 );
546
547 return pagination_list_footer($list);
548 }
549 }
550
551 return $this->getPaginationLinks();
552 }
553
554 555 556 557 558 559 560
561 public function getLimitBox()
562 {
563 $limits = array();
564
565
566 for ($i = 5; $i <= 30; $i += 5)
567 {
568 $limits[] = JHtml::_('select.option', "$i");
569 }
570
571 $limits[] = JHtml::_('select.option', '50', JText::_('J50'));
572 $limits[] = JHtml::_('select.option', '100', JText::_('J100'));
573 $limits[] = JHtml::_('select.option', '0', JText::_('JALL'));
574
575 $selected = $this->viewall ? 0 : $this->limit;
576
577
578 if ($this->app->isClient('administrator'))
579 {
580 $html = JHtml::_(
581 'select.genericlist',
582 $limits,
583 $this->prefix . 'limit',
584 'class="inputbox input-mini" size="1" onchange="Joomla.submitform();"',
585 'value',
586 'text',
587 $selected
588 );
589 }
590 else
591 {
592 $html = JHtml::_(
593 'select.genericlist',
594 $limits,
595 $this->prefix . 'limit',
596 'class="inputbox input-mini" size="1" onchange="this.form.submit()"',
597 'value',
598 'text',
599 $selected
600 );
601 }
602
603 return $html;
604 }
605
606 607 608 609 610 611 612 613 614 615 616 617 618 619
620 public function orderUpIcon($i, $condition = true, $task = 'orderup', $alt = 'JLIB_HTML_MOVE_UP', $enabled = true, $checkbox = 'cb')
621 {
622 if (($i > 0 || ($i + $this->limitstart > 0)) && $condition)
623 {
624 return JHtml::_('jgrid.orderUp', $i, $task, '', $alt, $enabled, $checkbox);
625 }
626 else
627 {
628 return ' ';
629 }
630 }
631
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
647 public function orderDownIcon($i, $n, $condition = true, $task = 'orderdown', $alt = 'JLIB_HTML_MOVE_DOWN', $enabled = true, $checkbox = 'cb')
648 {
649 if (($i < $n - 1 || $i + $this->limitstart < $this->total - 1) && $condition)
650 {
651 return JHtml::_('jgrid.orderDown', $i, $task, '', $alt, $enabled, $checkbox);
652 }
653 else
654 {
655 return ' ';
656 }
657 }
658
659 660 661 662 663 664 665 666 667
668 protected function ($list)
669 {
670 $html = "<div class=\"list-footer\">\n";
671
672 $html .= "\n<div class=\"limit\">" . JText::_('JGLOBAL_DISPLAY_NUM') . $list['limitfield'] . "</div>";
673 $html .= $list['pageslinks'];
674 $html .= "\n<div class=\"counter\">" . $list['pagescounter'] . "</div>";
675
676 $html .= "\n<input type=\"hidden\" name=\"" . $list['prefix'] . "limitstart\" value=\"" . $list['limitstart'] . "\" />";
677 $html .= "\n</div>";
678
679 return $html;
680 }
681
682 683 684 685 686 687 688 689 690
691 protected function _list_render($list)
692 {
693 return JLayoutHelper::render('joomla.pagination.list', array('list' => $list));
694 }
695
696 697 698 699 700 701 702 703 704 705
706 protected function _item_active(JPaginationObject $item)
707 {
708 $title = '';
709 $class = '';
710
711 if (!is_numeric($item->text))
712 {
713 JHtml::_('bootstrap.tooltip');
714 $title = ' title="' . $item->text . '"';
715 $class = 'hasTooltip ';
716 }
717
718 if ($this->app->isClient('administrator'))
719 {
720 return '<a' . $title . ' href="#" onclick="document.adminForm.' . $this->prefix
721 . 'limitstart.value=' . ($item->base > 0 ? $item->base : '0') . '; Joomla.submitform();return false;">' . $item->text . '</a>';
722 }
723 else
724 {
725 return '<a' . $title . ' href="' . $item->link . '" class="' . $class . 'pagenav">' . $item->text . '</a>';
726 }
727 }
728
729 730 731 732 733 734 735 736 737 738
739 protected function _item_inactive(JPaginationObject $item)
740 {
741 if ($this->app->isClient('administrator'))
742 {
743 return '<span>' . $item->text . '</span>';
744 }
745 else
746 {
747 return '<span class="pagenav">' . $item->text . '</span>';
748 }
749 }
750
751 752 753 754 755 756 757
758 protected function _buildDataObject()
759 {
760 $data = new stdClass;
761
762
763 $params = '';
764
765 if (!empty($this->additionalUrlParams))
766 {
767 foreach ($this->additionalUrlParams as $key => $value)
768 {
769 $params .= '&' . $key . '=' . $value;
770 }
771 }
772
773 $data->all = new JPaginationObject(JText::_('JLIB_HTML_VIEW_ALL'), $this->prefix);
774
775 if (!$this->viewall)
776 {
777 $data->all->base = '0';
778 $data->all->link = JRoute::_($params . '&' . $this->prefix . 'limitstart=');
779 }
780
781
782 $data->start = new JPaginationObject(JText::_('JLIB_HTML_START'), $this->prefix);
783 $data->previous = new JPaginationObject(JText::_('JPREV'), $this->prefix);
784
785 if ($this->pagesCurrent > 1)
786 {
787 $page = ($this->pagesCurrent - 2) * $this->limit;
788
789
790
791
792 $data->start->base = '0';
793 $data->start->link = JRoute::_($params . '&' . $this->prefix . 'limitstart=0');
794 $data->previous->base = $page;
795 $data->previous->link = JRoute::_($params . '&' . $this->prefix . 'limitstart=' . $page);
796 }
797
798
799 $data->next = new JPaginationObject(JText::_('JNEXT'), $this->prefix);
800 $data->end = new JPaginationObject(JText::_('JLIB_HTML_END'), $this->prefix);
801
802 if ($this->pagesCurrent < $this->pagesTotal)
803 {
804 $next = $this->pagesCurrent * $this->limit;
805 $end = ($this->pagesTotal - 1) * $this->limit;
806
807 $data->next->base = $next;
808 $data->next->link = JRoute::_($params . '&' . $this->prefix . 'limitstart=' . $next);
809 $data->end->base = $end;
810 $data->end->link = JRoute::_($params . '&' . $this->prefix . 'limitstart=' . $end);
811 }
812
813 $data->pages = array();
814 $stop = $this->pagesStop;
815
816 for ($i = $this->pagesStart; $i <= $stop; $i++)
817 {
818 $offset = ($i - 1) * $this->limit;
819
820 $data->pages[$i] = new JPaginationObject($i, $this->prefix);
821
822 if ($i != $this->pagesCurrent || $this->viewall)
823 {
824 $data->pages[$i]->base = $offset;
825 $data->pages[$i]->link = JRoute::_($params . '&' . $this->prefix . 'limitstart=' . $offset);
826 }
827 else
828 {
829 $data->pages[$i]->active = true;
830 }
831 }
832
833 return $data;
834 }
835
836 837 838 839 840 841 842 843 844 845 846
847 public function set($property, $value = null)
848 {
849 JLog::add('JPagination::set() is deprecated. Access the properties directly.', JLog::WARNING, 'deprecated');
850
851 if (strpos($property, '.'))
852 {
853 $prop = explode('.', $property);
854 $prop[1] = ucfirst($prop[1]);
855 $property = implode($prop);
856 }
857
858 $this->$property = $value;
859 }
860
861 862 863 864 865 866 867 868 869 870 871
872 public function get($property, $default = null)
873 {
874 JLog::add('JPagination::get() is deprecated. Access the properties directly.', JLog::WARNING, 'deprecated');
875
876 if (strpos($property, '.'))
877 {
878 $prop = explode('.', $property);
879 $prop[1] = ucfirst($prop[1]);
880 $property = implode($prop);
881 }
882
883 if (isset($this->$property))
884 {
885 return $this->$property;
886 }
887
888 return $default;
889 }
890 }
891