Pager.php 13 KB

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