Skip to content

Commit

Permalink
poc - pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
susannemoog committed May 9, 2021
1 parent 1cd8f26 commit bf05fd7
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 6 deletions.
16 changes: 15 additions & 1 deletion Classes/Controller/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
use T3G\AgencyPack\Blog\Service\CacheService;
use T3G\AgencyPack\Blog\Service\MetaTagService;
use T3G\AgencyPack\Blog\Utility\ArchiveUtility;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Exception;
use TYPO3\CMS\Core\Pagination\SimplePagination;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
use TYPO3\CMS\Extbase\Pagination\QueryResultPaginator;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
Expand Down Expand Up @@ -133,6 +136,7 @@ protected function initializeView(ViewInterface $view): void
$feedData = [
'title' => LocalizationUtility::translate('feed.title' . $action, 'blog', $arguments),
'description' => LocalizationUtility::translate('feed.description' . $action, 'blog', $arguments),
// @todo use request language instead
'language' => $this->getTypoScriptFontendController()->sys_language_isocode,
'link' => $this->uriBuilder->getRequest()->getRequestUri(),
'date' => date('r'),
Expand All @@ -147,18 +151,28 @@ protected function initializeView(ViewInterface $view): void
/**
* Show a list of recent posts.
*
* @param int $currentPage
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function listRecentPostsAction(): ResponseInterface
public function listRecentPostsAction(int $currentPage = 1): ResponseInterface
{
$maximumItems = (int) ($this->settings['lists']['posts']['maximumDisplayedItems'] ?? 0);
$itemsPerPage = (int) ($this->settings['lists']['pagination']['itemsPerPage'] ?? 10);
$posts = (0 === $maximumItems)
? $this->postRepository->findAll()
: $this->postRepository->findAllWithLimit($maximumItems);

$paginator = null;
$pagination = null;
if (count($posts) > $itemsPerPage) {
$paginator = new QueryResultPaginator($posts, $currentPage, $itemsPerPage);
$pagination = new SimplePagination($paginator);
}
$this->view->assign('type', 'recent');
$this->view->assign('posts', $posts);
$this->view->assign('pagination', $pagination);
$this->view->assign('paginator', $paginator);
return $this->htmlResponse();
}

Expand Down
12 changes: 7 additions & 5 deletions Resources/Private/Partials/List.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div class="postlist{f:if(condition: type, then: ' postlist--{type}')}">
<f:if condition="{posts}">
<f:then>
<f:if condition="{posts->f:count()} > {settings.lists.pagination.itemsPerPage}">
<f:if condition="{paginator}">
<f:then>
<f:render section="PaginatePosts" arguments="{posts: posts, pagination: settings.lists.pagination}" />
<f:render section="PaginatePosts" arguments="{_all}" />
</f:then>
<f:else>
<f:render section="ListPosts" arguments="{posts: posts}" />
Expand All @@ -18,9 +18,11 @@
</f:if>
</div>
<f:section name="PaginatePosts">
<f:widget.paginate objects="{posts}" as="paginatedPosts" configuration="{itemsPerPage: pagination.itemsPerPage, insertAbove: pagination.insertAbove, insertBelow: pagination.insertBelow, maximumNumberOfLinks: pagination.maximumNumberOfLinks, addQueryStringMethod: 'GET'}">
<f:render section="ListPosts" arguments="{posts: paginatedPosts}" />
</f:widget.paginate>
<f:for each="{paginator.paginatedItems}" iteration="iterator" as="post">
<blogvh:cache post="{post}" />
<f:render partial="List/Post" arguments="{_all}" />
</f:for>
<f:render partial="Utility/Paginator.html" arguments="{pagination: pagination, pages: pages, paginator: paginator}" />
</f:section>
<f:section name="ListPosts">
<f:for each="{posts}" as="post">
Expand Down
89 changes: 89 additions & 0 deletions Resources/Private/Partials/Utility/Paginator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<nav class="pagination-wrap">
<ul class="pagination">
<f:if condition="{pagination.previousPageNumber} && {pagination.previousPageNumber} >= {pagination.firstPageNumber}">
<f:then>
<li class="page-item">
<a href="{f:uri.action(arguments:{currentPage: 1})}" title="{f:translate(key:'pagination.first')}" class="page-link">
<core:icon identifier="actions-view-paging-first" />
</a>
</li>
<li class="page-item">
<a href="{f:uri.action(arguments:{currentPage: pagination.previousPageNumber})}" title="{f:translate(key:'pagination.previous')}" class="page-link">
<core:icon identifier="actions-view-paging-previous" />
</a>
</li>
</f:then>
<f:else>
<li class="page-item disabled">
<span class="page-link">
<core:icon identifier="actions-view-paging-first" />
</span>
</li>
<li class="page-item disabled">
<span class="page-link">
<core:icon identifier="actions-view-paging-previous" />
</span>
</li>
</f:else>
</f:if>
<li class="page-item">
<span class="page-link">
<f:translate key="pagination.records" /> {pagination.startRecordNumber} - {pagination.endRecordNumber}
</span>
</li>
<li class="page-item">
<span class="page-link">
<f:translate key="pagination.page" />
<form style="display:inline;"
data-global-event="submit"
data-action-navigate="$form=~s/$value/"
data-navigate-value="{f:uri.action(arguments:'{currentPage: \'$[value]\'}')}"
data-value-selector="input[name='paginator-target-page']">
<input
min="{pagination.firstPageNumber}"
max="{pagination.lastPageNumber}"
data-number-of-pages="{paginator.numberOfPages}"
name="paginator-target-page"
class="form-control form-control-sm paginator-input"
size="5"
value="{paginator.currentPageNumber}"
type="number"
/>
</form>
/ {pagination.lastPageNumber}
</span>
</li>

<f:if condition="{pagination.nextPageNumber} && {pagination.nextPageNumber} <= {pagination.lastPageNumber}">
<f:then>
<li class="page-item">
<a href="{f:uri.action(arguments:{currentPage: pagination.nextPageNumber})}" title="{f:translate(key:'pagination.next')}" class="page-link">
<core:icon identifier="actions-view-paging-next" />
</a>
</li>
<li class="page-item">
<a href="{f:uri.action(arguments:{currentPage: pagination.lastPageNumber})}" title="{f:translate(key:'pagination.last')}" class="page-link">
<core:icon identifier="actions-view-paging-last" />
</a>
</li>
</f:then>
<f:else>
<li class="page-item disabled">
<span class="page-link">
<core:icon identifier="actions-view-paging-next" />
</span>
</li>
<li class="page-item disabled">
<span class="page-link">
<core:icon identifier="actions-view-paging-last" />
</span>
</li>
</f:else>
</f:if>
<li class="page-item">
<a href="{f:uri.action(arguments:{currentPage: paginator.currentPageNumber})}" title="{f:translate(key:'pagination.refresh')}" class="page-link">
<core:icon identifier="actions-refresh" />
</a>
</li>
</ul>
</nav>

0 comments on commit bf05fd7

Please sign in to comment.