Pager.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. const TYPE_DEFAULT = 'default';
  18. const TYPE_SIMPLE = 'simple';
  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 integer $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 integer
  46. */
  47. public function getCurrentMaxLink()
  48. {
  49. return $this->currentMaxLink;
  50. }
  51. /**
  52. * Returns the current pager's max record limit.
  53. *
  54. * @return integer
  55. */
  56. public function getMaxRecordLimit()
  57. {
  58. return $this->maxRecordLimit;
  59. }
  60. /**
  61. * Sets the current pager's max record limit.
  62. *
  63. * @param integer $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 integer $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 boolean
  97. */
  98. public function haveToPaginate()
  99. {
  100. return $this->getMaxPerPage() && $this->getNbResults() > $this->getMaxPerPage();
  101. }
  102. /**
  103. * Returns the current cursor.
  104. *
  105. * @return integer
  106. */
  107. public function getCursor()
  108. {
  109. return $this->cursor;
  110. }
  111. /**
  112. * Sets the current cursor.
  113. *
  114. * @param integer $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 integer $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 null;
  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 null;
  171. } else {
  172. return $this->retrieveObject($this->cursor - 1);
  173. }
  174. }
  175. /**
  176. * Returns the first index on the current page.
  177. *
  178. * @return integer
  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 integer
  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 integer
  209. */
  210. public function getNbResults()
  211. {
  212. return $this->nbResults;
  213. }
  214. /**
  215. * Sets the number of results.
  216. *
  217. * @param integer $nb
  218. */
  219. protected function setNbResults($nb)
  220. {
  221. $this->nbResults = $nb;
  222. }
  223. /**
  224. * Returns the first page number.
  225. *
  226. * @return integer
  227. */
  228. public function getFirstPage()
  229. {
  230. return 1;
  231. }
  232. /**
  233. * Returns the last page number.
  234. *
  235. * @return integer
  236. */
  237. public function getLastPage()
  238. {
  239. return $this->lastPage;
  240. }
  241. /**
  242. * Sets the last page number.
  243. *
  244. * @param integer $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 integer
  257. */
  258. public function getPage()
  259. {
  260. return $this->page;
  261. }
  262. /**
  263. * Returns the next page.
  264. *
  265. * @return integer
  266. */
  267. public function getNextPage()
  268. {
  269. return min($this->getPage() + 1, $this->getLastPage());
  270. }
  271. /**
  272. * Returns the previous page.
  273. *
  274. * @return integer
  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. * {@inheritdoc}
  322. */
  323. public function getMaxPageLinks()
  324. {
  325. return $this->maxPageLinks;
  326. }
  327. /**
  328. * {@inheritdoc}
  329. */
  330. public function setMaxPageLinks($maxPageLinks)
  331. {
  332. $this->maxPageLinks = $maxPageLinks;
  333. }
  334. /**
  335. * Returns true if on the first page.
  336. *
  337. * @return boolean
  338. */
  339. public function isFirstPage()
  340. {
  341. return 1 == $this->page;
  342. }
  343. /**
  344. * Returns true if on the last page.
  345. *
  346. * @return boolean
  347. */
  348. public function isLastPage()
  349. {
  350. return $this->page == $this->lastPage;
  351. }
  352. /**
  353. * Returns the current pager's parameter holder.
  354. *
  355. * @return array
  356. */
  357. public function getParameters()
  358. {
  359. return $this->parameters;
  360. }
  361. /**
  362. * Returns a parameter.
  363. *
  364. * @param string $name
  365. * @param mixed $default
  366. *
  367. * @return mixed
  368. */
  369. public function getParameter($name, $default = null)
  370. {
  371. return isset($this->parameters[$name]) ? $this->parameters[$name] : $default;
  372. }
  373. /**
  374. * Checks whether a parameter has been set.
  375. *
  376. * @param string $name
  377. *
  378. * @return boolean
  379. */
  380. public function hasParameter($name)
  381. {
  382. return isset($this->parameters[$name]);
  383. }
  384. /**
  385. * Sets a parameter.
  386. *
  387. * @param string $name
  388. * @param mixed $value
  389. */
  390. public function setParameter($name, $value)
  391. {
  392. $this->parameters[$name] = $value;
  393. }
  394. /**
  395. * Returns true if the properties used for iteration have been initialized.
  396. *
  397. * @return boolean
  398. */
  399. protected function isIteratorInitialized()
  400. {
  401. return null !== $this->results;
  402. }
  403. /**
  404. * Loads data into properties used for iteration.
  405. */
  406. protected function initializeIterator()
  407. {
  408. $this->results = $this->getResults();
  409. $this->resultsCounter = count($this->results);
  410. }
  411. /**
  412. * Empties properties used for iteration.
  413. */
  414. protected function resetIterator()
  415. {
  416. $this->results = null;
  417. $this->resultsCounter = 0;
  418. }
  419. /**
  420. * {@inheritdoc}
  421. */
  422. public function current()
  423. {
  424. if (!$this->isIteratorInitialized()) {
  425. $this->initializeIterator();
  426. }
  427. return current($this->results);
  428. }
  429. /**
  430. * {@inheritdoc}
  431. */
  432. public function key()
  433. {
  434. if (!$this->isIteratorInitialized()) {
  435. $this->initializeIterator();
  436. }
  437. return key($this->results);
  438. }
  439. /**
  440. * {@inheritdoc}
  441. */
  442. public function next()
  443. {
  444. if (!$this->isIteratorInitialized()) {
  445. $this->initializeIterator();
  446. }
  447. --$this->resultsCounter;
  448. return next($this->results);
  449. }
  450. /**
  451. * {@inheritdoc}
  452. */
  453. public function rewind()
  454. {
  455. if (!$this->isIteratorInitialized()) {
  456. $this->initializeIterator();
  457. }
  458. $this->resultsCounter = count($this->results);
  459. return reset($this->results);
  460. }
  461. /**
  462. * {@inheritdoc}
  463. */
  464. public function valid()
  465. {
  466. if (!$this->isIteratorInitialized()) {
  467. $this->initializeIterator();
  468. }
  469. return $this->resultsCounter > 0;
  470. }
  471. /**
  472. * {@inheritdoc}
  473. */
  474. public function count()
  475. {
  476. return $this->getNbResults();
  477. }
  478. /**
  479. * {@inheritdoc}
  480. */
  481. public function serialize()
  482. {
  483. $vars = get_object_vars($this);
  484. unset($vars['query']);
  485. return serialize($vars);
  486. }
  487. /**
  488. * {@inheritdoc}
  489. */
  490. public function unserialize($serialized)
  491. {
  492. $array = unserialize($serialized);
  493. foreach ($array as $name => $values) {
  494. $this->$name = $values;
  495. }
  496. }
  497. /**
  498. * @return array
  499. */
  500. public function getCountColumn()
  501. {
  502. return $this->countColumn;
  503. }
  504. /**
  505. * @param array $countColumn
  506. *
  507. * @return array
  508. */
  509. public function setCountColumn(array $countColumn)
  510. {
  511. return $this->countColumn = $countColumn;
  512. }
  513. /**
  514. * Retrieve the object for a certain offset
  515. *
  516. * @param integer $offset
  517. *
  518. * @return object
  519. */
  520. protected function retrieveObject($offset)
  521. {
  522. $queryForRetrieve = clone $this->getQuery();
  523. $queryForRetrieve
  524. ->setFirstResult($offset - 1)
  525. ->setMaxResults(1);
  526. $results = $queryForRetrieve->execute();
  527. return $results[0];
  528. }
  529. /**
  530. * {@inheritdoc}
  531. */
  532. public function setQuery($query)
  533. {
  534. $this->query = $query;
  535. }
  536. /**
  537. * @return ProxyQueryInterface
  538. */
  539. public function getQuery()
  540. {
  541. return $this->query;
  542. }
  543. }