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