Revert "[BUGFIX] Assume pid 0 for global backend context"
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Configuration / BackendConfigurationManager.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Configuration;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the text file GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * A general purpose configuration manager used in backend mode.
32 */
33 class BackendConfigurationManager extends \TYPO3\CMS\Extbase\Configuration\AbstractConfigurationManager {
34
35 /**
36 * Needed to recursively fetch a page tree
37 *
38 * @var \TYPO3\CMS\Core\Database\QueryGenerator
39 * @inject
40 */
41 protected $queryGenerator;
42
43 /**
44 * @var array
45 */
46 protected $typoScriptSetupCache = array();
47
48 /**
49 * Returns TypoScript Setup array from current Environment.
50 *
51 * @return array the raw TypoScript setup
52 */
53 public function getTypoScriptSetup() {
54 $pageId = $this->getCurrentPageId();
55
56 if (!array_key_exists($pageId, $this->typoScriptSetupCache)) {
57 /** @var $template \TYPO3\CMS\Core\TypoScript\TemplateService */
58 $template = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\TemplateService');
59 // do not log time-performance information
60 $template->tt_track = 0;
61 // Explicitly trigger processing of extension static files
62 $template->setProcessExtensionStatics(TRUE);
63 $template->init();
64 // Get the root line
65 $rootline = array();
66 if ($pageId > 0) {
67 /** @var $sysPage \TYPO3\CMS\Frontend\Page\PageRepository */
68 $sysPage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
69 // Get the rootline for the current page
70 $rootline = $sysPage->getRootLine($pageId, '', TRUE);
71 }
72 // This generates the constants/config + hierarchy info for the template.
73 $template->runThroughTemplates($rootline, 0);
74 $template->generateConfig();
75 $this->typoScriptSetupCache[$pageId] = $template->setup;
76 }
77 return $this->typoScriptSetupCache[$pageId];
78 }
79
80 /**
81 * Returns the TypoScript configuration found in module.tx_yourextension_yourmodule
82 * merged with the global configuration of your extension from module.tx_yourextension
83 *
84 * @param string $extensionName
85 * @param string $pluginName in BE mode this is actually the module signature. But we're using it just like the plugin name in FE
86 * @return array
87 */
88 protected function getPluginConfiguration($extensionName, $pluginName = NULL) {
89 $setup = $this->getTypoScriptSetup();
90 $pluginConfiguration = array();
91 if (is_array($setup['module.']['tx_' . strtolower($extensionName) . '.'])) {
92 $pluginConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['module.']['tx_' . strtolower($extensionName) . '.']);
93 }
94 if ($pluginName !== NULL) {
95 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
96 if (is_array($setup['module.']['tx_' . $pluginSignature . '.'])) {
97 $overruleConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['module.']['tx_' . $pluginSignature . '.']);
98 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($pluginConfiguration, $overruleConfiguration);
99 }
100 }
101 return $pluginConfiguration;
102 }
103
104 /**
105 * Returns the configured controller/action pairs of the specified module in the format
106 * array(
107 * 'Controller1' => array('action1', 'action2'),
108 * 'Controller2' => array('action3', 'action4')
109 * )
110 *
111 * @param string $extensionName
112 * @param string $pluginName in BE mode this is actually the module signature. But we're using it just like the plugin name in FE
113 * @return array
114 */
115 protected function getSwitchableControllerActions($extensionName, $pluginName) {
116 $switchableControllerActions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$pluginName]['controllers'];
117 if (!is_array($switchableControllerActions)) {
118 $switchableControllerActions = array();
119 }
120 return $switchableControllerActions;
121 }
122
123 /**
124 * Returns the page uid of the current page.
125 * If no page is selected, we'll return the uid of the first root page.
126 *
127 * @return integer current page id. If no page is selected current root page id is returned
128 */
129 protected function getCurrentPageId() {
130 $pageId = (integer) \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('id');
131 if ($pageId > 0) {
132 return $pageId;
133 }
134 // get current site root
135 $rootPages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', 'pages', 'deleted=0 AND hidden=0 AND is_siteroot=1', '', '', '1');
136 if (count($rootPages) > 0) {
137 return $rootPages[0]['uid'];
138 }
139 // get root template
140 $rootTemplates = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('pid', 'sys_template', 'deleted=0 AND hidden=0 AND root=1', '', '', '1');
141 if (count($rootTemplates) > 0) {
142 return $rootTemplates[0]['pid'];
143 }
144 // fallback
145 return self::DEFAULT_BACKEND_STORAGE_PID;
146 }
147
148 /**
149 * Returns the default backend storage pid
150 *
151 * @return string
152 */
153 public function getDefaultBackendStoragePid() {
154 return $this->getCurrentPageId();
155 }
156
157 /**
158 * We need to set some default request handler if the framework configuration
159 * could not be loaded; to make sure Extbase also works in Backend modules
160 * in all contexts.
161 *
162 * @param array $frameworkConfiguration
163 * @return array
164 */
165 protected function getContextSpecificFrameworkConfiguration(array $frameworkConfiguration) {
166 if (!isset($frameworkConfiguration['mvc']['requestHandlers'])) {
167 $frameworkConfiguration['mvc']['requestHandlers'] = array(
168 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\FrontendRequestHandler' => 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\FrontendRequestHandler',
169 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\BackendRequestHandler' => 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\BackendRequestHandler'
170 );
171 }
172 return $frameworkConfiguration;
173 }
174
175
176 /**
177 * Returns a comma separated list of storagePid that are below a certain storage pid.
178 *
179 * @param string $storagePid Storage PID to start at; multiple PIDs possible as comma-separated list
180 * @param integer $recursionDepth Maximum number of levels to search, 0 to disable recursive lookup
181 * @return string storage PIDs
182 */
183 protected function getRecursiveStoragePids($storagePid, $recursionDepth = 0) {
184 if ($recursionDepth <= 0) {
185 return $storagePid;
186 }
187
188 $recursiveStoragePids = '';
189 $storagePids = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $storagePid);
190 foreach ($storagePids as $startPid) {
191 $pids = $this->queryGenerator->getTreeList($startPid, $recursionDepth, 0, 1);
192 if (strlen($pids) > 0) {
193 $recursiveStoragePids .= $pids . ',';
194 }
195 }
196
197 return rtrim($recursiveStoragePids, ',');
198 }
199
200 }