LinkPager.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\widgets;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use yii\helpers\Html;
  11. use yii\base\Widget;
  12. use yii\data\Pagination;
  13. /**
  14. * LinkPager displays a list of hyperlinks that lead to different pages of target.
  15. *
  16. * LinkPager works with a [[Pagination]] object which specifies the totally number
  17. * of pages and the current page number.
  18. *
  19. * Note that LinkPager only generates the necessary HTML markups. In order for it
  20. * to look like a real pager, you should provide some CSS styles for it.
  21. * With the default configuration, LinkPager should look good using Twitter Bootstrap CSS framework.
  22. *
  23. * @author Qiang Xue <[email protected]>
  24. * @since 2.0
  25. */
  26. class LinkPager extends Widget
  27. {
  28. /**
  29. * @var Pagination the pagination object that this pager is associated with.
  30. * You must set this property in order to make LinkPager work.
  31. */
  32. public $pagination;
  33. /**
  34. * @var array HTML attributes for the pager container tag.
  35. */
  36. public $options = ['class' => 'pagination'];
  37. /**
  38. * @var string the CSS class for the "first" page button.
  39. */
  40. public $firstPageCssClass = 'first';
  41. /**
  42. * @var string the CSS class for the "last" page button.
  43. */
  44. public $lastPageCssClass = 'last';
  45. /**
  46. * @var string the CSS class for the "previous" page button.
  47. */
  48. public $prevPageCssClass = 'prev';
  49. /**
  50. * @var string the CSS class for the "next" page button.
  51. */
  52. public $nextPageCssClass = 'next';
  53. /**
  54. * @var string the CSS class for the active (currently selected) page button.
  55. */
  56. public $activePageCssClass = 'active';
  57. /**
  58. * @var string the CSS class for the disabled page buttons.
  59. */
  60. public $disabledPageCssClass = 'disabled';
  61. /**
  62. * @var integer maximum number of page buttons that can be displayed. Defaults to 10.
  63. */
  64. public $maxButtonCount = 10;
  65. /**
  66. * @var string the label for the "next" page button. Note that this will NOT be HTML-encoded.
  67. * If this property is null, the "next" page button will not be displayed.
  68. */
  69. public $nextPageLabel = '&raquo;';
  70. /**
  71. * @var string the text label for the previous page button. Note that this will NOT be HTML-encoded.
  72. * If this property is null, the "previous" page button will not be displayed.
  73. */
  74. public $prevPageLabel = '&laquo;';
  75. /**
  76. * @var string the text label for the "first" page button. Note that this will NOT be HTML-encoded.
  77. * If this property is null, the "first" page button will not be displayed.
  78. */
  79. public $firstPageLabel;
  80. /**
  81. * @var string the text label for the "last" page button. Note that this will NOT be HTML-encoded.
  82. * If this property is null, the "last" page button will not be displayed.
  83. */
  84. public $lastPageLabel;
  85. /**
  86. * Initializes the pager.
  87. */
  88. public function init()
  89. {
  90. if ($this->pagination === null) {
  91. throw new InvalidConfigException('The "pagination" property must be set.');
  92. }
  93. }
  94. /**
  95. * Executes the widget.
  96. * This overrides the parent implementation by displaying the generated page buttons.
  97. */
  98. public function run()
  99. {
  100. echo $this->renderPageButtons();
  101. }
  102. /**
  103. * Renders the page buttons.
  104. * @return string the rendering result
  105. */
  106. protected function renderPageButtons()
  107. {
  108. $buttons = [];
  109. $pageCount = $this->pagination->getPageCount();
  110. $currentPage = $this->pagination->getPage();
  111. // first page
  112. if ($this->firstPageLabel !== null) {
  113. $buttons[] = $this->renderPageButton($this->firstPageLabel, 0, $this->firstPageCssClass, $currentPage <= 0, false);
  114. }
  115. // prev page
  116. if ($this->prevPageLabel !== null) {
  117. if (($page = $currentPage - 1) < 0) {
  118. $page = 0;
  119. }
  120. $buttons[] = $this->renderPageButton($this->prevPageLabel, $page, $this->prevPageCssClass, $currentPage <= 0, false);
  121. }
  122. // internal pages
  123. list($beginPage, $endPage) = $this->getPageRange();
  124. for ($i = $beginPage; $i <= $endPage; ++$i) {
  125. $buttons[] = $this->renderPageButton($i + 1, $i, null, false, $i == $currentPage);
  126. }
  127. // next page
  128. if ($this->nextPageLabel !== null) {
  129. if (($page = $currentPage + 1) >= $pageCount - 1) {
  130. $page = $pageCount - 1;
  131. }
  132. $buttons[] = $this->renderPageButton($this->nextPageLabel, $page, $this->nextPageCssClass, $currentPage >= $pageCount - 1, false);
  133. }
  134. // last page
  135. if ($this->lastPageLabel !== null) {
  136. $buttons[] = $this->renderPageButton($this->lastPageLabel, $pageCount - 1, $this->lastPageCssClass, $currentPage >= $pageCount - 1, false);
  137. }
  138. return Html::tag('ul', implode("\n", $buttons), $this->options);
  139. }
  140. /**
  141. * Renders a page button.
  142. * You may override this method to customize the generation of page buttons.
  143. * @param string $label the text label for the button
  144. * @param integer $page the page number
  145. * @param string $class the CSS class for the page button.
  146. * @param boolean $disabled whether this page button is disabled
  147. * @param boolean $active whether this page button is active
  148. * @return string the rendering result
  149. */
  150. protected function renderPageButton($label, $page, $class, $disabled, $active)
  151. {
  152. $options = ['class' => $class === '' ? null : $class];
  153. if ($active) {
  154. Html::addCssClass($options, $this->activePageCssClass);
  155. }
  156. if ($disabled) {
  157. Html::addCssClass($options, $this->disabledPageCssClass);
  158. return Html::tag('li', Html::tag('span', $label), $options);
  159. }
  160. return Html::tag('li', Html::a($label, $this->pagination->createUrl($page), ['data-page' => $page]), $options);
  161. }
  162. /**
  163. * @return array the begin and end pages that need to be displayed.
  164. */
  165. protected function getPageRange()
  166. {
  167. $currentPage = $this->pagination->getPage();
  168. $pageCount = $this->pagination->getPageCount();
  169. $beginPage = max(0, $currentPage - (int)($this->maxButtonCount / 2));
  170. if (($endPage = $beginPage + $this->maxButtonCount - 1) >= $pageCount) {
  171. $endPage = $pageCount - 1;
  172. $beginPage = max(0, $endPage - $this->maxButtonCount + 1);
  173. }
  174. return [$beginPage, $endPage];
  175. }
  176. }