[TASK] Integrate "Functions" as context menu
[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:func/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('functionMenuModuleContent', $this->getExtObjContent());
136 // Setting up the buttons and markers for docheader
137 $this->getButtons();
138 $this->generateMenu();
139 $this->content .= $this->view->render();
140 } else {
141 // If no access or if ID == zero
142 $title = $this->getLanguageService()->getLL('title');
143 $message = $this->getLanguageService()->getLL('clickAPage_content');
144 $this->view = $this->getFluidTemplateObject('func', 'func', 'InfoBox');
145 $this->view->assignMultiple([
146 'title' => $title,
147 'message' => $message,
148 'state' => InfoboxViewHelper::STATE_INFO
149 ]);
150 $this->content = $this->view->render();
151 // Setting up the buttons and markers for docheader
152 $this->getButtons();
153 }
154 }
155
156 /**
157 * Generates the menu based on $this->MOD_MENU
158 *
159 * @throws \InvalidArgumentException
160 */
161 protected function generateMenu()
162 {
163 $menu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
164 $menu->setIdentifier('WebFuncJumpMenu');
165 foreach ($this->MOD_MENU['function'] as $controller => $title) {
166 $item = $menu
167 ->makeMenuItem()
168 ->setHref(
169 BackendUtility::getModuleUrl(
170 $this->moduleName,
171 [
172 'id' => $this->id,
173 'SET' => [
174 'function' => $controller
175 ]
176 ]
177 )
178 )
179 ->setTitle($title);
180 if ($controller === $this->MOD_SETTINGS['function']) {
181 $item->setActive(true);
182 }
183 $menu->addMenuItem($item);
184 }
185 $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
186 }
187
188 /**
189 * Create the panel of buttons for submitting the form or otherwise perform operations.
190 */
191 protected function getButtons()
192 {
193 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
194 // CSH
195 $cshButton = $buttonBar->makeHelpButton()
196 ->setModuleName('_MOD_web_func')
197 ->setFieldName('');
198 $buttonBar->addButton($cshButton);
199 if ($this->id && is_array($this->pageinfo)) {
200 // View page
201 $viewButton = $buttonBar->makeLinkButton()
202 ->setOnClick(BackendUtility::viewOnClick($this->pageinfo['uid'], '', BackendUtility::BEgetRootLine($this->pageinfo['uid'])))
203 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
204 ->setIcon($this->iconFactory->getIcon('actions-view-page', Icon::SIZE_SMALL))
205 ->setHref('#');
206 $buttonBar->addButton($viewButton);
207 // Shortcut
208 $shortcutButton = $buttonBar->makeShortcutButton()
209 ->setModuleName($this->moduleName)
210 ->setGetVariables(['id', 'edit_record', 'pointer', 'new_unique_uid', 'search_field', 'search_levels', 'showLimit'])
211 ->setSetVariables(array_keys($this->MOD_MENU));
212 $buttonBar->addButton($shortcutButton);
213 }
214 }
215
216 /**
217 * Returns LanguageService
218 *
219 * @return \TYPO3\CMS\Core\Localization\LanguageService
220 */
221 protected function getLanguageService()
222 {
223 return $GLOBALS['LANG'];
224 }
225
226 /**
227 * Returns the current BE user.
228 *
229 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
230 */
231 protected function getBackendUser()
232 {
233 return $GLOBALS['BE_USER'];
234 }
235
236 /**
237 * returns a new standalone view, shorthand function
238 *
239 * @param string $extensionName
240 * @param string $controllerExtensionName
241 * @param string $templateName
242 * @return StandaloneView
243 */
244 protected function getFluidTemplateObject($extensionName, $controllerExtensionName, $templateName = 'Main')
245 {
246 /** @var StandaloneView $view */
247 $view = GeneralUtility::makeInstance(StandaloneView::class);
248 $view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:' . $extensionName . '/Resources/Private/Layouts')]);
249 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:' . $extensionName . '/Resources/Private/Partials')]);
250 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:' . $extensionName . '/Resources/Private/Templates')]);
251
252 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:' . $extensionName . '/Resources/Private/Templates/' . $templateName . '.html'));
253
254 $view->getRequest()->setControllerExtensionName($controllerExtensionName);
255 return $view;
256 }
257 }