Pager.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 the current pager's max link.
  42. *
  43. * @return integer
  44. */
  45. public function getCurrentMaxLink()
  46. {
  47. return $this->currentMaxLink;
  48. }
  49. /**
  50. * Returns the current pager's max record limit.
  51. *
  52. * @return integer
  53. */
  54. public function getMaxRecordLimit()
  55. {
  56. return $this->maxRecordLimit;
  57. }
  58. /**
  59. * Sets the current pager's max record limit.
  60. *
  61. * @param integer $limit
  62. */
  63. public function setMaxRecordLimit($limit)
  64. {
  65. $this->maxRecordLimit = $limit;
  66. }
  67. /**
  68. * Returns an array of page numbers to use in pagination links.
  69. *
  70. * @param integer $nbLinks The maximum number of page numbers to return
  71. *
  72. * @return array
  73. */
  74. public function getLinks($nbLinks = null)
  75. {
  76. if ($nbLinks == null) {
  77. $nbLinks = $this->getMaxPageLinks();
  78. }
  79. $links = array();
  80. $tmp = $this->page - floor($nbLinks / 2);
  81. $check = $this->lastPage - $nbLinks + 1;
  82. $limit = $check > 0 ? $check : 1;
  83. $begin = $tmp > 0 ? ($tmp > $limit ? $limit : $tmp) : 1;
  84. $i = (int) $begin;
  85. while ($i < $begin + $nbLinks && $i <= $this->lastPage) {
  86. $links[] = $i++;
  87. }
  88. $this->currentMaxLink = count($links) ? $links[count($links) - 1] : 1;
  89. return $links;
  90. }
  91. /**
  92. * Returns true if the current query requires pagination.
  93. *
  94. * @return boolean
  95. */
  96. public function haveToPaginate()
  97. {
  98. return $this->getMaxPerPage() && $this->getNbResults() > $this->getMaxPerPage();
  99. }
  100. /**
  101. * Returns the current cursor.
  102. *
  103. * @return integer
  104. */
  105. public function getCursor()
  106. {
  107. return $this->cursor;
  108. }
  109. /**
  110. * Sets the current cursor.
  111. *
  112. * @param integer $pos
  113. */
  114. public function setCursor($pos)
  115. {
  116. if ($pos < 1) {
  117. $this->cursor = 1;
  118. } else {
  119. if ($pos > $this->nbResults) {
  120. $this->cursor = $this->nbResults;
  121. } else {
  122. $this->cursor = $pos;
  123. }
  124. }
  125. }
  126. /**
  127. * Returns an object by cursor position.
  128. *
  129. * @param integer $pos
  130. *
  131. * @return mixed
  132. */
  133. public function getObjectByCursor($pos)
  134. {
  135. $this->setCursor($pos);
  136. return $this->getCurrent();
  137. }
  138. /**
  139. * Returns the current object.
  140. *
  141. * @return mixed
  142. */
  143. public function getCurrent()
  144. {
  145. return $this->retrieveObject($this->cursor);
  146. }
  147. /**
  148. * Returns the next object.
  149. *
  150. * @return mixed|null
  151. */
  152. public function getNext()
  153. {
  154. if ($this->cursor + 1 > $this->nbResults) {
  155. return null;
  156. } else {
  157. return $this->retrieveObject($this->cursor + 1);
  158. }
  159. }
  160. /**
  161. * Returns the previous object.
  162. *
  163. * @return mixed|null
  164. */
  165. public function getPrevious()
  166. {
  167. if ($this->cursor - 1 < 1) {
  168. return null;
  169. } else {
  170. return $this->retrieveObject($this->cursor - 1);
  171. }
  172. }
  173. /**
  174. * Returns the first index on the current page.
  175. *
  176. * @return integer
  177. */
  178. public function getFirstIndice()
  179. {
  180. if ($this->page == 0) {
  181. return 1;
  182. } else {
  183. return ($this->page - 1) * $this->maxPerPage + 1;
  184. }
  185. }
  186. /**
  187. * Returns the last index on the current page.
  188. *
  189. * @return integer
  190. */
  191. public function getLastIndice()
  192. {
  193. if ($this->page == 0) {
  194. return $this->nbResults;
  195. } else {
  196. if ($this->page * $this->maxPerPage >= $this->nbResults) {
  197. return $this->nbResults;
  198. } else {
  199. return $this->page * $this->maxPerPage;
  200. }
  201. }
  202. }
  203. /**
  204. * Returns the number of results.
  205. *
  206. * @return integer
  207. */
  208. public function getNbResults()
  209. {
  210. return $this->nbResults;
  211. }
  212. /**
  213. * Sets the number of results.
  214. *
  215. * @param integer $nb
  216. */
  217. protected function setNbResults($nb)
  218. {
  219. $this->nbResults = $nb;
  220. }
  221. /**
  222. * Returns the first page number.
  223. *
  224. * @return integer
  225. */
  226. public function getFirstPage()
  227. {
  228. return 1;
  229. }
  230. /**
  231. * Returns the last page number.
  232. *
  233. * @return integer
  234. */
  235. public function getLastPage()
  236. {
  237. return $this->lastPage;
  238. }
  239. /**
  240. * Sets the last page number.
  241. *
  242. * @param integer $page
  243. */
  244. protected function setLastPage($page)
  245. {
  246. $this->lastPage = $page;
  247. if ($this->getPage() > $page) {
  248. $this->setPage($page);
  249. }
  250. }
  251. /**
  252. * Returns the current page.
  253. *
  254. * @return integer
  255. */
  256. public function getPage()
  257. {
  258. return $this->page;
  259. }
  260. /**
  261. * Returns the next page.
  262. *
  263. * @return integer
  264. */
  265. public function getNextPage()
  266. {
  267. return min($this->getPage() + 1, $this->getLastPage());
  268. }
  269. /**
  270. * Returns the previous page.
  271. *
  272. * @return integer
  273. */
  274. public function getPreviousPage()
  275. {
  276. return max($this->getPage() - 1, $this->getFirstPage());
  277. }
  278. /**
  279. * {@inheritdoc}
  280. */
  281. public function setPage($page)
  282. {
  283. $this->page = intval($page);
  284. if ($this->page <= 0) {
  285. // set first page, which depends on a maximum set
  286. $this->page = $this->getMaxPerPage() ? 1 : 0;
  287. }
  288. }
  289. /**
  290. * {@inheritdoc}
  291. */
  292. public function getMaxPerPage()
  293. {
  294. return $this->maxPerPage;
  295. }
  296. /**
  297. * {@inheritdoc}
  298. */
  299. public function setMaxPerPage($max)
  300. {
  301. if ($max > 0) {
  302. $this->maxPerPage = $max;
  303. if ($this->page == 0) {
  304. $this->page = 1;
  305. }
  306. } else {
  307. if ($max == 0) {
  308. $this->maxPerPage = 0;
  309. $this->page = 0;
  310. } else {
  311. $this->maxPerPage = 1;
  312. if ($this->page == 0) {
  313. $this->page = 1;
  314. }
  315. }
  316. }
  317. }
  318. /**
  319. * Returns the maximum number of page numbers.
  320. *
  321. * @return integer
  322. */
  323. public function getMaxPageLinks()
  324. {
  325. return $this->maxPageLinks;
  326. }
  327. /**
  328. * Sets the maximum number of page numbers.
  329. *
  330. * @param integer $maxPageLinks
  331. */
  332. public function setMaxPageLinks($maxPageLinks)
  333. {
  334. $this->maxPageLinks = $maxPageLinks;
  335. }
  336. /**
  337. * Returns true if on the first page.
  338. *
  339. * @return boolean
  340. */
  341. public function isFirstPage()
  342. {
  343. return 1 == $this->page;
  344. }
  345. /**
  346. * Returns true if on the last page.
  347. *
  348. * @return boolean
  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 boolean
  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 boolean
  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 integer $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. }