ad03677d09dbae9acc1ba219f3268f30b5be7198
[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 * @var tslib_cObj
38 */
39 protected $contentObject;
40
41 /**
42 * @param tslib_cObj $contentObject
43 * @return void
44 */
45 public function setContentObject(tslib_cObj $contentObject) {
46 $this->contentObject = $contentObject;
47 }
48
49 /**
50 * Returns TypoScript Setup array from current Environment.
51 *
52 * @return array the TypoScript setup
53 */
54 public function loadTypoScriptSetup() {
55 return $GLOBALS['TSFE']->tmpl->setup;
56 }
57
58 /**
59 * Get context specific framework configuration.
60 * - Overrides storage PID with setting "Startingpoint"
61 * - merge flexform configuration, if needed
62 *
63 * @param array $frameworkConfiguration The framework configuration to modify
64 * @return array the modified framework configuration
65 */
66 protected function getContextSpecificFrameworkConfiguration($frameworkConfiguration) {
67 $frameworkConfiguration = $this->overrideStoragePidIfStartingPointIsSet($frameworkConfiguration);
68 $frameworkConfiguration = $this->overrideConfigurationFromPlugin($frameworkConfiguration);
69 $frameworkConfiguration = $this->overrideConfigurationFromFlexform($frameworkConfiguration);
70
71 return $frameworkConfiguration;
72 }
73
74 /**
75 * Overrides the storage PID settings, in case the "Startingpoint" settings
76 * is set in the plugin configuration.
77 *
78 * @param array $frameworkConfiguration the framework configurations
79 * @return array the framework configuration with overriden storagePid
80 */
81 protected function overrideStoragePidIfStartingPointIsSet($frameworkConfiguration) {
82 $pages = $this->contentObject->data['pages'];
83 if (is_string($pages) && strlen($pages) > 0) {
84 $list = array();
85 if($this->contentObject->data['recursive'] > 0) {
86 $explodedPages = t3lib_div::trimExplode(',', $pages);
87 foreach($explodedPages as $pid) {
88 $list[] = trim($this->contentObject->getTreeList($pid, $this->contentObject->data['recursive']), ',');
89 }
90 }
91 if (count($list) > 0) {
92 $pages = $pages . ',' . implode(',', $list);
93 }
94 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, array(
95 'persistence' => array(
96 'storagePid' => $pages
97 )
98 ));
99 }
100 return $frameworkConfiguration;
101 }
102
103 /**
104 * Overrides configuration settings from the plugin typoscript (plugin.tx_myext_pi1.)
105 *
106 * @param array the framework configuration
107 * @return array the framework configuration with overridden data from typoscript
108 */
109 protected function overrideConfigurationFromPlugin(array $frameworkConfiguration) {
110 $setup = $this->loadTypoScriptSetup();
111 $pluginConfiguration = $setup['plugin.']['tx_' . strtolower($frameworkConfiguration['extensionName'] . '_' . $frameworkConfiguration['pluginName']) . '.'];
112 if (is_array($pluginConfiguration)) {
113 $pluginConfiguration = Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($pluginConfiguration);
114 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $pluginConfiguration, 'settings');
115 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $pluginConfiguration, 'persistence');
116 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $pluginConfiguration, 'view');
117 }
118 return $frameworkConfiguration;
119 }
120
121 /**
122 * Overrides configuration settings from flexforms.
123 * This merges the whole flexform data, and overrides switchable controller actions.
124 *
125 * @param array the framework configuration
126 * @return array the framework configuration with overridden data from flexform
127 */
128 protected function overrideConfigurationFromFlexform(array $frameworkConfiguration) {
129 if (strlen($this->contentObject->data['pi_flexform']) > 0) {
130 $flexformConfiguration = $this->convertFlexformContentToArray($this->contentObject->data['pi_flexform']);
131
132 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $flexformConfiguration, 'settings');
133 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $flexformConfiguration, 'persistence');
134 $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $flexformConfiguration, 'view');
135
136 $frameworkConfiguration = $this->overrideSwitchableControllerActionsFromFlexform($frameworkConfiguration, $flexformConfiguration);
137 }
138 return $frameworkConfiguration;
139 }
140
141 /**
142 * Parses the FlexForm content recursivly and converts it to an array
143 * The resulting array will be multi-dimensional, as a value "bla.blubb"
144 * results in two levels, and a value "bla.blubb.bla" results in three levels.
145 *
146 * Note: multi-language FlexForms are not supported yet
147 *
148 * @param string $flexFormContent FlexForm xml string
149 * @return array the processed array
150 */
151 protected function convertFlexformContentToArray($flexFormContent) {
152 $settings = array();
153 $languagePointer = 'lDEF';
154 $valuePointer = 'vDEF';
155
156 $flexFormArray = t3lib_div::xml2array($flexFormContent);
157 $flexFormArray = isset($flexFormArray['data']) ? $flexFormArray['data'] : array();
158 foreach(array_values($flexFormArray) as $languages) {
159 if (!is_array($languages[$languagePointer])) {
160 continue;
161 }
162 foreach($languages[$languagePointer] as $valueKey => $valueDefinition) {
163 if (strpos($valueKey, '.') === false) {
164 $settings[$valueKey] = $valueDefinition[$valuePointer];
165 } else {
166 $valueKeyParts = explode('.', $valueKey);
167 $currentNode =& $settings;
168 foreach ($valueKeyParts as $valueKeyPart) {
169 $currentNode =& $currentNode[$valueKeyPart];
170 }
171 $currentNode = $valueDefinition[$valuePointer];
172 }
173 }
174 }
175 return $settings;
176 }
177
178 /**
179 * Merge a configuration into the framework configuration.
180 *
181 * @param array $frameworkConfiguration the framework configuration to merge the data on
182 * @param array $configuration The configuration
183 * @param string $configurationPartName The name of the configuration part which should be merged.
184 * @return array the processed framework configuration
185 */
186 protected function mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $configuration, $configurationPartName) {
187 if (is_array($frameworkConfiguration[$configurationPartName]) && is_array($configuration[$configurationPartName])) {
188 $frameworkConfiguration[$configurationPartName] = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration[$configurationPartName], $configuration[$configurationPartName]);
189 }
190 return $frameworkConfiguration;
191 }
192
193
194 /**
195 * Overrides the switchable controller actions from the flexform.
196 *
197 * @param $frameworkConfiguration The original framework configuration
198 * @param $flexformConfiguration The full flexform configuration
199 * @return array the modified framework configuration, if needed
200 * @todo: Check that the controller has been before inside the switchableControllerActions.
201 */
202 protected function overrideSwitchableControllerActionsFromFlexform($frameworkConfiguration, $flexformConfiguration) {
203 if (isset($flexformConfiguration['switchableControllerActions']) && !is_array($flexformConfiguration['switchableControllerActions'])) {
204
205 // As "," is the flexform field value delimiter, we need to use ";" as in-field delimiter. That's why we need to replace ; by , first.
206 $switchableControllerActionPartsFromFlexform = t3lib_div::trimExplode(',', str_replace(';', ',', $flexformConfiguration['switchableControllerActions']));
207
208 $newSwitchableControllerActionsFromFlexform = array();
209 foreach ($switchableControllerActionPartsFromFlexform as $switchableControllerActionPartFromFlexform) {
210 list($controller, $action) = explode('->', $switchableControllerActionPartFromFlexform);
211 if (empty($controller) || empty($action)) {
212 throw new Tx_Extbase_Configuration_Exception_ParseError('Controller or action were empty when overriding switchableControllerActions from flexform.', 1257146403);
213 }
214
215 $newSwitchableControllerActionsFromFlexform[$controller][] = $action;
216 }
217
218 if (count($newSwitchableControllerActionsFromFlexform)) {
219 $overriddenSwitchableControllerActions = array();
220 foreach ($newSwitchableControllerActionsFromFlexform as $controller => $actions) {
221 $overriddenSwitchableControllerActions[$controller] = array(
222 'controller' => $controller,
223 'actions' => implode(',', $actions)
224 );
225 $nonCacheableActions = t3lib_div::trimExplode(',', $frameworkConfiguration['switchableControllerActions'][$controller]['nonCacheableActions']);
226 $overriddenNonCacheableActions = array_intersect($nonCacheableActions, $actions);
227 if (!empty($overriddenNonCacheableActions)) {
228 $overriddenSwitchableControllerActions[$controller]['nonCacheableActions'] = implode(',', $overriddenNonCacheableActions);
229 }
230 }
231 $frameworkConfiguration['switchableControllerActions'] = $overriddenSwitchableControllerActions;
232
233 // We want the first controller/action be the default.
234 unset($frameworkConfiguration['controller']);
235 unset($frameworkConfiguration['action']);
236 }
237 }
238 return $frameworkConfiguration;
239 }
240 }
241 ?>