Skip to content

Commit

Permalink
[BUGFIX] Show only specific range of pages in pagination, add "..." (#…
Browse files Browse the repository at this point in the history
…1103)

Co-authored-by: BFallert <[email protected]>
Co-authored-by: Sebastian Meyer <[email protected]>
  • Loading branch information
3 people authored Feb 27, 2024
1 parent ddc8536 commit 117266e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
45 changes: 39 additions & 6 deletions Classes/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,25 @@ protected function buildSimplePagination(PaginationInterface $pagination, Pagina
$currentPageNumber = $paginator->getCurrentPageNumber();

$pages = [];

$pagesSect = [];
$aRange = [];
$nRange = 5; // ToDo: should be made configurable

// lower limit of the range
$nBottom = $currentPageNumber - $nRange;
// upper limit of the range
$nTop = $currentPageNumber + $nRange;
// page range
for ($i = $nBottom; $i <= $nTop; $i++) {
if ($i > 0 and $i <= $lastPage) {
array_push($aRange, $i);
};
};

// check whether the first screen page is > 1, if yes then points must be added
if ($aRange[0] > 1) {
array_push($pagesSect, ['label' => '...','startRecordNumber' => '...']);
};
$lastStartRecordNumberGrid = 0; // due to validity outside the loop
foreach (range($firstPage, $lastPage) as $i) {
// detect which pagination is active: ListView or GridView
Expand All @@ -340,6 +358,12 @@ protected function buildSimplePagination(PaginationInterface $pagination, Pagina
'label' => $i,
'startRecordNumber' => $i
];

// Check if screen page is in range
// <f:for each="{pagination.pagesR}" as="page">
if (in_array($i, $aRange)) {
array_push($pagesSect, ['label' => $i, 'startRecordNumber' => $i]);
};
} else { // GridView
// to calculate the values for generation the links for the pagination pages
/** @var \Kitodo\Dlf\Pagination\PageGridPaginator $paginator */
Expand All @@ -362,14 +386,22 @@ protected function buildSimplePagination(PaginationInterface $pagination, Pagina
'label' => $i,
'startRecordNumber' => $startRecordNumber
];
}
}

// Check if screen page is in range
if (in_array($i, $aRange)) {
array_push($pagesSect, ['label' => $i,'startRecordNumber' => $startRecordNumber]);
};
};
};

// check whether the last element from $aRange <= last screen page, if yes then points must be added
if ($aRange[array_key_last($aRange)] < $lastPage) {
array_push($pagesSect, ['label' => '...', 'startRecordNumber' => '...']);
};

$nextPageNumber = $pages[$currentPageNumber + 1]['startRecordNumber'];
$previousPageNumber = $pages[$currentPageNumber - 1]['startRecordNumber'];

// 'startRecordNumber' is not required in GridView, only the variant for each loop is required
// 'endRecordNumber' is not required in both views
// 'startRecordNumber' is not required in GridView, only the variant for each loop is required
// 'endRecordNumber' is not required in both views
//
Expand Down Expand Up @@ -397,7 +429,8 @@ protected function buildSimplePagination(PaginationInterface $pagination, Pagina
'endRecordNumber' => $pagination->getEndRecordNumber(),
'currentPageNumber' => $currentPageNumber,
'pages' => range($firstPage, $lastPage),
'pagesG' => $pages
'pagesG' => $pages,
'pagesR' => $pagesSect
];
}

Expand Down
26 changes: 15 additions & 11 deletions Resources/Private/Partials/Lists/Pagination.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
</li>
</f:else>
</f:if>
<f:if condition="{pagination.hasLessPages}">
<li></li>
</f:if>
<f:comment>add pages between first and last page</f:comment>
<f:for each="{pagination.pagesG}" as="page">
<f:comment>add pages between first and last page, with dots if there are more than 5 pages (before or after) the respective page</f:comment>
<f:for each="{pagination.pagesR}" as="page">
<f:switch expression="{page.label}">
<f:comment>If page 1 is not to be output twice, please remove the comment</f:comment>
<f:comment>
Expand All @@ -37,15 +34,22 @@
</f:case>
</f:comment>
<f:defaultCase>
<li class="{f:if(condition: '{page.label} == {paginator.currentPageNumber}', then:'current')}">
<f:link.action action="{action}" addQueryString="true" argumentsToBeExcludedFromQueryString="{0: 'tx_dlf[page]'}" additionalParams="{'tx_dlf[page]': page.startRecordNumber}" arguments="{searchParameter: lastSearch}">{page.label}</f:link.action>
</li>
<f:switch expression="{page.nIndex}">
<f:comment>add the dots</f:comment>
<f:case value="...">
<li class="dots">
<span class="dots">...</span>
</li>
</f:case>
<f:defaultCase>
<li class="{f:if(condition: '{page.label} == {paginator.currentPageNumber}', then:'current')}">
<f:link.action action="{action}" addQueryString="true" argumentsToBeExcludedFromQueryString="{0: 'tx_dlf[page]'}" additionalParams="{'tx_dlf[page]': page.startRecordNumber}" arguments="{searchParameter: lastSearch}">{page.label}</f:link.action>
</li>
</f:defaultCase>
</f:switch>
</f:defaultCase>
</f:switch>
</f:for>
<f:if condition="{pagination.hasMorePages}">
<li></li>
</f:if>
<f:if condition="{pagination.nextPageNumberG} && {pagination.nextPageNumberG} <= {pagination.lastPageNumber}">
<f:then>
<li class="next">
Expand Down

0 comments on commit 117266e

Please sign in to comment.