0209b3f5dc5ca89dbb0635a2caab0bbc8f16f249
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Configuration / BackendConfigurationManager.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Configuration;
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 /**
18 * A general purpose configuration manager used in backend mode.
19 */
20 class BackendConfigurationManager extends \TYPO3\CMS\Extbase\Configuration\AbstractConfigurationManager {
21
22 /**
23 * Needed to recursively fetch a page tree
24 *
25 * @var \TYPO3\CMS\Core\Database\QueryGenerator
26 * @inject
27 */
28 protected $queryGenerator;
29
30 /**
31 * @var array
32 */
33 protected $typoScriptSetupCache = array();
34
35 /**
36 * stores the current page ID
37 * @var int
38 */
39 protected $currentPageId;
40
41 /**
42 * Returns TypoScript Setup array from current Environment.
43 *
44 * @return array the raw TypoScript setup
45 */
46 public function getTypoScriptSetup() {
47 $pageId = $this->getCurrentPageId();
48
49 if (!array_key_exists($pageId, $this->typoScriptSetupCache)) {
50 /** @var $template \TYPO3\CMS\Core\TypoScript\TemplateService */
51 $template = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\TemplateService::class);
52 // do not log time-performance information
53 $template->tt_track = 0;
54 // Explicitly trigger processing of extension static files
55 $template->setProcessExtensionStatics(TRUE);
56 $template->init();
57 // Get the root line
58 $rootline = array();
59 if ($pageId > 0) {
60 /** @var $sysPage \TYPO3\CMS\Frontend\Page\PageRepository */
61 $sysPage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Page\PageRepository::class);
62 // Get the rootline for the current page
63 $rootline = $sysPage->getRootLine($pageId, '', TRUE);
64 }
65 // This generates the constants/config + hierarchy info for the template.
66 $template->runThroughTemplates($rootline, 0);
67 $template->generateConfig();
68 $this->typoScriptSetupCache[$pageId] = $template->setup;
69 }
70 return $this->typoScriptSetupCache[$pageId];
71 }
72
73 /**
74 * Returns the TypoScript configuration found in module.tx_yourextension_yourmodule
75 * merged with the global configuration of your extension from module.tx_yourextension
76 *
77 * @param string $extensionName
78 * @param string $pluginName in BE mode this is actually the module signature. But we're using it just like the plugin name in FE
79 * @return array
80 */
81 protected function getPluginConfiguration($extensionName, $pluginName = NULL) {
82 $setup = $this->getTypoScriptSetup();
83 $pluginConfiguration = array();
84 if (is_array($setup['module.']['tx_' . strtolower($extensionName) . '.'])) {
85 $pluginConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['module.']['tx_' . strtolower($extensionName) . '.']);
86 }
87 if ($pluginName !== NULL) {
88 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
89 if (is_array($setup['module.']['tx_' . $pluginSignature . '.'])) {
90 $overruleConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['module.']['tx_' . $pluginSignature . '.']);
91 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($pluginConfiguration, $overruleConfiguration);
92 }
93 }
94 return $pluginConfiguration;
95 }
96
97 /**
98 * Returns the configured controller/action pairs of the specified module in the format
99 * array(
100 * 'Controller1' => array('action1', 'action2'),
101 * 'Controller2' => array('action3', 'action4')
102 * )
103 *
104 * @param string $extensionName
105 * @param string $pluginName in BE mode this is actually the module signature. But we're using it just like the plugin name in FE
106 * @return array
107 */
108 protected function getSwitchableControllerActions($extensionName, $pluginName) {
109 $switchableControllerActions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$pluginName]['controllers'];
110 if (!is_array($switchableControllerActions)) {
111 $switchableControllerActions = array();
112 }
113 return $switchableControllerActions;
114 }
115
116 /**
117 * Returns the page uid of the current page.
118 * If no page is selected, we'll return the uid of the first root page.
119 *
120 * @return int current page id. If no page is selected current root page id is returned
121 */
122 protected function getCurrentPageId() {
123 if ($this->currentPageId !== NULL) {
124 return $this->currentPageId;
125 }
126
127 $this->currentPageId = $this->getCurrentPageIdFromGetPostData() ?: $this->getCurrentPageIdFromCurrentSiteRoot();
128 $this->currentPageId = $this->currentPageId ?: $this->getCurrentPageIdFromRootTemplate();
129 $this->currentPageId = $this->currentPageId ?: self::DEFAULT_BACKEND_STORAGE_PID;
130
131 return $this->currentPageId;
132 }
133
134 /**
135 * Gets the current page ID from the GET/POST data.
136 *
137 * @return int the page UID, will be 0 if none has been set
138 */
139 protected function getCurrentPageIdFromGetPostData() {
140 return (int)\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('id');
141 }
142
143 /**
144 * Gets the current page ID from the first site root in tree.
145 *
146 * @return int the page UID, will be 0 if none has been set
147 */
148 protected function getCurrentPageIdFromCurrentSiteRoot() {
149 $rootPage = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
150 'uid', 'pages', 'deleted=0 AND hidden=0 AND is_siteroot=1', '', 'sorting'
151 );
152 if (empty($rootPage)) {
153 return 0;
154 }
155
156 return (int)$rootPage['uid'];
157 }
158
159 /**
160 * Gets the current page ID from the first created root template.
161 *
162 * @return int the page UID, will be 0 if none has been set
163 */
164 protected function getCurrentPageIdFromRootTemplate() {
165 $rootTemplate = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
166 'pid', 'sys_template', 'deleted=0 AND hidden=0 AND root=1', '', 'crdate'
167 );
168 if (empty($rootTemplate)) {
169 return 0;
170 }
171
172 return (int)$rootTemplate['pid'];
173 }
174
175 /**
176 * Returns the default backend storage pid
177 *
178 * @return string
179 */
180 public function getDefaultBackendStoragePid() {
181 return $this->getCurrentPageId();
182 }
183
184 /**
185 * We need to set some default request handler if the framework configuration
186 * could not be loaded; to make sure Extbase also works in Backend modules
187 * in all contexts.
188 *
189 * @param array $frameworkConfiguration
190 * @return array
191 */
192 protected function getContextSpecificFrameworkConfiguration(array $frameworkConfiguration) {
193 if (!isset($frameworkConfiguration['mvc']['requestHandlers'])) {
194 $frameworkConfiguration['mvc']['requestHandlers'] = array(
195 \TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler::class => \TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler::class,
196 \TYPO3\CMS\Extbase\Mvc\Web\BackendRequestHandler::class => \TYPO3\CMS\Extbase\Mvc\Web\BackendRequestHandler::class
197 );
198 }
199 return $frameworkConfiguration;
200 }
201
202
203 /**
204 * Returns a comma separated list of storagePid that are below a certain storage pid.
205 *
206 * @param string $storagePid Storage PID to start at; multiple PIDs possible as comma-separated list
207 * @param int $recursionDepth Maximum number of levels to search, 0 to disable recursive lookup
208 * @return string storage PIDs
209 */
210 protected function getRecursiveStoragePids($storagePid, $recursionDepth = 0) {
211 if ($recursionDepth <= 0) {
212 return $storagePid;
213 }
214
215 $recursiveStoragePids = '';
216 $storagePids = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $storagePid);
217 foreach ($storagePids as $startPid) {
218 $pids = $this->queryGenerator->getTreeList($startPid, $recursionDepth, 0, 1);
219 if (strlen($pids) > 0) {
220 $recursiveStoragePids .= $pids . ',';
221 }
222 }
223
224 return rtrim($recursiveStoragePids, ',');
225 }
226
227 }