SimplePager.php 4.1 KB

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