LinkPager.php 9.8 KB

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