Skip to content
Snippets Groups Projects
Commit 1fc3c448 authored by Kevin Ditscheid's avatar Kevin Ditscheid
Browse files

[TASK] Remove the pagebrowser controller and let the view helper handle stuff

parent 39b41956
No related branches found
No related tags found
1 merge request!30[TASK] Remove the pagebrowser controller and let the view helper handle stuff
This commit is part of merge request !30. Comments created here will be created in the context of that merge request.
<?php
namespace SGalinski\SgJobs\Controller;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (https://www.sgalinski.de)
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
/**
* Controller that handles the page browser.
*/
class PageBrowserController extends ActionController {
/**
* @var string
*/
protected $pageParameterName = '';
/**
* @var int
*/
protected $numberOfPages = 0;
/**
* @var int
*/
protected $currentPage = 0;
/**
* @var int
*/
protected $pagesBefore = 0;
/**
* @var int
*/
protected $pagesAfter = 0;
/**
* @var bool
*/
protected $enableMorePages = FALSE;
/**
* @var bool
*/
protected $enableLessPages = FALSE;
/**
* Renders the index view.
*
* @param int $currentPage
* @return void
*/
public function indexAction($currentPage = 0): void {
$this->currentPage = (int) $currentPage;
$this->initializeConfiguration();
$this->addDataToView();
}
/**
* Initializes the configuration.
*
* @return void
*/
protected function initializeConfiguration(): void {
$this->pageParameterName = 'tx_sgjobs_pagebrowser[currentPage]';
$this->numberOfPages = (int) $this->settings['numberOfPages'];
$this->pagesBefore = (int) $this->settings['pagesBefore'];
$this->pagesAfter = (int) $this->settings['pagesAfter'];
$this->enableMorePages = (bool) $this->settings['enableMorePages'];
$this->enableLessPages = (bool) $this->settings['enableLessPages'];
}
/**
* Adds the necessary data to the view.
*
* @return void
*/
protected function addDataToView(): void {
if ($this->numberOfPages <= 1) {
$this->view = NULL;
return;
}
$pageLinks = [];
$start = max($this->currentPage - $this->pagesBefore, 0);
$end = min($this->numberOfPages, $this->currentPage + $this->pagesAfter + 1);
for ($i = $start; $i < $end; $i++) {
$pageLinks[] = [
'number' => $i + 1,
'link' => $this->getPageLink($i),
'isCurrentPage' => $i === $this->currentPage,
];
}
$this->view->assignMultiple(
[
'enableLessPages' => $this->enableLessPages,
'enableMorePages' => $this->enableMorePages,
'previousLink' => $this->getPageLink($this->currentPage - 1),
'nextLink' => $this->getPageLink($this->currentPage + 1),
'enableLessPagesLink' => $this->getPageLink($this->currentPage - $this->pagesBefore - 1),
'enableMorePagesLink' => $this->getPageLink($this->currentPage + $this->pagesAfter + 1),
'pageLinks' => $pageLinks,
'prevPageExist' => $this->currentPage > 0,
'showLessPages' => ($this->currentPage - $this->pagesBefore) > 0,
'showNextPages' => ($this->currentPage + $this->pagesAfter + 1) < $this->numberOfPages,
'nextPageExist' => $this->currentPage < ($this->numberOfPages - 1),
]
);
}
/**
* Generates page link. Keeps all current URL parameters except for cHash and tx_pagebrowse_pi1[page].
*
* @param int $page Page number starting from 1
* @return string
*/
protected function getPageLink($page): string {
return $this->uriBuilder->reset()->setAddQueryString(TRUE)
->uriFor('index', ['currentPage' => $page,]);
}
}
......@@ -26,8 +26,10 @@ namespace SGalinski\SgJobs\ViewHelpers;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
......@@ -55,13 +57,44 @@ class PageBrowserViewHelper extends AbstractViewHelper {
* @throws \UnexpectedValueException
*/
public function render(): string {
$configuration = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_sgjobs.']['pagebrowser.'];
$configuration['settings.']['numberOfPages'] = (int) $this->arguments['numberOfPages'];
$typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
$configuration = $typoScriptService->convertTypoScriptArrayToPlainArray($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_sgjobs.']);
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setRenderingContext($this->renderingContext);
$view->setTemplate('PageBrowser/Index');
$view->setTemplateRootPaths($configuration['view']['templateRootPaths']);
$view->setPartialRootPaths($configuration['view']['partialRootPaths']);
$view->setLayoutRootPaths($configuration['view']['layoutRootPaths']);
$pageBrowserVars = GeneralUtility::_GP('tx_sgnews_pagebrowser');
if (isset($pageBrowserVars['currentPage']) && (int) $pageBrowserVars['currentPage'] > 0) {
$currentPage = (int) $pageBrowserVars['currentPage'];
} else {
$currentPage = 1;
}
/** @var ContentObjectRenderer $contentObject */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$contentObject = $objectManager->get(ContentObjectRenderer::class);
$contentObject->start([]);
return $contentObject->cObjGetSingle('USER', $configuration);
$pageLinks = [];
$start = \max($currentPage - 1, 1);
$end = \min($this->arguments['numberOfPages'], $currentPage + 2);
for ($i = $start; $i < $end; $i++) {
$pageLinks[] = [
'number' => $i,
'isCurrentPage' => $i === $currentPage,
];
}
$view->assignMultiple(
[
'enableLessPages' => 1,
'enableMorePages' => 1,
'pageLinks' => $pageLinks,
'currentPage' => $currentPage,
'prevPageExist' => $currentPage > 1,
'showLessPages' => ($currentPage - 1) > 1,
'showNextPages' => ($currentPage + 2) < $this->arguments['numberOfPages'],
'nextPageExist' => $currentPage < $this->arguments['numberOfPages'] - 1,
'numberOfPages' => $this->arguments['numberOfPages']
]
);
return $view->render();
}
}
plugin.tx_sgjobs {
view {
# cat=plugin.tx_tx_extensionskeleton/file; type=string; label=Path to template root (FE)
templateRootPath = EXT:tx_sgjobs/Resources/Private/Templates/
templateRootPath = EXT:sg_jobs/Resources/Private/Templates/
# cat=plugin.tx_tx_extensionskeleton/file; type=string; label=Path to template partials (FE)
partialRootPath = EXT:tx_sgjobs/Resources/Private/Partials/
partialRootPath = EXT:sg_jobs/Resources/Private/Partials/
# cat=plugin.tx_tx_extensionskeleton/file; type=string; label=Path to template layouts (FE)
layoutRootPath = EXT:tx_sgjobs/Resources/Private/Layouts/
layoutRootPath = EXT:sg_jobs/Resources/Private/Layouts/
}
settings {
......
......@@ -2,17 +2,14 @@ plugin.tx_sgjobs {
view {
templateRootPaths {
0 = {$plugin.tx_sgjobs.view.templateRootPath}
1 =
}
partialRootPaths {
0 = {$plugin.tx_sgjobs.view.partialRootPath}
1 =
}
layoutRootPaths {
0 = {$plugin.tx_sgjobs.view.layoutRootPath}
1 =
}
}
......@@ -37,41 +34,6 @@ plugin.tx_sgjobs {
legacy {
enableLegacyFlashMessageHandling = 0
}
pagebrowser = USER
pagebrowser {
userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
extensionName = SgJobs
pluginName = PageBrowser
vendorName = SGalinski
controller = PageBrowser
action = index
view < plugin.tx_sgjobs.view
persistence < plugin.tx_sgjobs.persistence
features < plugin.tx_sgjobs.features
# cHash can't be generated for pageBrowser call; will cause a 404 error if not set to 0
features.requireCHashArgumentForActionArguments = 0
legacy < plugin.tx_sgjobs.legacy
settings {
# Number of page links to show before the current page
pagesBefore = {$plugin.tx_sgjobs.pagebrowser.settings.pagesBefore}
# Number of page links to show before the current page
pagesAfter = {$plugin.tx_sgjobs.pagebrowser.settings.pagesAfter}
# Enables section for "more" pages. This section is shown after links to next pages, normally like three dots (1 2 3 ...). Notice that you can also hide it by emptying corresponding template section.
enableMorePages = {$plugin.tx_sgjobs.pagebrowser.settings.enableMorePages}
# Enables section for "less" pages. This section is shown after links to next pages, normally like three dots (... 1 2 3) Notice that you can also hide it by emptying corresponding template section.
enableLessPages = {$plugin.tx_sgjobs.pagebrowser.settings.enableLessPages}
# The number of the pages
numberOfPages = 0
# The current page
currentPage = 0
}
}
}
config.recordLinks {
......
......@@ -8,9 +8,11 @@
<f:if condition="{prevPageExist}">
<f:then>
<li class="tx-pagebrowse-prev">
<a href="{previousLink}" aria-label="Previous">
<f:variable name="prevPage" value="{currentPage - 1}" />
<f:link.action additionalParams="{tx_sgnews_pagebrowser: {currentPage: prevPage}}"
additionalAttributes="{aria-label: 'Previous'}">
&laquo;
</a>
</f:link.action>
</li>
</f:then>
<f:else>
......@@ -26,9 +28,10 @@
<f:if condition="{enableLessPages} && {showLessPages}">
<li>
<a href="{enableLessPagesLink}">
<f:variable name="lessPage" value="{currentPage - 2}" />
<f:link.action additionalParams="{tx_sgnews_pagebrowser: {currentPage: lessPage}}">
...
</a>
</f:link.action>
</li>
</f:if>
......@@ -46,9 +49,9 @@
</f:then>
<f:else>
<li class="tx-pagebrowse-page">
<a href="{pageLink.link}">
<f:link.action additionalParams="{tx_sgnews_pagebrowser: {currentPage: pageLink.number}}">
{pageLink.number}
</a>
</f:link.action>
</li>
</f:else>
</f:if>
......@@ -56,18 +59,21 @@
<f:if condition="{enableMorePages} && {showNextPages}">
<li>
<a href="{enableMorePagesLink}">
<f:variable name="morePage" value="{currentPage + 2}" />
<f:link.action additionalParams="{tx_sgnews_pagebrowser: {currentPage: morePage}}">
...
</a>
</f:link.action>
</li>
</f:if>
<f:if condition="{nextPageExist}">
<f:then>
<li class="tx-pagebrowse-next">
<a href="{nextLink}" aria-label="Next">
<f:variable name="nextPage" value="{currentPage + 1}" />
<f:link.action additionalParams="{tx_sgnews_pagebrowser: {currentPage: nextPage}}"
additionalAttributes="{aria-label: 'Next'}">
&raquo;
</a>
</f:link.action>
</li>
</f:then>
<f:else>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment