Pager.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Datagrid;
  11. /**
  12. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  13. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  14. */
  15. abstract class Pager implements \Iterator, \Countable, \Serializable, PagerInterface
  16. {
  17. const TYPE_DEFAULT = 'default';
  18. const TYPE_SIMPLE = 'simple';
  19. /**
  20. * @var int
  21. */
  22. protected $page = 1;
  23. /**
  24. * @var int
  25. */
  26. protected $maxPerPage = 0;
  27. /**
  28. * @var int
  29. */
  30. protected $lastPage = 1;
  31. /**
  32. * @var int
  33. */
  34. protected $nbResults = 0;
  35. /**
  36. * @var int
  37. */
  38. protected $cursor = 1;
  39. /**
  40. * @var array
  41. */
  42. protected $parameters = array();
  43. /**
  44. * @var int
  45. */
  46. protected $currentMaxLink = 1;
  47. /**
  48. * @var mixed bool|int
  49. */
  50. protected $maxRecordLimit = false;
  51. /**
  52. * @var int
  53. */
  54. protected $maxPageLinks = 0;
  55. // used by iterator interface
  56. /**
  57. * @var \Traversable|array|null
  58. */
  59. protected $results = null;
  60. /**
  61. * @var int
  62. */
  63. protected $resultsCounter = 0;
  64. /**
  65. * @var ProxyQueryInterface|null
  66. */
  67. protected $query = null;
  68. /**
  69. * @var array
  70. */
  71. protected $countColumn = array('id');
  72. /**
  73. * Constructor.
  74. *
  75. * @param int $maxPerPage Number of records to display per page
  76. */
  77. public function __construct($maxPerPage = 10)
  78. {
  79. $this->setMaxPerPage($maxPerPage);
  80. }
  81. /**
  82. * Returns the current pager's max link.
  83. *
  84. * @return int
  85. */
  86. public function getCurrentMaxLink()
  87. {
  88. return $this->currentMaxLink;
  89. }
  90. /**
  91. * Returns the current pager's max record limit.
  92. *
  93. * @return int
  94. */
  95. public function getMaxRecordLimit()
  96. {
  97. return $this->maxRecordLimit;
  98. }
  99. /**
  100. * Sets the current pager's max record limit.
  101. *
  102. * @param int $limit
  103. */
  104. public function setMaxRecordLimit($limit)
  105. {
  106. $this->maxRecordLimit = $limit;
  107. }
  108. /**
  109. * Returns an array of page numbers to use in pagination links.
  110. *
  111. * @param int $nbLinks The maximum number of page numbers to return
  112. *
  113. * @return array
  114. */
  115. public function getLinks($nbLinks = null)
  116. {
  117. if ($nbLinks == null) {
  118. $nbLinks = $this->getMaxPageLinks();
  119. }
  120. $links = array();
  121. $tmp = $this->page - floor($nbLinks / 2);
  122. $check = $this->lastPage - $nbLinks + 1;
  123. $limit = $check > 0 ? $check : 1;
  124. $begin = $tmp > 0 ? ($tmp > $limit ? $limit : $tmp) : 1;
  125. $i = (int) $begin;
  126. while ($i < $begin + $nbLinks && $i <= $this->lastPage) {
  127. $links[] = $i++;
  128. }
  129. $this->currentMaxLink = count($links) ? $links[count($links) - 1] : 1;
  130. return $links;
  131. }
  132. /**
  133. * Returns true if the current query requires pagination.
  134. *
  135. * @return bool
  136. */
  137. public function haveToPaginate()
  138. {
  139. return $this->getMaxPerPage() && $this->getNbResults() > $this->getMaxPerPage();
  140. }
  141. /**
  142. * Returns the current cursor.
  143. *
  144. * @return int
  145. */
  146. public function getCursor()
  147. {
  148. return $this->cursor;
  149. }
  150. /**
  151. * Sets the current cursor.
  152. *
  153. * @param int $pos
  154. */
  155. public function setCursor($pos)
  156. {
  157. if ($pos < 1) {
  158. $this->cursor = 1;
  159. } else {
  160. if ($pos > $this->nbResults) {
  161. $this->cursor = $this->nbResults;
  162. } else {
  163. $this->cursor = $pos;
  164. }
  165. }
  166. }
  167. /**
  168. * Returns an object by cursor position.
  169. *
  170. * @param int $pos
  171. *
  172. * @return mixed
  173. */
  174. public function getObjectByCursor($pos)
  175. {
  176. $this->setCursor($pos);
  177. return $this->getCurrent();
  178. }
  179. /**
  180. * Returns the current object.
  181. *
  182. * @return mixed
  183. */
  184. public function getCurrent()
  185. {
  186. return $this->retrieveObject($this->cursor);
  187. }
  188. /**
  189. * Returns the next object.
  190. *
  191. * @return mixed|null
  192. */
  193. public function getNext()
  194. {
  195. if ($this->cursor + 1 > $this->nbResults) {
  196. return;
  197. }
  198. return $this->retrieveObject($this->cursor + 1);
  199. }
  200. /**
  201. * Returns the previous object.
  202. *
  203. * @return mixed|null
  204. */
  205. public function getPrevious()
  206. {
  207. if ($this->cursor - 1 < 1) {
  208. return;
  209. }
  210. return $this->retrieveObject($this->cursor - 1);
  211. }
  212. /**
  213. * Returns the first index on the current page.
  214. *
  215. * @return int
  216. */
  217. public function getFirstIndex()
  218. {
  219. if ($this->page == 0) {
  220. return 1;
  221. }
  222. return ($this->page - 1) * $this->maxPerPage + 1;
  223. }
  224. /**
  225. * NEXT_MAJOR: remove this method.
  226. *
  227. * @deprecated since 3.x, will be removed in 4.0
  228. */
  229. public function getFirstIndice()
  230. {
  231. @trigger_error(
  232. 'Method '.__METHOD__.' is deprecated since version 3.x and will be removed in 4.0, '.
  233. 'please use getFirstIndex() instead.',
  234. E_USER_DEPRECATED
  235. );
  236. return $this->getFirstIndex();
  237. }
  238. /**
  239. * Returns the last index on the current page.
  240. *
  241. * @return int
  242. */
  243. public function getLastIndex()
  244. {
  245. if ($this->page == 0) {
  246. return $this->nbResults;
  247. }
  248. if ($this->page * $this->maxPerPage >= $this->nbResults) {
  249. return $this->nbResults;
  250. }
  251. return $this->page * $this->maxPerPage;
  252. }
  253. /**
  254. * NEXT_MAJOR: remove this method.
  255. *
  256. * @deprecated since 3.x, will be removed in 4.0
  257. */
  258. public function getLastIndice()
  259. {
  260. @trigger_error(
  261. 'Method '.__METHOD__.' is deprecated since version 3.x and will be removed in 4.0, '.
  262. 'please use getLastIndex() instead.',
  263. E_USER_DEPRECATED
  264. );
  265. return $this->getLastIndex();
  266. }
  267. /**
  268. * Returns the number of results.
  269. *
  270. * @return int
  271. */
  272. public function getNbResults()
  273. {
  274. return $this->nbResults;
  275. }
  276. /**
  277. * Returns the first page number.
  278. *
  279. * @return int
  280. */
  281. public function getFirstPage()
  282. {
  283. return 1;
  284. }
  285. /**
  286. * Returns the last page number.
  287. *
  288. * @return int
  289. */
  290. public function getLastPage()
  291. {
  292. return $this->lastPage;
  293. }
  294. /**
  295. * Returns the current page.
  296. *
  297. * @return int
  298. */
  299. public function getPage()
  300. {
  301. return $this->page;
  302. }
  303. /**
  304. * Returns the next page.
  305. *
  306. * @return int
  307. */
  308. public function getNextPage()
  309. {
  310. return min($this->getPage() + 1, $this->getLastPage());
  311. }
  312. /**
  313. * Returns the previous page.
  314. *
  315. * @return int
  316. */
  317. public function getPreviousPage()
  318. {
  319. return max($this->getPage() - 1, $this->getFirstPage());
  320. }
  321. /**
  322. * {@inheritdoc}
  323. */
  324. public function setPage($page)
  325. {
  326. $this->page = intval($page);
  327. if ($this->page <= 0) {
  328. // set first page, which depends on a maximum set
  329. $this->page = $this->getMaxPerPage() ? 1 : 0;
  330. }
  331. }
  332. /**
  333. * {@inheritdoc}
  334. */
  335. public function getMaxPerPage()
  336. {
  337. return $this->maxPerPage;
  338. }
  339. /**
  340. * {@inheritdoc}
  341. */
  342. public function setMaxPerPage($max)
  343. {
  344. if ($max > 0) {
  345. $this->maxPerPage = $max;
  346. if ($this->page == 0) {
  347. $this->page = 1;
  348. }
  349. } else {
  350. if ($max == 0) {
  351. $this->maxPerPage = 0;
  352. $this->page = 0;
  353. } else {
  354. $this->maxPerPage = 1;
  355. if ($this->page == 0) {
  356. $this->page = 1;
  357. }
  358. }
  359. }
  360. }
  361. /**
  362. * {@inheritdoc}
  363. */
  364. public function getMaxPageLinks()
  365. {
  366. return $this->maxPageLinks;
  367. }
  368. /**
  369. * {@inheritdoc}
  370. */
  371. public function setMaxPageLinks($maxPageLinks)
  372. {
  373. $this->maxPageLinks = $maxPageLinks;
  374. }
  375. /**
  376. * Returns true if on the first page.
  377. *
  378. * @return bool
  379. */
  380. public function isFirstPage()
  381. {
  382. return 1 == $this->page;
  383. }
  384. /**
  385. * Returns true if on the last page.
  386. *
  387. * @return bool
  388. */
  389. public function isLastPage()
  390. {
  391. return $this->page == $this->lastPage;
  392. }
  393. /**
  394. * Returns the current pager's parameter holder.
  395. *
  396. * @return array
  397. */
  398. public function getParameters()
  399. {
  400. return $this->parameters;
  401. }
  402. /**
  403. * Returns a parameter.
  404. *
  405. * @param string $name
  406. * @param mixed $default
  407. *
  408. * @return mixed
  409. */
  410. public function getParameter($name, $default = null)
  411. {
  412. return isset($this->parameters[$name]) ? $this->parameters[$name] : $default;
  413. }
  414. /**
  415. * Checks whether a parameter has been set.
  416. *
  417. * @param string $name
  418. *
  419. * @return bool
  420. */
  421. public function hasParameter($name)
  422. {
  423. return isset($this->parameters[$name]);
  424. }
  425. /**
  426. * Sets a parameter.
  427. *
  428. * @param string $name
  429. * @param mixed $value
  430. */
  431. public function setParameter($name, $value)
  432. {
  433. $this->parameters[$name] = $value;
  434. }
  435. /**
  436. * {@inheritdoc}
  437. */
  438. public function current()
  439. {
  440. if (!$this->isIteratorInitialized()) {
  441. $this->initializeIterator();
  442. }
  443. return current($this->results);
  444. }
  445. /**
  446. * {@inheritdoc}
  447. */
  448. public function key()
  449. {
  450. if (!$this->isIteratorInitialized()) {
  451. $this->initializeIterator();
  452. }
  453. return key($this->results);
  454. }
  455. /**
  456. * {@inheritdoc}
  457. */
  458. public function next()
  459. {
  460. if (!$this->isIteratorInitialized()) {
  461. $this->initializeIterator();
  462. }
  463. --$this->resultsCounter;
  464. return next($this->results);
  465. }
  466. /**
  467. * {@inheritdoc}
  468. */
  469. public function rewind()
  470. {
  471. if (!$this->isIteratorInitialized()) {
  472. $this->initializeIterator();
  473. }
  474. $this->resultsCounter = count($this->results);
  475. return reset($this->results);
  476. }
  477. /**
  478. * {@inheritdoc}
  479. */
  480. public function valid()
  481. {
  482. if (!$this->isIteratorInitialized()) {
  483. $this->initializeIterator();
  484. }
  485. return $this->resultsCounter > 0;
  486. }
  487. /**
  488. * {@inheritdoc}
  489. */
  490. public function count()
  491. {
  492. return $this->getNbResults();
  493. }
  494. /**
  495. * {@inheritdoc}
  496. */
  497. public function serialize()
  498. {
  499. $vars = get_object_vars($this);
  500. unset($vars['query']);
  501. return serialize($vars);
  502. }
  503. /**
  504. * {@inheritdoc}
  505. */
  506. public function unserialize($serialized)
  507. {
  508. $array = unserialize($serialized);
  509. foreach ($array as $name => $values) {
  510. $this->$name = $values;
  511. }
  512. }
  513. /**
  514. * @return array
  515. */
  516. public function getCountColumn()
  517. {
  518. return $this->countColumn;
  519. }
  520. /**
  521. * @param array $countColumn
  522. *
  523. * @return array
  524. */
  525. public function setCountColumn(array $countColumn)
  526. {
  527. return $this->countColumn = $countColumn;
  528. }
  529. /**
  530. * {@inheritdoc}
  531. */
  532. public function setQuery($query)
  533. {
  534. $this->query = $query;
  535. }
  536. /**
  537. * @return ProxyQueryInterface
  538. */
  539. public function getQuery()
  540. {
  541. return $this->query;
  542. }
  543. /**
  544. * Sets the number of results.
  545. *
  546. * @param int $nb
  547. */
  548. protected function setNbResults($nb)
  549. {
  550. $this->nbResults = $nb;
  551. }
  552. /**
  553. * Sets the last page number.
  554. *
  555. * @param int $page
  556. */
  557. protected function setLastPage($page)
  558. {
  559. $this->lastPage = $page;
  560. if ($this->getPage() > $page) {
  561. $this->setPage($page);
  562. }
  563. }
  564. /**
  565. * Returns true if the properties used for iteration have been initialized.
  566. *
  567. * @return bool
  568. */
  569. protected function isIteratorInitialized()
  570. {
  571. return null !== $this->results;
  572. }
  573. /**
  574. * Loads data into properties used for iteration.
  575. */
  576. protected function initializeIterator()
  577. {
  578. $this->results = $this->getResults();
  579. $this->resultsCounter = count($this->results);
  580. }
  581. /**
  582. * Empties properties used for iteration.
  583. */
  584. protected function resetIterator()
  585. {
  586. $this->results = null;
  587. $this->resultsCounter = 0;
  588. }
  589. /**
  590. * Retrieve the object for a certain offset.
  591. *
  592. * @param int $offset
  593. *
  594. * @return object
  595. */
  596. protected function retrieveObject($offset)
  597. {
  598. $queryForRetrieve = clone $this->getQuery();
  599. $queryForRetrieve
  600. ->setFirstResult($offset - 1)
  601. ->setMaxResults(1);
  602. $results = $queryForRetrieve->execute();
  603. return $results[0];
  604. }
  605. }