Pager.php 13 KB

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