Pagination class.
Constructor.
NULL
public function __construct()
{
$config = Config::get('pagination');
$this->key = $config['page_key'];
$this->currentPage = max((int) (isset($_GET[$this->key]) ? $_GET[$this->key] : 1), 1);
$this->itemsPerPage = $config['items_per_page'];
$this->maxPageLinks = $config['max_page_links'];
}
Calculates the offset and number of pages and returns the offset.
| Type | Description |
|---|---|
| int | Number of items |
| int | Number of items to display on each page |
int
public function offset($itemCount, $itemsPerPage = null)
{
$itemsPerPage = ($itemsPerPage === null) ? max($this->itemsPerPage, 1) : max($itemsPerPage, 1);
$this->pages = ceil(($itemCount / $itemsPerPage));
$this->offset = ($this->currentPage - 1) * $itemsPerPage;
return $this->offset;
}
Returns an associative array of pagination links.
| Type | Description |
|---|---|
| string | (optional) URL segments |
| array | (optional) Associative array used to build URL-encoded query string |
| string | (optional) Argument separator |
array
public function links($url = '', array $params = array(), $separator = '&')
{
$links = array();
// Number of pages
$links['num_pages'] = I18n::translate('%d Pages', array($this->pages));
// First and previous page
if($this->currentPage > 1)
{
$links['first_page'] = array
(
'name' => I18n::translate('First Page'),
'url' => URL::to($url, array_merge($params, array($this->key => 1)), $separator),
);
$links['previous_page'] = array
(
'name' => '«',
'url' => URL::to($url, array_merge($params, array($this->key => ($this->currentPage - 1))), $separator),
);
}
// Last and next page
if($this->currentPage < $this->pages)
{
$links['last_page'] = array
(
'name' => I18n::translate('Last Page'),
'url' => URL::to($url, array_merge($params, array($this->key => $this->pages)), $separator),
);
$links['next_page'] = array
(
'name' => '»',
'url' => URL::to($url, array_merge($params, array($this->key => ($this->currentPage + 1))), $separator),
);
}
// Page links
if($this->pages > $this->maxPageLinks)
{
$start = max(($this->currentPage) - ceil($this->maxPageLinks / 2), 0);
$end = $start + $this->maxPageLinks;
if($end > $this->pages)
{
$end = $this->pages;
}
if($start > ($end - $this->maxPageLinks))
{
$start = $end - $this->maxPageLinks;
}
}
else
{
$start = 0;
$end = $this->pages;
}
for($i = $start + 1; $i <= $end; $i++)
{
$links['pages'][] = array
(
'name' => $i,
'url' => URL::to($url, array_merge($params, array($this->key => $i)), $separator),
'is_current' => ($i == $this->currentPage),
);
}
return $links;
}