123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693 |
- <?php
- /*
- * This file is part of the Sonata Project package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Datagrid;
- /**
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
- */
- abstract class Pager implements \Iterator, \Countable, \Serializable, PagerInterface
- {
- const TYPE_DEFAULT = 'default';
- const TYPE_SIMPLE = 'simple';
- /**
- * @var int
- */
- protected $page = 1;
- /**
- * @var int
- */
- protected $maxPerPage = 0;
- /**
- * @var int
- */
- protected $lastPage = 1;
- /**
- * @var int
- */
- protected $nbResults = 0;
- /**
- * @var int
- */
- protected $cursor = 1;
- /**
- * @var array
- */
- protected $parameters = array();
- /**
- * @var int
- */
- protected $currentMaxLink = 1;
- /**
- * @var mixed bool|int
- */
- protected $maxRecordLimit = false;
- /**
- * @var int
- */
- protected $maxPageLinks = 0;
- // used by iterator interface
- /**
- * @var \Traversable|array|null
- */
- protected $results = null;
- /**
- * @var int
- */
- protected $resultsCounter = 0;
- /**
- * @var ProxyQueryInterface|null
- */
- protected $query = null;
- /**
- * @var array
- */
- protected $countColumn = array('id');
- /**
- * @param int $maxPerPage Number of records to display per page
- */
- public function __construct($maxPerPage = 10)
- {
- $this->setMaxPerPage($maxPerPage);
- }
- /**
- * Returns the current pager's max link.
- *
- * @return int
- */
- public function getCurrentMaxLink()
- {
- return $this->currentMaxLink;
- }
- /**
- * Returns the current pager's max record limit.
- *
- * @return int
- */
- public function getMaxRecordLimit()
- {
- return $this->maxRecordLimit;
- }
- /**
- * Sets the current pager's max record limit.
- *
- * @param int $limit
- */
- public function setMaxRecordLimit($limit)
- {
- $this->maxRecordLimit = $limit;
- }
- /**
- * Returns an array of page numbers to use in pagination links.
- *
- * @param int $nbLinks The maximum number of page numbers to return
- *
- * @return array
- */
- public function getLinks($nbLinks = null)
- {
- if ($nbLinks == null) {
- $nbLinks = $this->getMaxPageLinks();
- }
- $links = array();
- $tmp = $this->page - floor($nbLinks / 2);
- $check = $this->lastPage - $nbLinks + 1;
- $limit = $check > 0 ? $check : 1;
- $begin = $tmp > 0 ? ($tmp > $limit ? $limit : $tmp) : 1;
- $i = (int) $begin;
- while ($i < $begin + $nbLinks && $i <= $this->lastPage) {
- $links[] = $i++;
- }
- $this->currentMaxLink = count($links) ? $links[count($links) - 1] : 1;
- return $links;
- }
- /**
- * Returns true if the current query requires pagination.
- *
- * @return bool
- */
- public function haveToPaginate()
- {
- return $this->getMaxPerPage() && $this->getNbResults() > $this->getMaxPerPage();
- }
- /**
- * Returns the current cursor.
- *
- * @return int
- */
- public function getCursor()
- {
- return $this->cursor;
- }
- /**
- * Sets the current cursor.
- *
- * @param int $pos
- */
- public function setCursor($pos)
- {
- if ($pos < 1) {
- $this->cursor = 1;
- } else {
- if ($pos > $this->nbResults) {
- $this->cursor = $this->nbResults;
- } else {
- $this->cursor = $pos;
- }
- }
- }
- /**
- * Returns an object by cursor position.
- *
- * @param int $pos
- *
- * @return mixed
- */
- public function getObjectByCursor($pos)
- {
- $this->setCursor($pos);
- return $this->getCurrent();
- }
- /**
- * Returns the current object.
- *
- * @return mixed
- */
- public function getCurrent()
- {
- return $this->retrieveObject($this->cursor);
- }
- /**
- * Returns the next object.
- *
- * @return mixed|null
- */
- public function getNext()
- {
- if ($this->cursor + 1 > $this->nbResults) {
- return;
- }
- return $this->retrieveObject($this->cursor + 1);
- }
- /**
- * Returns the previous object.
- *
- * @return mixed|null
- */
- public function getPrevious()
- {
- if ($this->cursor - 1 < 1) {
- return;
- }
- return $this->retrieveObject($this->cursor - 1);
- }
- /**
- * Returns the first index on the current page.
- *
- * @return int
- */
- public function getFirstIndex()
- {
- if ($this->page == 0) {
- return 1;
- }
- return ($this->page - 1) * $this->maxPerPage + 1;
- }
- /**
- * NEXT_MAJOR: remove this method.
- *
- * @deprecated since 3.11, will be removed in 4.0
- */
- public function getFirstIndice()
- {
- @trigger_error(
- 'Method '.__METHOD__.' is deprecated since version 3.11 and will be removed in 4.0, '.
- 'please use getFirstIndex() instead.',
- E_USER_DEPRECATED
- );
- return $this->getFirstIndex();
- }
- /**
- * Returns the last index on the current page.
- *
- * @return int
- */
- public function getLastIndex()
- {
- if ($this->page == 0) {
- return $this->nbResults;
- }
- if ($this->page * $this->maxPerPage >= $this->nbResults) {
- return $this->nbResults;
- }
- return $this->page * $this->maxPerPage;
- }
- /**
- * NEXT_MAJOR: remove this method.
- *
- * @deprecated since 3.11, will be removed in 4.0
- */
- public function getLastIndice()
- {
- @trigger_error(
- 'Method '.__METHOD__.' is deprecated since version 3.11 and will be removed in 4.0, '.
- 'please use getLastIndex() instead.',
- E_USER_DEPRECATED
- );
- return $this->getLastIndex();
- }
- /**
- * Returns the number of results.
- *
- * @return int
- */
- public function getNbResults()
- {
- return $this->nbResults;
- }
- /**
- * Returns the first page number.
- *
- * @return int
- */
- public function getFirstPage()
- {
- return 1;
- }
- /**
- * Returns the last page number.
- *
- * @return int
- */
- public function getLastPage()
- {
- return $this->lastPage;
- }
- /**
- * Returns the current page.
- *
- * @return int
- */
- public function getPage()
- {
- return $this->page;
- }
- /**
- * Returns the next page.
- *
- * @return int
- */
- public function getNextPage()
- {
- return min($this->getPage() + 1, $this->getLastPage());
- }
- /**
- * Returns the previous page.
- *
- * @return int
- */
- public function getPreviousPage()
- {
- return max($this->getPage() - 1, $this->getFirstPage());
- }
- /**
- * {@inheritdoc}
- */
- 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;
- }
- }
- /**
- * {@inheritdoc}
- */
- public function getMaxPerPage()
- {
- return $this->maxPerPage;
- }
- /**
- * {@inheritdoc}
- */
- 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;
- }
- }
- }
- }
- /**
- * {@inheritdoc}
- */
- public function getMaxPageLinks()
- {
- return $this->maxPageLinks;
- }
- /**
- * {@inheritdoc}
- */
- public function setMaxPageLinks($maxPageLinks)
- {
- $this->maxPageLinks = $maxPageLinks;
- }
- /**
- * Returns true if on the first page.
- *
- * @return bool
- */
- public function isFirstPage()
- {
- return 1 == $this->page;
- }
- /**
- * Returns true if on the last page.
- *
- * @return bool
- */
- public function isLastPage()
- {
- return $this->page == $this->lastPage;
- }
- /**
- * Returns the current pager's parameter holder.
- *
- * @return array
- */
- public function getParameters()
- {
- return $this->parameters;
- }
- /**
- * Returns a parameter.
- *
- * @param string $name
- * @param mixed $default
- *
- * @return mixed
- */
- public function getParameter($name, $default = null)
- {
- return isset($this->parameters[$name]) ? $this->parameters[$name] : $default;
- }
- /**
- * Checks whether a parameter has been set.
- *
- * @param string $name
- *
- * @return bool
- */
- public function hasParameter($name)
- {
- return isset($this->parameters[$name]);
- }
- /**
- * Sets a parameter.
- *
- * @param string $name
- * @param mixed $value
- */
- public function setParameter($name, $value)
- {
- $this->parameters[$name] = $value;
- }
- /**
- * {@inheritdoc}
- */
- public function current()
- {
- if (!$this->isIteratorInitialized()) {
- $this->initializeIterator();
- }
- return current($this->results);
- }
- /**
- * {@inheritdoc}
- */
- public function key()
- {
- if (!$this->isIteratorInitialized()) {
- $this->initializeIterator();
- }
- return key($this->results);
- }
- /**
- * {@inheritdoc}
- */
- public function next()
- {
- if (!$this->isIteratorInitialized()) {
- $this->initializeIterator();
- }
- --$this->resultsCounter;
- return next($this->results);
- }
- /**
- * {@inheritdoc}
- */
- public function rewind()
- {
- if (!$this->isIteratorInitialized()) {
- $this->initializeIterator();
- }
- $this->resultsCounter = count($this->results);
- return reset($this->results);
- }
- /**
- * {@inheritdoc}
- */
- public function valid()
- {
- if (!$this->isIteratorInitialized()) {
- $this->initializeIterator();
- }
- return $this->resultsCounter > 0;
- }
- /**
- * {@inheritdoc}
- */
- public function count()
- {
- return $this->getNbResults();
- }
- /**
- * {@inheritdoc}
- */
- public function serialize()
- {
- $vars = get_object_vars($this);
- unset($vars['query']);
- return serialize($vars);
- }
- /**
- * {@inheritdoc}
- */
- public function unserialize($serialized)
- {
- $array = unserialize($serialized);
- foreach ($array as $name => $values) {
- $this->$name = $values;
- }
- }
- /**
- * @return array
- */
- public function getCountColumn()
- {
- return $this->countColumn;
- }
- /**
- * @param array $countColumn
- *
- * @return array
- */
- public function setCountColumn(array $countColumn)
- {
- return $this->countColumn = $countColumn;
- }
- /**
- * {@inheritdoc}
- */
- public function setQuery($query)
- {
- $this->query = $query;
- }
- /**
- * @return ProxyQueryInterface
- */
- public function getQuery()
- {
- return $this->query;
- }
- /**
- * Sets the number of results.
- *
- * @param int $nb
- */
- protected function setNbResults($nb)
- {
- $this->nbResults = $nb;
- }
- /**
- * Sets the last page number.
- *
- * @param int $page
- */
- protected function setLastPage($page)
- {
- $this->lastPage = $page;
- if ($this->getPage() > $page) {
- $this->setPage($page);
- }
- }
- /**
- * Returns true if the properties used for iteration have been initialized.
- *
- * @return bool
- */
- protected function isIteratorInitialized()
- {
- return null !== $this->results;
- }
- /**
- * Loads data into properties used for iteration.
- */
- protected function initializeIterator()
- {
- $this->results = $this->getResults();
- $this->resultsCounter = count($this->results);
- }
- /**
- * Empties properties used for iteration.
- */
- protected function resetIterator()
- {
- $this->results = null;
- $this->resultsCounter = 0;
- }
- /**
- * Retrieve the object for a certain offset.
- *
- * @param int $offset
- *
- * @return object
- */
- protected function retrieveObject($offset)
- {
- $queryForRetrieve = clone $this->getQuery();
- $queryForRetrieve
- ->setFirstResult($offset - 1)
- ->setMaxResults(1);
- $results = $queryForRetrieve->execute();
- return $results[0];
- }
- }
|