Pager.php 12 KB

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