f9c48c1b52f267c8c184794da6aac09edb8adfce
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Controller / ActionController.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\Core\Registry;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Controller for handling extension related actions like
22 * installing, removing, downloading of data or files
23 */
24 class ActionController extends AbstractController
25 {
26 /**
27 * @var \TYPO3\CMS\Extensionmanager\Utility\InstallUtility
28 */
29 protected $installUtility;
30
31 /**
32 * @var \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
33 */
34 protected $fileHandlingUtility;
35
36 /**
37 * @var \TYPO3\CMS\Extensionmanager\Utility\ExtensionModelUtility
38 */
39 protected $extensionModelUtility;
40
41 /**
42 * @var \TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService
43 */
44 protected $managementService;
45
46 /**
47 * @param \TYPO3\CMS\Extensionmanager\Utility\InstallUtility $installUtility
48 */
49 public function injectInstallUtility(\TYPO3\CMS\Extensionmanager\Utility\InstallUtility $installUtility)
50 {
51 $this->installUtility = $installUtility;
52 }
53
54 /**
55 * @param \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility $fileHandlingUtility
56 */
57 public function injectFileHandlingUtility(\TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility $fileHandlingUtility)
58 {
59 $this->fileHandlingUtility = $fileHandlingUtility;
60 }
61
62 /**
63 * @param \TYPO3\CMS\Extensionmanager\Utility\ExtensionModelUtility $extensionModelUtility
64 */
65 public function injectExtensionModelUtility(\TYPO3\CMS\Extensionmanager\Utility\ExtensionModelUtility $extensionModelUtility)
66 {
67 $this->extensionModelUtility = $extensionModelUtility;
68 }
69
70 /**
71 * @param \TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService $managementService
72 */
73 public function injectManagementService(\TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService $managementService)
74 {
75 $this->managementService = $managementService;
76 }
77
78 /**
79 * Toggle extension installation state action
80 *
81 * @param string $extensionKey
82 */
83 protected function toggleExtensionInstallationStateAction($extensionKey)
84 {
85 $installedExtensions = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getLoadedExtensionListArray();
86 try {
87 if (in_array($extensionKey, $installedExtensions)) {
88 // uninstall
89 $this->installUtility->uninstall($extensionKey);
90 } else {
91 // install
92 $extension = $this->extensionModelUtility->mapExtensionArrayToModel(
93 $this->installUtility->enrichExtensionWithDetails($extensionKey, false)
94 );
95 if ($this->managementService->installExtension($extension) === false) {
96 $this->redirect('unresolvedDependencies', 'List', null, ['extensionKey' => $extensionKey]);
97 }
98 }
99 } catch (\TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException $e) {
100 $this->addFlashMessage($e->getMessage(), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
101 } catch (\TYPO3\CMS\Core\Package\Exception\PackageStatesFileNotWritableException $e) {
102 $this->addFlashMessage($e->getMessage(), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
103 }
104 $this->redirect('index', 'List', null, [
105 self::TRIGGER_RefreshModuleMenu => true,
106 self::TRIGGER_RefreshTopbar => true
107 ]);
108 }
109
110 /**
111 * Install an extension and omit dependency checking
112 *
113 * @param string $extensionKey
114 */
115 public function installExtensionWithoutSystemDependencyCheckAction($extensionKey)
116 {
117 $this->managementService->setSkipDependencyCheck(true);
118 $this->forward('toggleExtensionInstallationState', null, null, ['extensionKey' => $extensionKey]);
119 }
120
121 /**
122 * Remove an extension (if it is still installed, uninstall it first)
123 *
124 * @param string $extension
125 * @return string
126 */
127 protected function removeExtensionAction($extension)
128 {
129 try {
130 $this->installUtility->removeExtension($extension);
131 $this->addFlashMessage(
132 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
133 'extensionList.remove.message',
134 'extensionmanager',
135 [
136 'extension' => $extension,
137 ]
138 )
139 );
140 } catch (\TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException $e) {
141 $this->addFlashMessage($e->getMessage(), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
142 } catch (\TYPO3\CMS\Core\Package\Exception $e) {
143 $this->addFlashMessage($e->getMessage(), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
144 }
145
146 return '';
147 }
148
149 /**
150 * Download an extension as a zip file
151 *
152 * @param string $extension
153 */
154 protected function downloadExtensionZipAction($extension)
155 {
156 $fileName = $this->fileHandlingUtility->createZipFileFromExtension($extension);
157 $this->fileHandlingUtility->sendZipFileToBrowserAndDelete($fileName);
158 }
159
160 /**
161 * Reloads the static SQL data of an extension
162 *
163 * @param string $extension
164 */
165 protected function reloadExtensionDataAction($extension)
166 {
167 $extension = $this->installUtility->enrichExtensionWithDetails($extension, false);
168 $registryKey = $extension['siteRelPath'] . 'ext_tables_static+adt.sql';
169
170 $registry = GeneralUtility::makeInstance(Registry::class);
171 $registry->remove('extensionDataImport', $registryKey);
172
173 $this->installUtility->importStaticSqlFile($extension['siteRelPath']);
174
175 $this->redirect('index', 'List');
176 }
177 }