3f32ef88f64ed17b8b0d47dc624d71690e0b6614
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Module / AbstractFunctionModule.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\Template\DocumentTemplate;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20 use TYPO3\CMS\Core\Localization\LanguageService;
21 use TYPO3\CMS\Core\Page\PageRenderer;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * Parent class for 'Extension Objects' in backend modules.
26 *
27 * Used for 'submodules' to other modules. Also called 'Function menu modules'
28 * in \TYPO3\CMS\Core\Utility\ExtensionManagementUtility. And now its even called
29 * 'Extension Objects'. Or 'Module functions'. Wish we had just one name. Or a
30 * name at all...(?) Thank God its not so advanced when it works...
31 *
32 * In other words this class is used for backend modules which is not true
33 * backend modules appearing in the menu but rather adds themselves as a new
34 * entry in the function menu which typically exists for a backend
35 * module (like Web>Info or Tools etc...)
36 * The magic that binds this together is stored in the global variable
37 * $TBE_MODULES_EXT where extensions wanting to connect a module based on
38 * this class to an existing backend module store configuration which consists
39 * of the classname, script-path and a label (title/name).
40 *
41 * For more information about this, please see the large example comment for the
42 * class \TYPO3\CMS\Backend\Module\BaseScriptClass. This will show the principle of a
43 * 'level-1' connection. The more advanced example - having two layers as it is done
44 * with the 'web_info' module - can be seen in the comment above.
45 *
46 * EXAMPLE: One level.
47 * This can be seen in the extension 'frontend' where the info module have a
48 * function added. In 'ext_tables.php' this is done by this function call:
49 *
50 * \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::insertModuleFunction(
51 * 'web_info',
52 * \TYPO3\CMS\Frontend\Controller\PageInformationController::class,
53 * NULL,
54 * 'LLL:EXT:frontend/Resources/Private/Language/locallang_tca.xlf:mod_tx_cms_webinfo_page'
55 * );
56 *
57 * For this two-level thing to work it also requires that the parent
58 * module (the real backend module) supports it.
59 * This is the case for the modules web_info since it has two
60 * times inclusion sections in their index.php scripts.
61 *
62 * @see \TYPO3\CMS\Backend\Module\BaseScriptClass
63 */
64 abstract class AbstractFunctionModule
65 {
66 /**
67 * Contains a reference to the parent (calling) object (which is probably an instance of
68 * an extension class to \TYPO3\CMS\Backend\Module\BaseScriptClass
69 *
70 * @var BaseScriptClass
71 * @see init()
72 */
73 public $pObj;
74
75 /**
76 * @var BaseScriptClass
77 */
78 public $extObj;
79
80 /**
81 * Can be hardcoded to the name of a locallang.xlf file (from the same directory as the class file) to use/load
82 * and is included / added to $GLOBALS['LOCAL_LANG']
83 *
84 * @see init()
85 * @var string
86 */
87 public $localLangFile = '';
88
89 /**
90 * Contains module configuration parts from TBE_MODULES_EXT if found
91 *
92 * @see handleExternalFunctionValue()
93 * @var array
94 */
95 public $extClassConf;
96
97 /**
98 * If this value is set it points to a key in the TBE_MODULES_EXT array (not on the top level..) where another classname/filepath/title can be defined for sub-subfunctions.
99 * This is a little hard to explain, so see it in action; it used in the extension 'func_wizards' in order to provide yet a layer of interfacing with the backend module.
100 * The extension 'func_wizards' has this description: 'Adds the 'Wizards' item to the function menu in Web>Func. This is just a framework for wizard extensions.' - so as you can see it is designed to allow further connectivity - 'level 2'
101 *
102 * @see handleExternalFunctionValue(), \TYPO3\CMS\FuncWizards\Controller\WebFunctionWizardsBaseController
103 * @var string
104 */
105 public $function_key = '';
106
107 /**
108 * @var PageRenderer
109 */
110 protected $pageRenderer;
111
112 /**
113 * Initialize the object
114 *
115 * @param \object $pObj A reference to the parent (calling) object
116 * @throws \RuntimeException
117 * @see \TYPO3\CMS\Backend\Module\BaseScriptClass::checkExtObj()
118 */
119 public function init($pObj)
120 {
121 $this->pObj = $pObj;
122 // Local lang:
123 if (!empty($this->localLangFile)) {
124 $this->getLanguageService()->includeLLFile($this->localLangFile);
125 }
126 // Setting MOD_MENU items as we need them for logging:
127 $this->pObj->MOD_MENU = array_merge($this->pObj->MOD_MENU, $this->modMenu());
128 }
129
130 /**
131 * If $this->function_key is set (which means there are two levels of object connectivity) then
132 * $this->extClassConf is loaded with the TBE_MODULES_EXT configuration for that sub-sub-module
133 *
134 * @see $function_key, \TYPO3\CMS\FuncWizards\Controller\WebFunctionWizardsBaseController::init()
135 */
136 public function handleExternalFunctionValue()
137 {
138 // Must clean first to make sure the correct key is set...
139 $this->pObj->MOD_SETTINGS = BackendUtility::getModuleData($this->pObj->MOD_MENU, GeneralUtility::_GP('SET'), $this->pObj->MCONF['name']);
140 if ($this->function_key) {
141 $this->extClassConf = $this->pObj->getExternalItemConfig($this->pObj->MCONF['name'], $this->function_key, $this->pObj->MOD_SETTINGS[$this->function_key]);
142 }
143 }
144
145 /**
146 * Same as \TYPO3\CMS\Backend\Module\BaseScriptClass::checkExtObj()
147 *
148 * @see \TYPO3\CMS\Backend\Module\BaseScriptClass::checkExtObj()
149 */
150 public function checkExtObj()
151 {
152 if (is_array($this->extClassConf) && $this->extClassConf['name']) {
153 $this->extObj = GeneralUtility::makeInstance($this->extClassConf['name']);
154 $this->extObj->init($this->pObj, $this->extClassConf);
155 // Re-write:
156 $this->pObj->MOD_SETTINGS = BackendUtility::getModuleData($this->pObj->MOD_MENU, GeneralUtility::_GP('SET'), $this->pObj->MCONF['name']);
157 }
158 }
159
160 /**
161 * Calls the main function inside ANOTHER sub-submodule which might exist.
162 */
163 public function extObjContent()
164 {
165 if (is_object($this->extObj)) {
166 return $this->extObj->main();
167 }
168 }
169
170 /**
171 * Dummy function - but is used to set up additional menu items for this submodule.
172 *
173 * @return array A MOD_MENU array which will be merged together with the one from the parent object
174 * @see init(), \TYPO3\CMS\Frontend\Controller\PageInformationController::modMenu()
175 */
176 public function modMenu()
177 {
178 return [];
179 }
180
181 /**
182 * @return LanguageService
183 */
184 protected function getLanguageService()
185 {
186 return $GLOBALS['LANG'];
187 }
188
189 /**
190 * @return BackendUserAuthentication
191 */
192 protected function getBackendUserAuthentication()
193 {
194 return $GLOBALS['BE_USER'];
195 }
196
197 /**
198 * @return DocumentTemplate
199 */
200 protected function getDocumentTemplate()
201 {
202 return $GLOBALS['TBE_TEMPLATE'];
203 }
204
205 /**
206 * @return PageRenderer
207 */
208 protected function getPageRenderer()
209 {
210 if ($this->pageRenderer === null) {
211 $this->pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
212 }
213
214 return $this->pageRenderer;
215 }
216 }