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