Files
nl-admin-api/system/paginator.php

255 lines
5.4 KiB
PHP
Raw Normal View History

2011-07-20 23:31:02 -05:00
<?php namespace System;
class Paginator {
/**
* The results for the current page.
*
* @var array
*/
public $results;
/**
* The total number of results.
*
* @var int
*/
public $total;
/**
* The current page.
*
* @var int
*/
public $page;
/**
* The number of items per page.
*
* @var int
*/
public $per_page;
2011-07-25 14:07:46 -07:00
/**
* The last page available for the result set.
*
* @var int
*/
public $last_page;
/**
* The language that should be used when generating page links.
*
* @var string
*/
public $language;
2011-07-20 23:31:02 -05:00
/**
* Create a new Paginator instance.
*
* @param array $results
2011-07-25 14:07:46 -07:00
* @param int $page
2011-07-20 23:31:02 -05:00
* @param int $total
* @param int $per_page
2011-07-25 14:07:46 -07:00
* @param int $last_page
2011-07-20 23:31:02 -05:00
* @return void
*/
2011-07-25 14:07:46 -07:00
public function __construct($results, $page, $total, $per_page, $last_page)
2011-07-20 23:31:02 -05:00
{
2011-07-25 14:07:46 -07:00
$this->last_page = $last_page;
2011-07-20 23:31:02 -05:00
$this->per_page = $per_page;
$this->results = $results;
$this->total = $total;
2011-07-25 14:07:46 -07:00
$this->page = $page;
}
/**
* Create a new Paginator instance.
*
* @param array $results
* @param int $total
* @param int $per_page
* @return Paginator
*/
public static function make($results, $total, $per_page)
{
return new static($results, static::page($total, $per_page), $total, $per_page, ceil($total / $per_page));
2011-07-20 23:31:02 -05:00
}
/**
* Get the current page from the request query string.
*
2011-07-21 08:51:10 -07:00
* The page will be validated and adjusted if it is less than one or greater than the last page.
2011-07-25 14:07:46 -07:00
* For example, if the current page is not an integer or less than one, one will be returned.
* If the current page is greater than the last page, the last page will be returned.
2011-07-21 06:37:10 -07:00
*
* @param int $total
* @param int $per_page
2011-07-20 23:31:02 -05:00
* @return int
*/
public static function page($total, $per_page)
2011-07-20 23:31:02 -05:00
{
2011-07-22 09:55:11 -07:00
$page = Input::get('page', 1);
if (is_numeric($page) and $page > $last_page = ceil($total / $per_page))
2011-07-20 23:31:02 -05:00
{
return ($last_page > 0) ? $last_page : 1;
2011-07-20 23:31:02 -05:00
}
return ($page < 1 or filter_var($page, FILTER_VALIDATE_INT) === false) ? 1 : $page;
2011-07-20 23:31:02 -05:00
}
/**
* Create the HTML pagination links.
*
* @param int $adjacent
* @return string
*/
2011-07-22 09:36:07 -07:00
public function links($adjacent = 3)
2011-07-20 23:31:02 -05:00
{
2011-08-02 11:22:59 -05:00
if ($this->last_page <= 1) return '';
return '<div class="pagination">'.$this->previous().$this->numbers($adjacent).$this->next().'</div>';
2011-07-20 23:31:02 -05:00
}
/**
* Generate the HTML numeric page links.
*
2011-07-21 08:51:10 -07:00
* If there are not enough pages to make it worth sliding, all of the pages will be listed.
*
2011-07-20 23:31:02 -05:00
* @param int $adjacent
* @return string
*/
2011-07-21 08:51:10 -07:00
private function numbers($adjacent = 3)
2011-07-20 23:31:02 -05:00
{
2011-08-02 11:22:59 -05:00
// The hard-coded "7" is to account for all of the constant elements in a sliding range.
// Namely: The the current page, the two ellipses, the two beginning pages, and the two ending pages.
if ($this->last_page < 7 + ($adjacent * 2))
{
return $this->range(1, $this->last_page);
}
return $this->slider($adjacent);
2011-07-20 23:31:02 -05:00
}
/**
* Build sliding list of HTML numeric page links.
*
* @param int $adjacent
* @return string
*/
2011-07-21 08:51:10 -07:00
private function slider($adjacent)
2011-07-20 23:31:02 -05:00
{
if ($this->page <= $adjacent * 2)
{
2011-07-25 08:37:33 -07:00
return $this->range(1, 2 + ($adjacent * 2)).$this->ending();
2011-07-20 23:31:02 -05:00
}
2011-07-25 14:07:46 -07:00
elseif ($this->page >= $this->last_page - ($adjacent * 2))
{
2011-07-25 14:07:46 -07:00
return $this->beginning().$this->range($this->last_page - 2 - ($adjacent * 2), $this->last_page);
2011-07-20 23:31:02 -05:00
}
2011-07-25 14:07:46 -07:00
return $this->beginning().$this->range($this->page - $adjacent, $this->page + $adjacent).$this->ending();
2011-07-20 23:31:02 -05:00
}
/**
* Generate the "previous" HTML link.
*
* @return string
*/
2011-07-22 09:36:07 -07:00
public function previous()
2011-07-20 23:31:02 -05:00
{
2011-07-26 23:23:34 -05:00
$text = Lang::line('pagination.previous')->get($this->language);
2011-07-21 07:20:11 -07:00
2011-08-02 11:22:59 -05:00
if ($this->page > 1)
{
return $this->link($this->page - 1, $text, 'prev_page').' ';
}
return HTML::span($text, array('class' => 'disabled prev_page')).' ';
2011-07-20 23:31:02 -05:00
}
/**
* Generate the "next" HTML link.
*
* @return string
*/
2011-07-22 09:36:07 -07:00
public function next()
2011-07-20 23:31:02 -05:00
{
2011-07-26 23:23:34 -05:00
$text = Lang::line('pagination.next')->get($this->language);
2011-07-21 07:20:11 -07:00
2011-08-02 11:22:59 -05:00
if ($this->page < $this->last_page)
{
return $this->link($this->page + 1, $text, 'next_page');
}
return HTML::span($text, array('class' => 'disabled next_page'));
2011-07-20 23:31:02 -05:00
}
/**
* Build the first two page links for a sliding page range.
*
* @return string
*/
2011-07-21 08:51:10 -07:00
private function beginning()
2011-07-20 23:31:02 -05:00
{
2011-07-25 14:07:46 -07:00
return $this->range(1, 2).'<span class="dots">...</span>';
2011-07-20 23:31:02 -05:00
}
/**
* Build the last two page links for a sliding page range.
*
* @return string
*/
2011-07-21 08:51:10 -07:00
private function ending()
2011-07-20 23:31:02 -05:00
{
2011-07-25 14:07:46 -07:00
return '<span class="dots">...</span>'.$this->range($this->last_page - 1, $this->last_page);
}
2011-07-20 23:31:02 -05:00
/**
* Build a range of page links.
*
* For the current page, an HTML span element will be generated instead of a link.
*
* @param int $start
* @param int $end
2011-07-20 23:31:02 -05:00
* @return string
*/
2011-07-21 08:51:10 -07:00
private function range($start, $end)
2011-07-20 23:31:02 -05:00
{
$pages = '';
for ($i = $start; $i <= $end; $i++)
{
2011-07-25 08:37:33 -07:00
$pages .= ($this->page == $i) ? HTML::span($i, array('class' => 'current')).' ' : $this->link($i, $i, null).' ';
2011-07-20 23:31:02 -05:00
}
return $pages;
}
2011-07-25 14:07:46 -07:00
/**
* Create a HTML page link.
*
* @param int $page
* @param string $text
* @param string $attributes
* @return string
*/
private function link($page, $text, $class)
{
return HTML::link(Request::uri().'?page='.$page, $text, array('class' => $class), Request::is_secure());
}
/**
* Set the language that should be used when generating page links.
*
* @param string $language
* @return Paginator
*/
public function lang($language)
{
$this->language = $language;
return $this;
}
2011-07-20 23:31:02 -05:00
}