[TASK] Allow multiple extractor services with the same priority
[Packages/TYPO3.CMS.git] / typo3 / sysext / recycler / Classes / Controller / RecyclerModuleController.php
1 <?php
2 namespace TYPO3\CMS\Recycler\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Template\Components\ButtonBar;
20 use TYPO3\CMS\Backend\Template\ModuleTemplate;
21 use TYPO3\CMS\Backend\Utility\BackendUtility;
22 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
23 use TYPO3\CMS\Core\Http\HtmlResponse;
24 use TYPO3\CMS\Core\Http\NormalizedParams;
25 use TYPO3\CMS\Core\Imaging\Icon;
26 use TYPO3\CMS\Core\Localization\LanguageService;
27 use TYPO3\CMS\Core\Type\Bitmask\Permission;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29 use TYPO3\CMS\Core\Utility\MathUtility;
30 use TYPO3\CMS\Fluid\View\StandaloneView;
31
32 /**
33 * Backend Module for the 'recycler' extension.
34 * @internal This class is a specific Backend controller implementation and is not considered part of the Public TYPO3 API.
35 */
36 class RecyclerModuleController
37 {
38
39 /**
40 * @var array
41 */
42 protected $pageRecord = [];
43
44 /**
45 * @var bool
46 */
47 protected $isAccessibleForCurrentUser = false;
48
49 /**
50 * @var bool
51 */
52 protected $allowDelete = false;
53
54 /**
55 * @var int
56 */
57 protected $recordsPageLimit = 50;
58
59 /**
60 * @var int
61 */
62 protected $id;
63
64 /**
65 * @var StandaloneView
66 */
67 protected $view;
68
69 /**
70 * @var ModuleTemplate
71 */
72 protected $moduleTemplate;
73
74 /**
75 * Injects the request object for the current request, and renders correct action
76 *
77 * @param ServerRequestInterface $request the current request
78 * @return ResponseInterface the response with the content
79 */
80 public function handleRequest(ServerRequestInterface $request): ResponseInterface
81 {
82 $this->id = (int)($request->getQueryParams()['id'] ?? $request->getParsedBody()['id'] ?? 0);
83 $backendUser = $this->getBackendUser();
84 $this->pageRecord = BackendUtility::readPageAccess($this->id, $backendUser->getPagePermsClause(Permission::PAGE_SHOW));
85 $this->isAccessibleForCurrentUser = $this->id && is_array($this->pageRecord) || !$this->id && $this->getBackendUser()->isAdmin();
86 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
87
88 // don't access in workspace
89 if ($backendUser->workspace !== 0) {
90 $this->isAccessibleForCurrentUser = false;
91 }
92
93 // read configuration
94 if ($backendUser->isAdmin()) {
95 $this->allowDelete = true;
96 } else {
97 $this->allowDelete = (bool)($backendUser->getTSConfig()['mod.']['recycler.']['allowDelete'] ?? false);
98 }
99
100 $this->recordsPageLimit = MathUtility::forceIntegerInRange(
101 (int)($backendUser->getTSConfig()['mod.']['recycler.']['recordsPageLimit'] ?? 25),
102 1
103 );
104
105 $action = 'index';
106 $this->initializeView($action);
107
108 $result = call_user_func_array([$this, $action . 'Action'], [$request]);
109 if ($result instanceof ResponseInterface) {
110 return $result;
111 }
112
113 $this->registerDocheaderButtons();
114
115 $this->moduleTemplate->setContent($this->view->render());
116 return new HtmlResponse($this->moduleTemplate->renderContent());
117 }
118
119 /**
120 * @param string $templateName
121 */
122 protected function initializeView(string $templateName)
123 {
124 $this->view = GeneralUtility::makeInstance(StandaloneView::class);
125 $this->view->setTemplate($templateName);
126 $this->view->setTemplateRootPaths(['EXT:recycler/Resources/Private/Templates/RecyclerModule']);
127 $this->view->setPartialRootPaths(['EXT:recycler/Resources/Private/Partials']);
128 $this->view->setLayoutRootPaths(['EXT:recycler/Resources/Private/Layouts']);
129 $this->view->getRequest()->setControllerExtensionName('Recycler');
130 }
131
132 /**
133 * Renders the content of the module.
134 *
135 * @param ServerRequestInterface $request
136 */
137 public function indexAction(ServerRequestInterface $request)
138 {
139 $this->moduleTemplate->getPageRenderer()->addInlineSettingArray('Recycler', $this->getJavaScriptConfiguration($request->getAttribute('normalizedParams')));
140 $this->moduleTemplate->getPageRenderer()->addInlineLanguageLabelFile('EXT:recycler/Resources/Private/Language/locallang.xlf');
141 if ($this->isAccessibleForCurrentUser) {
142 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($this->pageRecord);
143 }
144
145 $this->view->assign('allowDelete', $this->allowDelete);
146 }
147
148 /**
149 * Registers the Icons into the docheader
150 *
151 * @throws \InvalidArgumentException
152 */
153 protected function registerDocheaderButtons()
154 {
155 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
156
157 $shortcutButton = $buttonBar->makeShortcutButton()
158 ->setModuleName('web_recycler')
159 ->setGetVariables(['id', 'route']);
160 $buttonBar->addButton($shortcutButton);
161
162 $reloadButton = $buttonBar->makeLinkButton()
163 ->setHref('#')
164 ->setDataAttributes(['action' => 'reload'])
165 ->setTitle($this->getLanguageService()->sL('LLL:EXT:recycler/Resources/Private/Language/locallang.xlf:button.reload'))
166 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-refresh', Icon::SIZE_SMALL));
167 $buttonBar->addButton($reloadButton, ButtonBar::BUTTON_POSITION_RIGHT);
168 }
169
170 /**
171 * Gets the JavaScript configuration.
172 *
173 * @param NormalizedParams $normalizedParams
174 * @return array The JavaScript configuration
175 */
176 protected function getJavaScriptConfiguration(NormalizedParams $normalizedParams): array
177 {
178 return [
179 'pagingSize' => $this->recordsPageLimit,
180 'showDepthMenu' => true,
181 'startUid' => $this->id,
182 'isSSL' => $normalizedParams->isHttps(),
183 'deleteDisable' => !$this->allowDelete,
184 'depthSelection' => $this->getDataFromSession('depthSelection', 0),
185 'tableSelection' => $this->getDataFromSession('tableSelection', ''),
186 'States' => $this->getBackendUser()->uc['moduleData']['web_recycler']['States']
187 ];
188 }
189
190 /**
191 * Gets data from the session of the current backend user.
192 *
193 * @param string $identifier The identifier to be used to get the data
194 * @param string $default The default date to be used if nothing was found in the session
195 * @return string The accordant data in the session of the current backend user
196 */
197 protected function getDataFromSession($identifier, $default = null)
198 {
199 $sessionData = &$this->getBackendUser()->uc['tx_recycler'];
200 if (isset($sessionData[$identifier]) && $sessionData[$identifier]) {
201 $data = $sessionData[$identifier];
202 } else {
203 $data = $default;
204 }
205 return $data;
206 }
207
208 /**
209 * Returns the current BE user.
210 *
211 * @return BackendUserAuthentication
212 */
213 protected function getBackendUser(): BackendUserAuthentication
214 {
215 return $GLOBALS['BE_USER'];
216 }
217
218 /**
219 * Returns an instance of LanguageService
220 *
221 * @return LanguageService
222 */
223 protected function getLanguageService(): LanguageService
224 {
225 return $GLOBALS['LANG'];
226 }
227 }