[FEATURE] Add new extension manager to the core
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Controller / DownloadController.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Susanne Moog <typo3@susannemoog.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28
29 /**
30 * Controller for actions related to the TER download of an extension
31 *
32 * @author Susanne Moog, <typo3@susannemoog.de>
33 * @package Extension Manager
34 * @subpackage Controller
35 */
36 class Tx_Extensionmanager_Controller_DownloadController extends Tx_Extensionmanager_Controller_AbstractController {
37
38 /**
39 * @var Tx_Extensionmanager_Domain_Repository_ExtensionRepository
40 */
41 protected $extensionRepository;
42
43 /**
44 * @var Tx_Extensionmanager_Utility_FileHandling
45 */
46 protected $fileHandlingUtility;
47
48
49 /**
50 * @var Tx_Extensionmanager_Service_Management
51 */
52 protected $managementService;
53
54 /**
55 * @var Tx_Extensionmanager_Utility_Install
56 */
57 protected $installUtility;
58
59 /**
60 * @param Tx_Extensionmanager_Utility_Install $installUtility
61 * @return void
62 */
63 public function injectInstallUtility(Tx_Extensionmanager_Utility_Install $installUtility) {
64 $this->installUtility = $installUtility;
65 }
66
67 /**
68 * Dependency injection of the Extension Repository
69 *
70 * @param Tx_Extensionmanager_Domain_Repository_ExtensionRepository $extensionRepository
71 * @return void
72 */
73 public function injectExtensionRepository(Tx_Extensionmanager_Domain_Repository_ExtensionRepository $extensionRepository) {
74 $this->extensionRepository = $extensionRepository;
75 }
76
77 /**
78 * @param Tx_Extensionmanager_Utility_FileHandling $fileHandlingUtility
79 * @return void
80 */
81 public function injectFileHandlingUtility(Tx_Extensionmanager_Utility_FileHandling $fileHandlingUtility) {
82 $this->fileHandlingUtility = $fileHandlingUtility;
83 }
84
85 /**
86 * @param Tx_Extensionmanager_Service_Management $managementService
87 * @return void
88 */
89 public function injectManagementService(Tx_Extensionmanager_Service_Management $managementService) {
90 $this->managementService = $managementService;
91 }
92
93 /**
94 * @var Tx_Extensionmanager_Utility_Download
95 */
96 protected $downloadUtility;
97
98 /**
99 * @param Tx_Extensionmanager_Utility_Download $downloadUtility
100 * @return void
101 */
102 public function injectDownloadUtility(Tx_Extensionmanager_Utility_Download $downloadUtility) {
103 $this->downloadUtility = $downloadUtility;
104 }
105
106 /**
107 * Check extension dependencies
108 *
109 * @throws Exception
110 * @return void
111 */
112 public function checkDependenciesAction() {
113 if (!$this->request->hasArgument('extension')) {
114 throw new Exception('Required argument extension not set.', 1334433342);
115 }
116 $extensionUid = $this->request->getArgument('extension');
117 /** @var $extension Tx_Extensionmanager_Domain_Model_Extension */
118 $extension = $this->extensionRepository->findByUid(intval($extensionUid));
119
120 $dependencyTypes = $this->managementService->getAndResolveDependencies($extension);
121 $message = '';
122 if (count($dependencyTypes) > 0) {
123 // @todo translate and beautify
124 $message = 'The following dependencies have to be resolved before installation:<br /><br />';
125 foreach ($dependencyTypes as $dependencyType => $dependencies) {
126 $message .= '<h3>Extensions marked for ' . $dependencyType . ':</h3>';
127 foreach ($dependencies as $extensionKey => $dependency) {
128 $message .= $extensionKey . '<br />';
129 }
130 $message .= 'Shall these dependencies be resolved automatically?';
131 }
132 }
133 $this->view->assign('dependencies', $dependencyTypes)
134 ->assign('extension', $extension)
135 ->assign('message', $message);
136 }
137
138 /**
139 * Install an extension from TER
140 *
141 * @throws Tx_Extensionmanager_Exception_ExtensionManager
142 * @return void
143 */
144 public function installFromTerAction() {
145 $result = FALSE;
146 $errorMessage = '';
147 try {
148 if (!$this->request->hasArgument('extension')) {
149 throw new Tx_Extensionmanager_Exception_ExtensionManager('Required argument extension not set.', 1334433342);
150 }
151 $extensionUid = $this->request->getArgument('extension');
152
153 if ($this->request->hasArgument('downloadPath')) {
154 $this->downloadUtility->setDownloadPath($this->request->getArgument('downloadPath'));
155 }
156
157 /** @var $extension Tx_Extensionmanager_Domain_Model_Extension */
158 $extension = $this->extensionRepository->findByUid(intval($extensionUid));
159 $this->prepareExtensionForImport($extension);
160 $result = $this->managementService->resolveDependenciesAndInstall($extension);
161 } catch (Tx_Extensionmanager_Exception_ExtensionManager $e) {
162 $errorMessage = $e->getMessage();
163 }
164 $this->view->assign('result', $result)
165 ->assign('extension', $extension)
166 ->assign('errorMessage', $errorMessage);
167 }
168
169 /**
170 * Prepares an extension for import from TER
171 * Uninstalls the extension if it is already loaded (case: update)
172 * and reloads the caches.
173 *
174 * @param Tx_Extensionmanager_Domain_Model_Extension $extension
175 * @return void
176 */
177 protected function prepareExtensionForImport(Tx_Extensionmanager_Domain_Model_Extension $extension) {
178 if (t3lib_extMgm::isLoaded($extension->getExtensionKey())) {
179 t3lib_extMgm::unloadExtension($extension->getExtensionKey());
180 $this->installUtility->reloadCaches();
181 }
182 }
183
184 /**
185 * Update an extension. Makes no sanity check but directly searches highest
186 * available version from TER and updates. Update check is done by the list
187 * already. This method should only be called if we are sure that there is
188 * an update.
189 *
190 * @return void
191 */
192 protected function updateExtensionAction() {
193 $extensionKey = $this->request->getArgument('extension');
194 /** @var $highestTerVersionExtension Tx_Extensionmanager_Domain_Model_Extension */
195 $highestTerVersionExtension = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
196 $this->prepareExtensionForImport($highestTerVersionExtension);
197 $result = $this->managementService->resolveDependenciesAndInstall($highestTerVersionExtension);
198 $this->view->assign('result', $result)
199 ->assign('extension', $highestTerVersionExtension);
200 }
201
202 /**
203 * Show update comments for extensions that can be updated.
204 * Fetches update comments for all versions between the current
205 * installed and the highest version.
206 *
207 * @return void
208 */
209 protected function updateCommentForUpdatableVersionsAction() {
210 $extensionKey = $this->request->getArgument('extension');
211 $version = $this->request->getArgument('integerVersion');
212 $updateComments = array();
213 /** @var $updatableVersion Tx_Extensionmanager_Domain_Model_Extension */
214 $updatableVersions = $this->extensionRepository->findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $version);
215 foreach ($updatableVersions as $updatableVersion) {
216 $updateComments[$updatableVersion->getVersion()] = $updatableVersion->getUpdateComment();
217 }
218 $this->view
219 ->assign('updateComments', $updateComments)
220 ->assign('extensionKey', $extensionKey);
221 }
222
223 }
224 ?>