[+BUGFIX] Extbase: use 3rd parameter = TRUE of t3lib_div::trimExplode to split switch...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Configuration / FrontendConfigurationManager.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * A general purpose configuration manager used in frontend mode.
27 *
28 * Should NOT be singleton, as a new configuration manager is needed per plugin.
29 *
30 * @package Extbase
31 * @subpackage Configuration
32 * @version $ID:$
33 */
34 class Tx_Extbase_Configuration_FrontendConfigurationManager extends Tx_Extbase_Configuration_AbstractConfigurationManager {
35
36 /**
37 * Returns TypoScript Setup array from current Environment.
38 *
39 * @return array the raw TypoScript setup
40 */
41 public function getTypoScriptSetup() {
42 return $GLOBALS['TSFE']->tmpl->setup;
43 }
44
45 /**
46 * Get context specific framework configuration.
47 * - Overrides storage PID with setting "Startingpoint"
48 * - merge flexform configuration, if needed
49 *
50 * @param array $frameworkConfiguration The framework configuration to modify
51 * @return array the modified framework configuration
52 */
53 protected function getContextSpecificFrameworkConfiguration($frameworkConfiguration) {
54 $frameworkConfiguration = $this->overrideStoragePidIfStartingPointIsSet($frameworkConfiguration);
55 $frameworkConfiguration = $this->overrideConfigurationFromPlugin($frameworkConfiguration);
56 $frameworkConfiguration = $this->overrideConfigurationFromFlexform($frameworkConfiguration);
57
58 return $frameworkConfiguration;
59 }
60
61 /**
62 * Overrides the storage PID settings, in case the "Startingpoint" settings
63 * is set in the plugin configuration.
64 *
65 * @param array $frameworkConfiguration the framework configurations
66 * @return array the framework configuration with overriden storagePid
67 */
68 protected function overrideStoragePidIfStartingPointIsSet($frameworkConfiguration) {
69 $pages = $this->contentObject->data['pages'];
70 if (is_string($pages) && strlen($pages) > 0) {
71 $list = array();
72 if($this->contentObject->data['recursive'] > 0) {
73 $explodedPages = t3lib_div::trimExplode(',', $pages);
74 foreach($explodedPages as $pid) {
75 $list[] = trim($this->contentObject->getTreeList($pid, $this->contentObject->data['recursive']), ',');
76 }
77 }
78 if (count($list) > 0) {
79 $pages = $pages . ',' . implode(',', $list);
80 }
81 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, array(
82 'persistence' => array(
83 'storagePid' => $pages
84 )
85 ));
86 }
87 return $frameworkConfiguration;
88 }
89
90 /**
91 * Overrides configuration settings from the plugin typoscript (plugin.tx_myext_pi1.)
92 *
93 * @param array the framework configuration
94 * @return array the framework configuration with overridden data from typoscript
95 */
96 protected function overrideConfigurationFromPlugin(array $frameworkConfiguration) {
97 $setup = $this->getTypoScriptSetup();
98 $pluginSignature = strtolower($frameworkConfiguration['extensionName'] . '_' . $frameworkConfiguration['pluginName']);
99 $pluginConfiguration = $setup['plugin.']['tx_' . $pluginSignature . '.'];
100 if (is_array($pluginConfiguration)) {
101 $pluginConfiguration = Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($pluginConfiguration);
102 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $pluginConfiguration, 'settings');
103 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $pluginConfiguration, 'persistence');
104 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $pluginConfiguration, 'view');
105 }
106 return $frameworkConfiguration;
107 }
108
109 /**
110 * Overrides configuration settings from flexforms.
111 * This merges the whole flexform data, and overrides switchable controller actions.
112 *
113 * @param array the framework configuration
114 * @return array the framework configuration with overridden data from flexform
115 */
116 protected function overrideConfigurationFromFlexform(array $frameworkConfiguration) {
117 if (strlen($this->contentObject->data['pi_flexform']) > 0) {
118 $flexformConfiguration = $this->convertFlexformContentToArray($this->contentObject->data['pi_flexform']);
119
120 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $flexformConfiguration, 'settings');
121 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $flexformConfiguration, 'persistence');
122 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $flexformConfiguration, 'view');
123
124 $frameworkConfiguration = $this->overrideSwitchableControllerActionsFromFlexform($frameworkConfiguration, $flexformConfiguration);
125 }
126 return $frameworkConfiguration;
127 }
128
129 /**
130 * Parses the FlexForm content recursivly and converts it to an array
131 * The resulting array will be multi-dimensional, as a value "bla.blubb"
132 * results in two levels, and a value "bla.blubb.bla" results in three levels.
133 *
134 * Note: multi-language FlexForms are not supported yet
135 *
136 * @param string $flexFormContent FlexForm xml string
137 * @return array the processed array
138 */
139 protected function convertFlexformContentToArray($flexFormContent) {
140 $settings = array();
141 $languagePointer = 'lDEF';
142 $valuePointer = 'vDEF';
143
144 $flexFormArray = t3lib_div::xml2array($flexFormContent);
145 $flexFormArray = isset($flexFormArray['data']) ? $flexFormArray['data'] : array();
146 foreach(array_values($flexFormArray) as $languages) {
147 if (!is_array($languages[$languagePointer])) {
148 continue;
149 }
150 foreach($languages[$languagePointer] as $valueKey => $valueDefinition) {
151 if (strpos($valueKey, '.') === false) {
152 $settings[$valueKey] = $valueDefinition[$valuePointer];
153 } else {
154 $valueKeyParts = explode('.', $valueKey);
155 $currentNode =& $settings;
156 foreach ($valueKeyParts as $valueKeyPart) {
157 $currentNode =& $currentNode[$valueKeyPart];
158 }
159 $currentNode = $valueDefinition[$valuePointer];
160 }
161 }
162 }
163 return $settings;
164 }
165
166 /**
167 * Merge a configuration into the framework configuration.
168 *
169 * @param array $frameworkConfiguration the framework configuration to merge the data on
170 * @param array $configuration The configuration
171 * @param string $configurationPartName The name of the configuration part which should be merged.
172 * @return array the processed framework configuration
173 */
174 protected function mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $configuration, $configurationPartName) {
175 if (is_array($frameworkConfiguration[$configurationPartName]) && is_array($configuration[$configurationPartName])) {
176 $frameworkConfiguration[$configurationPartName] = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration[$configurationPartName], $configuration[$configurationPartName]);
177 }
178 return $frameworkConfiguration;
179 }
180
181
182 /**
183 * Overrides the switchable controller actions from the flexform.
184 *
185 * @param $frameworkConfiguration The original framework configuration
186 * @param $flexformConfiguration The full flexform configuration
187 * @return array the modified framework configuration, if needed
188 * @todo: Check that the controller has been before inside the switchableControllerActions.
189 */
190 protected function overrideSwitchableControllerActionsFromFlexform($frameworkConfiguration, $flexformConfiguration) {
191 if (isset($flexformConfiguration['switchableControllerActions']) && !is_array($flexformConfiguration['switchableControllerActions'])) {
192
193 // As "," is the flexform field value delimiter, we need to use ";" as in-field delimiter. That's why we need to replace ; by , first.
194 $switchableControllerActionPartsFromFlexform = t3lib_div::trimExplode(',', str_replace(';', ',', $flexformConfiguration['switchableControllerActions']), TRUE);
195
196 $newSwitchableControllerActionsFromFlexform = array();
197 foreach ($switchableControllerActionPartsFromFlexform as $switchableControllerActionPartFromFlexform) {
198 list($controller, $action) = explode('->', $switchableControllerActionPartFromFlexform);
199 if (empty($controller) || empty($action)) {
200 throw new Tx_Extbase_Configuration_Exception_ParseError('Controller or action were empty when overriding switchableControllerActions from flexform.', 1257146403);
201 }
202
203 $newSwitchableControllerActionsFromFlexform[$controller][] = $action;
204 }
205
206 if (count($newSwitchableControllerActionsFromFlexform)) {
207 $overriddenSwitchableControllerActions = array();
208 foreach ($newSwitchableControllerActionsFromFlexform as $controller => $actions) {
209 $overriddenSwitchableControllerActions[$controller] = array(
210 'controller' => $controller,
211 'actions' => implode(',', $actions)
212 );
213 $nonCacheableActions = t3lib_div::trimExplode(',', $frameworkConfiguration['switchableControllerActions'][$controller]['nonCacheableActions']);
214 $overriddenNonCacheableActions = array_intersect($nonCacheableActions, $actions);
215 if (!empty($overriddenNonCacheableActions)) {
216 $overriddenSwitchableControllerActions[$controller]['nonCacheableActions'] = implode(',', $overriddenNonCacheableActions);
217 }
218 }
219 $frameworkConfiguration['switchableControllerActions'] = $overriddenSwitchableControllerActions;
220
221 // We want the first controller/action be the default.
222 unset($frameworkConfiguration['controller']);
223 unset($frameworkConfiguration['action']);
224 }
225 }
226 return $frameworkConfiguration;
227 }
228 }
229 ?>