0fdaaa5beb4e2cd221b99a579955a1598623f7b3
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Module / BaseScriptClass.php
1 <?php
2 namespace TYPO3\CMS\Backend\Module;
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 TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19 use TYPO3\CMS\Core\Localization\LanguageService;
20 use TYPO3\CMS\Core\Messaging\FlashMessage;
21 use TYPO3\CMS\Core\Messaging\FlashMessageService;
22 use TYPO3\CMS\Core\Page\PageRenderer;
23 use TYPO3\CMS\Core\Type\Bitmask\Permission;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * Parent class for 'ScriptClasses' in backend modules.
28 *
29 * EXAMPLE PROTOTYPE
30 *
31 * As for examples there are lots of them if you search for classes which extends \TYPO3\CMS\Backend\Module\BaseScriptClass
32 * However you can see a prototype example of how a module might use this class in an index.php file typically hosting a backend module.
33 *
34 * NOTICE: This example only outlines the basic structure of how this class is used.
35 * You should consult the documentation and other real-world examples for some actual things to do when building modules.
36 *
37 * TYPICAL SETUP OF A BACKEND MODULE:
38 *
39 * PrototypeController EXTENDS THE CLASS \TYPO3\CMS\Backend\Module\BaseScriptClass with a main() function:
40 *
41 * namespace Vendor\Prototype\Controller;
42 *
43 * class PrototypeController extends \TYPO3\CMS\Backend\Module\BaseScriptClass {
44 * public function __construct() {
45 * $this->getLanguageService()->includeLLFile('EXT:prototype/Resources/Private/Language/locallang.xlf');
46 * $this->getBackendUser()->modAccess($GLOBALS['MCONF'], TRUE);
47 * }
48 * }
49 *
50 * MAIN FUNCTION - HERE YOU CREATE THE MODULE CONTENT IN $this->content
51 * public function main() {
52 * TYPICALLY THE INTERNAL VAR, $this->doc is instantiated like this:
53 * $this->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
54 * ... AND OF COURSE A LOT OF OTHER THINGS GOES ON - LIKE PUTTING CONTENT INTO $this->content
55 * $this->content='';
56 * }
57 *
58 * MAKE INSTANCE OF THE SCRIPT CLASS AND CALL init()
59 * $GLOBALS['SOBE'] = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\Vendor\Prototype\Controller\PrototypeController::class);
60 * $GLOBALS['SOBE']->init();
61 *
62 *
63 * THEN WE WILL CHECK IF THERE IS A 'SUBMODULE' REGISTERED TO BE INITIALIZED AS WELL:
64 * $GLOBALS['SOBE']->checkExtObj();
65 *
66 * THEN WE CALL THE main() METHOD AND THIS SHOULD SPARK THE CREATION OF THE MODULE OUTPUT.
67 * $GLOBALS['SOBE']->main();
68 */
69 class BaseScriptClass
70 {
71 /**
72 * Loaded with the global array $MCONF which holds some module configuration from the conf.php file of backend modules.
73 *
74 * @see init()
75 * @var array
76 */
77 public $MCONF = [];
78
79 /**
80 * The integer value of the GET/POST var, 'id'. Used for submodules to the 'Web' module (page id)
81 *
82 * @see init()
83 * @var int
84 */
85 public $id;
86
87 /**
88 * The value of GET/POST var, 'CMD'
89 *
90 * @see init()
91 * @var mixed
92 */
93 public $CMD;
94
95 /**
96 * A WHERE clause for selection records from the pages table based on read-permissions of the current backend user.
97 *
98 * @see init()
99 * @var string
100 */
101 public $perms_clause;
102
103 /**
104 * The module menu items array. Each key represents a key for which values can range between the items in the array of that key.
105 *
106 * @see init()
107 * @var array
108 */
109 public $MOD_MENU = [
110 'function' => []
111 ];
112
113 /**
114 * Current settings for the keys of the MOD_MENU array
115 *
116 * @see $MOD_MENU
117 * @var array
118 */
119 public $MOD_SETTINGS = [];
120
121 /**
122 * Module TSconfig based on PAGE TSconfig / USER TSconfig
123 *
124 * @see menuConfig()
125 * @var array
126 */
127 public $modTSconfig;
128
129 /**
130 * If type is 'ses' then the data is stored as session-lasting data. This means that it'll be wiped out the next time the user logs in.
131 * Can be set from extension classes of this class before the init() function is called.
132 *
133 * @see menuConfig(), \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleData()
134 * @var string
135 */
136 public $modMenu_type = '';
137
138 /**
139 * dontValidateList can be used to list variables that should not be checked if their value is found in the MOD_MENU array. Used for dynamically generated menus.
140 * Can be set from extension classes of this class before the init() function is called.
141 *
142 * @see menuConfig(), \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleData()
143 * @var string
144 */
145 public $modMenu_dontValidateList = '';
146
147 /**
148 * List of default values from $MOD_MENU to set in the output array (only if the values from MOD_MENU are not arrays)
149 * Can be set from extension classes of this class before the init() function is called.
150 *
151 * @see menuConfig(), \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleData()
152 * @var string
153 */
154 public $modMenu_setDefaultList = '';
155
156 /**
157 * Contains module configuration parts from TBE_MODULES_EXT if found
158 *
159 * @see handleExternalFunctionValue()
160 * @var array
161 */
162 public $extClassConf;
163
164 /**
165 * Generally used for accumulating the output content of backend modules
166 *
167 * @var string
168 */
169 public $content = '';
170
171 /**
172 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
173 */
174 public $doc;
175
176 /**
177 * May contain an instance of a 'Function menu module' which connects to this backend module.
178 *
179 * @see checkExtObj()
180 * @var AbstractFunctionModule
181 */
182 public $extObj;
183
184 /**
185 * @var PageRenderer
186 */
187 protected $pageRenderer;
188
189 /**
190 * Initializes the backend module by setting internal variables, initializing the menu.
191 *
192 * @see menuConfig()
193 */
194 public function init()
195 {
196 // Name might be set from outside
197 if (!$this->MCONF['name']) {
198 $this->MCONF = $GLOBALS['MCONF'];
199 }
200 $this->id = (int)GeneralUtility::_GP('id');
201 $this->CMD = GeneralUtility::_GP('CMD');
202 $this->perms_clause = $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW);
203 $this->menuConfig();
204 $this->handleExternalFunctionValue();
205 }
206
207 /**
208 * Initializes the internal MOD_MENU array setting and unsetting items based on various conditions. It also merges in external menu items from the global array TBE_MODULES_EXT (see mergeExternalItems())
209 * Then MOD_SETTINGS array is cleaned up (see \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleData()) so it contains only valid values. It's also updated with any SET[] values submitted.
210 * Also loads the modTSconfig internal variable.
211 *
212 * @see init(), $MOD_MENU, $MOD_SETTINGS, \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleData(), mergeExternalItems()
213 */
214 public function menuConfig()
215 {
216 // Page / user TSconfig settings and blinding of menu-items
217 $this->modTSconfig['properties'] = BackendUtility::getPagesTSconfig($this->id)['mod.'][$this->MCONF['name'] . '.'] ?? [];
218 $this->MOD_MENU['function'] = $this->mergeExternalItems($this->MCONF['name'], 'function', $this->MOD_MENU['function']);
219 $blindActions = $this->modTSconfig['properties']['menu.']['function.'] ?? [];
220 foreach ($blindActions as $key => $value) {
221 if (!$value && array_key_exists($key, $this->MOD_MENU['function'])) {
222 unset($this->MOD_MENU['function'][$key]);
223 }
224 }
225 $this->MOD_SETTINGS = BackendUtility::getModuleData($this->MOD_MENU, GeneralUtility::_GP('SET'), $this->MCONF['name'], $this->modMenu_type, $this->modMenu_dontValidateList, $this->modMenu_setDefaultList);
226 }
227
228 /**
229 * Merges menu items from global array $TBE_MODULES_EXT
230 *
231 * @param string $modName Module name for which to find value
232 * @param string $menuKey Menu key, eg. 'function' for the function menu.
233 * @param array $menuArr The part of a MOD_MENU array to work on.
234 * @return array Modified array part.
235 * @access private
236 * @see \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::insertModuleFunction(), menuConfig()
237 */
238 public function mergeExternalItems($modName, $menuKey, $menuArr)
239 {
240 $mergeArray = $GLOBALS['TBE_MODULES_EXT'][$modName]['MOD_MENU'][$menuKey];
241 if (is_array($mergeArray)) {
242 foreach ($mergeArray as $k => $v) {
243 if (((string)$v['ws'] === '' || $this->getBackendUser()->workspace === 0 && GeneralUtility::inList($v['ws'], 'online')) || $this->getBackendUser()->workspace === -1 && GeneralUtility::inList($v['ws'], 'offline') || $this->getBackendUser()->workspace > 0 && GeneralUtility::inList($v['ws'], 'custom')) {
244 $menuArr[$k] = $this->getLanguageService()->sL($v['title']);
245 }
246 }
247 }
248 return $menuArr;
249 }
250
251 /**
252 * Loads $this->extClassConf with the configuration for the CURRENT function of the menu.
253 *
254 * @param string $MM_key The key to MOD_MENU for which to fetch configuration. 'function' is default since it is first and foremost used to get information per "extension object" (I think that is what its called)
255 * @param string $MS_value The value-key to fetch from the config array. If NULL (default) MOD_SETTINGS[$MM_key] will be used. This is useful if you want to force another function than the one defined in MOD_SETTINGS[function]. Call this in init() function of your Script Class: handleExternalFunctionValue('function', $forcedSubModKey)
256 * @see getExternalItemConfig(), init()
257 */
258 public function handleExternalFunctionValue($MM_key = 'function', $MS_value = null)
259 {
260 if ($MS_value === null) {
261 $MS_value = $this->MOD_SETTINGS[$MM_key];
262 }
263 $this->extClassConf = $this->getExternalItemConfig($this->MCONF['name'], $MM_key, $MS_value);
264 }
265
266 /**
267 * Returns configuration values from the global variable $TBE_MODULES_EXT for the module given.
268 * For example if the module is named "web_info" and the "function" key ($menuKey) of MOD_SETTINGS is "stat" ($value) then you will have the values of $TBE_MODULES_EXT['webinfo']['MOD_MENU']['function']['stat'] returned.
269 *
270 * @param string $modName Module name
271 * @param string $menuKey Menu key, eg. "function" for the function menu. See $this->MOD_MENU
272 * @param string $value Optionally the value-key to fetch from the array that would otherwise have been returned if this value was not set. Look source...
273 * @return mixed The value from the TBE_MODULES_EXT array.
274 * @see handleExternalFunctionValue()
275 */
276 public function getExternalItemConfig($modName, $menuKey, $value = '')
277 {
278 if (isset($GLOBALS['TBE_MODULES_EXT'][$modName])) {
279 return (string)$value !== '' ? $GLOBALS['TBE_MODULES_EXT'][$modName]['MOD_MENU'][$menuKey][$value] : $GLOBALS['TBE_MODULES_EXT'][$modName]['MOD_MENU'][$menuKey];
280 }
281 return null;
282 }
283
284 /**
285 * Creates an instance of the class found in $this->extClassConf['name'] in $this->extObj if any (this should hold three keys, "name", "path" and "title" if a "Function menu module" tries to connect...)
286 * This value in extClassConf might be set by an extension (in an ext_tables/ext_localconf file) which thus "connects" to a module.
287 * The array $this->extClassConf is set in handleExternalFunctionValue() based on the value of MOD_SETTINGS[function]
288 * If an instance is created it is initiated with $this passed as value and $this->extClassConf as second argument. Further the $this->MOD_SETTING is cleaned up again after calling the init function.
289 *
290 * @see handleExternalFunctionValue(), \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::insertModuleFunction(), $extObj
291 */
292 public function checkExtObj()
293 {
294 if (is_array($this->extClassConf) && $this->extClassConf['name']) {
295 $this->extObj = GeneralUtility::makeInstance($this->extClassConf['name']);
296 $this->extObj->init($this, $this->extClassConf);
297 // Re-write:
298 $this->MOD_SETTINGS = BackendUtility::getModuleData($this->MOD_MENU, GeneralUtility::_GP('SET'), $this->MCONF['name'], $this->modMenu_type, $this->modMenu_dontValidateList, $this->modMenu_setDefaultList);
299 }
300 }
301
302 /**
303 * Calls the checkExtObj function in sub module if present.
304 */
305 public function checkSubExtObj()
306 {
307 if (is_object($this->extObj)) {
308 $this->extObj->checkExtObj();
309 }
310 }
311
312 /**
313 * Calls the 'header' function inside the "Function menu module" if present.
314 * A header function might be needed to add JavaScript or other stuff in the head. This can't be done in the main function because the head is already written.
315 */
316 public function extObjHeader()
317 {
318 if (is_callable([$this->extObj, 'head'])) {
319 $this->extObj->head();
320 }
321 }
322
323 /**
324 * Calls the 'main' function inside the "Function menu module" if present
325 */
326 public function extObjContent()
327 {
328 if ($this->extObj === null) {
329 $flashMessage = GeneralUtility::makeInstance(
330 FlashMessage::class,
331 $this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang.xlf:no_modules_registered'),
332 $this->getLanguageService()->getLL('title'),
333 FlashMessage::ERROR
334 );
335 /** @var \TYPO3\CMS\Core\Messaging\FlashMessageService $flashMessageService */
336 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
337 /** @var \TYPO3\CMS\Core\Messaging\FlashMessageQueue $defaultFlashMessageQueue */
338 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
339 $defaultFlashMessageQueue->enqueue($flashMessage);
340 } else {
341 $this->extObj->pObj = $this;
342 if (is_callable([$this->extObj, 'main'])) {
343 $this->content .= $this->extObj->main();
344 }
345 }
346 }
347
348 /**
349 * Return the content of the 'main' function inside the "Function menu module" if present
350 *
351 * @return string
352 */
353 public function getExtObjContent()
354 {
355 $savedContent = $this->content;
356 $this->content = '';
357 $this->extObjContent();
358 $newContent = $this->content;
359 $this->content = $savedContent;
360 return $newContent;
361 }
362
363 /**
364 * Returns the Language Service
365 * @return LanguageService
366 */
367 protected function getLanguageService()
368 {
369 return $GLOBALS['LANG'];
370 }
371
372 /**
373 * Returns the Backend User
374 * @return BackendUserAuthentication
375 */
376 protected function getBackendUser()
377 {
378 return $GLOBALS['BE_USER'];
379 }
380
381 /**
382 * @return PageRenderer
383 */
384 protected function getPageRenderer()
385 {
386 if ($this->pageRenderer === null) {
387 $this->pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
388 }
389
390 return $this->pageRenderer;
391 }
392 }