Skip to content

Commit

Permalink
Hide paginator when there are 0 or 1 pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Richter committed Jan 9, 2025
1 parent 0f29edd commit b6dc2b7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
18 changes: 14 additions & 4 deletions Classes/Common/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,28 @@ public function setPage(int $page): Paginator
return $this;
}

public function getPagination(): array
public function getTotalPages(): int
{
if (
$this->totalItems < 0 ||
$this->currentPage < 0 ||
$this->itemsPerPage < 0
) {
throw new \Exception('Please specify total items, items per page and current page before retrieving the pagination.');
throw new \Exception('Please specify total items and items per page before retrieving the pagination.');
}

return (int) ceil($this->totalItems / $this->itemsPerPage);
}

public function getPagination(): array
{
if (
$this->currentPage < 0
) {
throw new \Exception('Please specify current page before retrieving the pagination.');
}

$pagination = new Collection();
$totalPages = (int) ceil($this->totalItems / $this->itemsPerPage);
$totalPages = $this->getTotalPages();
$currentPage = $this->currentPage;

$pagesBefore = $this->paginationRange->
Expand Down
10 changes: 8 additions & 2 deletions Classes/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,23 @@ public function indexAction(array $searchParams = []): ResponseInterface
$currentPage = 1;
}

// $totalItems = $this->elasticSearchService->count($searchParams, $this->settings);
// $totalItems = $this->elasticSearchService->count($searchParams, $this->settings);
//$totalItems = 100;

$elasticResponse = $this->elasticSearchService->search($searchParams, $this->settings);
$pagination = Paginator::createPagination($currentPage, $elasticResponse['hits']['total']['value'], $this->extConf);
$paginator = (new Paginator())->
setPage($currentPage)->
setTotalItems($elasticResponse['hits']['total']['value'])->
setExtensionConfiguration($this->extConf);
$pagination = $paginator->getPagination();
$showPagination = $paginator->getTotalPages() > 1 ? true : false;

$this->view->assign('locale', $locale);
$this->view->assign('totalItems', $elasticResponse['hits']['total']['value']);
$this->view->assign('searchParams', $searchParams);
$this->view->assign('searchResults', $elasticResponse);
$this->view->assign('pagination', $pagination);
$this->view->assign('showPagination', $showPagination);
// $this->view->assign('totalItems', $totalItems);
$this->view->assign('currentString', Paginator::CURRENT_PAGE);
$this->view->assign('dots', Paginator::DOTS);
Expand Down
4 changes: 3 additions & 1 deletion Resources/Private/Templates/Search/Index.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ <h3 >
</div>
</article>
</f:for>
<f:render partial="Pagination" arguments="{pagination: pagination, searchParams: searchParams, currentString: currentString, dots: dots}" />
<f:if condition="{showPagination}">
<f:render partial="Pagination" arguments="{pagination: pagination, searchParams: searchParams, currentString: currentString, dots: dots}" />
</f:if>
</div>
</div>

0 comments on commit b6dc2b7

Please sign in to comment.