33
44namespace 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 ;
88use 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 ;
1111use TYPO3 \CMS \Core \Utility \GeneralUtility ;
1212use TYPO3 \CMS \Fluid \View \StandaloneView ;
13- use TYPO3 \CMS \Redirects \Repository \Demand ;
14- use TYPO3 \CMS \Redirects \Repository \RedirectRepository ;
1513
1614class 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}
0 commit comments