Pagination
› Kohana_Pagination
Class Contents
Constants
- None
Properties
- None
Class declared in MODPATH/pagination/classes/pagination.php on line 3.
Methods
public __construct( )
› Kohana_Pagination
Source Code
public function __construct(array $config = array())
{
// Overwrite system defaults with application defaults
$this->config = $this->config_group() + $this->config;
// Pagination setup
$this->setup($config);
}
public __get( )
› Kohana_Pagination
Source Code
public function __get($key)
{
return isset($this->$key) ? $this->$key : NULL;
}
public __set( )
› Kohana_Pagination
Source Code
public function __set($key, $value)
{
$this->setup(array($key => $value));
}
public __toString( )
› Kohana_Pagination
Source Code
public function __toString()
{
return $this->render();
}
public config_group( )
› Kohana_Pagination
Source Code
public function config_group($group = 'default')
{
// Load the pagination config file
$config_file = Kohana::config('pagination');
// Initialize the $config array
$config['group'] = (string) $group;
// Recursively load requested config groups
while (isset($config['group']) AND isset($config_file->$config['group']))
{
// Temporarily store config group name
$group = $config['group'];
unset($config['group']);
// Add config group values, not overwriting existing keys
$config += $config_file->$group;
}
// Get rid of possible stray config group names
unset($config['group']);
// Return the merged config group settings
return $config;
}
public static factory( )
› Kohana_Pagination
Source Code
public static function factory(array $config = array())
{
return new Pagination($config);
}
public render( )
› Kohana_Pagination
Source Code
public function render($view = NULL)
{
// Automatically hide pagination whenever it is superfluous
if ($this->config['auto_hide'] === TRUE AND $this->total_pages <= 1)
return '';
if ($view === NULL)
{
// Use the view from config
$view = $this->config['view'];
}
if ( ! $view instanceof Kohana_View)
{
// Load the view file
$view = View::factory($view);
}
// Pass on the whole Pagination object
return $view->set(get_object_vars($this))->set('page', $this)->render();
}
public setup( )
› Kohana_Pagination
Source Code
public function setup(array $config = array())
{
if (isset($config['group']))
{
// Recursively load requested config groups
$config += $this->config_group($config['group']);
}
// Overwrite the current config settings
$this->config = $config + $this->config;
// Only (re)calculate pagination when needed
if ($this->current_page === NULL
OR isset($config['current_page'])
OR isset($config['total_items'])
OR isset($config['items_per_page']))
{
// Retrieve the current page number
if ( ! empty($this->config['current_page']['page']))
{
// The current page number has been set manually
$this->current_page = (int) $this->config['current_page']['page'];
}
else
{
switch ($this->config['current_page']['source'])
{
case 'query_string':
$this->current_page = isset($_GET[$this->config['current_page']['key']])
? (int) $_GET[$this->config['current_page']['key']]
: 1;
break;
case 'route':
$this->current_page = (int) Request::current()->param($this->config['current_page']['key'], 1);
break;
}
}
// Calculate and clean all pagination variables
$this->total_items = (int) max(0, $this->config['total_items']);
$this->items_per_page = (int) max(1, $this->config['items_per_page']);
$this->total_pages = (int) ceil($this->total_items / $this->items_per_page);
$this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages));
$this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items);
$this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items);
$this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : FALSE;
$this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE;
$this->first_page = ($this->current_page === 1) ? FALSE : 1;
$this->last_page = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages;
$this->offset = (int) (($this->current_page - 1) * $this->items_per_page);
}
// Chainable method
return $this;
}
public url( )
› Kohana_Pagination
Source Code
public function url($page = 1)
{
// Clean the page number
$page = max(1, (int) $page);
// No page number in URLs to first page
if ($page === 1 AND ! $this->config['first_page_in_url'])
{
$page = NULL;
}
switch ($this->config['current_page']['source'])
{
case 'query_string':
return URL::site(Request::current()->uri).URL::query(array($this->config['current_page']['key'] => $page));
case 'route':
return URL::site(Request::current()->uri(array($this->config['current_page']['key'] => $page))).URL::query();
}
return '#';
}
public valid_page( )
› Kohana_Pagination
Source Code
public function valid_page($page)
{
// Page number has to be a clean integer
if ( ! Validate::digit($page))
return FALSE;
return $page > 0 AND $page <= $this->total_pages;
}