westdc-core/Westdc/Helpers/Paginator.php

132 lines
3.9 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\TableGateway;
class Paginator implements ServiceManagerAwareInterface{
protected $serviceManager;
public $sqlQuery,$sqlOrder,$sqlGroup,$sqlHaving;
2014-12-26 05:28:50 +00:00
private $pageLimit,$pageRange,$route,$params;
public function setServiceManager(ServiceManager $serviceManager)
{
$this->serviceManager = $serviceManager;
2014-12-26 05:28:50 +00:00
$this->setPageLimit();
$this->setPageRange();
$this->setRoute();
return $this;
}
2014-12-26 05:28:50 +00:00
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(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)
$data = new DbTableGateway($data,$this->sqlQuery,$this->sqlOrder,$this->sqlGroup,$this->sqlHaving);
$paginator = new Zend_Paginator($data);
$paginator->setCurrentPageNumber($page)
2014-12-26 05:28:50 +00:00
->setItemCountPerPage($this->pageLimit)
->setPageRange($this->pageRange);
$pagination = $ctl->getServiceLocator()->get('viewhelpermanager')->get('PaginationControl');
2014-12-26 05:28:50 +00:00
$pageSliding = $pagination(
$paginator,
'Sliding',
$viewPartial,
['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;
}
}