Pager.php 12 KB

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