<?php
/**
* @package sequence
* @subpackage util
* @author Filip Szczechowiak <filip.szczechowiak@gmail.com>
*/
class Pagination
{
protected $currentPage;
protected $perPage;
protected $totalCount;
protected $range;
protected $baseLink;
protected $link;
protected $navBack;
protected $navNext;
protected $html;
public function __construct()
{
$this->currentPage = 1;
$this->perPage = 20;
$this->totalCount = 24;
$this->range = 3;
$this->url = '?page=#PAGE#';
$this->baseUrl = $_SERVER['SCRIPT_NAME'];
$this->navBack = 'Poprzednia';
$this->navNext = 'Następna';
$this->html = null;
}
/**
* Ustawia aktualną stronę
*
* @param int $val - aktualna strona
* @return Pagination
*/
public function setCurrentPage($val)
{
$val = (int) $val;
if($val === 0 || $val > $this->totalPages())
{
$this->currentPage = 0;
} else
{
$this->currentPage = $val;
}
return $this;
}
/**
* Ustawia ilość wyświetlanych wierszy na stronie.
*
* @param int $val - ilość wyświetlanych wierszy na stronie
* @return Pagination
*/
public function setPerPage($val)
{
$this->perPage = $val;
return $this;
}
/**
* Ustawia ilość wszystkich wierszy.
*
* @param int $val - ilość wszystkich wierszy
* @return Pagination
*/
public function setTotalCount($val)
{
$this->totalCount = $val;
return $this;
}
/**
* Ustawia ilość wyświetlanych stron, przed i za aktualną stroną.
*
* @param int $val - ilość stron
* @return Pagination
*/
public function setRange($val)
{
$this->range = $val;
return $this;
}
/**
* Ustawia podstawowy link, bez paginacji (pierwsza strona), przykładowo:
* - /custompage
* - custompage.php
*
* @param string $val - podstawowy link
* @return Pagination
*/
public function setBaseUrl($val)
{
$this->baseUrl = $val;
return $this;
}
/**
* Ustawia link z paginacją, przykładowo:
* - /custompage/page/#PAGE#
* - custompage.php?page=#PAGE#
*
*
* @param string $val - link z dodatkowa paginacja
* @return Pagination
*/
public function setUrl($val)
{
$this->url = $val;
return $this;
}
/**
* Zmienia nazwę przycisku "poprzedni", możliwe jest też wstawienie kodu html,
* naprzykład obrazka, bądź jakiegoś innego elementu.
*
* @param mixed $val - nazwa przycisku "poprzedni"
* @return Pagination
*/
public function navBack($val)
{
$this->navBack = $val;
return $this;
}
/**
* Zmienia nazwę przycisku "następny", możliwe jest też wstawienie kodu html,
* naprzykład obrazka, bądź jakiegoś innego elementu.
*
* @param mixed $val - nazwa przycisku "nastepny"
* @return Pagination
*/
public function navNext($val)
{
$this->navNext = $val;
return $this;
}
/**
* @return int - zwraca całkowitą liczbe stron
*/
protected function totalPages()
{
return $this->totalCount >= $this->perPage ?
ceil($this->totalCount/$this->perPage) : 1; }
/**
* @return int - zwraca poprzedną stronę
*/
protected function previousPage()
{
return $this->currentPage-1;
}
/**
* @return int - zwraca następną stronę
*/
protected function nextPage()
{
return $this->currentPage+1;
}
/**
* @return string - zwraca podmieniony fragment linka
*/
protected function createLink($page)
{
if(1 == $page)
{
return $this->baseUrl;
} else
{
}
}
/**
* Zwraca początek ilości wyświetlanych wierszy.
*
* @return int - początek przedziału
*/
public function offset()
{
if($this->currentPage > 1)
{
return $this->currentPage * $this->perPage - $this->perPage;
} else
{
return 0;
}
}
/**
* Zwraca przedział ilości wyświetlonych wierszy.
*
* @return int - koniec przedziału
*/
public function limit()
{
return $this->perPage;
}
/**
* Zwraca pierwszy indeks tablicy.
*
* @return int - pierwszy indeks tablicy
*/
public function indexStart()
{
if($this->currentPage > 1)
{
$result = $this->perPage * ($this->currentPage - 1);
} else
{
$result = 0;
}
return $result;
}
/**
* Zwraca ostatni indeks tablicy.
*
* @return int - ostatni indeks tablicy
*/
public function indexEnd()
{
if($this->totalCount > $this->perPage)
{
$val = $this->indexStart() + ($this->perPage - 1);
if($val > $this->totalCount)
{
$result = $this->totalCount - 1;
} else
{
$result = $val;
}
} else
{
$result = $this->totalCount - 1;
}
return $result;
}
/**
* Zwraca wygenerowany kod html pagera.
*
* @param string $style - dodatkowy parametr przeznaczony dla lokalnych styli
* @return string - zwraca wygenerowany kod html
*/
public function getHtml($style = null)
{
if(null !== $this->html)
{
return $this->html;
}
if ($this->totalCount <= $this->perPage)
{
return '';
}
$res = '';
if($this->currentPage >= 2)
{
$res = '<a href="'.$this->createLink($this->previousPage()).'" class="previous_page"><span>'.$this->navBack.'</span></a> ';
if($this->currentPage > ($this->range + 1))
{
if(1 === $this->currentPage)
{
$res .= ' <span class="selected">1</span> ';
} else
{
$res .= ' <a href="'.$this->createLink(1).'">1</a>';
}
if($this->currentPage > ($this->range + 2))
{
$res .= ' <span class="dotted">...</span> ';
}
}
}
$idxFst = max($this->currentPage - $this->range, 1
); $idxLst = min($this->currentPage + $this->range, $this->totalPages());
for($i = $idxFst; $i <= $idxLst; $i++)
{
if($i == $this->currentPage)
{
$res .= ' <span class="selected">'.$i.'</span> ';
} else
{
$res .= ' <a href="'.$this->createLink($i).'">'.$i.'</a> ';
}
}
if($this->nextPage() <= $this->totalPages())
{
if($this->currentPage < ($this->totalPages() - $this->range))
{
if($this->currentPage < ($this->totalPages() - ($this->range + 1)))
{
$res .= ' <span class="dotted">...</span> ';
}
$res .= ' <a href="'.$this->createLink($this->totalPages()).'">'.$this->totalPages().'</a> ';
}
$res .= ' <a href="'.$this->createLink($this->nextPage()).'" class="next_page"><span>'.$this->navNext.'</span></a> ';
}
$this->html = '<div class="pagination"'.(null === $style ? null : 'style="'.$style.'"').'>'.$res.'</div>';
return $this->html;
}
}
?>