Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

235 lines
8.1KB

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