Skip to content

Commit

Permalink
Merge pull request #3194 from michalsn/pagination_fix
Browse files Browse the repository at this point in the history
Fix default value for page in Model::paginate()
  • Loading branch information
michalsn authored Jul 2, 2020
2 parents da43f0b + 1cd7d16 commit effa61b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
10 changes: 8 additions & 2 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1141,10 +1141,16 @@ 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);

if ($segment)
{
$pager->setSegment($segment);
}

$page = $page >= 1 ? $page : $pager->getCurrentPage($group);

$total = $this->countAllResults(false);

Expand Down
24 changes: 22 additions & 2 deletions system/Pager/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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.
*
Expand Down Expand Up @@ -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;
}

//--------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/libraries/pagination.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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]*.

::

Expand Down

0 comments on commit effa61b

Please sign in to comment.