[BUGFIX] Fix configuration of distribution packages
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Controller / ConfigurationController.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Controller;
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\Backend\Template\Components\ButtonBar;
18 use TYPO3\CMS\Backend\View\BackendTemplateView;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
21 use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
22 use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
23
24 /**
25 * Controller for configuration related actions.
26 */
27 class ConfigurationController extends AbstractModuleController
28 {
29 /**
30 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository
31 */
32 protected $configurationItemRepository;
33
34 /**
35 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
36 */
37 protected $extensionRepository;
38
39 /**
40 * @param \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository $configurationItemRepository
41 */
42 public function injectConfigurationItemRepository(\TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository $configurationItemRepository)
43 {
44 $this->configurationItemRepository = $configurationItemRepository;
45 }
46
47 /**
48 * @param \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository
49 */
50 public function injectExtensionRepository(\TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository)
51 {
52 $this->extensionRepository = $extensionRepository;
53 }
54
55 /**
56 * Set up the doc header properly here
57 *
58 * @param ViewInterface $view
59 */
60 protected function initializeView(ViewInterface $view)
61 {
62 if ($view instanceof BackendTemplateView) {
63 /** @var BackendTemplateView $view */
64 parent::initializeView($view);
65 $this->generateMenu();
66 $this->registerDocheaderButtons();
67 }
68 }
69
70 /**
71 * Show the extension configuration form. The whole form field handling is done
72 * in the corresponding view helper
73 *
74 * @param array $extension Extension information, must contain at least the key
75 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
76 */
77 public function showConfigurationFormAction(array $extension)
78 {
79 if (!isset($extension['key'])) {
80 throw new ExtensionManagerException('Extension key not found.', 1359206803);
81 }
82 $this->handleTriggerArguments();
83
84 $extKey = $extension['key'];
85 $configuration = $this->configurationItemRepository->findByExtensionKey($extKey);
86 if ($configuration) {
87 $this->view
88 ->assign('configuration', $configuration)
89 ->assign('extension', $extension);
90 } else {
91 throw new ExtensionManagerException('The extension ' . $extKey . ' has no configuration.', 1476047775);
92 }
93 }
94
95 /**
96 * Save configuration and redirects back to form
97 * or to the show page of a distribution
98 *
99 * @param array $config The new extension configuration
100 * @param string $extensionKey The extension key
101 */
102 public function saveAction(array $config, $extensionKey)
103 {
104 $this->saveConfiguration($config, $extensionKey);
105 /** @var Extension $extension */
106 $extension = $this->extensionRepository->findOneByCurrentVersionByExtensionKey($extensionKey);
107 // Different handling for distribution installation
108 if ($extension instanceof Extension &&
109 $extension->getCategory() === Extension::DISTRIBUTION_CATEGORY
110 ) {
111 $this->redirect('show', 'Distribution', null, ['extension' => $extension->getUid()]);
112 } else {
113 $this->redirect('showConfigurationForm', null, null, [
114 'extension' => [
115 'key' => $extensionKey
116 ],
117 self::TRIGGER_RefreshTopbar => true
118 ]);
119 }
120 }
121
122 /**
123 * Saves new configuration and redirects back to list
124 *
125 * @param array $config
126 * @param string $extensionKey
127 */
128 public function saveAndCloseAction(array $config, $extensionKey)
129 {
130 $this->saveConfiguration($config, $extensionKey);
131 $this->redirect('index', 'List', null, [
132 self::TRIGGER_RefreshTopbar => true
133 ]);
134 }
135
136 /**
137 * Emits a signal after the configuration file was written
138 *
139 * @param string $extensionKey
140 * @param array $newConfiguration
141 */
142 protected function emitAfterExtensionConfigurationWriteSignal($extensionKey, array $newConfiguration)
143 {
144 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionConfigurationWrite', [$extensionKey, $newConfiguration, $this]);
145 }
146
147 /**
148 * Merge and save new configuration
149 *
150 * @param array $config
151 * @param $extensionKey
152 */
153 protected function saveConfiguration(array $config, $extensionKey)
154 {
155 /** @var $configurationUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
156 $configurationUtility = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility::class);
157 $newConfiguration = $configurationUtility->getCurrentConfiguration($extensionKey);
158 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($newConfiguration, $config);
159 $configurationUtility->writeConfiguration(
160 $configurationUtility->convertValuedToNestedConfiguration($newConfiguration),
161 $extensionKey
162 );
163 $this->emitAfterExtensionConfigurationWriteSignal($extensionKey, $newConfiguration);
164 }
165
166 /**
167 * Registers the Icons into the docheader
168 *
169 * @throws \InvalidArgumentException
170 */
171 protected function registerDocheaderButtons()
172 {
173 $moduleTemplate = $this->view->getModuleTemplate();
174 $lang = $this->getLanguageService();
175
176 /** @var ButtonBar $buttonBar */
177 $buttonBar = $moduleTemplate->getDocHeaderComponent()->getButtonBar();
178
179 $uriBuilder = $this->controllerContext->getUriBuilder();
180 $uri = $uriBuilder->reset()->uriFor('index', [], 'List');
181
182 $icon = $this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL);
183 $goBackButton = $buttonBar->makeLinkButton()
184 ->setHref($uri)
185 ->setTitle($this->translate('extConfTemplate.backToList'))
186 ->setIcon($icon);
187 $buttonBar->addButton($goBackButton, ButtonBar::BUTTON_POSITION_LEFT);
188
189 $saveSplitButton = $buttonBar->makeSplitButton();
190 // SAVE button:
191 $saveButton = $buttonBar->makeInputButton()
192 ->setName('_savedok')
193 ->setValue('1')
194 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.saveDoc'))
195 ->setForm('configurationform')
196 ->setIcon($moduleTemplate->getIconFactory()->getIcon('actions-document-save', Icon::SIZE_SMALL));
197 $saveSplitButton->addItem($saveButton, true);
198
199 // SAVE / CLOSE
200 $saveAndCloseButton = $buttonBar->makeInputButton()
201 ->setName('_saveandclosedok')
202 ->setClasses('t3js-save-close')
203 ->setValue('1')
204 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.saveCloseDoc'))
205 ->setForm('configurationform')
206 ->setIcon($moduleTemplate->getIconFactory()->getIcon(
207 'actions-document-save-close',
208 Icon::SIZE_SMALL
209 ));
210 $saveSplitButton->addItem($saveAndCloseButton);
211 $buttonBar->addButton($saveSplitButton, ButtonBar::BUTTON_POSITION_LEFT, 2);
212 }
213
214 /**
215 * @return \TYPO3\CMS\Lang\LanguageService
216 */
217 protected function getLanguageService()
218 {
219 return $GLOBALS['LANG'];
220 }
221 }