westdc-core/Westdc/Helpers/Paginator.php

237 lines
6.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Created by PhpStorm.
* User: liujin834
* Date: 14/12/25
* Time: 下午5:29
*/
namespace Westdc\Helpers;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
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;
private $pageLimit,$pageRange,$route,$params,$viewPartial,$currentPage;
public function setServiceManager(ServiceManager $serviceManager)
{
$this->serviceManager = $serviceManager;
return $this;
}
/**
* 每页显示数量
* @param int $limit
*/
public function setPageLimit($limit = 0)
{
$this->pageLimit = (int)$limit;
if(empty($this->pageLimit))
$this->pageLimit = 10;
}
/**
* 页码显示范围
* @param int $range
*/
public function setPageRange($range = 0)
{
$this->pageRange = (int)$range;
if(empty($this->pageRange))
$this->pageRange = 6;
}
/**
* 设置路由
* @param string $route
* @param string $params
*/
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);
}
/**
* 设置分页模板
* @param $viewPartial
*/
public function setTemplate($viewPartial = "pagination")
{
$this->viewPartial = $viewPartial;
}
/**
* 预处理分页使用到的参数
*/
private function prepareParams()
{
if(empty($this->pageLimit))
$this->setPageLimit();
if(empty($this->pageRange))
$this->setPageRange();
if(empty($this->route))
$this->setRoute();
if(empty($this->viewPartial))
$this->setTemplate();
}
/**
* 预处理分页数据
* @param $data
* @return ArrayAdapter|DbSelect|DbTableGateway
*/
private function prepareData($data)
{
if(is_array($data))
return new ArrayAdapter($data);
if($data instanceof Select)
{
$dbService = $this->serviceManager->get('Db');
$zendDb = $dbService->getZendDb();
return new DbSelect($data,$zendDb);
}
if($data instanceof TableGateway\TableGatewayInterface || $data instanceof TableGateway\AbstractTableGateway)
return new DbTableGateway($data,$this->sqlQuery,$this->sqlOrder,$this->sqlGroup,$this->sqlHaving);
}
/**
* 获得Zend\Paginator\Paginator对象
* @param $data
* @param $page
* @return Zend_Paginator
*/
private function getPaginator($data,$page)
{
$paginator = new Zend_Paginator($data);
$paginator->setCurrentPageNumber($page)
->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');
$pageSliding = $pagination(
$paginator,
'Sliding',
$this->viewPartial,
['route'=>$this->route,'params'=>$this->params]
);
$ctl->ViewModel->setVariable('pagination',$pageSliding);
$ctl->ViewModel->setVariable('paginator',$paginator);
return true;
}//add()
/**
* 设置ajax分页的当前页码
* @param $page
*/
public function setAjaxCurrentPage($page){
$this->currentPage = (int)$page;
}
/**
* 输出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
}