Pagination.class.php 6.77 KB
<?php
/**
 * 分页处理类
 * 
 * @version 0.1 
 * @author xiaoma
 * @name Lib_Helper_Pagination
 *
 */
class Lib_Helper_Pagination
{
    /**
     * 需要跨域的列表
     *  array ('域名前缀' => '命名空间')
     *
     * @var unknown_type
     */
    public static $domain_list = array(
        'my' => 'my',
    );
	/**
	 * 上下文对象
	 *
	 * @var Framework_YHttpRequest
	 */
	private $_request ;
	/**
	 * 记录总数
	 *
	 * @var int
	 */
	private $_record_count = 0;
	/**
	 * 每页条数
	 *
	 * @var int
	 */
	private $_page_size = null  ;
	/**
	 * 当前页码
	 *
	 * @var int
	 */
	private $_current_page = null  ;
	/**
	 * 条件参数
	 *
	 * @var array
	 */
	private $_parames = array() ;
	/**
	 * 配置项
	 *
	 * @var unknown_type
	 */
	private $_options = array(
		'udi'	=>	null,		//路径UDI
		'model'	=>	'default',	//分页模式
		'htmlTarget'	=>	'', //如是异步请求,需要设置请求后返回的数据往哪个节点里填充
	    'pageTarget'	=>  '', //如果设置了分页容器,则将页码填充至此容器,默认填充至上面配置的htmlTarget
	    'loadPageCount'	=> 3 ,  //如是autoload模式,则此选项标志翻多少页后显示页码
	);
	 
	/**
	 * 初始化
	 *
	 * @param int $count
	 * @param int $size
	 */
	public function __construct($count = 0, $pageSize=10)
	{
		$this->_record_count = intval($count) ;
		$this->setPageSize($pageSize) ;;
		$this->_request = Framework_YHttpRequest::instance();
		$this->setUdi( $this->_request->requestUDI(false) );
	}
	
	/**
	 * 设置每页记录条数
	 *
	 * @param int $pageSize
	 * @return Lib_Helper_Pagination
	 */
	public function setPageSize($pageSize)
	{
		$this->_page_size = abs(intval($pageSize)) ;
		return $this ;
	}
	
    /**
     * 设置每页记录数
     *
     * @return Lib_Helper_Pagination
     */
	public function setRecordCount($count)
	{
	   $this->_record_count = intval($count);
	   return $this;
	}
	
	/**
	 * 改变请求的udi,默认为当前访问的udi
	 *
	 * @param string $udi
	 * @return Lib_Helper_Pagination 
	 */
	public function setUdi ($udi)
	{
	    //检查是否跨域
        $domain = $this->_request->currentDomain();
        $parse = parse_url($domain) ;
        if (isset($parse['host']))
        {
            $split = explode('.',$parse['host']);
            if (isset($split[0]) && in_array($split[0],self::$domain_list))
            {
                $url_args['forward'] = urlencode(urlencode( $udi )) ;
                $this->setParames($url_args);
                $udi = self::$domain_list[$split[0]].'::ajax/index' ;
            }
        }
		$this->_options['udi'] = $udi ;
		
		return $this ;
	}
	
	/**
	 * 设置分页模式
	 *
	 * @param string $model 分页模式
	 * 参数如下:
	 * 		default	:	默认常规分页, 
	 * 		autoload:	滚动自加载, 
	 * 		ajax	:	异步加载
	 * @param string $htmlTarget 如是异步请求,需要设置请求后返回的数据往哪个节点里填充,
	 * 		如将返回的结果插入至id为content的DIV,则将htmlTarget设置为#content
	 * @param int $loadPageCount 当使用autoload模式,可以设置翻了多少页后进行显示页码
	 * @return Lib_Helper_Pagination
	 */
	public function setModel($model = 'default',$htmlTarget = '', $loadPageCount=3, $pageTarget = '')
	{
		$this->_options['model'] = $model ;
		$this->_options['htmlTarget'] = $htmlTarget ;
		$this->_options['loadPageCount'] = $loadPageCount ;
		$this->_options['pageTarget'] = $pageTarget ;
		return $this ;
	}
	/**
	 * 设置配置项
	 *
	 * @param array $opts 配置项
	 * @return Lib_Helper_Pagination 
	 */
	public function setOptions(array $opts)
	{
		$this->_options = array_merge($this->_options,$opts);
		return $this ;
	}
	/**
	 * 获取配置项
	 *
	 * @return Array
	 */
	public function getOptions()
	{
		return $this->_options ;
	}
	
	/**
	 * 设置当前页码
	 *
	 * @param int $current_page 当前页码
	 * @return Lib_Helper_Pagination
	 */
	public function setCurrentPage($current_page)
	{
		$this->_current_page = $current_page ;
		return $this ;
	}
	/**
	 * 获取当前页码
	 * 
	 * @return Lib_Helper_Pagination
	 */
	public function getCurrentPage()
	{
		if (!$this->_current_page)
		{
			$this->_current_page = ($this->_request->page)?$this->_request->page:1 ;
		} 
		//@FIXED xiaoma 2013.1.28 修复如果页码大于实际页码时,显示为空情况 
		if ($this->_current_page > $this->getPageCount())
		{
		    $this->_current_page = $this->getPageCount();
		}
		return intval($this->_current_page) ;
	}
	/**
	 * 设置请求参数
	 *
	 * @param array $parames
	 */
	public function setParames( array $parames )
	{
		$this->_parames = array_merge($this->_parames, $parames);
		return $this ; 
	}
	/**
	 * 获取分页基本信息,供其它地方使用
	 * 
	 * 返回内容为数组,包括以下内容:
	 * options: 配置项,详细配置见:$this->_options
     * record_count: 总记录数
     * page_count: 按照页大小计算出来的总页数
     * first: 第一页的索引 
     * last: 最后一页的索引
     * current: 当前页的索引
     * next: 下一页的索引
     * prev: 上一页的索引
     * page_size: 页大小
	 * @return array
	 */
	public function getPagination()
	{
		$pagination = array('options'=> $this->_options,'parames'=>$this->_parames);
		$pagination['first'] = 1 ;
		$pagination['record_count'] = $this->_record_count;
        $pagination['page_count'] = $this->getPageCount();
		$pagination['last'] = $pagination['page_count'] ;
		$current_page = $this->getCurrentPage() ;
		$pagination['current'] = $current_page ;
        $pagination['next'] = ($current_page < $pagination['last'] - 1) ? $current_page + 1 : $pagination['last'];
        $pagination['prev'] = ($current_page > 1)?$current_page - 1 : 1 ;
        $pagination['page_size'] = $this->_page_size ;
        return $pagination ;
	}
	/**
	 * 获取当前偏移量
	 *
	 * @return int 
	 */
	public function getOffset()
	{
	    if ($this->getCurrentPage())
	    {
	        return ($this->_current_page - 1) * $this->_page_size ;
	    }
		return 0 ;
	}
	/**
	 * 获取每页记录数
	 *
	 * @return int
	 */
	public function getPageSize()
	{
		return $this->_page_size ;
	}
	/**
	 * 获取记录条数
	 *
	 * @return int
	 */
	public function getRecordCount()
	{
		return $this->_record_count ;
	}
	
	/**
	 * 获取总页码
	 *
	 * @return int 
	 */
	public function getPageCount()
	{
		return ceil($this->_record_count / $this->_page_size);
	}
	
    /**
     * 根据请求参数计算数据库查询的LIMIT值
     * 
     * @return array(page 第几页, limit 限制数)
     */
    public function getLimit()
    {
        $page = intval($this->_request->query('page', 1));
        $page = ($page > 1) ? $page : 1;
        $limit = intval($this->_request->query('limit', $this->_page_size));
        $offset = ($page - 1) * $limit;
        return array($offset, $limit);
    }
}