<?php
/**
 * 分页器类[只获取需要显示的数据]
 * (使用该类需要创建对象)
 *
 * @author Wzb
 *        
 */
class COM_PagingDevice {
	/**
	 * 当前页数
	 *
	 * @var int
	 */
	private $cPage;
	/**
	 * 记录总数
	 *
	 * @var int
	 */
	private $recordCount;
	/**
	 * 总页数
	 *
	 * @var int
	 */
	private $maxPage;
	/**
	 * 每页显示的数量
	 *
	 * @var int
	 */
	private $pageSize;
	/**
	 * 当前页的参数名称
	 *
	 * @var string
	 */
	private $pageCode;
	/**
	 * 翻页后缀
	 *
	 * @var string
	 */
	private $suffixal;
	/**
	 * 数据列表
	 *
	 * @var array
	 */
	private $dataList;
	/**
	 * 设置翻页后缀
	 *
	 * @param string $value
	 */
	public function _setSuffixal($value) {
		$this->suffixal = $value;
	}
	/**
	 * 获取数据列表
	 */
	public function _getDataList() {
		return $this->dataList;
	}
	
	/**
	 * 构造函数
	 *
	 * @param int $pageSize=20 每页显示的数量
	 * @param string $pageCode='page' 当前页的参数名称
	 */
	public function __construct($pageSize = 20, $pageCode = "page") {
		$this->pageSize = $pageSize;
		$this->pageCode = $pageCode;
		
		if (array_key_exists ( $this->pageCode, $_REQUEST )) {
			$this->cPage = intval ( $_REQUEST [$this->pageCode] );
			$this->cPage = $this->cPage > 0 ? $this->cPage : 0;
		} else {
			$this->cPage = 1;
		}
	}
	
	/**
	 * 获取需要显示的记录数组
	 *
	 * @return array('begin'=>0,'num'=>0) 第一项表示开始位置，第二项目显示的条数
	 */
	public function _getLimitArr() {
		return array ('begin' => ($this->cPage - 1) * $this->pageSize,'num' => $this->pageSize );
	}
	
	/**
	 * 设置数据
	 *
	 * @param array $dataList 当前页的数据，在$dataList[0]中包含了PdAllNum项 即总记录数
	 */
	public function _setDataList($dataList) {
		$this->dataList = $dataList;
		$this->recordCount = 0;
		if (count ( $this->dataList ) > 0) {
			$this->recordCount = $this->dataList [0] ['PdAllNum'];
		}
		// 总页数
		if ($this->recordCount % $this->pageSize == 0) {
			$this->maxPage = intval ( $this->recordCount / $this->pageSize );
		} else {
			$this->maxPage = intval ( $this->recordCount / $this->pageSize ) + 1;
		}
	}
	
	/**
	 * 常规翻页数组
	 */
	public function _getPageInfo() {
		$theArray = array ();
		$theArray ['maxPage'] = $this->maxPage;
		$theArray ['recordCount'] = $this->recordCount;
		$theArray ['pageList'] = array ();
		
		if ($this->cPage > 1) {
			$theArray ['frist'] = "?{$this->pageCode}=1";
			$theArray ['pre'] = "?{$this->pageCode}=" . ($this->cPage - 1);
		} else {
			$theArray ['frist'] = "";
			$theArray ['pre'] = "";
		}
		if ($this->cPage < $this->maxPage) {
			$theArray ['next'] = "?{$this->pageCode}=" . ($this->cPage + 1);
			$theArray ['end'] = "?{$this->pageCode}={$this->maxPage}";
		} else {
			$theArray ['next'] = "";
			$theArray ['end'] = "";
		}
		
		if (! empty ( $this->suffixal )) {
			if ($this->cPage > 1) {
				$theArray ['frist'] .= "&{$this->suffixal}";
				$theArray ['pre'] .= "&{$this->suffixal}";
			}
			if ($this->cPage < $this->maxPage) {
				$theArray ['next'] .= "&{$this->suffixal}";
				$theArray ['end'] .= "&{$this->suffixal}";
			}
		}
		if ($this->maxPage > 0) {
			for($i = 1; $i <= $this->maxPage; $i ++) {
				$url = "?{$this->pageCode}=$i";
				if (! empty ( $this->suffixal )) {
					$url .= "&{$this->suffixal}";
				}
				$row = array ('num' => $i,'url' => $url,'isSelected' => false );
				if (intval ( $i ) == intval ( $this->cPage )) {
					$row ['isSelected'] = true;
				}
				$theArray ['pageList'] [] = $row;
			}
		} else {
			$url = "?{$this->pageCode}=1";
			if (! empty ( $this->suffixal )) {
				$url .= "&{$this->suffixal}";
			}
			$theArray ['pageList'] [] = array ('num' => 1,'url' => $url,'isSelected' => true );
		}
		return $theArray;
	}
	
	/**
	 * 常规翻页数组
	 */
	public function _getPageInfo1() {
		$value = "";
		$url = "";
		if ($this->cPage > 1) {
			$thePage = $this->cPage - 1;
			$value .= "<em><a href=\"?{$this->pageCode}=$thePage&{$this->suffixal}\">上一页</a></em>\r\n";
		}
		if ($this->maxPage <= 5) {
			for($i = 1; $i <= $this->maxPage; $i ++) {
				$url = "?{$this->pageCode}=$i";
				if (! empty ( $this->suffixal )) {
					$url .= "&{$this->suffixal}";
				}
				if ($this->cPage == $i) {
					$value .= "<em class=\"onfocus\"><a href=\"$url\">$i</a></em>\r\n";
				} else {
					$value .= "<em><a href=\"$url\">$i</a></em>\r\n";
				}
			}
		} else {
			if ($this->cPage >= $this->maxPage - 4) {
				$value .= "<em><a href=\"?&{$this->pageCode}=1&{$this->suffixal}\">1</a></em>\r\n";
				$value .= "<em>...</em>\r\n";
				for($i = $this->maxPage - 4; $i <= $this->maxPage; $i ++) {
					$url = "?{$this->pageCode}=$i";
					if (! empty ( $this->suffixal )) {
						$url .= "&{$this->suffixal}";
					}
					if ($this->cPage == $i) {
						$value .= "<em class=\"onfocus\"><a href=\"$url\">$i</a></em>\r\n";
					} else {
						$value .= "<em><a href=\"$url\">$i</a></em>\r\n";
					}
				}
			} elseif ($this->cPage < 5) {
				for($i = 1; $i <= 5; $i ++) {
					$url = "?{$this->pageCode}=$i";
					if (! empty ( $this->suffixal )) {
						$url .= "&{$this->suffixal}";
					}
					if ($this->cPage == $i) {
						$value .= "<em class=\"onfocus\"><a href=\"$url\">$i</a></em>\r\n";
					} else {
						$value .= "<em><a href=\"$url\">$i</a></em>\r\n";
					}
				}
				$value .= "<em>...</em>\r\n";
				$value .= "<em><a href=\"?&{$this->pageCode}={$this->maxPage}&{$this->suffixal}\">{$this->maxPage}</a></em>\r\n";
			} else {
				$value .= "<em><a href=\"?&{$this->pageCode}=1&{$this->suffixal}\">1</a></em>\r\n";
				$value .= "<em>...</em>\r\n";
				for($i = $this->cPage - 2; $i <= $this->cPage + 2; $i ++) {
					$url = "?{$this->pageCode}=$i";
					if (! empty ( $this->suffixal )) {
						$url .= "&{$this->suffixal}";
					}
					if ($this->cPage == $i) {
						$value .= "<em class=\"onfocus\"><a href=\"$url\">$i</a></em>\r\n";
					} else {
						$value .= "<em><a href=\"$url\">$i</a></em>\r\n";
					}
				}
				$value .= "<em>...</em>\r\n";
				$value .= "<em><a href=\"?&{$this->pageCode}={$this->maxPage}&{$this->suffixal}\">{$this->maxPage}</a></em>\r\n";
			}
		}
		if ($this->cPage < $this->maxPage) {
			$thePage = $this->cPage + 1;
			$value .= "<em><a href=\"?{$this->pageCode}=$thePage&{$this->suffixal}\">下一页</a></em>\r\n";
		}
		$value .= "<span>&nbsp;&nbsp;共{$this->maxPage}页&nbsp;&nbsp;当前为第{$this->cPage}页</span>";
		return $value;
	}
}
?>