d9b35f52575b297ac009fb53b6ecf656bd5fa8d4
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Configuration / FrontendConfigurationManager.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 frontend mode.
32 *
33 * Should NOT be singleton, as a new configuration manager is needed per plugin.
34 */
35 class FrontendConfigurationManager extends \TYPO3\CMS\Extbase\Configuration\AbstractConfigurationManager {
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Service\FlexFormService
39 * @inject
40 */
41 protected $flexFormService;
42
43 /**
44 * Returns TypoScript Setup array from current Environment.
45 *
46 * @return array the raw TypoScript setup
47 */
48 public function getTypoScriptSetup() {
49 return $GLOBALS['TSFE']->tmpl->setup;
50 }
51
52 /**
53 * Returns the TypoScript configuration found in plugin.tx_yourextension_yourplugin
54 * merged with the global configuration of your extension from plugin.tx_yourextension
55 *
56 * @param string $extensionName
57 * @param string $pluginName
58 * @return array
59 */
60 protected function getPluginConfiguration($extensionName, $pluginName = NULL) {
61 $setup = $this->getTypoScriptSetup();
62 $pluginConfiguration = array();
63 if (is_array($setup['plugin.']['tx_' . strtolower($extensionName) . '.'])) {
64 $pluginConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['plugin.']['tx_' . strtolower($extensionName) . '.']);
65 }
66 if ($pluginName !== NULL) {
67 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
68 if (is_array($setup['plugin.']['tx_' . $pluginSignature . '.'])) {
69 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule(
70 $pluginConfiguration,
71 $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['plugin.']['tx_' . $pluginSignature . '.'])
72 );
73 }
74 }
75 return $pluginConfiguration;
76 }
77
78 /**
79 * Returns the configured controller/action pairs of the specified plugin in the format
80 * array(
81 * 'Controller1' => array('action1', 'action2'),
82 * 'Controller2' => array('action3', 'action4')
83 * )
84 *
85 * @param string $extensionName
86 * @param string $pluginName
87 * @return array
88 */
89 protected function getSwitchableControllerActions($extensionName, $pluginName) {
90 $switchableControllerActions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'];
91 if (!is_array($switchableControllerActions)) {
92 $switchableControllerActions = array();
93 }
94 return $switchableControllerActions;
95 }
96
97 /**
98 * Get context specific framework configuration.
99 * - Overrides storage PID with setting "Startingpoint"
100 * - merge flexForm configuration, if needed
101 *
102 * @param array $frameworkConfiguration The framework configuration to modify
103 * @return array the modified framework configuration
104 */
105 protected function getContextSpecificFrameworkConfiguration(array $frameworkConfiguration) {
106 $frameworkConfiguration = $this->overrideStoragePidIfStartingPointIsSet($frameworkConfiguration);
107 $frameworkConfiguration = $this->overrideConfigurationFromPlugin($frameworkConfiguration);
108 $frameworkConfiguration = $this->overrideConfigurationFromFlexForm($frameworkConfiguration);
109 return $frameworkConfiguration;
110 }
111
112 /**
113 * Overrides the storage PID settings, in case the "Startingpoint" settings
114 * is set in the plugin configuration.
115 *
116 * @param array $frameworkConfiguration the framework configurations
117 * @return array the framework configuration with overriden storagePid
118 */
119 protected function overrideStoragePidIfStartingPointIsSet(array $frameworkConfiguration) {
120 $pages = $this->contentObject->data['pages'];
121 if (is_string($pages) && strlen($pages) > 0) {
122 $list = array();
123 if ($this->contentObject->data['recursive'] > 0) {
124 $explodedPages = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $pages);
125 foreach ($explodedPages as $pid) {
126 $list[] = $this->contentObject->getTreeList($pid, $this->contentObject->data['recursive']);
127 }
128 }
129 if (count($list) > 0) {
130 $pages = $pages . ',' . implode(',', $list);
131 }
132 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($frameworkConfiguration, array(
133 'persistence' => array(
134 'storagePid' => $pages
135 )
136 ));
137 }
138 return $frameworkConfiguration;
139 }
140
141 /**
142 * Overrides configuration settings from the plugin typoscript (plugin.tx_myext_pi1.)
143 *
144 * @param array $frameworkConfiguration the framework configuration
145 * @return array the framework configuration with overridden data from typoscript
146 */
147 protected function overrideConfigurationFromPlugin(array $frameworkConfiguration) {
148 $setup = $this->getTypoScriptSetup();
149 $pluginSignature = strtolower($frameworkConfiguration['extensionName'] . '_' . $frameworkConfiguration['pluginName']);
150 $pluginConfiguration = $setup['plugin.']['tx_' . $pluginSignature . '.'];
151 if (is_array($pluginConfiguration)) {
152 $pluginConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($pluginConfiguration);
153 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $pluginConfiguration, 'settings');
154 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $pluginConfiguration, 'persistence');
155 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $pluginConfiguration, 'view');
156 }
157 return $frameworkConfiguration;
158 }
159
160 /**
161 * Overrides configuration settings from flexForms.
162 * This merges the whole flexForm data, and overrides switchable controller actions.
163 *
164 * @param array $frameworkConfiguration the framework configuration
165 * @return array the framework configuration with overridden data from flexForm
166 */
167 protected function overrideConfigurationFromFlexForm(array $frameworkConfiguration) {
168 $flexFormConfiguration = $this->contentObject->data['pi_flexform'];
169 if (is_string($flexFormConfiguration)) {
170 if (strlen($flexFormConfiguration) > 0) {
171 $flexFormConfiguration = $this->flexFormService->convertFlexFormContentToArray($flexFormConfiguration);
172 } else {
173 $flexFormConfiguration = array();
174 }
175 }
176 if (is_array($flexFormConfiguration) && count($flexFormConfiguration)) {
177 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $flexFormConfiguration, 'settings');
178 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $flexFormConfiguration, 'persistence');
179 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $flexFormConfiguration, 'view');
180 $frameworkConfiguration = $this->overrideSwitchableControllerActionsFromFlexForm($frameworkConfiguration, $flexFormConfiguration);
181 }
182 return $frameworkConfiguration;
183 }
184
185 /**
186 * Merge a configuration into the framework configuration.
187 *
188 * @param array $frameworkConfiguration the framework configuration to merge the data on
189 * @param array $configuration The configuration
190 * @param string $configurationPartName The name of the configuration part which should be merged.
191 * @return array the processed framework configuration
192 */
193 protected function mergeConfigurationIntoFrameworkConfiguration(array $frameworkConfiguration, array $configuration, $configurationPartName) {
194 if (is_array($frameworkConfiguration[$configurationPartName]) && is_array($configuration[$configurationPartName])) {
195 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($frameworkConfiguration[$configurationPartName], $configuration[$configurationPartName]);
196 }
197 return $frameworkConfiguration;
198 }
199
200 /**
201 * Overrides the switchable controller actions from the flexForm.
202 *
203 * @param array $frameworkConfiguration The original framework configuration
204 * @param array $flexFormConfiguration The full flexForm configuration
205 * @throws Exception\ParseErrorException
206 * @return array the modified framework configuration, if needed
207 */
208 protected function overrideSwitchableControllerActionsFromFlexForm(array $frameworkConfiguration, array $flexFormConfiguration) {
209 if (!isset($flexFormConfiguration['switchableControllerActions']) || is_array($flexFormConfiguration['switchableControllerActions'])) {
210 return $frameworkConfiguration;
211 }
212 // As "," is the flexForm field value delimiter, we need to use ";" as in-field delimiter. That's why we need to replace ; by , first.
213 // The expected format is: "Controller1->action2;Controller2->action3;Controller2->action1"
214 $switchableControllerActionPartsFromFlexForm = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', str_replace(';', ',', $flexFormConfiguration['switchableControllerActions']), TRUE);
215 $newSwitchableControllerActionsFromFlexForm = array();
216 foreach ($switchableControllerActionPartsFromFlexForm as $switchableControllerActionPartFromFlexForm) {
217 list($controller, $action) = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('->', $switchableControllerActionPartFromFlexForm);
218 if (empty($controller) || empty($action)) {
219 throw new \TYPO3\CMS\Extbase\Configuration\Exception\ParseErrorException('Controller or action were empty when overriding switchableControllerActions from flexForm.', 1257146403);
220 }
221 $newSwitchableControllerActionsFromFlexForm[$controller][] = $action;
222 }
223 if (count($newSwitchableControllerActionsFromFlexForm) > 0) {
224 $this->overrideSwitchableControllerActions($frameworkConfiguration, $newSwitchableControllerActionsFromFlexForm);
225 }
226 return $frameworkConfiguration;
227 }
228
229 /**
230 * Returns a comma separated list of storagePid that are below a certain storage pid.
231 *
232 * @param string $storagePid Storage PID to start at; multiple PIDs possible as comma-separated list
233 * @param integer $recursionDepth Maximum number of levels to search, 0 to disable recursive lookup
234 * @return string storage PIDs
235 */
236 protected function getRecursiveStoragePids($storagePid, $recursionDepth = 0) {
237 if ($recursionDepth <= 0) {
238 return $storagePid;
239 }
240
241 $recursiveStoragePids = '';
242 $storagePids = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $storagePid);
243 foreach ($storagePids as $startPid) {
244 $pids = $this->getContentObject()->getTreeList($startPid, $recursionDepth, 0);
245 if (strlen($pids) > 0) {
246 $recursiveStoragePids .= $pids . ',';
247 }
248 }
249 return rtrim($recursiveStoragePids, ',');
250 }
251 }