From a733ecd40c625101024a42ceb64dc24c4076c074 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Fri, 17 Apr 2015 11:51:48 +0300 Subject: [PATCH] `yii\widgets\LinkPager::$firstPageLabel` and `yii\widgets\LinkPager::$lastPageLabel` now could be set to true in order to use page number as label --- framework/CHANGELOG.md | 1 + framework/widgets/LinkPager.php | 12 ++++--- tests/framework/widgets/LinkPagerTest.php | 59 +++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 tests/framework/widgets/LinkPagerTest.php diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 7956245..15bbfa6 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -46,6 +46,7 @@ Yii Framework 2 Change Log - Enh: Added `yii\helper\Console::wrapText()` method to wrap indented text by console window width and used it in `yii help` command (cebe) - Enh: Implement batchInsert for oci (nineinchnick) - Enh: Detecting IntegrityException for oci (nineinchnick) +- Enh: `yii\widgets\LinkPager::$firstPageLabel` and `yii\widgets\LinkPager::$lastPageLabel` now could be set to true in order to use page number as label (samdark) - Chg #7924: Migrations in history are now ordered by time applied allowing to roll back in reverse order no matter how these were applied (samdark) - Chg: Updated dependency to `cebe/markdown` to version `1.1.x` (cebe) diff --git a/framework/widgets/LinkPager.php b/framework/widgets/LinkPager.php index 36f8430..3c95779 100644 --- a/framework/widgets/LinkPager.php +++ b/framework/widgets/LinkPager.php @@ -83,11 +83,13 @@ class LinkPager extends Widget public $prevPageLabel = '«'; /** * @var string|boolean the text label for the "first" page button. Note that this will NOT be HTML-encoded. + * If it's specified as true, page number will be used as label. * Default is false that means the "first" page button will not be displayed. */ public $firstPageLabel = false; /** * @var string|boolean the text label for the "last" page button. Note that this will NOT be HTML-encoded. + * If it's specified as true, page number will be used as label. * Default is false that means the "last" page button will not be displayed. */ public $lastPageLabel = false; @@ -154,8 +156,9 @@ class LinkPager extends Widget $currentPage = $this->pagination->getPage(); // first page - if ($this->firstPageLabel !== false) { - $buttons[] = $this->renderPageButton($this->firstPageLabel, 0, $this->firstPageCssClass, $currentPage <= 0, false); + $firstPageLabel = $this->firstPageLabel === true ? '1' : $this->firstPageLabel; + if ($firstPageLabel !== false) { + $buttons[] = $this->renderPageButton($firstPageLabel, 0, $this->firstPageCssClass, $currentPage <= 0, false); } // prev page @@ -181,8 +184,9 @@ class LinkPager extends Widget } // last page - if ($this->lastPageLabel !== false) { - $buttons[] = $this->renderPageButton($this->lastPageLabel, $pageCount - 1, $this->lastPageCssClass, $currentPage >= $pageCount - 1, false); + $lastPageLabel = $this->lastPageLabel === true ? $pageCount - 1 : $this->lastPageLabel; + if ($lastPageLabel !== false) { + $buttons[] = $this->renderPageButton($lastPageLabel, $pageCount - 1, $this->lastPageCssClass, $currentPage >= $pageCount - 1, false); } return Html::tag('ul', implode("\n", $buttons), $this->options); diff --git a/tests/framework/widgets/LinkPagerTest.php b/tests/framework/widgets/LinkPagerTest.php new file mode 100644 index 0000000..55d8f65 --- /dev/null +++ b/tests/framework/widgets/LinkPagerTest.php @@ -0,0 +1,59 @@ +mockApplication([ + 'components' => [ + 'urlManager' => [ + 'scriptUrl' => '/' + ] + ] + ]); + } + + public function testFirstLastPageLabels() + { + $pagination = new Pagination(); + $pagination->setPage(5); + $pagination->totalCount = 500; + $pagination->route = 'test'; + + $output = LinkPager::widget([ + 'pagination' => $pagination, + 'firstPageLabel' => true, + 'lastPageLabel' => true + ]); + + static::assertContains('
  • 1
  • ', $output); + static::assertContains('
  • 24
  • ', $output); + + $output = LinkPager::widget([ + 'pagination' => $pagination, + 'firstPageLabel' => 'First', + 'lastPageLabel' => 'Last' + ]); + + static::assertContains('
  • First
  • ', $output); + static::assertContains('
  • Last
  • ', $output); + + $output = LinkPager::widget([ + 'pagination' => $pagination, + 'firstPageLabel' => false, + 'lastPageLabel' => false + ]); + + static::assertNotContains('
  • ', $output); + static::assertNotContains('
  • ', $output); + } +}