<?php
class Page {
	public $total;
	public $maxpage;
	public $nextpage;
	public $page;
	public function set($total, $maxpage=null){
		$this->total = (int)$total;
		$this->page = isset($_GET['page']) ? intval($_GET['page']) : 1;	//当前页码e
		$this->nextpage = 7;
		$this->config = $GLOBALS['config'];
		if (is_null($maxpage)){
			$this->maxpage = $this->config['maxpage'];
		}else{
			$this->maxpage = $maxpage;
		}
	}
	public function PrintPage(){
		$array = array_merge($_POST,$_GET);
		unset($array['submit'], $array['page'], $array['controller'], $array['action']);
		$this->url = null;
		$array['keyword'] = urlencode(urldecode($array['keyword']));
		$this->url = str_replace(array('&','='), '/', http_build_query($array));
		unset($array);
		if ($this->url){
			$this->url = INSTALLDIR.strtolower($_GET['controller']).'/'.$_GET['action'].'/'.$this->url;
		}else{
			$this->url = INSTALLDIR.strtolower($_GET['controller']).'/'.$_GET['action'];
		}
		$this->countpage = ceil($this->total / $this->maxpage) ;	//总页数
		if ($this->countpage == 0){
			$this->countpage = 1;
		}
		if ($this->page > $this->countpage){
			$this->page = $this->countpage;
		}
		$body = '<div class="BlockDof"><div class="pagings"><div class="paging"><a class="total">'.$this->total.'</a><a class="pages">'.$this->page.'/'.$this->countpage.'</a>';
		if ($this->page>1){
			$body.= '<a class="pages" href="'.$this->url.'/page/1/">首页</a><a class="pages" href="'.$this->url.'/page/'.($this->page-1).'/">上一页</a>';
		}
		if ($this->countpage < $this->nextpage){
			$this->nextpage = $this->countpage;
		}
		if ($this->page >= $this->nextpage){
			$min = floor($this->nextpage /2);
			$end = $this->page+$min;
			$start = $this->page - floor($this->nextpage /2);
		}else{
			$start = 1;
			$end = $this->nextpage;
		}
		if ($end > $this->countpage){
			$end = $this->countpage;
		}
		while($start <= $end){
			if ($this->page == $start){
				$body.= '<a class="pagenow"  href="'.$this->url.'/page/'.$start.'/">'.$start.'</a>';
			}else{
				$body.= '<a class="page" href="'.$this->url.'/page/'.$start.'/">'.$start.'</a>';
			}
			$start++;
		}
		if ($this->page < $this->countpage){
			$body.= '<a class="pages" href="'.$this->url.'/page/'.($this->page+1).'/">下一页</a>';
			$body.= '<a class="pages" href="'.$this->url.'/page/'.($this->countpage).'/">尾页</a>';
		}
		$body.= '<a class="input"><input type="text" name="paging_num" value="'.$this->page.'" size="3" style="text-align:center;"> <input type="button" class="btn" value="GO" onclick="javascript:location.href=\''.$this->url.'/page/\'+this.parentNode.firstChild.value+\'/\';"></a></div></div>';
		return $body;
	}
	public function __set($key, $value){
		$this->$key = $value;
	}
	public function limit(){
		$this->pagesize = ($this->page-1) * $this->maxpage;
		return ' LIMIT '.$this->pagesize.','.$this->maxpage;
	}
	/*
	前端分页
	*/
	public function PrintBody(){
		$array = array_merge($_POST,$_GET);
		$this->maxpage = $this->config['pagesize'];
		unset($array['submit'], $array['page'], $array['controller'], $array['action'], $array['make']);
		$array['keyword'] = urlencode(urldecode($array['keyword']));
		$this->url = str_replace(array('&','='), '/', http_build_query($array));
		if ($this->config['rewrite'] != 'html' || 
		in_array($_GET['action'], array('category', 'view')) === false){
			$this->url = INSTALLDIR.strtolower($_GET['controller']).'/'.$_GET['action'].'/'.$this->url;
		}else{
			$this->url = $this->config['basedir'].'content/category/'.$this->url;
		}
		$this->countpage = ceil($this->total / $this->maxpage) ;	//总页数
		if ($this->countpage == 0){
			$this->countpage = 1;
		}
		if ($this->page > $this->countpage){
			$this->page = $this->countpage;
		}
		$indexpage = $this->url.'/page/1/';
		if ($this->page>1){
			$firstpage = '<li class="home"><a href="'.$indexpage.'">首&nbsp;页</a></li>';
			$pageup= '<li class="previous"><a href="'.$this->url.'/page/'.($this->page-1).'/">上一页</a></li>';
		}
		if ($this->page<$this->countpage){
			$pagedown = '<li class="next"><a href="'.$this->url.'/page/'.($this->page+1).'/">下一页</a></li>';
		}
		if ($this->page <= ($this->countpage-10)){
			$pagedown.= '<li class="next10"><a href="'.$this->url.'/page/'.($this->page+10).'/" title="下十页">下十页</a></li>';
		}
		if ($this->page != $this->countpage){
			$pageover = '<li class="lastly"><a href="'.$this->url.'/page/'.($this->countpage).'/">尾&nbsp;页</a></li>';
		}
		$body = '<div class="page"><ul><li class="home"><a href="#">'.$this->countpage.'/'.$this->total.'</a></li>'.$firstpage.$pageup;
		$cutpage = 7;
		if ($this->countpage<10)
		{
			$endpage = $this->countpage;
		}
		if ($this->page >= 10){
			$endpage = $this->page+5;
		}else{
			$endpage = $cutpage;
		}
		if ($endpage > $this->countpage)
		{
			$endpage = $this->countpage;
		}
		$start = $this->page-5;
		if ($start<1) $start = 1;
		for($i = $start; $i<=$endpage; $i++){
			if ($this->page == $i){
				$body.= '<li class="active">'.$i.'</li>';
			}else{
				$body.= '<li><a href="'.$this->url.'/page/'.$i.'/">'.$i.'</a></li>';
			}
		}
		$body.= $pagedown.$pageover.'</ul></div>';
		return $body;
	}
}?>