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