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