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