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