[TASK] Add 'form' attribute to ModuleTemplate buttons
[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 /**
139 * Emits a signal after the configuration file was written
140 *
141 * @param string $extensionKey
142 * @param array $newConfiguration
143 * @return void
144 */
145 protected function emitAfterExtensionConfigurationWriteSignal($extensionKey, array $newConfiguration)
146 {
147 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionConfigurationWrite', array($extensionKey, $newConfiguration, $this));
148 }
149
150 /**
151 * Merge and save new configuration
152 *
153 * @param array $config
154 * @param $extensionKey
155 * @return void
156 */
157 protected function saveConfiguration(array $config, $extensionKey)
158 {
159 /** @var $configurationUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
160 $configurationUtility = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility::class);
161 $newConfiguration = $configurationUtility->getCurrentConfiguration($extensionKey);
162 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($newConfiguration, $config);
163 $configurationUtility->writeConfiguration(
164 $configurationUtility->convertValuedToNestedConfiguration($newConfiguration),
165 $extensionKey
166 );
167 $this->emitAfterExtensionConfigurationWriteSignal($extensionKey, $newConfiguration);
168 }
169
170 /**
171 * Registers the Icons into the docheader
172 *
173 * @return void
174 * @throws \InvalidArgumentException
175 */
176 protected function registerDocheaderButtons()
177 {
178 $moduleTemplate = $this->view->getModuleTemplate();
179 $lang = $this->getLanguageService();
180
181 /** @var ButtonBar $buttonBar */
182 $buttonBar = $moduleTemplate->getDocHeaderComponent()->getButtonBar();
183
184 $uriBuilder = $this->controllerContext->getUriBuilder();
185 $uri = $uriBuilder->reset()->uriFor('index', [], 'List');
186
187 $icon = $this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL);
188 $goBackButton = $buttonBar->makeLinkButton()
189 ->setHref($uri)
190 ->setTitle($this->translate('extConfTemplate.backToList'))
191 ->setIcon($icon);
192 $buttonBar->addButton($goBackButton, ButtonBar::BUTTON_POSITION_LEFT);
193
194 $saveSplitButton = $buttonBar->makeSplitButton();
195 // SAVE button:
196 $saveButton = $buttonBar->makeInputButton()
197 ->setName('_savedok')
198 ->setValue('1')
199 ->setTitle($lang->sL('LLL:EXT:lang/locallang_core.xlf:rm.saveDoc', true))
200 ->setForm('configurationform')
201 ->setIcon($moduleTemplate->getIconFactory()->getIcon('actions-document-save', Icon::SIZE_SMALL));
202 $saveSplitButton->addItem($saveButton, true);
203
204 // SAVE / CLOSE
205 $saveAndCloseButton = $buttonBar->makeInputButton()
206 ->setName('_saveandclosedok')
207 ->setClasses('t3js-save-close')
208 ->setValue('1')
209 ->setTitle($lang->sL('LLL:EXT:lang/locallang_core.xlf:rm.saveCloseDoc', true))
210 ->setForm('configurationform')
211 ->setIcon($moduleTemplate->getIconFactory()->getIcon(
212 'actions-document-save-close',
213 Icon::SIZE_SMALL
214 ));
215 $saveSplitButton->addItem($saveAndCloseButton);
216 $buttonBar->addButton($saveSplitButton, ButtonBar::BUTTON_POSITION_LEFT, 2);
217 }
218
219 /**
220 * @return \TYPO3\CMS\Lang\LanguageService
221 */
222 protected function getLanguageService()
223 {
224 return $GLOBALS['LANG'];
225 }
226 }