123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- /*
- * This file is part of the symfony package.
- * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- * @package symfony
- * @subpackage addon
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version SVN: $Id: sfPager.class.php 16260 2009-03-12 11:48:02Z fabien $
- */
- /**
- *
- * sfPager class.
- *
- * @package symfony
- * @subpackage addon
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version SVN: $Id: sfPager.class.php 16260 2009-03-12 11:48:02Z fabien $
- */
- abstract class sfPager
- {
- protected
- $page = 1,
- $maxPerPage = 0,
- $lastPage = 1,
- $nbResults = 0,
- $class = '',
- $tableName = '',
- $objects = null,
- $cursor = 1,
- $parameters = array(),
- $currentMaxLink = 1,
- $parameterHolder = null,
- $maxRecordLimit = false;
- public function __construct($class, $maxPerPage = 10)
- {
- $this->setClass($class);
- $this->setMaxPerPage($maxPerPage);
- $this->parameterHolder = new sfParameterHolder();
- }
- // function to be called after parameters have been set
- abstract public function init();
- // main method: returns an array of result on the given page
- abstract public function getResults();
- // used internally by getCurrent()
- abstract protected function retrieveObject($offset);
- public function getCurrentMaxLink()
- {
- return $this->currentMaxLink;
- }
- public function getMaxRecordLimit()
- {
- return $this->maxRecordLimit;
- }
- public function setMaxRecordLimit($limit)
- {
- $this->maxRecordLimit = $limit;
- }
- public function getLinks($nb_links = 5)
- {
- $links = array();
- $tmp = $this->page - floor($nb_links / 2);
- $check = $this->lastPage - $nb_links + 1;
- $limit = ($check > 0) ? $check : 1;
- $begin = ($tmp > 0) ? (($tmp > $limit) ? $limit : $tmp) : 1;
- $i = $begin;
- while (($i < $begin + $nb_links) && ($i <= $this->lastPage))
- {
- $links[] = $i++;
- }
- $this->currentMaxLink = count($links) ? $links[count($links) - 1] : 1;
- return $links;
- }
- public function haveToPaginate()
- {
- return (($this->getMaxPerPage() != 0) && ($this->getNbResults() > $this->getMaxPerPage()));
- }
- public function getCursor()
- {
- return $this->cursor;
- }
- public function setCursor($pos)
- {
- if ($pos < 1)
- {
- $this->cursor = 1;
- }
- else if ($pos > $this->nbResults)
- {
- $this->cursor = $this->nbResults;
- }
- else
- {
- $this->cursor = $pos;
- }
- }
- public function getObjectByCursor($pos)
- {
- $this->setCursor($pos);
- return $this->getCurrent();
- }
- public function getCurrent()
- {
- return $this->retrieveObject($this->cursor);
- }
- public function getNext()
- {
- if (($this->cursor + 1) > $this->nbResults)
- {
- return null;
- }
- else
- {
- return $this->retrieveObject($this->cursor + 1);
- }
- }
- public function getPrevious()
- {
- if (($this->cursor - 1) < 1)
- {
- return null;
- }
- else
- {
- return $this->retrieveObject($this->cursor - 1);
- }
- }
- public function getFirstIndice()
- {
- if ($this->page == 0)
- {
- return 1;
- }
- else
- {
- return ($this->page - 1) * $this->maxPerPage + 1;
- }
- }
- public function getLastIndice()
- {
- if ($this->page == 0)
- {
- return $this->nbResults;
- }
- else
- {
- if (($this->page * $this->maxPerPage) >= $this->nbResults)
- {
- return $this->nbResults;
- }
- else
- {
- return ($this->page * $this->maxPerPage);
- }
- }
- }
- public function getClass()
- {
- return $this->class;
- }
- public function setClass($class)
- {
- $this->class = $class;
- }
- public function getNbResults()
- {
- return $this->nbResults;
- }
- protected function setNbResults($nb)
- {
- $this->nbResults = $nb;
- }
- public function getFirstPage()
- {
- return 1;
- }
- public function getLastPage()
- {
- return $this->lastPage;
- }
- protected function setLastPage($page)
- {
- $this->lastPage = $page;
- if ($this->getPage() > $page)
- {
- $this->setPage($page);
- }
- }
- public function getPage()
- {
- return $this->page;
- }
- public function getNextPage()
- {
- return min($this->getPage() + 1, $this->getLastPage());
- }
- public function getPreviousPage()
- {
- return max($this->getPage() - 1, $this->getFirstPage());
- }
- public function setPage($page)
- {
- $this->page = intval($page);
- if ($this->page <= 0)
- {
- //set first page, which depends on a maximum set
- $this->page = $this->getMaxPerPage() ? 1 : 0;
- }
- }
- public function getMaxPerPage()
- {
- return $this->maxPerPage;
- }
- public function setMaxPerPage($max)
- {
- if ($max > 0)
- {
- $this->maxPerPage = $max;
- if ($this->page == 0)
- {
- $this->page = 1;
- }
- }
- else if ($max == 0)
- {
- $this->maxPerPage = 0;
- $this->page = 0;
- }
- else
- {
- $this->maxPerPage = 1;
- if ($this->page == 0)
- {
- $this->page = 1;
- }
- }
- }
- public function getParameterHolder()
- {
- return $this->parameterHolder;
- }
- public function getParameter($name, $default = null)
- {
- return $this->parameterHolder->get($name, $default);
- }
- public function hasParameter($name)
- {
- return $this->parameterHolder->has($name);
- }
- public function setParameter($name, $value)
- {
- return $this->parameterHolder->set($name, $value);
- }
- }
|