| Server IP : 162.214.74.102 / Your IP : 216.73.217.80 Web Server : Apache System : Linux dedi-4363141.lrsys.com.br 3.10.0-1160.119.1.el7.tuxcare.els25.x86_64 #1 SMP Wed Oct 1 17:37:27 UTC 2025 x86_64 User : lrsys ( 1015) PHP Version : 5.6.40 Disable Function : exec,passthru,shell_exec,system MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/lrsys/www/lrsys_apps/mundotennis/application/autoload/ |
Upload File : |
<?php
/**
* Classe de paginação para o ERP iBilling.
*
* @author Cleberson Falk <cleberson.falk@gmail.com>
*/
class ModulePaginator
{
private $table;
private $query;
protected $params;
public $items_per_page = 20;
public $current_page = 0;
public $total_items = 0;
public $total_pages = 0;
public $paginator;
public $visible_pages = 9;
/**
* Método constructor. Recebe o nome da tabela que será paginada,
* a query que será usada na paginação e os parâmetros que serão
* passados para url.
*
* @param $table
* @param $query
* @param array $params
*/
public function __construct($table, $query, $params = array())
{
$this->table = $table;
$this->query = $query;
if(count($params) > 0)
$this->params = $params;
}
/**
* Retorna a quantidade de registros por página
* @return int
*/
public function getItemsPerPage()
{
return $this->items_per_page;
}
/**
* Seta a quantidade de registros por página.
*
* @param $items_per_page
* @return $this
*/
public function setTotalItems($total_items)
{
$this->total_items = $total_items;
return $this;
}
/**
* Retorna a quantidade de registros por página
* @return int
*/
public function getTotalItems()
{
return $this->total_items;
}
/**
* Seta a quantidade de registros por página.
*
* @param $items_per_page
* @return $this
*/
public function setItemsPerPage($items_per_page)
{
$this->items_per_page = $items_per_page;
return $this;
}
/**
* Retorna a página atual.
*
* @return int
*/
public function getCurrentPageNumber()
{
return $this->current_page;
}
/**
* Seta a página atual.
*
* @param $page
* @return $this
*/
public function setCurrentPageNumber($page)
{
$this->current_page = ($page <= 0) ? 1 : $page;
return $this;
}
/**
* Acrescenta os parâmetros no final da url informada.
*
* @param $base_url
* @param $page
* @return string
*/
public function url($base_url, $page)
{
$url = $base_url . $page . '/';
if(is_array($this->params) && (count($this->params) > 0)) {
foreach($this->params as $param => $value) {
if($value != '' && !is_array($value))
$url .= $param .'/'. $value .'/';
}
}
return $url;
}
/**
* Monta o Html com os links da paginação.
*
* @param $base_url
* @return string
*/
public function paginate($base_url)
{
global $_L;
if($this->total_pages > 1) {
// Ir para primeira página
$li_first = '<li><a href="' .$this->url($base_url, 1). '"><i class="fa fa-fast-backward pg-arrow"></i></a></li>';
// Monta o link anterior
$li_previous = "";
if($this->current_page > 1)
$li_previous .= '<li><a href="' .$this->url($base_url, $this->current_page-1). '"><i class="fa fa-backward pg-arrow"></i></a></li>';
else if($this->current_page <= 1)
$li_previous .= '<li class="disabled"><a href="#"><i class="fa fa-backward pg-arrow"></i></a></li>';
$li_pages = "";
$min = max(1, $this->current_page - floor($this->visible_pages / 2));
$max = min($this->total_pages, ($this->current_page + floor($this->visible_pages / 2)));
for($pag = $min; $pag <= $max; $pag++) {
$li_pages .= '<li' .(($this->current_page == $pag) ? ' class="active"' : ''). '><a href="' .$this->url($base_url, $pag). '">' .$pag. '</a></li>';
}
// Monta o link do próximo
$li_next = "";
if($this->current_page < $this->total_pages)
$li_next = '<li><a href="' .$this->url($base_url, $this->current_page+1). '"><i class="fa fa-forward pg-arrow"></i></a></li>';
else if($this->current_page >= $this->total_pages)
$li_next = '<li class="disabled"><a href="#"><i class="fa fa-forward pg-arrow"></i></a></li>';
// Ir para primeira página
$li_last = '<li><a href="' .$this->url($base_url, $this->total_pages). '"><i class="fa fa-fast-forward pg-arrow"></i></a></li>';
return $li_first . $li_previous . $li_pages . $li_next . $li_last;
}
}
/**
* Método principal que faz a paginação, executa a query
* e retorna um array com os dados da página atual.
*
* @return array
*/
public function run()
{
if($this->total_items == 0)
{
$result_total = ORM::for_table($this->table)
->raw_query($this->query)->find_array();
$this->total_items = count($result_total);
}
$items_per_page = $this->getItemsPerPage();
if($items_per_page == 'all')
$this->total_pages = 1;
else
$this->total_pages = ceil($this->total_items / $items_per_page);
if($items_per_page == 'all') {
$this->paginator = ORM::for_table($this->table)
->raw_query($this->query)->find_array();
}
else {
$this->paginator = ORM::for_table($this->table)
->raw_query(
$this->query.
" LIMIT " . (int) $items_per_page .
" OFFSET " . (int) (($this->getCurrentPageNumber() - 1) * $items_per_page)
)
->find_array();
}
return $this->paginator;
}
}