[TASK] Create own response instance in controller actions
[Packages/TYPO3.CMS.git] / typo3 / sysext / info / Classes / Controller / InfoModuleController.php
1 <?php
2 namespace TYPO3\CMS\Info\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\Module\BaseScriptClass;
20 use TYPO3\CMS\Backend\Template\Components\ButtonBar;
21 use TYPO3\CMS\Backend\Template\ModuleTemplate;
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
23 use TYPO3\CMS\Core\Http\HtmlResponse;
24 use TYPO3\CMS\Core\Imaging\Icon;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Fluid\View\StandaloneView;
27
28 /**
29 * Script Class for the Web > Info module
30 * This class creates the framework to which other extensions can connect their sub-modules
31 */
32 class InfoModuleController extends BaseScriptClass
33 {
34 /**
35 * @var array
36 */
37 public $pageinfo;
38
39 /**
40 * @var \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
41 */
42 protected $backendUser;
43
44 /**
45 * @var \TYPO3\CMS\Core\Localization\LanguageService
46 */
47 protected $languageService;
48
49 /**
50 * The name of the module
51 *
52 * @var string
53 */
54 protected $moduleName = 'web_info';
55
56 /**
57 * ModuleTemplate Container
58 *
59 * @var ModuleTemplate
60 */
61 protected $moduleTemplate;
62
63 /**
64 * @var StandaloneView
65 */
66 protected $view;
67
68 /**
69 * Constructor
70 */
71 public function __construct()
72 {
73 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
74 $this->languageService = $GLOBALS['LANG'];
75 $this->languageService->includeLLFile('EXT:info/Resources/Private/Language/locallang_mod_web_info.xlf');
76
77 $this->backendUser = $GLOBALS['BE_USER'];
78
79 $this->MCONF = [
80 'name' => $this->moduleName,
81 ];
82 }
83
84 /**
85 * Initialize module header etc and call extObjContent function
86 */
87 public function main()
88 {
89 // We leave this here because of dependencies to submodules
90 $this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
91
92 // The page will show only if there is a valid page and if this page
93 // may be viewed by the user
94 $this->pageinfo = BackendUtility::readPageAccess($this->id, $this->perms_clause);
95 if ($this->pageinfo) {
96 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($this->pageinfo);
97 }
98 $access = is_array($this->pageinfo);
99 if ($this->id && $access || $this->backendUser->isAdmin() && !$this->id) {
100 if ($this->backendUser->isAdmin() && !$this->id) {
101 $this->pageinfo = ['title' => '[root-level]', 'uid' => 0, 'pid' => 0];
102 }
103 // JavaScript
104 $this->moduleTemplate->addJavaScriptCode(
105 'WebFuncInLineJS',
106 'if (top.fsMod) top.fsMod.recentIds["web"] = ' . (int)$this->id . ';
107 function jumpToUrl(URL) {
108 window.location.href = URL;
109 return false;
110 }
111 '
112 );
113 // Setting up the context sensitive menu:
114 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
115
116 $this->view = $this->getFluidTemplateObject();
117 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
118 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
119 $this->view->assign('moduleName', (string)$uriBuilder->buildUriFromRoute($this->moduleName));
120 $this->view->assign('functionMenuModuleContent', $this->getExtObjContent());
121 // Setting up the buttons and markers for docheader
122 $this->getButtons();
123 $this->generateMenu();
124 $this->content .= $this->view->render();
125 } else {
126 // If no access or if ID == zero
127 $this->content = $this->moduleTemplate->header($this->languageService->getLL('title'));
128 }
129 }
130
131 /**
132 * Injects the request object for the current request or subrequest
133 * Then checks for module functions that have hooked in, and renders menu etc.
134 *
135 * @param ServerRequestInterface $request the current request
136 * @return ResponseInterface the response with the content
137 */
138 public function mainAction(ServerRequestInterface $request): ResponseInterface
139 {
140 $GLOBALS['SOBE'] = $this;
141 $this->init();
142
143 // Checking for first level external objects
144 $this->checkExtObj();
145
146 // Checking second level external objects
147 $this->checkSubExtObj();
148 $this->main();
149
150 $this->moduleTemplate->setContent($this->content);
151 return new HtmlResponse($this->moduleTemplate->renderContent());
152 }
153
154 /**
155 * Create the panel of buttons for submitting the form or otherwise perform operations.
156 */
157 protected function getButtons()
158 {
159 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
160 // View page
161 $viewButton = $buttonBar->makeLinkButton()
162 ->setHref('#')
163 ->setOnClick(BackendUtility::viewOnClick(
164 $this->pageinfo['uid'],
165 '',
166 BackendUtility::BEgetRootLine($this->pageinfo['uid'])
167 ))
168 ->setTitle($this->languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
169 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-page', Icon::SIZE_SMALL));
170 $buttonBar->addButton($viewButton, ButtonBar::BUTTON_POSITION_LEFT, 1);
171 // Shortcut
172 $shortCutButton = $buttonBar->makeShortcutButton()
173 ->setModuleName($this->moduleName)
174 ->setDisplayName($this->MOD_MENU['function'][$this->MOD_SETTINGS['function']])
175 ->setGetVariables([
176 'route',
177 'id',
178 'edit_record',
179 'pointer',
180 'new_unique_uid',
181 'search_field',
182 'search_levels',
183 'showLimit'
184 ])
185 ->setSetVariables(array_keys($this->MOD_MENU));
186 $buttonBar->addButton($shortCutButton, ButtonBar::BUTTON_POSITION_RIGHT);
187
188 // CSH
189 $cshButton = $buttonBar->makeHelpButton()
190 ->setModuleName('xMOD_csh_corebe')
191 ->setFieldName('pagetree_overview');
192 $buttonBar->addButton($cshButton);
193 }
194
195 /**
196 * Generate the ModuleMenu
197 */
198 protected function generateMenu()
199 {
200 $menu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
201 $menu->setIdentifier('WebInfoJumpMenu');
202 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
203 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
204 foreach ($this->MOD_MENU['function'] as $controller => $title) {
205 $item = $menu
206 ->makeMenuItem()
207 ->setHref(
208 (string)$uriBuilder->buildUriFromRoute(
209 $this->moduleName,
210 [
211 'id' => $this->id,
212 'SET' => [
213 'function' => $controller
214 ]
215 ]
216 )
217 )
218 ->setTitle($title);
219 if ($controller === $this->MOD_SETTINGS['function']) {
220 $item->setActive(true);
221 }
222 $menu->addMenuItem($item);
223 }
224 $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
225 }
226
227 /**
228 * Returns the ModuleTemplate container
229 * This is used by PageLayoutView.php
230 *
231 * @return ModuleTemplate
232 */
233 public function getModuleTemplate()
234 {
235 return $this->moduleTemplate;
236 }
237
238 /**
239 * returns a new standalone view, shorthand function
240 *
241 * @return StandaloneView
242 */
243 protected function getFluidTemplateObject()
244 {
245 /** @var StandaloneView $view */
246 $view = GeneralUtility::makeInstance(StandaloneView::class);
247 $view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:info/Resources/Private/Layouts')]);
248 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:info/Resources/Private/Partials')]);
249 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:info/Resources/Private/Templates')]);
250
251 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:info/Resources/Private/Templates/Main.html'));
252
253 $view->getRequest()->setControllerExtensionName('info');
254 return $view;
255 }
256 }