LinkPager.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. use yii\helpers\ArrayHelper;
  14. /**
  15. * LinkPager displays a list of hyperlinks that lead to different pages of target.
  16. *
  17. * LinkPager works with a [[Pagination]] object which specifies the total number
  18. * of pages and the current page number.
  19. *
  20. * Note that LinkPager only generates the necessary HTML markups. In order for it
  21. * to look like a real pager, you should provide some CSS styles for it.
  22. * With the default configuration, LinkPager should look good using Twitter Bootstrap CSS framework.
  23. *
  24. * For more details and usage information on LinkPager, see the [guide article on pagination](guide:output-pagination).
  25. *
  26. * @author Qiang Xue <qiang.xue@gmail.com>
  27. * @since 2.0
  28. */
  29. class LinkPager extends Widget
  30. {
  31. /**
  32. * @var Pagination the pagination object that this pager is associated with.
  33. * You must set this property in order to make LinkPager work.
  34. */
  35. public $pagination;
  36. /**
  37. * @var array HTML attributes for the pager container tag.
  38. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  39. */
  40. public $options = ['class' => 'pagination'];
  41. /**
  42. * @var array HTML attributes for the link in a pager container tag.
  43. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  44. */
  45. public $linkOptions = [];
  46. /**
  47. * @var string the CSS class for the each page button.
  48. * @since 2.0.7
  49. */
  50. public $pageCssClass;
  51. /**
  52. * @var string the CSS class for the "first" page button.
  53. */
  54. public $firstPageCssClass = 'first';
  55. /**
  56. * @var string the CSS class for the "last" page button.
  57. */
  58. public $lastPageCssClass = 'last';
  59. /**
  60. * @var string the CSS class for the "previous" page button.
  61. */
  62. public $prevPageCssClass = 'prev';
  63. /**
  64. * @var string the CSS class for the "next" page button.
  65. */
  66. public $nextPageCssClass = 'next';
  67. /**
  68. * @var string the CSS class for the active (currently selected) page button.
  69. */
  70. public $activePageCssClass = 'active';
  71. /**
  72. * @var string the CSS class for the disabled page buttons.
  73. */
  74. public $disabledPageCssClass = 'disabled';
  75. /**
  76. * @var array the options for the disabled tag to be generated inside the disabled list element.
  77. * In order to customize the html tag, please use the tag key.
  78. *
  79. * ```php
  80. * $disabledListItemSubTagOptions = ['tag' => 'div', 'class' => 'disabled-div'];
  81. * ```
  82. * @since 2.0.11
  83. */
  84. public $disabledListItemSubTagOptions = [];
  85. /**
  86. * @var int maximum number of page buttons that can be displayed. Defaults to 10.
  87. */
  88. public $maxButtonCount = 10;
  89. /**
  90. * @var string|bool the label for the "next" page button. Note that this will NOT be HTML-encoded.
  91. * If this property is false, the "next" page button will not be displayed.
  92. */
  93. public $nextPageLabel = '&raquo;';
  94. /**
  95. * @var string|bool the text label for the previous page button. Note that this will NOT be HTML-encoded.
  96. * If this property is false, the "previous" page button will not be displayed.
  97. */
  98. public $prevPageLabel = '&laquo;';
  99. /**
  100. * @var string|bool the text label for the "first" page button. Note that this will NOT be HTML-encoded.
  101. * If it's specified as true, page number will be used as label.
  102. * Default is false that means the "first" page button will not be displayed.
  103. */
  104. public $firstPageLabel = false;
  105. /**
  106. * @var string|bool the text label for the "last" page button. Note that this will NOT be HTML-encoded.
  107. * If it's specified as true, page number will be used as label.
  108. * Default is false that means the "last" page button will not be displayed.
  109. */
  110. public $lastPageLabel = false;
  111. /**
  112. * @var bool whether to register link tags in the HTML header for prev, next, first and last page.
  113. * Defaults to `false` to avoid conflicts when multiple pagers are used on one page.
  114. * @see http://www.w3.org/TR/html401/struct/links.html#h-12.1.2
  115. * @see registerLinkTags()
  116. */
  117. public $registerLinkTags = false;
  118. /**
  119. * @var bool Hide widget when only one page exist.
  120. */
  121. public $hideOnSinglePage = true;
  122. /**
  123. * @var bool whether to render current page button as disabled.
  124. * @since 2.0.12
  125. */
  126. public $disableCurrentPageButton = false;
  127. /**
  128. * Initializes the pager.
  129. */
  130. public function init()
  131. {
  132. if ($this->pagination === null) {
  133. throw new InvalidConfigException('The "pagination" property must be set.');
  134. }
  135. }
  136. /**
  137. * Executes the widget.
  138. * This overrides the parent implementation by displaying the generated page buttons.
  139. */
  140. public function run()
  141. {
  142. if ($this->registerLinkTags) {
  143. $this->registerLinkTags();
  144. }
  145. echo $this->renderPageButtons();
  146. }
  147. /**
  148. * Registers relational link tags in the html header for prev, next, first and last page.
  149. * These links are generated using [[\yii\data\Pagination::getLinks()]].
  150. * @see http://www.w3.org/TR/html401/struct/links.html#h-12.1.2
  151. */
  152. protected function registerLinkTags()
  153. {
  154. $view = $this->getView();
  155. foreach ($this->pagination->getLinks() as $rel => $href) {
  156. $view->registerLinkTag(['rel' => $rel, 'href' => $href], $rel);
  157. }
  158. }
  159. /**
  160. * Renders the page buttons.
  161. * @return string the rendering result
  162. */
  163. protected function renderPageButtons()
  164. {
  165. $pageCount = $this->pagination->getPageCount();
  166. if ($pageCount < 2 && $this->hideOnSinglePage) {
  167. return '';
  168. }
  169. $buttons = [];
  170. $currentPage = $this->pagination->getPage();
  171. // first page
  172. $firstPageLabel = $this->firstPageLabel === true ? '1' : $this->firstPageLabel;
  173. if ($firstPageLabel !== false) {
  174. $buttons[] = $this->renderPageButton($firstPageLabel, 0, $this->firstPageCssClass, $currentPage <= 0, false);
  175. }
  176. // prev page
  177. if ($this->prevPageLabel !== false) {
  178. if (($page = $currentPage - 1) < 0) {
  179. $page = 0;
  180. }
  181. $buttons[] = $this->renderPageButton($this->prevPageLabel, $page, $this->prevPageCssClass, $currentPage <= 0, false);
  182. }
  183. // internal pages
  184. list($beginPage, $endPage) = $this->getPageRange();
  185. for ($i = $beginPage; $i <= $endPage; ++$i) {
  186. $buttons[] = $this->renderPageButton($i + 1, $i, null, $this->disableCurrentPageButton && $i == $currentPage, $i == $currentPage);
  187. }
  188. // next page
  189. if ($this->nextPageLabel !== false) {
  190. if (($page = $currentPage + 1) >= $pageCount - 1) {
  191. $page = $pageCount - 1;
  192. }
  193. $buttons[] = $this->renderPageButton($this->nextPageLabel, $page, $this->nextPageCssClass, $currentPage >= $pageCount - 1, false);
  194. }
  195. // last page
  196. $lastPageLabel = $this->lastPageLabel === true ? $pageCount : $this->lastPageLabel;
  197. if ($lastPageLabel !== false) {
  198. $buttons[] = $this->renderPageButton($lastPageLabel, $pageCount - 1, $this->lastPageCssClass, $currentPage >= $pageCount - 1, false);
  199. }
  200. return Html::tag('ul', implode("\n", $buttons), $this->options);
  201. }
  202. /**
  203. * Renders a page button.
  204. * You may override this method to customize the generation of page buttons.
  205. * @param string $label the text label for the button
  206. * @param int $page the page number
  207. * @param string $class the CSS class for the page button.
  208. * @param bool $disabled whether this page button is disabled
  209. * @param bool $active whether this page button is active
  210. * @return string the rendering result
  211. */
  212. protected function renderPageButton($label, $page, $class, $disabled, $active)
  213. {
  214. $options = ['class' => empty($class) ? $this->pageCssClass : $class];
  215. if ($active) {
  216. Html::addCssClass($options, $this->activePageCssClass);
  217. }
  218. if ($disabled) {
  219. Html::addCssClass($options, $this->disabledPageCssClass);
  220. $tag = ArrayHelper::remove($this->disabledListItemSubTagOptions, 'tag', 'span');
  221. return Html::tag('li', Html::tag($tag, $label, $this->disabledListItemSubTagOptions), $options);
  222. }
  223. $linkOptions = $this->linkOptions;
  224. $linkOptions['data-page'] = $page;
  225. return Html::tag('li', Html::a($label, $this->pagination->createUrl($page), $linkOptions), $options);
  226. }
  227. /**
  228. * @return array the begin and end pages that need to be displayed.
  229. */
  230. protected function getPageRange()
  231. {
  232. $currentPage = $this->pagination->getPage();
  233. $pageCount = $this->pagination->getPageCount();
  234. $beginPage = max(0, $currentPage - (int) ($this->maxButtonCount / 2));
  235. if (($endPage = $beginPage + $this->maxButtonCount - 1) >= $pageCount) {
  236. $endPage = $pageCount - 1;
  237. $beginPage = max(0, $endPage - $this->maxButtonCount + 1);
  238. }
  239. return [$beginPage, $endPage];
  240. }
  241. }