pager.php 1.36 KB
<?php
class Flora_Collection_Pager
{
	protected $collection;
	protected $pageName = "page";
	protected $perPage = 20;

	public function __construct(Flora_Collection $collection, $perPage = 20)
	{
		$this->collection = $collection;
		$this->setPerPage($perPage);
	}

	public function getCollection()
	{
		return $this->collection;
	}

	public function setPageName($pageID)
	{
		$this->pageName = $pageID;
	}

	public function getPageName()
	{
		return $this->pageName;
	}

	public function getCurrentPage()
	{
		return isset($_GET[$this->pageName]) && (int) $_GET[$this->pageName] > 1 ? $_GET[$this->pageName] : 1;
	}

	public function setCurrentPage($page = 1)
	{
		$_GET[$this->pageName] = (int) $page >= 1 ? (int) $page : 1;
	}

	public function getTotalPages()
	{
		return ceil($this->collection->getCount() / $this->getPerPage());
	}

	public function setPerPage($perPage = 20)
	{
		$this->perPage = (int) $perPage > 0 ? (int) $perPage : 20;
	}

	public function getPerPage()
	{
		return $this->perPage;
	}

	public function getPageData($page = 1)
	{
		$page = (int) $page >= 1 ? (int) $page : 1;
		return array_slice($this->getCollection()->getItems(), $this->perPage * ($page - 1), $this->perPage);
	}

	public function getCurrentPageData()
	{
		return $this->getPageData($this->getCurrentPage());
	}

	public function getTotalItemsCount()
	{
		return $this->collection->getCount();
	}
}