Skip to content

Commit 75c5315

Browse files
committed
[TASK] Add pagination and update redirect demand query
1 parent 4c4c68f commit 75c5315

13 files changed

Lines changed: 329 additions & 95 deletions

File tree

Classes/Form/Element/RedirectElement.php

Lines changed: 33 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -3,114 +3,45 @@
33

44
namespace Ayacoo\RedirectTab\Form\Element;
55

6-
use Ayacoo\RedirectTab\Event\ModifyRedirectsEvent;
7-
use Psr\EventDispatcher\EventDispatcherInterface;
6+
use Ayacoo\RedirectTab\Service\RedirectDemandService;
7+
use Psr\Http\Message\UriInterface;
88
use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
9-
use TYPO3\CMS\Core\Site\Entity\NullSite;
10-
use TYPO3\CMS\Core\Site\Entity\Site;
9+
use TYPO3\CMS\Backend\Routing\UriBuilder;
10+
use TYPO3\CMS\Core\Http\Uri;
1111
use TYPO3\CMS\Core\Utility\GeneralUtility;
1212
use TYPO3\CMS\Fluid\View\StandaloneView;
13-
use TYPO3\CMS\Redirects\Repository\Demand;
14-
use TYPO3\CMS\Redirects\Repository\RedirectRepository;
1513

1614
class RedirectElement extends AbstractFormElement
1715
{
1816
private StandaloneView $view;
1917

20-
private RedirectRepository $redirectRepository;
21-
2218
public function render(): array
2319
{
24-
$this->redirectRepository = GeneralUtility::makeInstance(RedirectRepository::class);
25-
$eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class);
20+
/** @var RedirectDemandService $redirectDemandService */
21+
$redirectDemandService = GeneralUtility::makeInstance(RedirectDemandService::class);
22+
$redirectDemandService->setData($this->data ?? []);
23+
24+
$request = $GLOBALS['TYPO3_REQUEST'];
25+
$currentPage = (int)($request->getQueryParams()['page'] ?? $request->getParsedBody()['page'] ?? 1);
26+
if ($currentPage < 1) {
27+
$currentPage = 1;
28+
}
2629

2730
$this->prepareView();
28-
list($demand, $redirects) = $this->getRedirects();
29-
30-
$event = $eventDispatcher->dispatch(new ModifyRedirectsEvent($redirects));
31-
$redirects = $event->getRedirects();
32-
33-
$backendUriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
34-
$uriParameters = ['edit' => ['pages' => [$this->data['effectivePid'] => 'edit']]];
35-
$returnUrl = $backendUriBuilder->buildUriFromRoute('record_edit', $uriParameters);
3631

3732
$this->view->assignMultiple([
38-
'redirects' => $redirects,
39-
'demand' => $demand,
40-
'pagination' => $this->preparePagination($demand),
41-
'returnUrl' => $returnUrl
33+
'redirects' => $redirectDemandService->getRedirects($currentPage),
34+
'demand' => $redirectDemandService->getDemand(),
35+
'pagination' => $redirectDemandService->preparePagination($redirectDemandService->getDemand()),
36+
'returnUrl' => $this->buildRedirectUrl($currentPage),
37+
'recordUid' => (int) $this->data['effectivePid'],
4238
]);
4339

4440
$result = $this->initializeResultArray();
4541
$result['html'] = $this->view->render();
4642
return $result;
4743
}
4844

49-
/**
50-
* @return array
51-
*/
52-
protected function getRedirects(): array
53-
{
54-
$demand = null;
55-
$redirects = [];
56-
57-
/** @var Site $site */
58-
$site = $this->data['site'];
59-
if (!$site instanceof NullSite) {
60-
$languageUid = (int)$this->data['databaseRow']['sys_language_uid'];
61-
$language = $site->getLanguageById($languageUid);
62-
$host = $language->getBase()->getHost();
63-
64-
$demand = new Demand(
65-
$site->getRootPageId(),
66-
'source_host',
67-
'asc',
68-
['*', $host],
69-
'',
70-
't3://page?uid=' . $this->data['effectivePid'],
71-
[],
72-
0,
73-
null
74-
);
75-
$redirects = $this->redirectRepository->findRedirectsByDemand($demand);
76-
}
77-
return [$demand, $redirects];
78-
}
79-
80-
/**
81-
* Prepares information for the pagination of the module
82-
* @param Demand|null $demand
83-
* @return array
84-
*/
85-
protected function preparePagination(Demand $demand = null): array
86-
{
87-
$pagination = [];
88-
if ($demand) {
89-
$count = $this->redirectRepository->countRedirectsByByDemand($demand);
90-
$numberOfPages = ceil($count / $demand->getLimit());
91-
$endRecord = $demand->getOffset() + $demand->getLimit();
92-
if ($endRecord > $count) {
93-
$endRecord = $count;
94-
}
95-
96-
$pagination = [
97-
'current' => $demand->getPage(),
98-
'numberOfPages' => $numberOfPages,
99-
'hasLessPages' => $demand->getPage() > 1,
100-
'hasMorePages' => $demand->getPage() < $numberOfPages,
101-
'startRecord' => $demand->getOffset() + 1,
102-
'endRecord' => $endRecord
103-
];
104-
if ($pagination['current'] < $pagination['numberOfPages']) {
105-
$pagination['nextPage'] = $pagination['current'] + 1;
106-
}
107-
if ($pagination['current'] > 1) {
108-
$pagination['previousPage'] = $pagination['current'] - 1;
109-
}
110-
}
111-
return $pagination;
112-
}
113-
11445
protected function prepareView(): void
11546
{
11647
$templateName = 'List';
@@ -120,4 +51,19 @@ protected function prepareView(): void
12051
$this->view->setPartialRootPaths(['EXT:redirect_tab/Resources/Private/Partials/Backend']);
12152
$this->view->setLayoutRootPaths(['EXT:redirect_tab/Resources/Private/Layouts']);
12253
}
54+
55+
protected function buildRedirectUrl(int $currentPage): UriInterface|Uri
56+
{
57+
$backendUriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
58+
$uriParameters = [
59+
'edit' => [
60+
'pages' => [
61+
$this->data['effectivePid'] => 'edit'
62+
]
63+
],
64+
'page' => $currentPage
65+
];
66+
67+
return $backendUriBuilder->buildUriFromRoute('record_edit', $uriParameters);
68+
}
12369
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Ayacoo\RedirectTab\Service;
5+
6+
use Ayacoo\RedirectTab\Event\ModifyRedirectsEvent;
7+
use Psr\EventDispatcher\EventDispatcherInterface;
8+
use TYPO3\CMS\Core\Site\Entity\NullSite;
9+
use TYPO3\CMS\Core\Site\Entity\Site;
10+
use TYPO3\CMS\Core\Utility\GeneralUtility;
11+
use TYPO3\CMS\Redirects\Repository\Demand;
12+
use TYPO3\CMS\Redirects\Repository\RedirectRepository;
13+
14+
class RedirectDemandService
15+
{
16+
protected array $data = [];
17+
18+
protected Demand $demand;
19+
20+
private RedirectRepository $redirectRepository;
21+
private EventDispatcherInterface $eventDispatcher;
22+
23+
public function __construct()
24+
{
25+
$this->redirectRepository = GeneralUtility::makeInstance(RedirectRepository::class);
26+
$this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class);
27+
}
28+
29+
public function getData(): array
30+
{
31+
return $this->data;
32+
}
33+
34+
public function setData(array $data): void
35+
{
36+
$this->data = $data;
37+
}
38+
39+
public function getDemand(): Demand
40+
{
41+
return $this->demand;
42+
}
43+
44+
public function setDemand(Demand $demand): void
45+
{
46+
$this->demand = $demand;
47+
}
48+
49+
/**
50+
* @param int $page
51+
* @return array
52+
*/
53+
public function getRedirects(int $page = 1): array
54+
{
55+
$redirects = [];
56+
57+
/** @var Site $site */
58+
$site = $this->data['site'] ?? null;
59+
if (!$site instanceof NullSite) {
60+
$languageUid = (int)$this->data['databaseRow']['sys_language_uid'];
61+
$language = $site->getLanguageById($languageUid);
62+
$host = $language->getBase()->getHost();
63+
64+
$this->demand = new Demand(
65+
$page,
66+
'source_host',
67+
'asc',
68+
['*', $host],
69+
'',
70+
't3://page?uid=' . $this->data['effectivePid'],
71+
[],
72+
0,
73+
null
74+
);
75+
$redirects = $this->redirectRepository->findRedirectsByDemand($this->demand);
76+
}
77+
78+
$event = $this->eventDispatcher->dispatch(new ModifyRedirectsEvent($redirects));
79+
return $event->getRedirects();
80+
}
81+
82+
public function preparePagination(Demand $demand = null): array
83+
{
84+
$pagination = [];
85+
if ($demand) {
86+
$count = $this->redirectRepository->countRedirectsByByDemand($demand);
87+
$numberOfPages = ceil($count / $demand->getLimit());
88+
$endRecord = $demand->getOffset() + $demand->getLimit();
89+
if ($endRecord > $count) {
90+
$endRecord = $count;
91+
}
92+
93+
$pagination = [
94+
'current' => $demand->getPage(),
95+
'numberOfPages' => $numberOfPages,
96+
'hasLessPages' => $demand->getPage() > 1,
97+
'hasMorePages' => $demand->getPage() < $numberOfPages,
98+
'startRecord' => $demand->getOffset() + 1,
99+
'endRecord' => $endRecord
100+
];
101+
if ($pagination['current'] < $pagination['numberOfPages']) {
102+
$pagination['nextPage'] = $pagination['current'] + 1;
103+
}
104+
if ($pagination['current'] > 1) {
105+
$pagination['previousPage'] = $pagination['current'] - 1;
106+
}
107+
}
108+
return $pagination;
109+
}
110+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Ayacoo\RedirectTab\ViewHelpers;
4+
5+
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
6+
7+
/**
8+
* Unfortunately, the be.editRecord ViewHelper is not sufficient. Therefore, a custom ViewHelper that also provides the
9+
* page parameter for pagination in addition to the edit record.
10+
*/
11+
class RouteParametersViewHelper extends AbstractViewHelper
12+
{
13+
public function initializeArguments(): void
14+
{
15+
parent::initializeArguments();
16+
$this->registerArgument('recordUid', 'integer', 'Record Uid', true, 0);
17+
$this->registerArgument('page', 'integer', 'Pagination Page', true, 0);
18+
}
19+
20+
public function render(): array
21+
{
22+
return [
23+
'edit' => [
24+
'pages' => [
25+
$this->arguments['recordUid'] => 'edit'
26+
]
27+
],
28+
'page' => $this->arguments['page']
29+
];
30+
}
31+
}

Configuration/TCA/Overrides/pages.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// Add additional tab for page properties
2828
ExtensionManagementUtility::addToAllTCAtypes(
2929
$table,
30-
'--div--;LLL:EXT:' . $extKey . '/Resources/Private/Language/locallang.xlf:tab,' . implode(',', array_keys($temporaryColumns)),
30+
'--div--;LLL:EXT:redirects/Resources/Private/Language/locallang_module_redirect.xlf:mlang_tabs_tab,' . implode(',', array_keys($temporaryColumns)),
3131
'',
3232
''
3333
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
2+
<xliff version="1.0">
3+
<file source-language="en" target-language="de" datatype="plaintext" original="messages" date="2023-11-18T14:44:00Z" product-name="redirect_tab">
4+
<header/>
5+
<body>
6+
<trans-unit id="redirect_list" xml:space="preserve">
7+
<source>List of linked redirects for this page</source>
8+
<target>Liste der verlinkten Weiterleitungen für diese Seite</target>
9+
</trans-unit>
10+
</body>
11+
</file>
12+
</xliff>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
2+
<xliff version="1.0">
3+
<file source-language="en" target-language="es" datatype="plaintext" original="messages" date="2023-11-18T14:44:00Z" product-name="redirect_tab">
4+
<header/>
5+
<body>
6+
<trans-unit id="redirect_list" xml:space="preserve">
7+
<source>List of linked redirects for this page</source>
8+
<target>Lista de redirecciones enlazadas para esta página</target>
9+
</trans-unit>
10+
</body>
11+
</file>
12+
</xliff>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
2+
<xliff version="1.0">
3+
<file source-language="en" target-language="fr" datatype="plaintext" original="messages" date="2023-11-18T14:44:00Z" product-name="redirect_tab">
4+
<header/>
5+
<body>
6+
<trans-unit id="redirect_list" xml:space="preserve">
7+
<source>List of linked redirects for this page</source>
8+
<target>Liste des redirections liées à cette page</target>
9+
</trans-unit>
10+
</body>
11+
</file>
12+
</xliff>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
2+
<xliff version="1.0">
3+
<file source-language="en" target-language="it" datatype="plaintext" original="messages" date="2023-11-18T14:44:00Z" product-name="redirect_tab">
4+
<header/>
5+
<body>
6+
<trans-unit id="redirect_list" xml:space="preserve">
7+
<source>List of linked redirects for this page</source>
8+
<target>Elenco dei reindirizzamenti collegati a questa pagina</target>
9+
</trans-unit>
10+
</body>
11+
</file>
12+
</xliff>

Resources/Private/Language/locallang.xlf

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
<file source-language="en" datatype="plaintext" original="messages" date="2021-04-17T11:47:18Z" product-name="redirect_tab">
44
<header/>
55
<body>
6-
<trans-unit id="tab" xml:space="preserve">
7-
<source>Redirects</source>
8-
</trans-unit>
96
<trans-unit id="redirect_list" xml:space="preserve">
107
<source>List of linked redirects for this page</source>
118
</trans-unit>

0 commit comments

Comments
 (0)