westdc-core/Westdc/Helpers/Paginator.php

237 lines
6.0 KiB
PHP
Raw Normal View History

<?php
/**
* Created by PhpStorm.
* User: liujin834
* Date: 14/12/25
* Time: 下午5:29
*/
namespace Westdc\Helpers;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
2014-12-26 05:28:50 +00:00
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Paginator\Adapter\ArrayAdapter;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Adapter\DbTableGateway;
use Zend\Paginator\Paginator as Zend_Paginator;
use Zend\Db\Sql\Select;
use Zend\Db\TableGateway;
class Paginator implements ServiceManagerAwareInterface{
protected $serviceManager;
public $sqlQuery,$sqlOrder,$sqlGroup,$sqlHaving;
2015-02-17 11:25:25 +00:00
private $pageLimit,$pageRange,$route,$params,$viewPartial,$currentPage;
2014-12-26 05:28:50 +00:00
public function setServiceManager(ServiceManager $serviceManager)
{
$this->serviceManager = $serviceManager;
return $this;
}
2015-02-17 11:25:25 +00:00
/**
* 每页显示数量
* @param int $limit
*/
2014-12-26 05:28:50 +00:00
public function setPageLimit($limit = 0)
{
$this->pageLimit = (int)$limit;
if(empty($this->pageLimit))
$this->pageLimit = 10;
}
2015-02-17 11:25:25 +00:00
/**
* 页码显示范围
* @param int $range
*/
2014-12-26 05:28:50 +00:00
public function setPageRange($range = 0)
{
$this->pageRange = (int)$range;
if(empty($this->pageRange))
$this->pageRange = 6;
}
2015-02-17 11:25:25 +00:00
/**
* 设置路由
* @param string $route
* @param string $params
*/
2014-12-26 05:28:50 +00:00
public function setRoute($route = "",$params = "")
{
if(empty($route)) {
$routeMatch = $this->serviceManager->get('Application')->getMvcEvent()->getRouteMatch();
$this->route = str_replace("/wildcard", "", $routeMatch->getMatchedRouteName());
$this->params = [];
}else{
$this->route = $route;
}
if(empty($params)) {
if(!isset($routeMatch))
$routeMatch = $this->serviceManager->get('Application')->getMvcEvent()->getRouteMatch();
if (!empty($routeMatch->getParam('__CONTROLLER__')))
$this->params['controller'] = $routeMatch->getParam('__CONTROLLER__');
if (!empty($routeMatch->getParam('action')))
$this->params['action'] = $routeMatch->getParam('action');
if (!empty($routeMatch->getParam('ac')))
$this->params['ac'] = $routeMatch->getParam('ac');
if (!empty($routeMatch->getParam['id']))
$this->params['id'] = $routeMatch->getParam('id');
if (!empty($routeMatch->getParam['uuid']))
$this->params['uuid'] = $routeMatch->getParam('uuid');
}else{
$this->params = $params;
}
unset($routeMatch);
}
/**
2015-02-17 11:25:25 +00:00
* 设置分页模板
* @param $viewPartial
2014-12-26 05:28:50 +00:00
*/
2015-02-17 11:25:25 +00:00
public function setTemplate($viewPartial = "pagination")
{
2015-02-17 11:25:25 +00:00
$this->viewPartial = $viewPartial;
}
2015-02-17 11:25:25 +00:00
/**
* 预处理分页使用到的参数
*/
private function prepareParams()
{
if(empty($this->pageLimit))
$this->setPageLimit();
if(empty($this->pageRange))
$this->setPageRange();
if(empty($this->route))
$this->setRoute();
2015-02-17 11:25:25 +00:00
if(empty($this->viewPartial))
$this->setTemplate();
}
2015-02-17 11:25:25 +00:00
/**
* 预处理分页数据
* @param $data
* @return ArrayAdapter|DbSelect|DbTableGateway
*/
private function prepareData($data)
{
if(is_array($data))
2015-02-17 11:25:25 +00:00
return new ArrayAdapter($data);
if($data instanceof Select)
{
$dbService = $this->serviceManager->get('Db');
$zendDb = $dbService->getZendDb();
2015-02-17 11:25:25 +00:00
return new DbSelect($data,$zendDb);
}
if($data instanceof TableGateway\TableGatewayInterface || $data instanceof TableGateway\AbstractTableGateway)
2015-02-17 11:25:25 +00:00
return new DbTableGateway($data,$this->sqlQuery,$this->sqlOrder,$this->sqlGroup,$this->sqlHaving);
}
2015-02-17 11:25:25 +00:00
/**
* 获得Zend\Paginator\Paginator对象
* @param $data
* @param $page
* @return Zend_Paginator
*/
private function getPaginator($data,$page)
{
$paginator = new Zend_Paginator($data);
$paginator->setCurrentPageNumber($page)
2015-02-17 11:25:25 +00:00
->setItemCountPerPage($this->pageLimit)
->setPageRange($this->pageRange);
return $paginator;
}
/**
* 输出html分页放置在paginator变量中
* @param AbstractActionController $ctl
* @param mixed $data
* @return bool
*/
public function add(AbstractActionController $ctl,$data)
{
$page = $ctl->params()->fromRoute('page');
$this->prepareParams();
$data = $this->prepareData($data);
$paginator = $this->getPaginator($data,$page);
$pagination = $ctl->getServiceLocator()->get('viewhelpermanager')->get('PaginationControl');
2014-12-26 05:28:50 +00:00
$pageSliding = $pagination(
$paginator,
'Sliding',
2015-02-17 11:25:25 +00:00
$this->viewPartial,
2014-12-26 05:28:50 +00:00
['route'=>$this->route,'params'=>$this->params]
);
$ctl->ViewModel->setVariable('pagination',$pageSliding);
$ctl->ViewModel->setVariable('paginator',$paginator);
2014-12-26 05:28:50 +00:00
return true;
2015-02-17 11:25:25 +00:00
}//add()
/**
* 设置ajax分页的当前页码
* @param $page
*/
public function setAjaxCurrentPage($page){
$this->currentPage = (int)$page;
}
2015-02-17 11:25:25 +00:00
/**
* 输出ajax分页
* @param $data
* @return array
*/
public function ajax($data){
$this->prepareParams();
$data = $this->prepareData($data);
if(empty($this->currentPage))
$page = 0;
else
$page = $this->currentPage;
$paginator = $this->getPaginator($data,$page);
$pagination = $this->serviceManager->get('viewhelpermanager')->get('PaginationControl');
$pageSliding = $pagination(
$paginator,
'Sliding',
$this->viewPartial,
[]
);
return [
'paginator' => $paginator->toJson(),
'pagination' => $pageSliding
];
}//ajax
}