Pager.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. } else {
  200. return $this->retrieveObject($this->cursor + 1);
  201. }
  202. }
  203. /**
  204. * Returns the previous object.
  205. *
  206. * @return mixed|null
  207. */
  208. public function getPrevious()
  209. {
  210. if ($this->cursor - 1 < 1) {
  211. return;
  212. } else {
  213. return $this->retrieveObject($this->cursor - 1);
  214. }
  215. }
  216. /**
  217. * Returns the first index on the current page.
  218. *
  219. * @return int
  220. */
  221. public function getFirstIndice()
  222. {
  223. if ($this->page == 0) {
  224. return 1;
  225. } else {
  226. return ($this->page - 1) * $this->maxPerPage + 1;
  227. }
  228. }
  229. /**
  230. * Returns the last index on the current page.
  231. *
  232. * @return int
  233. */
  234. public function getLastIndice()
  235. {
  236. if ($this->page == 0) {
  237. return $this->nbResults;
  238. } else {
  239. if ($this->page * $this->maxPerPage >= $this->nbResults) {
  240. return $this->nbResults;
  241. } else {
  242. return $this->page * $this->maxPerPage;
  243. }
  244. }
  245. }
  246. /**
  247. * Returns the number of results.
  248. *
  249. * @return int
  250. */
  251. public function getNbResults()
  252. {
  253. return $this->nbResults;
  254. }
  255. /**
  256. * Returns the first page number.
  257. *
  258. * @return int
  259. */
  260. public function getFirstPage()
  261. {
  262. return 1;
  263. }
  264. /**
  265. * Returns the last page number.
  266. *
  267. * @return int
  268. */
  269. public function getLastPage()
  270. {
  271. return $this->lastPage;
  272. }
  273. /**
  274. * Returns the current page.
  275. *
  276. * @return int
  277. */
  278. public function getPage()
  279. {
  280. return $this->page;
  281. }
  282. /**
  283. * Returns the next page.
  284. *
  285. * @return int
  286. */
  287. public function getNextPage()
  288. {
  289. return min($this->getPage() + 1, $this->getLastPage());
  290. }
  291. /**
  292. * Returns the previous page.
  293. *
  294. * @return int
  295. */
  296. public function getPreviousPage()
  297. {
  298. return max($this->getPage() - 1, $this->getFirstPage());
  299. }
  300. /**
  301. * {@inheritdoc}
  302. */
  303. public function setPage($page)
  304. {
  305. $this->page = intval($page);
  306. if ($this->page <= 0) {
  307. // set first page, which depends on a maximum set
  308. $this->page = $this->getMaxPerPage() ? 1 : 0;
  309. }
  310. }
  311. /**
  312. * {@inheritdoc}
  313. */
  314. public function getMaxPerPage()
  315. {
  316. return $this->maxPerPage;
  317. }
  318. /**
  319. * {@inheritdoc}
  320. */
  321. public function setMaxPerPage($max)
  322. {
  323. if ($max > 0) {
  324. $this->maxPerPage = $max;
  325. if ($this->page == 0) {
  326. $this->page = 1;
  327. }
  328. } else {
  329. if ($max == 0) {
  330. $this->maxPerPage = 0;
  331. $this->page = 0;
  332. } else {
  333. $this->maxPerPage = 1;
  334. if ($this->page == 0) {
  335. $this->page = 1;
  336. }
  337. }
  338. }
  339. }
  340. /**
  341. * {@inheritdoc}
  342. */
  343. public function getMaxPageLinks()
  344. {
  345. return $this->maxPageLinks;
  346. }
  347. /**
  348. * {@inheritdoc}
  349. */
  350. public function setMaxPageLinks($maxPageLinks)
  351. {
  352. $this->maxPageLinks = $maxPageLinks;
  353. }
  354. /**
  355. * Returns true if on the first page.
  356. *
  357. * @return bool
  358. */
  359. public function isFirstPage()
  360. {
  361. return 1 == $this->page;
  362. }
  363. /**
  364. * Returns true if on the last page.
  365. *
  366. * @return bool
  367. */
  368. public function isLastPage()
  369. {
  370. return $this->page == $this->lastPage;
  371. }
  372. /**
  373. * Returns the current pager's parameter holder.
  374. *
  375. * @return array
  376. */
  377. public function getParameters()
  378. {
  379. return $this->parameters;
  380. }
  381. /**
  382. * Returns a parameter.
  383. *
  384. * @param string $name
  385. * @param mixed $default
  386. *
  387. * @return mixed
  388. */
  389. public function getParameter($name, $default = null)
  390. {
  391. return isset($this->parameters[$name]) ? $this->parameters[$name] : $default;
  392. }
  393. /**
  394. * Checks whether a parameter has been set.
  395. *
  396. * @param string $name
  397. *
  398. * @return bool
  399. */
  400. public function hasParameter($name)
  401. {
  402. return isset($this->parameters[$name]);
  403. }
  404. /**
  405. * Sets a parameter.
  406. *
  407. * @param string $name
  408. * @param mixed $value
  409. */
  410. public function setParameter($name, $value)
  411. {
  412. $this->parameters[$name] = $value;
  413. }
  414. /**
  415. * {@inheritdoc}
  416. */
  417. public function current()
  418. {
  419. if (!$this->isIteratorInitialized()) {
  420. $this->initializeIterator();
  421. }
  422. return current($this->results);
  423. }
  424. /**
  425. * {@inheritdoc}
  426. */
  427. public function key()
  428. {
  429. if (!$this->isIteratorInitialized()) {
  430. $this->initializeIterator();
  431. }
  432. return key($this->results);
  433. }
  434. /**
  435. * {@inheritdoc}
  436. */
  437. public function next()
  438. {
  439. if (!$this->isIteratorInitialized()) {
  440. $this->initializeIterator();
  441. }
  442. --$this->resultsCounter;
  443. return next($this->results);
  444. }
  445. /**
  446. * {@inheritdoc}
  447. */
  448. public function rewind()
  449. {
  450. if (!$this->isIteratorInitialized()) {
  451. $this->initializeIterator();
  452. }
  453. $this->resultsCounter = count($this->results);
  454. return reset($this->results);
  455. }
  456. /**
  457. * {@inheritdoc}
  458. */
  459. public function valid()
  460. {
  461. if (!$this->isIteratorInitialized()) {
  462. $this->initializeIterator();
  463. }
  464. return $this->resultsCounter > 0;
  465. }
  466. /**
  467. * {@inheritdoc}
  468. */
  469. public function count()
  470. {
  471. return $this->getNbResults();
  472. }
  473. /**
  474. * {@inheritdoc}
  475. */
  476. public function serialize()
  477. {
  478. $vars = get_object_vars($this);
  479. unset($vars['query']);
  480. return serialize($vars);
  481. }
  482. /**
  483. * {@inheritdoc}
  484. */
  485. public function unserialize($serialized)
  486. {
  487. $array = unserialize($serialized);
  488. foreach ($array as $name => $values) {
  489. $this->$name = $values;
  490. }
  491. }
  492. /**
  493. * @return array
  494. */
  495. public function getCountColumn()
  496. {
  497. return $this->countColumn;
  498. }
  499. /**
  500. * @param array $countColumn
  501. *
  502. * @return array
  503. */
  504. public function setCountColumn(array $countColumn)
  505. {
  506. return $this->countColumn = $countColumn;
  507. }
  508. /**
  509. * {@inheritdoc}
  510. */
  511. public function setQuery($query)
  512. {
  513. $this->query = $query;
  514. }
  515. /**
  516. * @return ProxyQueryInterface
  517. */
  518. public function getQuery()
  519. {
  520. return $this->query;
  521. }
  522. /**
  523. * Sets the number of results.
  524. *
  525. * @param int $nb
  526. */
  527. protected function setNbResults($nb)
  528. {
  529. $this->nbResults = $nb;
  530. }
  531. /**
  532. * Sets the last page number.
  533. *
  534. * @param int $page
  535. */
  536. protected function setLastPage($page)
  537. {
  538. $this->lastPage = $page;
  539. if ($this->getPage() > $page) {
  540. $this->setPage($page);
  541. }
  542. }
  543. /**
  544. * Returns true if the properties used for iteration have been initialized.
  545. *
  546. * @return bool
  547. */
  548. protected function isIteratorInitialized()
  549. {
  550. return null !== $this->results;
  551. }
  552. /**
  553. * Loads data into properties used for iteration.
  554. */
  555. protected function initializeIterator()
  556. {
  557. $this->results = $this->getResults();
  558. $this->resultsCounter = count($this->results);
  559. }
  560. /**
  561. * Empties properties used for iteration.
  562. */
  563. protected function resetIterator()
  564. {
  565. $this->results = null;
  566. $this->resultsCounter = 0;
  567. }
  568. /**
  569. * Retrieve the object for a certain offset.
  570. *
  571. * @param int $offset
  572. *
  573. * @return object
  574. */
  575. protected function retrieveObject($offset)
  576. {
  577. $queryForRetrieve = clone $this->getQuery();
  578. $queryForRetrieve
  579. ->setFirstResult($offset - 1)
  580. ->setMaxResults(1);
  581. $results = $queryForRetrieve->execute();
  582. return $results[0];
  583. }
  584. }