Pager.php 13 KB

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