Files
Kleeja/includes/pager.php

184 lines
5.9 KiB
PHP
Raw Normal View History

2018-01-09 02:09:07 +03:00
<?php
/**
*
* @package Kleeja
2020-04-11 22:45:48 +02:00
* @copyright (c) 2007 Kleeja.net
2018-01-09 02:09:07 +03:00
* @license ./docs/license.txt
*
*/
//no for directly open
2019-05-03 23:52:08 +03:00
if (! defined('IN_COMMON'))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
exit();
2018-01-09 02:09:07 +03:00
}
class Pagination
{
2019-05-03 23:52:08 +03:00
protected $totalPages, $startRow , $currentPage;
2018-01-09 02:09:07 +03:00
/**
* @param $rowsPerPage
* @param $numRows
2018-01-09 02:09:07 +03:00
* @param int $currentPage
*/
2019-05-03 23:52:08 +03:00
public function __construct($rowsPerPage, $numRows, $currentPage = 1)
{
// Calculate the total number of pages
$this->setTotalPages(ceil($numRows/$rowsPerPage));
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
// Check that a valid page has been provided
$this->currentPage = $currentPage < 1 ? 1 : ($currentPage > $this->totalPages ? $this->totalPages : $currentPage);
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
// Calculate the row to start the select with
$this->startRow = ($this->currentPage - 1) * $rowsPerPage;
}
2018-01-09 02:09:07 +03:00
/**
* Get the total pages
* @return float
*/
public function getTotalPages()
2019-05-03 23:52:08 +03:00
{
return $this->totalPages;
}
2018-01-09 02:09:07 +03:00
/**
* Set the total pages
2019-05-03 23:52:08 +03:00
* @param int $totalPages
2018-01-09 02:09:07 +03:00
* @return int
*/
public function setTotalPages($totalPages = 0)
{
return $this->totalPages = $totalPages;
}
/**
* @return int
*/
public function getCurrentPage()
{
return $this->currentPage;
}
/**
* @param int $currentPage
*/
public function setCurrentPage($currentPage)
{
$this->currentPage = $currentPage;
}
/**
* @return int
*/
public function getStartRow()
2019-05-03 23:52:08 +03:00
{
return $this->startRow;
}
2018-01-09 02:09:07 +03:00
/**
* @param int $startRow
*/
public function setStartRow($startRow)
{
$this->startRow = $startRow;
}
/**
* @param $link
2019-05-03 23:52:08 +03:00
* @param string $link_plus
2018-01-09 02:09:07 +03:00
* @return string
*/
public function print_nums($link, $link_plus = '')
2019-05-03 23:52:08 +03:00
{
global $lang, $config;
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
//if no page
if ($this->totalPages <= 1)
{
return '';
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
$link_plus .= $link_plus != '' ? ' ' : '';
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
$re = '<nav aria-label="Page navigation example">';
$re = '<ul id="pagination" class="pagination">';
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
// Add a previous page link
if ($this->totalPages > 1 && $this->currentPage > 1)
{
$re .= '<li class="page-item">';
$re .= $config['mod_writer'] && ! defined('IN_ADMIN')
2018-01-09 02:09:07 +03:00
? '<a class="paginate phover page-link" href="' . $link . '-' . ($this->currentPage-1) . '.html"' . $link_plus . '><span>' . $lang['PREV'] . '</span></a>'
: '<a class="paginate phover page-link" href="' . $link . '&amp;page=' . ($this->currentPage-1) . '"' . $link_plus . '><span>' . $lang['PREV'] . '</span></a>';
$re .= '</li>';
2019-05-03 23:52:08 +03:00
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if ($this->currentPage > 3)
{
2018-01-09 02:09:07 +03:00
$re .= '<li class="page-item">';
2019-05-03 23:52:08 +03:00
$re .= $config['mod_writer'] && ! defined('IN_ADMIN')
2018-01-09 02:09:07 +03:00
? '<a class="paginate page-link" href="' . $link . '-1.html"' . $link_plus . '><span>1</span></a>' . ($this->currentPage > 5 ? '<a class="paginate dots"><span>...</span></a>' : '')
: '<a class="paginate page-link" href="' . $link . '&amp;page=1"' . $link_plus . '><span>1</span></a>' . ($this->currentPage > 5 ? '<a class="paginate dots"><span>...</span></a>' : '');
$re .= '</li>';
2019-05-03 23:52:08 +03:00
}
for ($current = ($this->currentPage == 5) ? $this->currentPage - 3 : $this->currentPage - 2, $stop = ($this->currentPage + 4 == $this->totalPages) ? $this->currentPage + 4 : $this->currentPage + 3; $current < $stop; ++$current)
{
if ($current < 1 || $current > $this->totalPages)
{
continue;
}
elseif ($current != $this->currentPage)
{
2018-01-09 02:09:07 +03:00
$re .= '<li class="page-item">';
2019-05-03 23:52:08 +03:00
$re .= $config['mod_writer'] && ! defined('IN_ADMIN')
2018-01-09 02:09:07 +03:00
? '<a class="paginate page-link" href="' . $link . '-' . $current . '.html"' . $link_plus . '><span>' . $current . '</span></a>'
: '<a class="paginate page-link" href="' . $link . '&amp;page=' . $current . '"' . $link_plus . '><span>' . $current . '</span></a>';
$re .= '</li>';
2019-05-03 23:52:08 +03:00
}
else
{
2018-01-09 02:09:07 +03:00
$re .= '<li class="page-item">';
2019-05-03 23:52:08 +03:00
$re .= '<a class="paginate page-link current"><span>' . $current . '</span></a>';
2018-01-09 02:09:07 +03:00
$re .= '</li>';
2019-05-03 23:52:08 +03:00
}
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if ($this->currentPage <= ($this->totalPages-3))
{
if ($this->currentPage != ($this->totalPages-3) && $this->currentPage != ($this->totalPages-4))
{
$re .= '<li class="page-item"><a class="paginate page-link dots"><span>...</span></a></li>';
}
2018-01-09 02:09:07 +03:00
$re .= '<li class="page-item">';
2019-05-03 23:52:08 +03:00
$re .= $config['mod_writer'] && ! defined('IN_ADMIN')
2018-01-09 02:09:07 +03:00
? '<a class="paginate page-link" href="' . $link . '-' . $this->totalPages . '.html"' . $link_plus . '><span>' . $this->totalPages . '</span></a>'
: '<a class="paginate page-link" href="' . $link . '&amp;page=' . $this->totalPages . '"' . $link_plus . '><span>' . $this->totalPages . '</span></a>';
$re .= '</li>';
2019-05-03 23:52:08 +03:00
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
// Add a next page link
if ($this->totalPages > 1 && $this->currentPage < $this->totalPages)
{
2018-01-09 02:09:07 +03:00
$re .= '<li class="page-item">';
2019-05-03 23:52:08 +03:00
$re .= $config['mod_writer'] && ! defined('IN_ADMIN')
2018-01-09 02:09:07 +03:00
? '<a class="paginate page-link phover" href="' . $link . '-' . ($this->currentPage+1) . '.html"' . $link_plus . '><span>' . $lang['NEXT'] . '</span></a>'
: '<a class="paginate phover page-link" href="' . $link . '&amp;page=' . ($this->currentPage+1) . '"' . $link_plus . '><span>' . $lang['NEXT'] . '</span></a>';
$re .= '</li>';
2019-05-03 23:52:08 +03:00
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
$re .= '</ul>';
$re .= '</nav>';
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
return $re;
}
2018-01-09 02:09:07 +03:00
}