BackendConfigurationManager.php 8.39 KB
Newer Older
1
<?php
Thomas Maroschik's avatar
Thomas Maroschik committed
2
3
namespace TYPO3\CMS\Extbase\Configuration;

4
/***************************************************************
5
6
 *  Copyright notice
 *
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
10
11
12
13
14
15
16
17
18
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  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
21
 *  from the author is found in LICENSE.txt distributed with these scripts.
 *
22
23
24
25
26
27
28
29
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/
30
31
32
/**
 * A general purpose configuration manager used in backend mode.
 */
Thomas Maroschik's avatar
Thomas Maroschik committed
33
class BackendConfigurationManager extends \TYPO3\CMS\Extbase\Configuration\AbstractConfigurationManager {
34

35
	/**
36
	 * Needed to recursively fetch a page tree
37
	 *
38
39
	 * @var \TYPO3\CMS\Core\Database\QueryGenerator
	 * @inject
40
	 */
41
	protected $queryGenerator;
42

43
44
45
	/**
	 * @var array
	 */
46
	protected $typoScriptSetupCache = array();
47

48
49
50
51
52
53
	/**
	 * stores the current page ID
	 * @var integer
	 */
	protected $currentPageId;

54
55
56
	/**
	 * Returns TypoScript Setup array from current Environment.
	 *
57
	 * @return array the raw TypoScript setup
58
	 */
59
	public function getTypoScriptSetup() {
60
61
62
		$pageId = $this->getCurrentPageId();

		if (!array_key_exists($pageId, $this->typoScriptSetupCache)) {
63
			/** @var $template \TYPO3\CMS\Core\TypoScript\TemplateService */
Thomas Maroschik's avatar
Thomas Maroschik committed
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
68
			// Explicitly trigger processing of extension static files
			$template->setProcessExtensionStatics(TRUE);
69
			$template->init();
70
			// Get the root line
71
72
			$rootline = array();
			if ($pageId > 0) {
Thomas Maroschik's avatar
Thomas Maroschik committed
73
74
				/** @var $sysPage \TYPO3\CMS\Frontend\Page\PageRepository */
				$sysPage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
75
				// Get the rootline for the current page
76
				$rootline = $sysPage->getRootLine($pageId, '', TRUE);
77
			}
78
			// This generates the constants/config + hierarchy info for the template.
79
80
			$template->runThroughTemplates($rootline, 0);
			$template->generateConfig();
81
			$this->typoScriptSetupCache[$pageId] = $template->setup;
82
		}
83
		return $this->typoScriptSetupCache[$pageId];
84
85
	}

86
87
88
89
90
91
92
93
	/**
	 * Returns the TypoScript configuration found in module.tx_yourextension_yourmodule
	 * merged with the global configuration of your extension from module.tx_yourextension
	 *
	 * @param string $extensionName
	 * @param string $pluginName in BE mode this is actually the module signature. But we're using it just like the plugin name in FE
	 * @return array
	 */
94
	protected function getPluginConfiguration($extensionName, $pluginName = NULL) {
95
96
		$setup = $this->getTypoScriptSetup();
		$pluginConfiguration = array();
97
98
		if (is_array($setup['module.']['tx_' . strtolower($extensionName) . '.'])) {
			$pluginConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['module.']['tx_' . strtolower($extensionName) . '.']);
99
		}
100
		if ($pluginName !== NULL) {
101
102
			$pluginSignature = strtolower($extensionName . '_' . $pluginName);
			if (is_array($setup['module.']['tx_' . $pluginSignature . '.'])) {
103
104
				$overruleConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['module.']['tx_' . $pluginSignature . '.']);
				\TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($pluginConfiguration, $overruleConfiguration);
105
			}
106
107
108
109
110
111
112
		}
		return $pluginConfiguration;
	}

	/**
	 * Returns the configured controller/action pairs of the specified module in the format
	 * array(
113
114
	 * 'Controller1' => array('action1', 'action2'),
	 * 'Controller2' => array('action3', 'action4')
115
116
117
118
119
120
121
122
123
124
125
126
127
128
	 * )
	 *
	 * @param string $extensionName
	 * @param string $pluginName in BE mode this is actually the module signature. But we're using it just like the plugin name in FE
	 * @return array
	 */
	protected function getSwitchableControllerActions($extensionName, $pluginName) {
		$switchableControllerActions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$pluginName]['controllers'];
		if (!is_array($switchableControllerActions)) {
			$switchableControllerActions = array();
		}
		return $switchableControllerActions;
	}

129
130
131
132
133
134
135
	/**
	 * Returns the page uid of the current page.
	 * If no page is selected, we'll return the uid of the first root page.
	 *
	 * @return integer current page id. If no page is selected current root page id is returned
	 */
	protected function getCurrentPageId() {
136
137
		if ($this->currentPageId !== NULL) {
			return $this->currentPageId;
138
		}
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

		$this->currentPageId = $this->getCurrentPageIdFromGetPostData() ?: $this->getCurrentPageIdFromCurrentSiteRoot();
		$this->currentPageId = $this->currentPageId ?: $this->getCurrentPageIdFromRootTemplate();
		$this->currentPageId = $this->currentPageId ?: self::DEFAULT_BACKEND_STORAGE_PID;

		return $this->currentPageId;
	}

	/**
	 * Gets the current page ID from the GET/POST data.
	 *
	 * @return int the page UID, will be 0 if none has been set
	 */
	protected function getCurrentPageIdFromGetPostData() {
		return (int)\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('id');
	}

	/**
	 * Gets the current page ID from the first site root in tree.
	 *
	 * @return int the page UID, will be 0 if none has been set
	 */
	protected function getCurrentPageIdFromCurrentSiteRoot() {
		$rootPage = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
			'uid', 'pages', 'deleted=0 AND hidden=0 AND is_siteroot=1', '', 'sorting'
		);
		if (empty($rootPage)) {
			return 0;
167
		}
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

		return (int)$rootPage['uid'];
	}

	/**
	 * Gets the current page ID from the first created root template.
	 *
	 * @return int the page UID, will be 0 if none has been set
	 */
	protected function getCurrentPageIdFromRootTemplate() {
		$rootTemplate = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
			'pid', 'sys_template', 'deleted=0 AND hidden=0 AND root=1', '', 'crdate'
		);
		if (empty($rootTemplate)) {
			return 0;
183
		}
184
185

		return (int)$rootTemplate['pid'];
186
	}
187

188
189
190
191
192
193
194
195
196
	/**
	 * Returns the default backend storage pid
	 *
	 * @return string
	 */
	public function getDefaultBackendStoragePid() {
		return $this->getCurrentPageId();
	}

197
	/**
198
199
200
201
	 * We need to set some default request handler if the framework configuration
	 * could not be loaded; to make sure Extbase also works in Backend modules
	 * in all contexts.
	 *
202
	 * @param array $frameworkConfiguration
203
204
205
	 * @return array
	 */
	protected function getContextSpecificFrameworkConfiguration(array $frameworkConfiguration) {
206
207
		if (!isset($frameworkConfiguration['mvc']['requestHandlers'])) {
			$frameworkConfiguration['mvc']['requestHandlers'] = array(
Thomas Maroschik's avatar
Thomas Maroschik committed
208
209
				'TYPO3\\CMS\\Extbase\\Mvc\\Web\\FrontendRequestHandler' => 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\FrontendRequestHandler',
				'TYPO3\\CMS\\Extbase\\Mvc\\Web\\BackendRequestHandler' => 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\BackendRequestHandler'
210
211
			);
		}
212
213
		return $frameworkConfiguration;
	}
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231


	/**
	 * Returns a comma separated list of storagePid that are below a certain storage pid.
	 *
	 * @param string $storagePid Storage PID to start at; multiple PIDs possible as comma-separated list
	 * @param integer $recursionDepth Maximum number of levels to search, 0 to disable recursive lookup
	 * @return string storage PIDs
	 */
	protected function getRecursiveStoragePids($storagePid, $recursionDepth = 0) {
		if ($recursionDepth <= 0) {
			return $storagePid;
		}

		$recursiveStoragePids = '';
		$storagePids = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $storagePid);
		foreach ($storagePids as $startPid) {
			$pids = $this->queryGenerator->getTreeList($startPid, $recursionDepth, 0, 1);
232
			if (strlen($pids) > 0) {
233
234
235
236
237
238
239
				$recursiveStoragePids .= $pids . ',';
			}
		}

		return rtrim($recursiveStoragePids, ',');
	}

240
}