Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

244 lines
8.5KB

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