Modules

Pagination
extends Kohana_Pagination

Pagination links generator.

package
Koseven/Pagination
category
Base
author
Koseven Team
copyright
© Kohana Team
license
https://koseven.ga/LICENSE.md

Class declared in MODPATH/pagination/classes/Pagination.php on line 3.

Properties

protected $_request

Default value:
NULL

protected $_route

Default value:
NULL

protected $_route_params

Default value:
array(0) 

protected $config

Default value:
array(6) (
    "current_page" => array(2) (
        "source" => string(12) "query_string"
        "key" => string(4) "page"
    )
    "total_items" => integer 0
    "items_per_page" => integer 10
    "view" => string(16) "pagination/basic"
    "auto_hide" => bool TRUE
    "first_page_in_url" => bool FALSE
)

protected $current_first_item

Default value:
NULL

protected $current_last_item

Default value:
NULL

protected $current_page

Default value:
NULL

protected $first_page

Default value:
NULL

protected $items_per_page

Default value:
NULL

protected $last_page

Default value:
NULL

protected $next_page

Default value:
NULL

protected $offset

Default value:
NULL

protected $previous_page

Default value:
NULL

protected $total_items

Default value:
NULL

protected $total_pages

Default value:
NULL

Methods

public __construct( [ array $config = array(0) , $request = NULL ] ) (defined in Kohana_Pagination)

Creates a new Pagination object.

Parameters

  • array $config = array(0) - Configuration
  • unknown $request = NULL

Return Values

  • void

Source Code

public function __construct(array $config = [], Request $request = NULL)
{
	// Overwrite system defaults with application defaults
	$this->config = $this->config_group() + $this->config;

	// Assing Request
	if ($request === NULL)
	{
		$request = Request::current();
	}

	$this->_request = $request;

	// Assign default Route
	$this->_route = $request->route();

	// Assign default route params
	$this->_route_params = $request->param();

	// Add controller and action to route params for routes with variable controllers and actions
	$this->_route_params['controller'] = $request->controller();
	$this->_route_params['action'] = $request->action();
	$this->_route_params['directory'] = $request->directory();

	// Pagination setup
	$this->setup($config);
}

public __get( string $key ) (defined in Kohana_Pagination)

Returns a Pagination property.

Parameters

  • string $key required - Property name

Return Values

  • mixed - Pagination property; NULL if not found

Source Code

public function __get($key)
{
	return isset($this->$key) ? $this->$key : NULL;
}

public __set( string $key , mixed $value ) (defined in Kohana_Pagination)

Updates a single config setting, and recalculates pagination if needed.

Parameters

  • string $key required - Config key
  • mixed $value required - Config value

Return Values

  • void

Source Code

public function __set($key, $value)
{
	$this->setup([$key => $value]);
}

public __toString( ) (defined in Kohana_Pagination)

Renders the pagination links.

Return Values

  • string - Pagination output (HTML)

Source Code

public function __toString()
{
	try
	{
		return $this->render();
	}
	catch(Exception $e)
	{
		Kohana_Exception::handler($e);
		return '';
	}
}

public config_group( [ string $group = string(7) "default" ] ) (defined in Kohana_Pagination)

Retrieves a pagination config group from the config file. One config group can refer to another as its parent, which will be recursively loaded.

Parameters

  • string $group = string(7) "default" - Pagination config group; "default" if none given

Return Values

  • array - Config settings

Source Code

public function config_group($group = 'default')
{
	// Load the pagination config file
	$config_file = Kohana::$config->load('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( [ array $config = array(0) , $request = NULL ] ) (defined in Kohana_Pagination)

Creates a new Pagination object.

Parameters

  • array $config = array(0) - Configuration
  • unknown $request = NULL

Return Values

  • Pagination

Source Code

public static function factory(array $config = [], Request $request = NULL)
{
	return new Pagination($config, $request);
}

public query( [ array $params = NULL ] ) (defined in Kohana_Pagination)

URL::query() replacement for Pagination use only

Parameters

  • array $params = NULL - Parameters to override

Return Values

  • string

Source Code

public function query(array $params = NULL)
{
	if ($params === NULL)
	{
		// Use only the current parameters
		$params = $this->_request->query();
	}
	else
	{
		// Merge the current and new parameters
		$params = array_merge($this->_request->query(), $params);
	}

	if (empty($params))
	{
		// No query parameters
		return '';
	}

	// Note: http_build_query returns an empty string for a params array with only NULL values
	$query = http_build_query($params, '', '&');

	// Don't prepend '?' to an empty string
	return ($query === '') ? '' : ('?'.$query);
}

public render( [ mixed $view = NULL ] ) (defined in Kohana_Pagination)

Renders the pagination links.

Parameters

  • mixed $view = NULL - String of the view to use, or a Kohana_View object

Return Values

  • string - Pagination output (HTML)

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 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 request( [ Request $request = NULL ] ) (defined in Kohana_Pagination)

Request setter / getter

Parameters

  • Request $request = NULL

Return Values

  • Request - If used as getter
  • $this - Chainable as setter

Source Code

public function request(Request $request = NULL)
{
	if ($request === NULL)
		return $this->_request;

	$this->_request = $request;

	return $this;
}

public route( [ Route $route = NULL ] ) (defined in Kohana_Pagination)

Route setter / getter

Parameters

  • Route $route = NULL

Return Values

  • Route - Route if used as getter
  • $this - Chainable as setter

Source Code

public function route(Route $route = NULL)
{
	if ($route === NULL)
		return $this->_route;

	$this->_route = $route;

	return $this;
}

public route_params( [ array $route_params = NULL ] ) (defined in Kohana_Pagination)

Route parameters setter / getter

Parameters

  • array $route_params = NULL - Route parameters to set

Return Values

  • array - Route parameters if used as getter
  • $this - Chainable as setter

Source Code

public function route_params(array $route_params = NULL)
{
	if ($route_params === NULL)
		return $this->_route_params;

	$this->_route_params = $route_params;

	return $this;
}

public setup( [ array $config = array(0) ] ) (defined in Kohana_Pagination)

Loads configuration settings into the object and (re)calculates pagination if needed. Allows you to update config settings after a Pagination object has been constructed.

Parameters

  • array $config = array(0) - Configuration

Return Values

  • object - Pagination

Source Code

public function setup(array $config = [])
{
	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
		{
			$query_key = $this->config['current_page']['key'];

			switch ($this->config['current_page']['source'])
			{
				case 'query_string':
					$this->current_page = ($this->_request->query($query_key) !== NULL)
						? (int) $this->_request->query($query_key)
						: 1;
					break;

				case 'route':
					$this->current_page = (int) $this->_request->param($query_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( [ integer $page = integer 1 ] ) (defined in Kohana_Pagination)

Generates the full URL for a certain page.

Parameters

  • integer $page = integer 1 - Page number

Return Values

  • string - Page URL

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($this->_route->uri($this->_route_params).
				$this->query([$this->config['current_page']['key'] => $page]));

		case 'route':
			return URL::site($this->_route->uri(array_merge($this->_route_params,
				[$this->config['current_page']['key'] => $page])).$this->query());
	}

	return '#';
}

public valid_page( integer $page ) (defined in Kohana_Pagination)

Checks whether the given page number exists.

Parameters

  • integer $page required - Page number

Tags

  • Since - 3.0.7

Return Values

  • boolean

Source Code

public function valid_page($page)
{
	// Page number has to be a clean integer
	if ( ! Valid::digit($page))
		return FALSE;

	return $page > 0 AND $page <= $this->total_pages;
}