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