ManagementController.php 7.06 KB
Newer Older
1
<?php
2

3
declare(strict_types=1);
4
5
6
7
8
9
10
11
12
13
14
15
16
17

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

18
19
namespace TYPO3\CMS\Redirects\Controller;

20
21
22
23
24
25
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
26
use TYPO3\CMS\Core\Configuration\Features;
27
28
29
30
31
32
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
33
34
use TYPO3\CMS\Redirects\Repository\Demand;
use TYPO3\CMS\Redirects\Repository\RedirectRepository;
35
36
37
38
use TYPO3Fluid\Fluid\View\ViewInterface;

/**
 * Lists all redirects in the TYPO3 Backend as a module
39
 * @internal This class is a specific TYPO3 Backend controller implementation and is not part of the Public TYPO3 API.
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 */
class ManagementController
{
    /**
     * @var ModuleTemplate
     */
    protected $moduleTemplate;

    /**
     * @var ViewInterface
     */
    protected $view;

    /**
     * @var ServerRequestInterface
     */
    protected $request;

    /**
     * @var IconFactory
     */
    protected $iconFactory;

    /**
Helmut Hummel's avatar
Helmut Hummel committed
64
     * @var RedirectRepository
65
     */
Helmut Hummel's avatar
Helmut Hummel committed
66
67
68
69
70
71
72
73
74
75
    protected $redirectRepository;

    public function __construct(
        ModuleTemplate $moduleTemplate,
        IconFactory $iconFactory,
        RedirectRepository $redirectRepository
    ) {
        $this->moduleTemplate = $moduleTemplate;
        $this->iconFactory = $iconFactory;
        $this->redirectRepository = $redirectRepository;
76
        $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Modal');
77
        $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Redirects/RedirectsModule');
78
79
80
81
82
83
84
85
86
        $this->getLanguageService()->includeLLFile('EXT:redirects/Resources/Private/Language/locallang_module_redirect.xlf');
    }

    /**
     * Injects the request object for the current request, and renders the overview of all redirects
     */
    public function handleRequest(ServerRequestInterface $request): ResponseInterface
    {
        $this->request = $request;
87
88
        $this->initializeView('overview');
        $this->overviewAction($request);
89
90
91
92
93
94
95
        $this->moduleTemplate->setContent($this->view->render());
        return new HtmlResponse($this->moduleTemplate->renderContent());
    }

    /**
     * Show all redirects, and add a button to create a new redirect
     */
Helmut Hummel's avatar
Helmut Hummel committed
96
    protected function overviewAction(ServerRequestInterface $request): void
97
98
    {
        $this->getButtons();
Helmut Hummel's avatar
Helmut Hummel committed
99
        $demand = Demand::fromRequest($request);
100
        $this->view->assignMultiple([
Helmut Hummel's avatar
Helmut Hummel committed
101
102
103
            'redirects' => $this->redirectRepository->findRedirectsByDemand($demand),
            'hosts' => $this->redirectRepository->findHostsOfRedirects(),
            'statusCodes' => $this->redirectRepository->findStatusCodesOfRedirects(),
104
105
            'demand' => $demand,
            'showHitCounter' => GeneralUtility::makeInstance(Features::class)->isFeatureEnabled('redirects.hitCount'),
Helmut Hummel's avatar
Helmut Hummel committed
106
            'pagination' => $this->preparePagination($demand),
107
        ]);
108
109
    }

110
111
112
    /**
     * Prepares information for the pagination of the module
     */
Helmut Hummel's avatar
Helmut Hummel committed
113
    protected function preparePagination(Demand $demand): array
114
    {
Helmut Hummel's avatar
Helmut Hummel committed
115
        $count = $this->redirectRepository->countRedirectsByByDemand($demand);
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
        $numberOfPages = ceil($count / $demand->getLimit());
        $endRecord = $demand->getOffset() + $demand->getLimit();
        if ($endRecord > $count) {
            $endRecord = $count;
        }

        $pagination = [
            'current' => $demand->getPage(),
            'numberOfPages' => $numberOfPages,
            'hasLessPages' => $demand->getPage() > 1,
            'hasMorePages' => $demand->getPage() < $numberOfPages,
            'startRecord' => $demand->getOffset() + 1,
            'endRecord' => $endRecord
        ];
        if ($pagination['current'] < $pagination['numberOfPages']) {
            $pagination['nextPage'] = $pagination['current'] + 1;
        }
        if ($pagination['current'] > 1) {
            $pagination['previousPage'] = $pagination['current'] - 1;
        }
        return $pagination;
    }

Helmut Hummel's avatar
Helmut Hummel committed
139
    protected function initializeView(string $templateName): void
140
141
142
143
144
145
146
147
148
149
150
    {
        $this->view = GeneralUtility::makeInstance(StandaloneView::class);
        $this->view->setTemplate($templateName);
        $this->view->setTemplateRootPaths(['EXT:redirects/Resources/Private/Templates/Management']);
        $this->view->setPartialRootPaths(['EXT:redirects/Resources/Private/Partials']);
        $this->view->setLayoutRootPaths(['EXT:redirects/Resources/Private/Layouts']);
    }

    /**
     * Create document header buttons
     */
Helmut Hummel's avatar
Helmut Hummel committed
151
    protected function getButtons(): void
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    {
        /** @var UriBuilder $uriBuilder */
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);

        $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();

        // Create new
        $newRecordButton = $buttonBar->makeLinkButton()
            ->setHref((string)$uriBuilder->buildUriFromRoute(
                'record_edit',
                [
                    'edit' => ['sys_redirect' => ['new'],
                ],
                'returnUrl' => (string)$uriBuilder->buildUriFromRoute('site_redirects'),
            ]
            ))
            ->setTitle($this->getLanguageService()->getLL('redirect_add_text'))
            ->setIcon($this->iconFactory->getIcon('actions-add', Icon::SIZE_SMALL));
        $buttonBar->addButton($newRecordButton, ButtonBar::BUTTON_POSITION_LEFT, 10);

        // Reload
        $reloadButton = $buttonBar->makeLinkButton()
            ->setHref(GeneralUtility::getIndpEnv('REQUEST_URI'))
175
            ->setTitle($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.reload'))
176
177
178
179
            ->setIcon($this->iconFactory->getIcon('actions-refresh', Icon::SIZE_SMALL));
        $buttonBar->addButton($reloadButton, ButtonBar::BUTTON_POSITION_RIGHT);

        // Shortcut
180
        $shortcutButton = $buttonBar->makeShortcutButton()
181
            ->setRouteIdentifier('site_redirects')
182
            ->setDisplayName($this->getLanguageService()->sL('LLL:EXT:redirects/Resources/Private/Language/locallang_module_redirect.xlf:mlang_labels_tablabel'));
183
        $buttonBar->addButton($shortcutButton, ButtonBar::BUTTON_POSITION_RIGHT);
184
185
186
187
188
189
190
191
192
193
194
195
    }

    protected function getLanguageService(): LanguageService
    {
        return $GLOBALS['LANG'];
    }

    protected function getBackendUserAuthentication(): BackendUserAuthentication
    {
        return $GLOBALS['BE_USER'];
    }
}