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