Deprecated: Assigning the return value of new by reference is deprecated in /home/bluestat/public_html/source/index.php on line 477
ISSO - Blob - ViewGit - Blue Static
<?php
/*=====================================================================*\
|| ###################################################################
|| # Blue Static ISSO Framework
|| # Copyright (c)2005-2009 Blue Static
|| #
|| # This program is free software; you can redistribute it and/or modify
|| # it under the terms of the GNU General Public License as published by
|| # the Free Software Foundation; version 2 of the License.
|| #
|| # This program is distributed in the hope that it will be useful, but
|| # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|| # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|| # more details.
|| #
|| # You should have received a copy of the GNU General Public License along
|| # with this program; if not, write to the Free Software Foundation, Inc.,
|| # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|| ###################################################################
\*=====================================================================*/

/**
 * Pagination System (Pagination.php)
 *
 * @package	ISSO
 */

/**
 * Pagination System
 *
 * On many pages, it is necessary to limit the amount of records to display.
 * Using this class, you can set the maximum and minimum values to display,
 * and then the input variables for page number and perpage. This will
 * then create a page navigator and manage the SQL LIMIT statements.
 *
 * @author		Blue Static
 * @copyright	Copyright (c)2005 - 2009, Blue Static
 * @package		ISSO
 *
 */
abstract class BSPagination
{
	/**
	 * Current page number
	 * @var	integer
	 */
	public $page;

	/**
	 * Per-page value
	 * @var	integer
	 */
	public $perpage;

	/**
	 * Number of page links
	 * @var	integer
	 */
	protected $pagelinks;

	/**
	 * Total number of results
	 * @var	integer
	 */
	protected $total;

	/**
	 * Total number of pages
	 * @var	integer
	 */
	protected $pagecount;

	/**
	 * Maximum number of per-page results
	 * @var	integer
	 */
	protected $maxperpage = 100;

	/**
	 * Default number of per-page results
	 * @var	integer
	 */
	protected $defaultperpage = 20;

	/**
	 * Callback public function for the processing of an indivdual page link
	 *
	 * @param	string	The base link
	 * @param	boolean	Wether or not the item is the current page
	 * @param	integer	Page number
	 *
	 * @return	string	Compiled HTML
	 */
	protected abstract function _bitProcessor($baselink, $isCurrent, $pagenumber);

	/**
	 * Callback public function for the processing the entire page navigator
	 *
	 * @param	string	The base link
	 * @param	integer	Next page number
	 * @param	integer	Previous page number
	 * @param	array	Show options: array('first' => (boolean)first page link, 'last' => boolean, 'prev' => boolean, 'next' => boolean)
	 * @param	string	Bits to process
	 *
	 * @return	string	Compiled HTML
	 */
	protected abstract function _navigationProcessor($baselink, $next, $prev, $show, $bits);

	/**
	 * Set the Pagination->perpage and Pagination->page variables
	 */
	protected abstract function _setVariables();

	/**
	 * Constructor
	 */
	public function __construct()
	{
		if (!BSApp::$input instanceof BSInput)
		{
			throw new Exception('BSApp::$input is not an instance of BSInput');
		}
	}

	/**
	 * Returns the current page number
	 *
	 * @return	integer	Current page
	 */
	public function getPage()
	{
		return $this->page;
	}

	/**
	 * Returns the current perpage value
	 *
	 * @return	integer	Current perpage
	 */
	public function getPerPage()
	{
		return $this->perpage;
	}

	/**
	 * Sets total
	 *
	 * @param	integer	Total number
	 */
	public function setTotal($total)
	{
		$this->total = $total;
	}

	/**
	 * Returns the number of pages to be in the navigator
	 *
	 * @param	integer	Number of pages
	 */
	public function getPageCount()
	{
		return $this->pagecount;
	}

	/**
	 * Takes all of the information from the set() functions and then
	 * prepares all of the data through verification
	 */
	public function processIncomingData()
	{
		$this->_setVariables();
		$this->page = BSApp::$input->clean($this->page, TYPE_INT);
		$this->perpage = BSApp::$input->clean($this->perpage, TYPE_INT);
		$this->pagelinks = BSApp::$input->clean($this->pagelinks, TYPE_INT);

		if ($this->page <= 0)
		{
			$this->page = 1;
		}

		if ($this->perpage <= 0)
		{
			$this->perpage = $this->defaultperpage;
		}
		if ($this->perpage > $this->maxperpage)
		{
			$this->perpage = $this->maxperpage;
		}

		$this->perpage = BSApp::$input->clean($this->perpage, TYPE_INT);
	}

	/**
	 * Takes the variables and splits up the pages
	 */
	public function splitPages()
	{
		$this->pagecount = ceil($this->total / $this->perpage);
		if ($this->pagelinks == 0)
		{
			$this->pagelinks = $this->pagecount;
		}
	}

	/**
	 * Returns the lower limit of the pages
	 *
	 * @param	integer	Page number
	 *
	 * @return	integer	Lower result limit
	 */
	public function fetchLimit($page = null)
	{
		if ($page === null)
		{
			$page = $this->page;
		}

		$limit = $page * $this->perpage;

		if ($page < 1)
		{
			$page = 1;
			$limit = 0;
		}
		else if ($page > $this->pagecount)
		{
			$page = $this->pagecount - 1;
			$limit = $this->total;
		}

		if ($limit < 0)
		{
			return 0;
		}
		else if ($limit > $this->total)
		{
			return $this->total;
		}
		else
		{
			return $limit;
		}
	}

	/**
	 * Constructs the page navigator
	 *
	 * @param	string	Base link path
	 * @param	bool	Add a ? or a & to the path so it's link-friendly
	 *
	 * @return	string	Generated HTML page navigator
	 */
	public function constructPageNav($baselink, $addParam = true)
	{
		// handle base link
		if ($addParam)
		{
			if (strpos($baselink, '?') === false)
			{
				$baselink .= '?';
			}
			else if (!strpos($baselink, '#\?$#') && !strpos($baselink, '&'))
			{
				$baselink .= '&amp;';
			}
		}

		// first page number in page nav
		$startpage = $this->page - $this->pagelinks;
		if ($startpage < 1)
		{
			$startpage = 1;
		}

		// last page number in page nav
		$endpage = $this->page + $this->pagelinks;
		if ($endpage > $this->pagecount)
		{
			$endpage = $this->pagecount;
		}

		// prev page in page nav
		$prevpage = $this->page - 1;
		if ($prevpage < 1)
		{
			$prevpage = 1;
		}

		// next page in page nav
		$nextpage = $this->page + 1;
		if ($nextpage > $this->pagecount)
		{
			$nextpage = $this->pagecount;
		}

		// show the prev page
		$show['prev'] = true;
		if ($this->page == $startpage)
		{
			$show['prev'] = false;
		}

		// show the next page
		$show['next'] = true;
		if ($this->page == $endpage)
		{
			$show['next'] = false;
		}

		// show the first page
		$show['first'] = false;
		if ($startpage > 1)
		{
			$show['first'] = true;
		}

		// show the last page
		$show['last'] = false;
		if ($endpage < $this->pagecount)
		{
			$show['last'] = true;
		}

		// construct the page bits
		$bits = '';
		for ($i = $startpage; $i <= $endpage; $i++)
		{
			if ($i == $this->page)
			{
				$nolink = true;
			}
			else
			{
				$nolink = false;
			}

			$bits .= $this->_bitProcessor($baselink, $nolink, $i);
		}

		return $this->_navigationProcessor($baselink, $nextpage, $prevpage, $show, $bits);
	}
}

?>