c229f29d866430e3b55063a7c7ab22002be4de54
[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 /** @var Extension $extension */
92 $extension = $this->extensionRepository->findOneByCurrentVersionByExtensionKey($extKey);
93 // Extension has no configuration and is a distribution
94 if ($extension->getCategory() === Extension::DISTRIBUTION_CATEGORY) {
95 $this->redirect('welcome', 'Distribution', null, ['extension' => $extension->getUid()]);
96 }
97 throw new ExtensionManagerException('The extension ' . $extKey . ' has no configuration.', 1476047775);
98 }
99 }
100
101 /**
102 * Save configuration and redirects back to form
103 * or to the welcome page of a distribution
104 *
105 * @param array $config The new extension configuration
106 * @param string $extensionKey The extension key
107 */
108 public function saveAction(array $config, $extensionKey)
109 {
110 $this->saveConfiguration($config, $extensionKey);
111 /** @var Extension $extension */
112 $extension = $this->extensionRepository->findOneByCurrentVersionByExtensionKey($extensionKey);
113 // Different handling for distribution installation
114 if ($extension instanceof Extension &&
115 $extension->getCategory() === Extension::DISTRIBUTION_CATEGORY
116 ) {
117 $this->redirect('welcome', 'Distribution', null, ['extension' => $extension->getUid()]);
118 } else {
119 $this->redirect('showConfigurationForm', null, null, [
120 'extension' => [
121 'key' => $extensionKey
122 ],
123 self::TRIGGER_RefreshTopbar => true
124 ]);
125 }
126 }
127
128 /**
129 * Saves new configuration and redirects back to list
130 *
131 * @param array $config
132 * @param string $extensionKey
133 */
134 public function saveAndCloseAction(array $config, $extensionKey)
135 {
136 $this->saveConfiguration($config, $extensionKey);
137 $this->redirect('index', 'List', null, [
138 self::TRIGGER_RefreshTopbar => true
139 ]);
140 }
141
142 /**
143 * Emits a signal after the configuration file was written
144 *
145 * @param string $extensionKey
146 * @param array $newConfiguration
147 */
148 protected function emitAfterExtensionConfigurationWriteSignal($extensionKey, array $newConfiguration)
149 {
150 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionConfigurationWrite', [$extensionKey, $newConfiguration, $this]);
151 }
152
153 /**
154 * Merge and save new configuration
155 *
156 * @param array $config
157 * @param $extensionKey
158 */
159 protected function saveConfiguration(array $config, $extensionKey)
160 {
161 /** @var $configurationUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
162 $configurationUtility = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility::class);
163 $newConfiguration = $configurationUtility->getCurrentConfiguration($extensionKey);
164 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($newConfiguration, $config);
165 $configurationUtility->writeConfiguration(
166 $configurationUtility->convertValuedToNestedConfiguration($newConfiguration),
167 $extensionKey
168 );
169 $this->emitAfterExtensionConfigurationWriteSignal($extensionKey, $newConfiguration);
170 }
171
172 /**
173 * Registers the Icons into the docheader
174 *
175 * @throws \InvalidArgumentException
176 */
177 protected function registerDocheaderButtons()
178 {
179 $moduleTemplate = $this->view->getModuleTemplate();
180 $lang = $this->getLanguageService();
181
182 /** @var ButtonBar $buttonBar */
183 $buttonBar = $moduleTemplate->getDocHeaderComponent()->getButtonBar();
184
185 $uriBuilder = $this->controllerContext->getUriBuilder();
186 $uri = $uriBuilder->reset()->uriFor('index', [], 'List');
187
188 $icon = $this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL);
189 $goBackButton = $buttonBar->makeLinkButton()
190 ->setHref($uri)
191 ->setTitle($this->translate('extConfTemplate.backToList'))
192 ->setIcon($icon);
193 $buttonBar->addButton($goBackButton, ButtonBar::BUTTON_POSITION_LEFT);
194
195 $saveSplitButton = $buttonBar->makeSplitButton();
196 // SAVE button:
197 $saveButton = $buttonBar->makeInputButton()
198 ->setName('_savedok')
199 ->setValue('1')
200 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.saveDoc'))
201 ->setForm('configurationform')
202 ->setIcon($moduleTemplate->getIconFactory()->getIcon('actions-document-save', Icon::SIZE_SMALL));
203 $saveSplitButton->addItem($saveButton, true);
204
205 // SAVE / CLOSE
206 $saveAndCloseButton = $buttonBar->makeInputButton()
207 ->setName('_saveandclosedok')
208 ->setClasses('t3js-save-close')
209 ->setValue('1')
210 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.saveCloseDoc'))
211 ->setForm('configurationform')
212 ->setIcon($moduleTemplate->getIconFactory()->getIcon(
213 'actions-document-save-close',
214 Icon::SIZE_SMALL
215 ));
216 $saveSplitButton->addItem($saveAndCloseButton);
217 $buttonBar->addButton($saveSplitButton, ButtonBar::BUTTON_POSITION_LEFT, 2);
218 }
219
220 /**
221 * @return \TYPO3\CMS\Lang\LanguageService
222 */
223 protected function getLanguageService()
224 {
225 return $GLOBALS['LANG'];
226 }
227 }