[TASK] Removes extra empty lines
[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 * @return void
60 */
61 protected function initializeView(ViewInterface $view)
62 {
63 if ($view instanceof BackendTemplateView) {
64 /** @var BackendTemplateView $view */
65 parent::initializeView($view);
66 $this->generateMenu();
67 $this->registerDocheaderButtons();
68 }
69 }
70
71 /**
72 * Show the extension configuration form. The whole form field handling is done
73 * in the corresponding view helper
74 *
75 * @param array $extension Extension information, must contain at least the key
76 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
77 * @return void
78 */
79 public function showConfigurationFormAction(array $extension)
80 {
81 if (!isset($extension['key'])) {
82 throw new ExtensionManagerException('Extension key not found.', 1359206803);
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, array('extension' => $extension->getUid()));
96 }
97 throw new ExtensionManagerException('The extension ' . $extKey . ' has no configuration.');
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 * @return void
108 */
109 public function saveAction(array $config, $extensionKey)
110 {
111 $this->saveConfiguration($config, $extensionKey);
112 /** @var Extension $extension */
113 $extension = $this->extensionRepository->findOneByCurrentVersionByExtensionKey($extensionKey);
114 // Different handling for distribution installation
115 if ($extension instanceof Extension &&
116 $extension->getCategory() === Extension::DISTRIBUTION_CATEGORY
117 ) {
118 $this->redirect('welcome', 'Distribution', null, array('extension' => $extension->getUid()));
119 } else {
120 $this->redirect('showConfigurationForm', null, null, array('extension' => array('key' => $extensionKey)));
121 }
122 }
123
124 /**
125 * Saves new configuration and redirects back to list
126 *
127 * @param array $config
128 * @param string $extensionKey
129 * @return void
130 */
131 public function saveAndCloseAction(array $config, $extensionKey)
132 {
133 $this->saveConfiguration($config, $extensionKey);
134 $this->redirect('index', 'List');
135 }
136
137 /**
138 * Emits a signal after the configuration file was written
139 *
140 * @param string $extensionKey
141 * @param array $newConfiguration
142 * @return void
143 */
144 protected function emitAfterExtensionConfigurationWriteSignal($extensionKey, array $newConfiguration)
145 {
146 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionConfigurationWrite', array($extensionKey, $newConfiguration, $this));
147 }
148
149 /**
150 * Merge and save new configuration
151 *
152 * @param array $config
153 * @param $extensionKey
154 * @return void
155 */
156 protected function saveConfiguration(array $config, $extensionKey)
157 {
158 /** @var $configurationUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
159 $configurationUtility = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility::class);
160 $newConfiguration = $configurationUtility->getCurrentConfiguration($extensionKey);
161 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($newConfiguration, $config);
162 $configurationUtility->writeConfiguration(
163 $configurationUtility->convertValuedToNestedConfiguration($newConfiguration),
164 $extensionKey
165 );
166 $this->emitAfterExtensionConfigurationWriteSignal($extensionKey, $newConfiguration);
167 }
168
169 /**
170 * Registers the Icons into the docheader
171 *
172 * @return void
173 * @throws \InvalidArgumentException
174 */
175 protected function registerDocheaderButtons()
176 {
177 $moduleTemplate = $this->view->getModuleTemplate();
178 $lang = $this->getLanguageService();
179
180 /** @var ButtonBar $buttonBar */
181 $buttonBar = $moduleTemplate->getDocHeaderComponent()->getButtonBar();
182
183 $uriBuilder = $this->controllerContext->getUriBuilder();
184 $uri = $uriBuilder->reset()->uriFor('index', [], 'List');
185
186 $icon = $this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL);
187 $goBackButton = $buttonBar->makeLinkButton()
188 ->setHref($uri)
189 ->setTitle($this->translate('extConfTemplate.backToList'))
190 ->setIcon($icon);
191 $buttonBar->addButton($goBackButton, ButtonBar::BUTTON_POSITION_LEFT);
192
193 $saveSplitButton = $buttonBar->makeSplitButton();
194 // SAVE button:
195 $saveButton = $buttonBar->makeInputButton()
196 ->setName('_savedok')
197 ->setValue('1')
198 ->setTitle($lang->sL('LLL:EXT:lang/locallang_core.xlf:rm.saveDoc', true))
199 ->setForm('configurationform')
200 ->setIcon($moduleTemplate->getIconFactory()->getIcon('actions-document-save', Icon::SIZE_SMALL));
201 $saveSplitButton->addItem($saveButton, true);
202
203 // SAVE / CLOSE
204 $saveAndCloseButton = $buttonBar->makeInputButton()
205 ->setName('_saveandclosedok')
206 ->setClasses('t3js-save-close')
207 ->setValue('1')
208 ->setTitle($lang->sL('LLL:EXT:lang/locallang_core.xlf:rm.saveCloseDoc', true))
209 ->setForm('configurationform')
210 ->setIcon($moduleTemplate->getIconFactory()->getIcon(
211 'actions-document-save-close',
212 Icon::SIZE_SMALL
213 ));
214 $saveSplitButton->addItem($saveAndCloseButton);
215 $buttonBar->addButton($saveSplitButton, ButtonBar::BUTTON_POSITION_LEFT, 2);
216 }
217
218 /**
219 * @return \TYPO3\CMS\Lang\LanguageService
220 */
221 protected function getLanguageService()
222 {
223 return $GLOBALS['LANG'];
224 }
225 }