138 lines
4.0 KiB
PHP
138 lines
4.0 KiB
PHP
<?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;
|
|
|
|
public function setServiceManager(ServiceManager $serviceManager)
|
|
{
|
|
$this->serviceManager = $serviceManager;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function setPageLimit($limit = 0)
|
|
{
|
|
$this->pageLimit = (int)$limit;
|
|
if(empty($this->pageLimit))
|
|
$this->pageLimit = 10;
|
|
}
|
|
|
|
public function setPageRange($range = 0)
|
|
{
|
|
$this->pageRange = (int)$range;
|
|
if(empty($this->pageRange))
|
|
$this->pageRange = 6;
|
|
}
|
|
|
|
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 AbstractActionController $ctl
|
|
* @param mixed $data
|
|
* @param string $viewPartial
|
|
* @return bool
|
|
*/
|
|
public function add(AbstractActionController $ctl,$data,$viewPartial = "pagination")
|
|
{
|
|
$page = $ctl->params()->fromRoute('page');
|
|
|
|
if(empty($this->pageLimit))
|
|
$this->setPageLimit();
|
|
|
|
if(empty($this->pageRange))
|
|
$this->setPageRange();
|
|
|
|
if(empty($this->route))
|
|
$this->setRoute();
|
|
|
|
|
|
if(is_array($data))
|
|
$data = new ArrayAdapter($data);
|
|
|
|
if($data instanceof Select)
|
|
{
|
|
$dbService = $this->serviceManager->get('Db');
|
|
$zendDb = $dbService->getZendDb();
|
|
$data = new DbSelect($data,$zendDb);
|
|
}
|
|
|
|
if($data instanceof TableGateway\TableGatewayInterface || $data instanceof TableGateway\AbstractTableGateway)
|
|
$data = new DbTableGateway($data,$this->sqlQuery,$this->sqlOrder,$this->sqlGroup,$this->sqlHaving);
|
|
|
|
$paginator = new Zend_Paginator($data);
|
|
$paginator->setCurrentPageNumber($page)
|
|
->setItemCountPerPage($this->pageLimit)
|
|
->setPageRange($this->pageRange);
|
|
|
|
$pagination = $ctl->getServiceLocator()->get('viewhelpermanager')->get('PaginationControl');
|
|
|
|
$pageSliding = $pagination(
|
|
$paginator,
|
|
'Sliding',
|
|
$viewPartial,
|
|
['route'=>$this->route,'params'=>$this->params]
|
|
);
|
|
|
|
$ctl->ViewModel->setVariable('pagination',$pageSliding);
|
|
$ctl->ViewModel->setVariable('paginator',$paginator);
|
|
return true;
|
|
}
|
|
|
|
}
|