LinkPager.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. * Initializes the pager.
  124. */
  125. public function init()
  126. {
  127. if ($this->pagination === null) {
  128. throw new InvalidConfigException('The "pagination" property must be set.');
  129. }
  130. }
  131. /**
  132. * Executes the widget.
  133. * This overrides the parent implementation by displaying the generated page buttons.
  134. */
  135. public function run()
  136. {
  137. if ($this->registerLinkTags) {
  138. $this->registerLinkTags();
  139. }
  140. echo $this->renderPageButtons();
  141. }
  142. /**
  143. * Registers relational link tags in the html header for prev, next, first and last page.
  144. * These links are generated using [[\yii\data\Pagination::getLinks()]].
  145. * @see http://www.w3.org/TR/html401/struct/links.html#h-12.1.2
  146. */
  147. protected function registerLinkTags()
  148. {
  149. $view = $this->getView();
  150. foreach ($this->pagination->getLinks() as $rel => $href) {
  151. $view->registerLinkTag(['rel' => $rel, 'href' => $href], $rel);
  152. }
  153. }
  154. /**
  155. * Renders the page buttons.
  156. * @return string the rendering result
  157. */
  158. protected function renderPageButtons()
  159. {
  160. $pageCount = $this->pagination->getPageCount();
  161. if ($pageCount < 2 && $this->hideOnSinglePage) {
  162. return '';
  163. }
  164. $buttons = [];
  165. $currentPage = $this->pagination->getPage();
  166. // first page
  167. $firstPageLabel = $this->firstPageLabel === true ? '1' : $this->firstPageLabel;
  168. if ($firstPageLabel !== false) {
  169. $buttons[] = $this->renderPageButton($firstPageLabel, 0, $this->firstPageCssClass, $currentPage <= 0, false);
  170. }
  171. // prev page
  172. if ($this->prevPageLabel !== false) {
  173. if (($page = $currentPage - 1) < 0) {
  174. $page = 0;
  175. }
  176. $buttons[] = $this->renderPageButton($this->prevPageLabel, $page, $this->prevPageCssClass, $currentPage <= 0, false);
  177. }
  178. // internal pages
  179. list($beginPage, $endPage) = $this->getPageRange();
  180. for ($i = $beginPage; $i <= $endPage; ++$i) {
  181. $buttons[] = $this->renderPageButton($i + 1, $i, null, false, $i == $currentPage);
  182. }
  183. // next page
  184. if ($this->nextPageLabel !== false) {
  185. if (($page = $currentPage + 1) >= $pageCount - 1) {
  186. $page = $pageCount - 1;
  187. }
  188. $buttons[] = $this->renderPageButton($this->nextPageLabel, $page, $this->nextPageCssClass, $currentPage >= $pageCount - 1, false);
  189. }
  190. // last page
  191. $lastPageLabel = $this->lastPageLabel === true ? $pageCount : $this->lastPageLabel;
  192. if ($lastPageLabel !== false) {
  193. $buttons[] = $this->renderPageButton($lastPageLabel, $pageCount - 1, $this->lastPageCssClass, $currentPage >= $pageCount - 1, false);
  194. }
  195. return Html::tag('ul', implode("\n", $buttons), $this->options);
  196. }
  197. /**
  198. * Renders a page button.
  199. * You may override this method to customize the generation of page buttons.
  200. * @param string $label the text label for the button
  201. * @param int $page the page number
  202. * @param string $class the CSS class for the page button.
  203. * @param bool $disabled whether this page button is disabled
  204. * @param bool $active whether this page button is active
  205. * @return string the rendering result
  206. */
  207. protected function renderPageButton($label, $page, $class, $disabled, $active)
  208. {
  209. $options = ['class' => empty($class) ? $this->pageCssClass : $class];
  210. if ($active) {
  211. Html::addCssClass($options, $this->activePageCssClass);
  212. }
  213. if ($disabled) {
  214. Html::addCssClass($options, $this->disabledPageCssClass);
  215. $tag = ArrayHelper::remove($this->disabledListItemSubTagOptions, 'tag', 'span');
  216. return Html::tag('li', Html::tag($tag, $label, $this->disabledListItemSubTagOptions), $options);
  217. }
  218. $linkOptions = $this->linkOptions;
  219. $linkOptions['data-page'] = $page;
  220. return Html::tag('li', Html::a($label, $this->pagination->createUrl($page), $linkOptions), $options);
  221. }
  222. /**
  223. * @return array the begin and end pages that need to be displayed.
  224. */
  225. protected function getPageRange()
  226. {
  227. $currentPage = $this->pagination->getPage();
  228. $pageCount = $this->pagination->getPageCount();
  229. $beginPage = max(0, $currentPage - (int) ($this->maxButtonCount / 2));
  230. if (($endPage = $beginPage + $this->maxButtonCount - 1) >= $pageCount) {
  231. $endPage = $pageCount - 1;
  232. $beginPage = max(0, $endPage - $this->maxButtonCount + 1);
  233. }
  234. return [$beginPage, $endPage];
  235. }
  236. }