[!!!][FEATURE] Flash message view helper add deferred rendering
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Controller / DownloadController.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\Messaging\FlashMessage;
18 use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
19
20 /**
21 * Controller for actions related to the TER download of an extension
22 *
23 * @author Susanne Moog, <typo3@susannemoog.de>
24 */
25 class DownloadController extends AbstractController {
26
27 /**
28 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
29 * @inject
30 */
31 protected $extensionRepository;
32
33 /**
34 * @var \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
35 * @inject
36 */
37 protected $fileHandlingUtility;
38
39 /**
40 * @var \TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService
41 * @inject
42 */
43 protected $managementService;
44
45 /**
46 * @var \TYPO3\CMS\Extensionmanager\Utility\InstallUtility
47 * @inject
48 */
49 protected $installUtility;
50
51 /**
52 * @var \TYPO3\CMS\Extensionmanager\Utility\DownloadUtility
53 * @inject
54 */
55 protected $downloadUtility;
56
57 /**
58 * @var \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility
59 * @inject
60 */
61 protected $configurationUtility;
62
63 /**
64 * Check extension dependencies
65 *
66 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
67 * @throws \Exception
68 */
69 public function checkDependenciesAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension) {
70 $message = '';
71 $title = '';
72 $hasDependencies = FALSE;
73 $hasErrors = FALSE;
74 try {
75 $dependencyTypes = $this->managementService->getAndResolveDependencies($extension);
76 if (count($dependencyTypes) > 0) {
77 $hasDependencies = TRUE;
78 $message = '<p>' . $this->translate('downloadExtension.dependencies.headline') . '</p>';
79 foreach ($dependencyTypes as $dependencyType => $dependencies) {
80 $extensions = '';
81 foreach ($dependencies as $extensionKey => $dependency) {
82 $extensions .= $this->translate('downloadExtension.dependencies.extensionWithVersion', array(
83 $extensionKey, $dependency->getVersion()
84 )) . '<br />';
85 }
86 $message .= $this->translate('downloadExtension.dependencies.typeHeadline',
87 array(
88 $this->translate('downloadExtension.dependencyType.' . $dependencyType),
89 $extensions
90 )
91 );
92 }
93 $title = $this->translate('downloadExtension.dependencies.resolveAutomatically');
94 }
95 $this->view->assign('dependencies', $dependencyTypes);
96 } catch (\Exception $e) {
97 $hasErrors = TRUE;
98 $title = $this->translate('downloadExtension.dependencies.errorTitle');
99 $message = $e->getMessage();
100 }
101 $this->view->assign('extension', $extension)
102 ->assign('hasDependencies', $hasDependencies)
103 ->assign('hasErrors', $hasErrors)
104 ->assign('message', $message)
105 ->assign('title', $title);
106 }
107
108 /**
109 * Install an extension from TER action
110 *
111 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
112 * @param string $downloadPath
113 */
114 public function installFromTerAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension, $downloadPath) {
115 list($result, $errorMessages) = $this->installFromTer($extension, $downloadPath);
116 $emConfiguration = $this->configurationUtility->getCurrentConfiguration('extensionmanager');
117 $this->view
118 ->assign('result', $result)
119 ->assign('extension', $extension)
120 ->assign('installationTypeLanguageKey', (bool)$emConfiguration['automaticInstallation']['value'] ? '' : '.downloadOnly')
121 ->assign('unresolvedDependencies', $errorMessages);
122 }
123
124 /**
125 * Check extension dependencies with special dependencies
126 *
127 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
128 * @throws \Exception
129 */
130 public function installExtensionWithoutSystemDependencyCheckAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension) {
131 $this->managementService->setSkipSystemDependencyCheck(TRUE);
132 $this->forward('installFromTer', NULL, NULL, array('extension' => $extension, 'downloadPath' => 'Local'));
133 }
134
135 /**
136 * Action for installing a distribution -
137 * redirects directly to configuration after installing
138 *
139 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
140 * @return void
141 */
142 public function installDistributionAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension) {
143 if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('impexp')) {
144 $this->forward('distributions', 'List');
145 }
146 list($result, $errorMessages) = $this->installFromTer($extension);
147 if ($errorMessages) {
148 foreach ($errorMessages as $extensionKey => $messages) {
149 foreach ($messages as $message) {
150 $this->addFlashMessage(
151 $message['message'],
152 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
153 'distribution.error.headline',
154 'extensionmanager',
155 array($extensionKey)
156 ),
157 \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR
158 );
159 }
160 }
161
162 // Redirect back to distributions list action
163 $this->redirect(
164 'distributions',
165 'List'
166 );
167 } else {
168 // FlashMessage that extension is installed
169 $this->addFlashMessage(
170 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('distribution.welcome.message', 'extensionmanager')
171 . $extension->getExtensionKey(),
172 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('distribution.welcome.headline', 'extensionmanager')
173 );
174
175 // Redirect to show action
176 $this->redirect(
177 'show',
178 'Distribution',
179 NULL,
180 array('extension' => $extension)
181 );
182 }
183 }
184
185 /**
186 * Update an extension. Makes no sanity check but directly searches highest
187 * available version from TER and updates. Update check is done by the list
188 * already. This method should only be called if we are sure that there is
189 * an update.
190 *
191 * @return string
192 */
193 protected function updateExtensionAction() {
194 $extensionKey = $this->request->getArgument('extension');
195 $version = $this->request->getArgument('version');
196 $extension = $this->extensionRepository->findOneByExtensionKeyAndVersion($extensionKey, $version);
197 if (!$extension instanceof Extension) {
198 $extension = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
199 }
200 $installedExtensions = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getLoadedExtensionListArray();
201 try {
202 if (in_array($extensionKey, $installedExtensions, TRUE)) {
203 // To resolve new dependencies the extension is installed again
204 $this->managementService->installExtension($extension);
205 } else {
206 $this->managementService->downloadMainExtension($extension);
207 }
208 $this->addFlashMessage(
209 $this->translate('extensionList.updateFlashMessage.body', array($extensionKey)),
210 $this->translate('extensionList.updateFlashMessage.title')
211 );
212 } catch (\Exception $e) {
213 $this->addFlashMessage($e->getMessage(), '', FlashMessage::ERROR);
214 }
215
216 return '';
217 }
218
219 /**
220 * Show update comments for extensions that can be updated.
221 * Fetches update comments for all versions between the current
222 * installed and the highest version.
223 *
224 * @return void
225 */
226 protected function updateCommentForUpdatableVersionsAction() {
227 $extensionKey = $this->request->getArgument('extension');
228 $versionStart = $this->request->getArgument('integerVersionStart');
229 $versionStop = $this->request->getArgument('integerVersionStop');
230 $updateComments = array();
231 /** @var Extension[] $updatableVersions */
232 $updatableVersions = $this->extensionRepository->findByVersionRangeAndExtensionKeyOrderedByVersion(
233 $extensionKey,
234 $versionStart,
235 $versionStop
236 );
237 $highestPossibleVersion = FALSE;
238
239 foreach ($updatableVersions as $updatableVersion) {
240 if ($highestPossibleVersion === FALSE) {
241 $highestPossibleVersion = $updatableVersion->getVersion();
242 }
243 $updateComments[$updatableVersion->getVersion()] = $updatableVersion->getUpdateComment();
244 }
245 $this->view->assign('updateComments', $updateComments)
246 ->assign('extensionKey', $extensionKey)
247 ->assign('version', $highestPossibleVersion);
248 }
249
250 /**
251 * Install an action from TER
252 * Downloads the extension, resolves dependencies and installs it
253 *
254 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
255 * @param string $downloadPath
256 * @return array
257 */
258 protected function installFromTer(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension, $downloadPath = 'Local') {
259 $result = FALSE;
260 $errorMessages = array();
261 try {
262 $this->downloadUtility->setDownloadPath($downloadPath);
263 $this->managementService->setAutomaticInstallationEnabled($this->configurationUtility->getCurrentConfiguration('extensionmanager')['automaticInstallation']['value']);
264 if (($result = $this->managementService->installExtension($extension)) === FALSE) {
265 $errorMessages = $this->managementService->getDependencyErrors();
266 }
267 } catch (\TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException $e) {
268 $errorMessages = array(
269 $extension->getExtensionKey() => array(
270 array(
271 'code' => $e->getCode(),
272 'message' => $e->getMessage(),
273 )
274 ),
275 );
276 }
277
278 return array($result, $errorMessages);
279 }
280
281 }