From f099744dc2e5cf5516b5faee6394b1386d0feb9e Mon Sep 17 00:00:00 2001 From: michalsn Date: Wed, 1 Jul 2020 18:17:44 +0200 Subject: [PATCH 1/2] Fix default value for page in Model::paginate(). Fixes #3188 --- system/Model.php | 2 +- user_guide_src/source/libraries/pagination.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Model.php b/system/Model.php index cc6bee16dc78..0bfbce2d6580 100644 --- a/system/Model.php +++ b/system/Model.php @@ -1141,7 +1141,7 @@ public function chunk(int $size, Closure $userFunc) * * @return array|null */ - public function paginate(int $perPage = null, string $group = 'default', int $page = 0, int $segment = 0) + public function paginate(int $perPage = null, string $group = 'default', int $page = null, int $segment = 0) { $pager = \Config\Services::pager(null, null, false); $page = $page >= 1 ? $page : $pager->getCurrentPage($group); diff --git a/user_guide_src/source/libraries/pagination.rst b/user_guide_src/source/libraries/pagination.rst index 452c22c13499..d984be66cf5b 100644 --- a/user_guide_src/source/libraries/pagination.rst +++ b/user_guide_src/source/libraries/pagination.rst @@ -112,7 +112,7 @@ Specifying the URI Segment for Page It is also possible to use a URI segment for the page number, instead of the page query parameter. Simply specify the segment number to use as the fourth argument. URIs generated by the pager would then look -like *https://domain.tld/model/[pageNumber]* instead of *https://domain.tld/model?page=[pageNumber]*.:: +like *https://domain.tld/model/[pageNumber]* instead of *https://domain.tld/model?page=[pageNumber]*. :: From 1cd7d168d5cccda943369c4ddc7f722eff9396d1 Mon Sep 17 00:00:00 2001 From: michalsn Date: Thu, 2 Jul 2020 13:28:39 +0200 Subject: [PATCH 2/2] Fix paginate() when using segment --- system/Model.php | 8 +++++++- system/Pager/Pager.php | 24 ++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/system/Model.php b/system/Model.php index 0bfbce2d6580..5d8aa86298ee 100644 --- a/system/Model.php +++ b/system/Model.php @@ -1144,7 +1144,13 @@ public function chunk(int $size, Closure $userFunc) public function paginate(int $perPage = null, string $group = 'default', int $page = null, int $segment = 0) { $pager = \Config\Services::pager(null, null, false); - $page = $page >= 1 ? $page : $pager->getCurrentPage($group); + + if ($segment) + { + $pager->setSegment($segment); + } + + $page = $page >= 1 ? $page : $pager->getCurrentPage($group); $total = $this->countAllResults(false); diff --git a/system/Pager/Pager.php b/system/Pager/Pager.php index 143505496942..d34fd3d08518 100644 --- a/system/Pager/Pager.php +++ b/system/Pager/Pager.php @@ -202,7 +202,10 @@ protected function displayLinks(string $group, string $template): string */ public function store(string $group, int $page, int $perPage = null, int $total, int $segment = 0) { - $this->segment[$group] = $segment; + if ($segment) + { + $this->setSegment($segment, $group); + } $this->ensureGroup($group, $perPage); @@ -223,6 +226,23 @@ public function store(string $group, int $page, int $perPage = null, int $total, //-------------------------------------------------------------------- + /** + * Sets segment for a group. + * + * @param integer $number + * @param string $group + * + * @return mixed + */ + public function setSegment(int $number, string $group = 'default') + { + $this->segment[$group] = $number; + + return $this; + } + + //-------------------------------------------------------------------- + /** * Sets the path that an aliased group of links will use. * @@ -269,7 +289,7 @@ public function getCurrentPage(string $group = 'default'): int { $this->ensureGroup($group); - return $this->groups[$group]['currentPage']; + return $this->groups[$group]['currentPage'] ?: 1; } //--------------------------------------------------------------------