<?php
/**
 * @version $Id: page.class.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

class Page
{
	/**
	 * 当前页
	 */
	var $currentPage;
	
	/**
	 * 总记录数
	 */
	var $totalRecords;
	
	/**
	 * 总页数
	 */
	var $totalPages;
	
	/**
	 * 页记录数
	 */
	var $pageSize;
	
	/**
	 * 链接前缀
	 */
	var $url;

	/**
	 * 构造函数
	 * 
	 * @param Int $currentPage 当前页ID
	 * @param Int $totalRecords 总记录数
	 * @param String $url 链接前缀
	 * @param Int $pageSize 每页显示记录数
	 */
	function Page($currentPage, $totalRecords, $url, $pageSize = 10)
	{
		$this->totalRecords = $totalRecords;
		$this->pageSize = $pageSize;
		if ($this->totalRecords)
		{
			$this->totalPages = ceil($this->totalRecords / $this->pageSize);
		}
		else
		{
			$this->totalPages = 1;
		}
		if ($currentPage > $this->totalPages)
		{
			$this->currentPage = $this->totalPages;
		}
		elseif ($currentPage < 1)
		{
			$this->currentPage = 1;
		}
		else
		{
			$this->currentPage = $currentPage;
		}
		$this->url = $url;
	}

	/**
	 * 返回上一页编号
	 *
	 * @return Int
	 */
	function getPreLink()
	{
	    return ($this->currentPage > 1) ? ($this->currentPage - 1) : 1;
	}
	
	/**
	 * 返回下一页编号
	 *
	 * @return Int
	 */
	function getNextLink()
	{
	    return ($this->currentPage < $this->totalPages) ? ($this->currentPage + 1) : $this->totalPages;
	}
	
	/**
	 * 返回当前页编号
	 *
	 * @return Int
	 */
	function getCurrentLink()
	{
	    return $this->currentPage;
	}

	/**
	 * 返回链接
	 *
	 * @return String
	 */
	function getMultLink()
	{
		global $cp_message;
		$mult = '';
		if ($this->currentPage == 1)
		{
			$mult .= '<span class="nextprev">'.$cp_message['prev'].'</span><span class="current">1</span>';
			for ($i = 2; $i <= $this->totalPages; $i++)
			{
				$mult .= '<a href="'.$this->url.'page='.$i.'">'.$i.'</a>';
				$prev = $this->totalPages - 1;
				if ($i >= 10 && $this->totalPages > 10)
				{
					$mult .= '<span>&#8230;</span><a href="'.$this->url.'page='.$prev.'">'.$prev.'</a>';
					$mult .= '<a href="'.$this->url.'page='.$this->totalPages.'">'.$this->totalPages.'</a>';
					break;
				}
			}
		}
		else
		{
			$mult .= '<a href="'.$this->url.'page='.($this->currentPage - 1).'" class="nextprev">'.$cp_message['prev'].'</a>';
			if ($this->currentPage <= 8)
			{
				for ($i = 1; $i < $this->currentPage; $i++)
				{
					$mult .= '<a href="'.$this->url.'page='.$i.'">'.$i.'</a>';
				}
			}
			else
			{
				$mult .= '<a href="'.$this->url.'page=1">1</a><a href="'.$this->url.'page=2">2</a><span>&#8230;</span>';
				for ($i = $this->currentPage -5; $i < $this->currentPage; $i++)
				{
					$mult .= '<a href="'.$this->url.'page='.$i.'">'.$i.'</a>';
				}
			}
			$mult .= '<span class="current">'.$this->currentPage.'</span>';
		}
		if (($this->currentPage == $this->totalPages) || $this->totalPages == 0)
		{
			$mult .= '<span class="nextprev">'.$cp_message['next'].'</span>';
		}
		else
		{
			if ($this->currentPage != 1)
			{
				if ($this->currentPage <= 8)
				{
					for ($i = $this->currentPage + 1; $i <= $this->totalPages; $i++)
					{
						$mult .= '<a href="'.$this->url.'page='.$i.'">'.$i.'</a>';
						if ($i >= 10)
						{
							break;
						}
					}
					if ($this->totalPages > 10)
					{
						$mult .= '<span>&#8230;</span>';
						$mult .= '<a href="'.$this->url.'page='.($this->totalPages - 1).'">'.($this->totalPages - 1).'</a>';
						$mult .= '<a href="'.$this->url.'page='.$this->totalPages.'">'.$this->totalPages.'</a>';	
					}
				}
				elseif (($this->totalPages - $this->currentPage) > 6)
				{
					$endlink = $this->currentPage + 5;
					for ($i = $this->currentPage + 1; $i < $endlink; $i++)
					{
						$mult .= '<a href="'.$this->url.'page='.$i.'">'.$i.'</a>';
					}
					$mult .= '<span>&#8230;</span>';
					$mult .= '<a href="'.$this->url.'page='.($this->totalPages - 1).'">'.($this->totalPages - 1).'</a>';
					$mult .= '<a href="'.$this->url.'page='.$this->totalPages.'">'.$this->totalPages.'</a>';
				}
				else
				{
					$endlink = $this->totalPages - $this->currentPage;
					for ($i = 1; $i <= $endlink; $i++)
					{
						$next = $this->currentPage + $i;
						$mult .= '<a href="'.$this->url.'page='.$next.'">'.$next.'</a>';
					}
				}
			}
			$mult .= '<a href="'.$this->url.'page='.($this->currentPage + 1).'" class="nextprev">'.$cp_message['next'].'</a>';
		}
		return $mult;
	}
}
?>