Pager.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Sonata\AdminBundle\Datagrid;
  10. /**
  11. * Pager class.
  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. protected $page = 1;
  18. protected $maxPerPage = 0;
  19. protected $lastPage = 1;
  20. protected $nbResults = 0;
  21. protected $cursor = 1;
  22. protected $parameters = array();
  23. protected $currentMaxLink = 1;
  24. protected $maxRecordLimit = false;
  25. protected $maxPageLinks = 0;
  26. // used by iterator interface
  27. protected $results = null;
  28. protected $resultsCounter = 0;
  29. protected $query = null;
  30. protected $countColumn = array('id');
  31. /**
  32. * Constructor.
  33. *
  34. * @param integer $maxPerPage Number of records to display per page
  35. */
  36. public function __construct($maxPerPage = 10)
  37. {
  38. $this->setMaxPerPage($maxPerPage);
  39. }
  40. /**
  41. * Returns an array of results on the given page.
  42. *
  43. * @return array
  44. */
  45. abstract public function getResults();
  46. /**
  47. * Returns the current pager's max link.
  48. *
  49. * @return integer
  50. */
  51. public function getCurrentMaxLink()
  52. {
  53. return $this->currentMaxLink;
  54. }
  55. /**
  56. * Returns the current pager's max record limit.
  57. *
  58. * @return integer
  59. */
  60. public function getMaxRecordLimit()
  61. {
  62. return $this->maxRecordLimit;
  63. }
  64. /**
  65. * Sets the current pager's max record limit.
  66. *
  67. * @param integer $limit
  68. */
  69. public function setMaxRecordLimit($limit)
  70. {
  71. $this->maxRecordLimit = $limit;
  72. }
  73. /**
  74. * Returns an array of page numbers to use in pagination links.
  75. *
  76. * @param integer $nb_links The maximum number of page numbers to return
  77. *
  78. * @return array
  79. */
  80. public function getLinks($nb_links = null)
  81. {
  82. if ($nb_links == null) {
  83. $nb_links = $this->getMaxPageLinks();
  84. }
  85. $links = array();
  86. $tmp = $this->page - floor($nb_links / 2);
  87. $check = $this->lastPage - $nb_links + 1;
  88. $limit = $check > 0 ? $check : 1;
  89. $begin = $tmp > 0 ? ($tmp > $limit ? $limit : $tmp) : 1;
  90. $i = (int) $begin;
  91. while ($i < $begin + $nb_links && $i <= $this->lastPage) {
  92. $links[] = $i++;
  93. }
  94. $this->currentMaxLink = count($links) ? $links[count($links) - 1] : 1;
  95. return $links;
  96. }
  97. /**
  98. * Returns true if the current query requires pagination.
  99. *
  100. * @return boolean
  101. */
  102. public function haveToPaginate()
  103. {
  104. return $this->getMaxPerPage() && $this->getNbResults() > $this->getMaxPerPage();
  105. }
  106. /**
  107. * Returns the current cursor.
  108. *
  109. * @return integer
  110. */
  111. public function getCursor()
  112. {
  113. return $this->cursor;
  114. }
  115. /**
  116. * Sets the current cursor.
  117. *
  118. * @param integer $pos
  119. */
  120. public function setCursor($pos)
  121. {
  122. if ($pos < 1) {
  123. $this->cursor = 1;
  124. } else {
  125. if ($pos > $this->nbResults) {
  126. $this->cursor = $this->nbResults;
  127. } else {
  128. $this->cursor = $pos;
  129. }
  130. }
  131. }
  132. /**
  133. * Returns an object by cursor position.
  134. *
  135. * @param integer $pos
  136. *
  137. * @return mixed
  138. */
  139. public function getObjectByCursor($pos)
  140. {
  141. $this->setCursor($pos);
  142. return $this->getCurrent();
  143. }
  144. /**
  145. * Returns the current object.
  146. *
  147. * @return mixed
  148. */
  149. public function getCurrent()
  150. {
  151. return $this->retrieveObject($this->cursor);
  152. }
  153. /**
  154. * Returns the next object.
  155. *
  156. * @return mixed|null
  157. */
  158. public function getNext()
  159. {
  160. if ($this->cursor + 1 > $this->nbResults) {
  161. return null;
  162. } else {
  163. return $this->retrieveObject($this->cursor + 1);
  164. }
  165. }
  166. /**
  167. * Returns the previous object.
  168. *
  169. * @return mixed|null
  170. */
  171. public function getPrevious()
  172. {
  173. if ($this->cursor - 1 < 1) {
  174. return null;
  175. } else {
  176. return $this->retrieveObject($this->cursor - 1);
  177. }
  178. }
  179. /**
  180. * Returns the first index on the current page.
  181. *
  182. * @return integer
  183. */
  184. public function getFirstIndice()
  185. {
  186. if ($this->page == 0) {
  187. return 1;
  188. } else {
  189. return ($this->page - 1) * $this->maxPerPage + 1;
  190. }
  191. }
  192. /**
  193. * Returns the last index on the current page.
  194. *
  195. * @return integer
  196. */
  197. public function getLastIndice()
  198. {
  199. if ($this->page == 0) {
  200. return $this->nbResults;
  201. } else {
  202. if ($this->page * $this->maxPerPage >= $this->nbResults) {
  203. return $this->nbResults;
  204. } else {
  205. return $this->page * $this->maxPerPage;
  206. }
  207. }
  208. }
  209. /**
  210. * Returns the number of results.
  211. *
  212. * @return integer
  213. */
  214. public function getNbResults()
  215. {
  216. return $this->nbResults;
  217. }
  218. /**
  219. * Sets the number of results.
  220. *
  221. * @param integer $nb
  222. */
  223. protected function setNbResults($nb)
  224. {
  225. $this->nbResults = $nb;
  226. }
  227. /**
  228. * Returns the first page number.
  229. *
  230. * @return integer
  231. */
  232. public function getFirstPage()
  233. {
  234. return 1;
  235. }
  236. /**
  237. * Returns the last page number.
  238. *
  239. * @return integer
  240. */
  241. public function getLastPage()
  242. {
  243. return $this->lastPage;
  244. }
  245. /**
  246. * Sets the last page number.
  247. *
  248. * @param integer $page
  249. */
  250. protected function setLastPage($page)
  251. {
  252. $this->lastPage = $page;
  253. if ($this->getPage() > $page) {
  254. $this->setPage($page);
  255. }
  256. }
  257. /**
  258. * Returns the current page.
  259. *
  260. * @return integer
  261. */
  262. public function getPage()
  263. {
  264. return $this->page;
  265. }
  266. /**
  267. * Returns the next page.
  268. *
  269. * @return integer
  270. */
  271. public function getNextPage()
  272. {
  273. return min($this->getPage() + 1, $this->getLastPage());
  274. }
  275. /**
  276. * Returns the previous page.
  277. *
  278. * @return integer
  279. */
  280. public function getPreviousPage()
  281. {
  282. return max($this->getPage() - 1, $this->getFirstPage());
  283. }
  284. /**
  285. * Sets the current page.
  286. *
  287. * @param integer $page
  288. */
  289. public function setPage($page)
  290. {
  291. $this->page = intval($page);
  292. if ($this->page <= 0) {
  293. // set first page, which depends on a maximum set
  294. $this->page = $this->getMaxPerPage() ? 1 : 0;
  295. }
  296. }
  297. /**
  298. * Returns the maximum number of results per page.
  299. *
  300. * @return integer
  301. */
  302. public function getMaxPerPage()
  303. {
  304. return $this->maxPerPage;
  305. }
  306. /**
  307. * Sets the maximum number of results per page.
  308. *
  309. * @param integer $max
  310. */
  311. public function setMaxPerPage($max)
  312. {
  313. if ($max > 0) {
  314. $this->maxPerPage = $max;
  315. if ($this->page == 0) {
  316. $this->page = 1;
  317. }
  318. } else {
  319. if ($max == 0) {
  320. $this->maxPerPage = 0;
  321. $this->page = 0;
  322. } else {
  323. $this->maxPerPage = 1;
  324. if ($this->page == 0) {
  325. $this->page = 1;
  326. }
  327. }
  328. }
  329. }
  330. /**
  331. * Returns the maximum number of page numbers.
  332. *
  333. * @return integer
  334. */
  335. public function getMaxPageLinks()
  336. {
  337. return $this->maxPageLinks;
  338. }
  339. /**
  340. * Sets the maximum number of page numbers.
  341. *
  342. * @param integer $maxPageLinks
  343. */
  344. public function setMaxPageLinks($maxPageLinks)
  345. {
  346. $this->maxPageLinks = $maxPageLinks;
  347. }
  348. /**
  349. * Returns true if on the first page.
  350. *
  351. * @return boolean
  352. */
  353. public function isFirstPage()
  354. {
  355. return 1 == $this->page;
  356. }
  357. /**
  358. * Returns true if on the last page.
  359. *
  360. * @return boolean
  361. */
  362. public function isLastPage()
  363. {
  364. return $this->page == $this->lastPage;
  365. }
  366. /**
  367. * Returns the current pager's parameter holder.
  368. *
  369. * @return array
  370. */
  371. public function getParameters()
  372. {
  373. return $this->parameters;
  374. }
  375. /**
  376. * Returns a parameter.
  377. *
  378. * @param string $name
  379. * @param mixed $default
  380. *
  381. * @return mixed
  382. */
  383. public function getParameter($name, $default = null)
  384. {
  385. return isset($this->parameters[$name]) ? $this->parameters[$name] : $default;
  386. }
  387. /**
  388. * Checks whether a parameter has been set.
  389. *
  390. * @param string $name
  391. *
  392. * @return boolean
  393. */
  394. public function hasParameter($name)
  395. {
  396. return isset($this->parameters[$name]);
  397. }
  398. /**
  399. * Sets a parameter.
  400. *
  401. * @param string $name
  402. * @param mixed $value
  403. */
  404. public function setParameter($name, $value)
  405. {
  406. $this->parameters[$name] = $value;
  407. }
  408. /**
  409. * Returns true if the properties used for iteration have been initialized.
  410. *
  411. * @return boolean
  412. */
  413. protected function isIteratorInitialized()
  414. {
  415. return null !== $this->results;
  416. }
  417. /**
  418. * Loads data into properties used for iteration.
  419. */
  420. protected function initializeIterator()
  421. {
  422. $this->results = $this->getResults();
  423. $this->resultsCounter = count($this->results);
  424. }
  425. /**
  426. * Empties properties used for iteration.
  427. */
  428. protected function resetIterator()
  429. {
  430. $this->results = null;
  431. $this->resultsCounter = 0;
  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. * Retrieve the object for a certain offset
  529. *
  530. * @param integer $offset
  531. *
  532. * @return object
  533. */
  534. protected function retrieveObject($offset)
  535. {
  536. $queryForRetrieve = clone $this->getQuery();
  537. $queryForRetrieve
  538. ->setFirstResult($offset - 1)
  539. ->setMaxResults(1);
  540. $results = $queryForRetrieve->execute();
  541. return $results[0];
  542. }
  543. /**
  544. * @param mixed $query
  545. */
  546. public function setQuery($query)
  547. {
  548. $this->query = $query;
  549. }
  550. /**
  551. * @return ProxyQueryInterface
  552. */
  553. public function getQuery()
  554. {
  555. return $this->query;
  556. }
  557. }