Location: PHPKode > projects > Bugdar > upload/framework/pagination.php
<?php
/*=====================================================================*\
|| ###################################################################
|| # Blue Static ISSO Framework
|| # Copyright ©2002-[#]year[#] 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 [#]gpl[#] 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.
*
* Templates required:
*		pagenav_bit - The individual page numbers in the page navigator
*		pagenav - The entirity of the page navigtaor
*
* @author		Blue Static
* @copyright	Copyright ©2002 - [#]year[#], Blue Static
* @version		$Revision$
* @package		ISSO
* 
*/
class Pagination
{
	/**
	* Current page number
	* @var	integer
	* @access	private
	*/
	var $page;
	
	/**
	* Per-page value
	* @var	integer
	* @access	private
	*/
	var $perpage;
	
	/**
	* Number of page links
	* @var	integer
	* @access	private
	*/
	var $pagelinks;
	
	/**
	* Total number of results
	* @var	integer
	* @access	private
	*/
	var $total;
	
	/**
	* Total number of pages
	* @var	integer
	* @access	private
	*/
	var $pagecount;
	
	/**
	* Name of page variable
	* @var	array
	* @access	private
	*/
	var $pagevar;
	
	/**
	* Name of per-page variable
	* @var	integer
	* @access	private
	*/
	var $perpagevar;
	
	/**
	* Maximum number of per-page results
	* @var	integer
	* @access	private
	*/
	var $maxperpage = 100;
	
	/**
	* Default number of per-page results
	* @var	integer
	* @access	private
	*/
	var $defaultperpage = 20;
	
	/**
	* The processing callback function for individual pagenav bits
	* @var string
	* @access	private
	*/
	var $bitprocessor = ':undefined:';
	
	/**
	* The processing callback function for the entire pagenav system
	* @var string
	* @access	private
	*/
	var $pagenavprocessor = ':undefined:';
	
	// ###################################################################
	/**
	* Constructor
	*/
	function __construct(&$registry)
	{
		$this->registry =& $registry;
	}
	
	// ###################################################################
	/**
	* (PHP 4) Constructor
	*/
	function Pagination(&$registry)
	{
		$this->__construct($registry);
	}
	
	// ###################################################################
	/**
	* Callback function for the processing of an indivdual page. Needs
	* the signature:
	* public string callback(string $baseLink, boolean $noLink, integer $pageNumber, Pagination $this)
	*
	* @access	public
	*
	* @param	string	Callback function
	*/
	function setBitProcessor($callback)
	{
		$this->bitprocessor = $callback;
	}
	
	// ###################################################################
	/**
	* Callback function for the processing the entire page navigator. Needs
	* the signature:
	* public string callback(string $baseLink, integer $nextPage, integer $prevPage array $show['first'=>first page, 'last'=>last page, 'prev'=>previous page, 'next'=>next page], string $bits, Pagination $this)
	*
	* @access	public
	*
	* @param	string	Callback function
	*/
	function setNavigatorProcessor($callback)
	{
		$this->pagenavprocessor = $callback;
	}
	
	// ###################################################################
	/**
	* Returns the current page number
	*
	* @access	public
	*
	* @return	integer	Current page
	*/
	function getPage()
	{
		return $this->page;
	}
	
	// ###################################################################
	/**
	* Returns the current perpage value
	*
	* @access	public
	*
	* @return	integer	Current perpage
	*/
	function getPerPage()
	{
		return $this->perpage;
	}
	
	// ###################################################################
	/**
	* Sets total
	*
	* @access	public
	*
	* @param	integer	Total number
	*/
	function setTotal($total)
	{
		$this->total = $total;
	}
	
	// ###################################################################
	/**
	* Returns the number of pages to be in the navigator
	*
	* @access	public
	*
	* @param	integer	Number of pages
	*/
	function getPageCount()
	{
		return $this->pagecount;
	}
	
	// ###################################################################
	/**
	* Sets pagelinks
	*
	* @access	public
	*
	* @param	integer	Number of page links
	*/
	function setPageLinks($pagelinks)
	{
		$this->pagelinks = $pagelinks;
	}
	
	// ###################################################################
	/**
	* Sets pagevar
	*
	* @access	public
	*
	* @param	string	Page variable
	*/
	function setPageVar($pagevar)
	{
		$this->pagevar = $pagevar;
	}
	
	// ###################################################################
	/**
	* Sets perpagevar
	*
	* @access	public
	*
	* @param	string	Per-page variable
	*/
	function setPerPageVar($perpagevar)
	{
		$this->perpagevar = $perpagevar;
	}
	
	// ###################################################################
	/**
	* Sets maxperpage
	*
	* @access	public
	*
	* @param	integer	Maximum number per page
	*/
	function setMaxPerPage($maxperpage)
	{
		$this->maxperpage = $maxperpage;
	}
	
	// ###################################################################
	/**
	* Sets defaultperpage
	*
	* @access	public
	*
	* @param	integer	Total number
	*/
	function setDefaultPerPage($defaultperpage)
	{
		$this->defaultperpage = $defaultperpage;
	}
	
	
	// ###################################################################
	/**
	* Takes all of the information from the set() functions and then
	* prepares all of the data through verification
	*
	* @access	public
	*/
	function processIncomingData()
	{
		$this->page = $this->registry->input_clean($this->pagevar, TYPE_INT);
		$this->perpage = $this->registry->input_clean($this->perpagevar, TYPE_INT);
		$this->pagelinks = $this->registry->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 = $this->registry->clean($this->perpage, TYPE_INT);
	}
	
	// ###################################################################
	/**
	* Takes the variables and splits up the pages
	*
	* @access	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
	*
	* @access	public
	*
	* @param	integer	Page number
	*
	* @return	integer	Lower result limit
	*/
	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
	*
	* @access	public
	*
	* @param	string	Base link path
	*
	* @return	string	Generated HTML page navigator
	*/
	function constructPageNav($baselink)
	{
		global $bugsys;
		
		// handle base link
		if (strpos($baselink, '?') === false)
		{
			$baselink .= '?';
		}
		else if (!preg_match('#\?$#', $baselink) AND !preg_match('#(&|&amp;)$#', $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 = '';
		$call = $this->bitprocessor;
		for ($i = $startpage; $i <= $endpage; $i++)
		{
			if ($i == $this->page)
			{
				$nolink = true;
			}
			else
			{
				$nolink = false;
			}
			
			$bits .= $call($baselink, $nolink, $i, $this);
		}
				
		$call = $this->pagenavprocessor;
		return $call($baselink, $nextpage, $prevpage, $show, $bits, $this);
	}
}

/*=====================================================================*\
|| ###################################################################
|| # $HeadURL$
|| # $Id$
|| ###################################################################
\*=====================================================================*/
?>
Return current item: Bugdar