bludit/bl-kernel/helpers/paginator.class.php

199 lines
4.7 KiB
PHP
Raw Normal View History

2015-07-20 05:14:12 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
class Paginator {
public static $pager = array(
2017-05-16 00:46:20 +02:00
'itemsPerPage'=>0,
2018-08-06 21:46:58 +02:00
'numberOfPages'=>1,
'numberOfItems'=>0,
2017-05-24 00:48:29 +02:00
'firstPage'=>1,
'nextPage'=>1,
'prevPage'=>1,
'currentPage'=>1,
'showPrev'=>false,
'showNext'=>false,
'showNextPrev'=>false
2015-07-20 05:14:12 +02:00
);
public static function set($key, $value)
{
self::$pager[$key] = $value;
}
public static function get($key)
{
return self::$pager[$key];
}
2018-08-06 21:46:58 +02:00
public static function numberOfPages()
2016-01-21 03:16:32 +01:00
{
2018-08-06 21:46:58 +02:00
return self::get('numberOfPages');
2017-05-24 00:48:29 +02:00
}
2016-01-21 03:16:32 +01:00
2018-01-27 20:34:26 +01:00
public static function currentPage()
{
return self::get('currentPage');
}
2017-05-24 00:48:29 +02:00
public static function nextPage()
{
return self::get('nextPage');
2016-01-21 03:16:32 +01:00
}
2017-05-24 00:48:29 +02:00
public static function prevPage()
2015-07-20 05:14:12 +02:00
{
2017-05-24 00:48:29 +02:00
return self::get('prevPage');
}
2016-01-17 01:11:58 +01:00
2017-05-24 00:48:29 +02:00
public static function showNext()
{
return self::get('showNext');
}
2016-01-17 01:11:58 +01:00
2017-05-24 00:48:29 +02:00
public static function showPrev()
{
return self::get('showPrev');
}
2015-07-20 05:14:12 +02:00
2017-05-24 00:48:29 +02:00
public static function firstPage()
{
return self::get('firstPage');
2016-01-21 03:16:32 +01:00
}
2017-05-24 00:48:29 +02:00
// Returns the absolute URL for the first page
public static function firstPageUrl()
{
return self::numberUrl( self::firstPage() );
2017-05-24 00:48:29 +02:00
}
2017-05-24 00:48:29 +02:00
// Returns the absolute URL for the last page
public static function lastPageUrl()
{
2018-08-06 21:46:58 +02:00
return self::numberUrl( self::numberOfPages() );
2017-05-24 00:48:29 +02:00
}
2017-05-24 00:48:29 +02:00
// Returns the absolute URL for the next page
public static function nextPageUrl()
{
return self::numberUrl( self::nextPage() );
2017-05-24 00:48:29 +02:00
}
2017-05-24 00:48:29 +02:00
// Returns the absolute URL for the previous page
public static function previousPageUrl()
2017-05-24 00:48:29 +02:00
{
return self::numberUrl( self::prevPage() );
}
// Return the absoulte URL with the page number
public static function numberUrl($pageNumber)
{
global $url;
$domain = trim(DOMAIN_BASE,'/');
$filter = trim($url->activeFilter(), '/');
if(empty($filter)) {
$uri = $domain.'/'.$url->slug();
}
else {
$uri = $domain.'/'.$filter.'/'.$url->slug();
}
return $uri.'?page='.$pageNumber;
}
2016-01-21 03:16:32 +01:00
public static function html($textPrevPage=false, $textNextPage=false, $showPageNumber=false)
{
global $L;
2016-01-21 03:16:32 +01:00
2015-07-20 05:14:12 +02:00
$html = '<div id="paginator">';
$html .= '<ul>';
2017-08-06 21:47:17 +02:00
if(self::get('showNext'))
2015-07-20 05:14:12 +02:00
{
if($textPrevPage===false) {
$textPrevPage = '« '.$L->g('Previous page');
2015-07-20 05:14:12 +02:00
}
$html .= '<li class="left">';
2017-08-06 21:47:17 +02:00
$html .= '<a href="'.self::nextPageUrl().'">'.$textPrevPage.'</a>';
2015-07-20 05:14:12 +02:00
$html .= '</li>';
}
if($showPageNumber) {
$html .= '<li class="list">'.(self::get('currentPage')+1).' / '.(self::get('numberOfPages')+1).'</li>';
}
2017-08-06 21:47:17 +02:00
if(self::get('showPrev'))
2015-07-20 05:14:12 +02:00
{
if($textNextPage===false) {
$textNextPage = $L->g('Next page').' »';
2015-07-20 05:14:12 +02:00
}
$html .= '<li class="right">';
$html .= '<a href="'.self::previousPageUrl().'">'.$textNextPage.'</a>';
2015-07-20 05:14:12 +02:00
$html .= '</li>';
}
$html .= '</ul>';
$html .= '</div>';
return $html;
}
2018-08-06 21:46:58 +02:00
/*
* Bootstrap Pagination
*/
public static function bootstrap_html($textPrevPage=false, $textNextPage=false, $showPageNumber=false){
2018-08-06 21:46:58 +02:00
2018-10-17 22:35:30 +02:00
global $language;
2018-08-06 21:46:58 +02:00
$total_pages = self::numberOfPages();
$howMany = 2;
$currentPage = self::currentPage();
$first_page = self::firstPage();
$last_page = self::lastPageUrl();
$show_next = (self::showNext()) ? "" : "disabled";
$show_previois = (self::showPrev()) ? "" : "disabled";
2018-08-06 21:46:58 +02:00
$html = '<nav aria-label="Page navigation">';
$html .= '<ul class="pagination">';
if ($currentPage > 3 || $currentPage === $total_pages){
$html .= '<li class="page-item">';
2018-10-17 22:35:30 +02:00
$html .= '<a class="page-link" href="'.self::firstPageUrl().'" aria-label="First"><span aria-hidden="true">&laquo;</span> '.$language->get('First').'</a>';
2018-08-06 21:46:58 +02:00
$html .= '</li>';
}
if ($currentPage > 1){
$html .= '<li class="page-item'.$show_previois.'">';
2018-10-17 22:35:30 +02:00
$html .= '<a class="page-link" href="'.self::prevPageUrl().'" aria-label="Previous"><span aria-hidden="true">&laquo;</span> '.$language->get('Previous').'</a>';
$html .= '</li>';
2018-08-06 21:46:58 +02:00
}
if ($currentPage > $howMany + 1){
$html .= '<li class="page-item disabled"><span>...</span></li>';
}
for ($pageIndex = $currentPage - $howMany; $pageIndex <= $currentPage + $howMany; $pageIndex++){
2018-08-06 21:46:58 +02:00
$active = ($pageIndex==self::currentPage()) ? "active" : false;
2018-08-06 21:46:58 +02:00
if ($pageIndex >= 1 && $pageIndex <= $total_pages){
$html .= '<li class ="'.$active.'"><a href="'.self::numberUrl($pageIndex).'">'.$pageIndex.'</a></li>';
2018-08-06 21:46:58 +02:00
}
}
if ($currentPage < $total_pages){
$html .= '<li class="page-item disabled"><span>...</span></li>';
2018-08-06 21:46:58 +02:00
}
if ($currentPage < $total_pages){
$html .= '<li class="page-item'.$show_next.'">';
2018-10-17 22:35:30 +02:00
$html .= '<a class="page-link" href="'.self::nextPageUrl().'" aria-label="Next">'.$language->get('Next').' <span aria-hidden="true">&raquo;</span></a>';
$html .= '</li>';
2018-10-17 22:35:30 +02:00
$html .= '<li><a href="'.$last_page.'">'.$language->get('Last').'</a></li>';
}
$html .= '</ul>';
$html .= '</nav>';
2018-08-06 21:46:58 +02:00
return $html;
}
2015-07-20 05:14:12 +02:00
}