[CLEANUP] Use Permission constants consistently
[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 = null;
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/be_user TSconfig settings and blinding of menu-items
217 $this->modTSconfig = BackendUtility::getModTSconfig($this->id, 'mod.' . $this->MCONF['name']);
218 $this->MOD_MENU['function'] = $this->mergeExternalItems($this->MCONF['name'], 'function', $this->MOD_MENU['function']);
219 $this->MOD_MENU['function'] = BackendUtility::unsetMenuItems($this->modTSconfig['properties'], $this->MOD_MENU['function'], 'menu.function');
220 $this->MOD_SETTINGS = BackendUtility::getModuleData($this->MOD_MENU, GeneralUtility::_GP('SET'), $this->MCONF['name'], $this->modMenu_type, $this->modMenu_dontValidateList, $this->modMenu_setDefaultList);
221 }
222
223 /**
224 * Merges menu items from global array $TBE_MODULES_EXT
225 *
226 * @param string $modName Module name for which to find value
227 * @param string $menuKey Menu key, eg. 'function' for the function menu.
228 * @param array $menuArr The part of a MOD_MENU array to work on.
229 * @return array Modified array part.
230 * @access private
231 * @see \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::insertModuleFunction(), menuConfig()
232 */
233 public function mergeExternalItems($modName, $menuKey, $menuArr)
234 {
235 $mergeArray = $GLOBALS['TBE_MODULES_EXT'][$modName]['MOD_MENU'][$menuKey];
236 if (is_array($mergeArray)) {
237 foreach ($mergeArray as $k => $v) {
238 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')) {
239 $menuArr[$k] = $this->getLanguageService()->sL($v['title']);
240 }
241 }
242 }
243 return $menuArr;
244 }
245
246 /**
247 * Loads $this->extClassConf with the configuration for the CURRENT function of the menu.
248 *
249 * @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)
250 * @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)
251 * @see getExternalItemConfig(), init()
252 */
253 public function handleExternalFunctionValue($MM_key = 'function', $MS_value = null)
254 {
255 if ($MS_value === null) {
256 $MS_value = $this->MOD_SETTINGS[$MM_key];
257 }
258 $this->extClassConf = $this->getExternalItemConfig($this->MCONF['name'], $MM_key, $MS_value);
259 }
260
261 /**
262 * Returns configuration values from the global variable $TBE_MODULES_EXT for the module given.
263 * 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.
264 *
265 * @param string $modName Module name
266 * @param string $menuKey Menu key, eg. "function" for the function menu. See $this->MOD_MENU
267 * @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...
268 * @return mixed The value from the TBE_MODULES_EXT array.
269 * @see handleExternalFunctionValue()
270 */
271 public function getExternalItemConfig($modName, $menuKey, $value = '')
272 {
273 if (isset($GLOBALS['TBE_MODULES_EXT'][$modName])) {
274 return (string)$value !== '' ? $GLOBALS['TBE_MODULES_EXT'][$modName]['MOD_MENU'][$menuKey][$value] : $GLOBALS['TBE_MODULES_EXT'][$modName]['MOD_MENU'][$menuKey];
275 }
276 return null;
277 }
278
279 /**
280 * 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...)
281 * This value in extClassConf might be set by an extension (in an ext_tables/ext_localconf file) which thus "connects" to a module.
282 * The array $this->extClassConf is set in handleExternalFunctionValue() based on the value of MOD_SETTINGS[function]
283 * 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.
284 *
285 * @see handleExternalFunctionValue(), \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::insertModuleFunction(), $extObj
286 */
287 public function checkExtObj()
288 {
289 if (is_array($this->extClassConf) && $this->extClassConf['name']) {
290 $this->extObj = GeneralUtility::makeInstance($this->extClassConf['name']);
291 $this->extObj->init($this, $this->extClassConf);
292 // Re-write:
293 $this->MOD_SETTINGS = BackendUtility::getModuleData($this->MOD_MENU, GeneralUtility::_GP('SET'), $this->MCONF['name'], $this->modMenu_type, $this->modMenu_dontValidateList, $this->modMenu_setDefaultList);
294 }
295 }
296
297 /**
298 * Calls the checkExtObj function in sub module if present.
299 */
300 public function checkSubExtObj()
301 {
302 if (is_object($this->extObj)) {
303 $this->extObj->checkExtObj();
304 }
305 }
306
307 /**
308 * Calls the 'header' function inside the "Function menu module" if present.
309 * 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.
310 */
311 public function extObjHeader()
312 {
313 if (is_callable([$this->extObj, 'head'])) {
314 $this->extObj->head();
315 }
316 }
317
318 /**
319 * Calls the 'main' function inside the "Function menu module" if present
320 */
321 public function extObjContent()
322 {
323 if ($this->extObj === null) {
324 $flashMessage = GeneralUtility::makeInstance(
325 FlashMessage::class,
326 $this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang.xlf:no_modules_registered'),
327 $this->getLanguageService()->getLL('title'),
328 FlashMessage::ERROR
329 );
330 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
331 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
332 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
333 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
334 $defaultFlashMessageQueue->enqueue($flashMessage);
335 } else {
336 $this->extObj->pObj = $this;
337 if (is_callable([$this->extObj, 'main'])) {
338 $this->content .= $this->extObj->main();
339 }
340 }
341 }
342
343 /**
344 * Return the content of the 'main' function inside the "Function menu module" if present
345 *
346 * @return string
347 */
348 public function getExtObjContent()
349 {
350 $savedContent = $this->content;
351 $this->content = '';
352 $this->extObjContent();
353 $newContent = $this->content;
354 $this->content = $savedContent;
355 return $newContent;
356 }
357
358 /**
359 * Returns the Language Service
360 * @return LanguageService
361 */
362 protected function getLanguageService()
363 {
364 return $GLOBALS['LANG'];
365 }
366
367 /**
368 * Returns the Backend User
369 * @return BackendUserAuthentication
370 */
371 protected function getBackendUser()
372 {
373 return $GLOBALS['BE_USER'];
374 }
375
376 /**
377 * @return PageRenderer
378 */
379 protected function getPageRenderer()
380 {
381 if ($this->pageRenderer === null) {
382 $this->pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
383 }
384
385 return $this->pageRenderer;
386 }
387 }