PageRenderTime 81ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/yiisoft/yii2/widgets/LinkPager.php

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