[TASK] Add a "save and close" button in the extensionmanager
[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\Extensionmanager\Domain\Model\Extension;
18 use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
19
20 /**
21 * Controller for configuration related actions.
22 *
23 * @author Susanne Moog <typo3@susannemoog.de>
24 */
25 class ConfigurationController extends AbstractController {
26
27 /**
28 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository
29 * @inject
30 */
31 protected $configurationItemRepository;
32
33 /**
34 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
35 * @inject
36 */
37 protected $extensionRepository;
38
39 /**
40 * Show the extension configuration form. The whole form field handling is done
41 * in the corresponding view helper
42 *
43 * @param array $extension Extension information, must contain at least the key
44 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
45 * @return void
46 */
47 public function showConfigurationFormAction(array $extension) {
48 if (!array_key_exists('key', $extension)) {
49 throw new ExtensionManagerException(
50 'Extension key not found.',
51 1359206803
52 );
53 }
54 $configuration = $this->configurationItemRepository->findByExtensionKey($extension['key']);
55 if ($configuration) {
56 $this->view
57 ->assign('configuration', $configuration)
58 ->assign('extension', $extension);
59 } else {
60 /** @var Extension $extension */
61 $extension = $this->extensionRepository->findOneByCurrentVersionByExtensionKey($extension['key']);
62 // Extension has no configuration and is a distribution
63 if ($extension->getCategory() === Extension::DISTRIBUTION_CATEGORY) {
64 $this->redirect('welcome', 'Distribution', NULL, array('extension' => $extension->getUid()));
65 }
66 throw new ExtensionManagerException('The extension ' . htmlspecialchars($extension['key']) . ' has no configuration.');
67 }
68 }
69
70 /**
71 * Save configuration and redirects back to form
72 * or to the welcome page of a distribution
73 *
74 * @param array $config The new extension configuration
75 * @param string $extensionKey The extension key
76 * @return void
77 */
78 public function saveAction(array $config, $extensionKey) {
79 $this->saveConfiguration($config, $extensionKey);
80 /** @var Extension $extension */
81 $extension = $this->extensionRepository->findOneByCurrentVersionByExtensionKey($extensionKey);
82 // Different handling for distribution installation
83 if ($extension instanceof Extension &&
84 $extension->getCategory() === Extension::DISTRIBUTION_CATEGORY
85 ) {
86 $this->redirect('welcome', 'Distribution', NULL, array('extension' => $extension->getUid()));
87 } else {
88 $this->redirect('showConfigurationForm', NULL, NULL, array('extension' => array('key' => $extensionKey)));
89 }
90 }
91
92 /**
93 * Saves new configuration and redirects back to list
94 *
95 * @param array $config
96 * @param string $extensionKey
97 * @return void
98 */
99 public function saveAndCloseAction(array $config, $extensionKey) {
100 $this->saveConfiguration($config, $extensionKey);
101 $this->redirect('index', 'List');
102 }
103
104
105 /**
106 * Emits a signal after the configuration file was written
107 *
108 * @param array $newConfiguration
109 */
110 protected function emitAfterExtensionConfigurationWriteSignal(array $newConfiguration) {
111 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionConfigurationWrite', array($newConfiguration, $this));
112 }
113
114 /**
115 * Merge and save new configuration
116 *
117 * @param array $config
118 * @param $extensionKey
119 * @return void
120 */
121 protected function saveConfiguration(array $config, $extensionKey) {
122 /** @var $configurationUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
123 $configurationUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
124 $newConfiguration = $configurationUtility->getCurrentConfiguration($extensionKey);
125 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($newConfiguration, $config);
126 $configurationUtility->writeConfiguration(
127 $configurationUtility->convertValuedToNestedConfiguration($newConfiguration),
128 $extensionKey
129 );
130 $this->emitAfterExtensionConfigurationWriteSignal($newConfiguration);
131 }
132
133 }