Pager.php 12 KB

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