westdc-core/Westdc/Helpers/Paginator.php

62 lines
1.8 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;
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;
public function setServiceManager(ServiceManager $serviceManager)
{
$this->serviceManager = $serviceManager;
return $this;
}
public function add($data,$ctl,$limit = 10,$route = "",$viewPartial = "layout/manager/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)
->setItemCountPerPage($limit)
->setPageRange(6);
$pagination = $ctl->getServiceLocator()->get('viewhelpermanager')->get('PaginationControl');
$pageSliding = $pagination($paginator,'Sliding',$viewPartial,array('route'=>$route));
$ctl->ViewModel->setVariable('pagination',$pageSliding);
$ctl->ViewModel->setVariable('paginator',$paginator);
}
}