Redirect user to last available page in list widget if current page is unavailable (#4020)

Credit to @bennothommo. Fixes #4008.
This commit is contained in:
Ben Thomson 2018-12-31 02:49:42 +08:00 committed by Luke Towers
parent 108838f824
commit ee003c416c
1 changed files with 32 additions and 5 deletions

View File

@ -585,11 +585,7 @@ class Lists extends WidgetBase
}
elseif ($this->showPagination) {
$method = $this->showPageNumbers ? 'paginate' : 'simplePaginate';
$currentPageNumber = $this->currentPageNumber;
if (!$currentPageNumber && empty($this->searchTerm)) {
// Restore the last visited page from the session if available.
$currentPageNumber = $this->getSession('lastVisitedPage');
}
$currentPageNumber = $this->getCurrentPageNumber($query);
$records = $query->{$method}($this->recordsPerPage, $currentPageNumber);
}
else {
@ -622,6 +618,37 @@ class Lists extends WidgetBase
return $this->records = $records;
}
/**
* Returns the current page number for the list.
*
* This will override the current page number provided by the user if it is past the last page of available records.
*
* @param object $query
* @return int
*/
protected function getCurrentPageNumber($query)
{
$currentPageNumber = $this->currentPageNumber;
if (!$currentPageNumber && empty($this->searchTerm)) {
// Restore the last visited page from the session if available.
$currentPageNumber = $this->getSession('lastVisitedPage');
}
$currentPageNumber = intval($currentPageNumber);
if ($currentPageNumber > 1) {
$count = $query->count();
// If the current page number is higher than the amount of available pages, go to the last available page
if ($count <= (($currentPageNumber - 1) * $this->recordsPerPage)) {
$currentPageNumber = ceil($count / $this->recordsPerPage);
}
}
return $currentPageNumber;
}
/**
* Returns the record URL address for a list row.
* @param Model $record