SimplePager.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. use Doctrine\Common\Collections\ArrayCollection;
  12. /**
  13. * Class SimplePager.
  14. *
  15. * @author Lukas Kahwe Smith <smith@pooteeweet.org>
  16. * @author Sjoerd Peters <sjoerd.peters@gmail.com>
  17. */
  18. class SimplePager extends Pager
  19. {
  20. /**
  21. * @var bool
  22. */
  23. protected $haveToPaginate;
  24. /**
  25. * How many pages to look forward to create links to next pages.
  26. *
  27. * @var int
  28. */
  29. protected $threshold;
  30. /**
  31. * @var int
  32. */
  33. protected $thresholdCount;
  34. /**
  35. * The threshold parameter can be used to determine how far ahead the pager
  36. * should fetch results.
  37. *
  38. * If set to 1 which is the minimal value the pager will generate a link to the next page
  39. * If set to 2 the pager will generate links to the next two pages
  40. * If set to 3 the pager will generate links to the next three pages
  41. * etc.
  42. *
  43. * @param int $maxPerPage Number of records to display per page
  44. * @param int $threshold
  45. */
  46. public function __construct($maxPerPage = 10, $threshold = 1)
  47. {
  48. parent::__construct($maxPerPage);
  49. $this->setThreshold($threshold);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getNbResults()
  55. {
  56. $n = ceil(($this->getLastPage() - 1) * $this->getMaxPerPage());
  57. if ($this->getLastPage() == $this->getPage()) {
  58. return $n + $this->thresholdCount;
  59. }
  60. return $n;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getResults($hydrationMode = null)
  66. {
  67. if ($this->results) {
  68. return $this->results;
  69. }
  70. $this->results = $this->getQuery()->execute(array(), $hydrationMode);
  71. $this->thresholdCount = count($this->results);
  72. if (count($this->results) > $this->getMaxPerPage()) {
  73. $this->haveToPaginate = true;
  74. if ($this->results instanceof ArrayCollection) {
  75. $this->results = new ArrayCollection($this->results->slice(0, $this->getMaxPerPage()));
  76. } else {
  77. $this->results = new ArrayCollection(array_slice($this->results, 0, $this->getMaxPerPage()));
  78. }
  79. } else {
  80. $this->haveToPaginate = false;
  81. }
  82. return $this->results;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function haveToPaginate()
  88. {
  89. return $this->haveToPaginate || $this->getPage() > 1;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. *
  94. * @throws \RuntimeException the QueryBuilder is uninitialized.
  95. */
  96. public function init()
  97. {
  98. if (!$this->getQuery()) {
  99. throw new \RuntimeException('Uninitialized QueryBuilder');
  100. }
  101. $this->resetIterator();
  102. if (0 == $this->getPage() || 0 == $this->getMaxPerPage()) {
  103. $this->setLastPage(0);
  104. $this->getQuery()->setFirstResult(0);
  105. $this->getQuery()->setMaxResults(0);
  106. } else {
  107. $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
  108. $this->getQuery()->setFirstResult($offset);
  109. $maxOffset = $this->getThreshold() > 0
  110. ? $this->getMaxPerPage() * $this->threshold + 1 : $this->getMaxPerPage() + 1;
  111. $this->getQuery()->setMaxResults($maxOffset);
  112. $this->initializeIterator();
  113. $t = (int) ceil($this->thresholdCount / $this->getMaxPerPage()) + $this->getPage() - 1;
  114. $this->setLastPage($t);
  115. }
  116. }
  117. /**
  118. * Set how many pages to look forward to create links to next pages.
  119. *
  120. * @param int $threshold
  121. */
  122. public function setThreshold($threshold)
  123. {
  124. $this->threshold = (int) $threshold;
  125. }
  126. /**
  127. * @return int
  128. */
  129. public function getThreshold()
  130. {
  131. return $this->threshold;
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. protected function resetIterator()
  137. {
  138. parent::resetIterator();
  139. $this->haveToPaginate = false;
  140. }
  141. }