Pager.php 12 KB

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