[FEATURE] Add distribution management to extension manager
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Controller / DownloadController.php
index 7aa57cb..d2b0584 100644 (file)
@@ -1,8 +1,10 @@
 <?php
+namespace TYPO3\CMS\Extensionmanager\Controller;
+
 /***************************************************************
  *  Copyright notice
  *
- *  (c) 2012 Susanne Moog <typo3@susannemoog.de>
+ *  (c) 2012-2013 Susanne Moog <typo3@susannemoog.de>
  *  All rights reserved
  *
  *  This script is part of the TYPO3 project. The TYPO3 project is
  *
  *  This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
-
-
 /**
  * Controller for actions related to the TER download of an extension
  *
  * @author Susanne Moog, <typo3@susannemoog.de>
- * @package Extension Manager
- * @subpackage Controller
  */
-class Tx_Extensionmanager_Controller_DownloadController extends Tx_Extensionmanager_Controller_AbstractController {
+class DownloadController extends \TYPO3\CMS\Extensionmanager\Controller\AbstractController {
 
        /**
-        * @var Tx_Extensionmanager_Domain_Repository_ExtensionRepository
+        * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
+        * @inject
         */
        protected $extensionRepository;
 
        /**
-        * @var Tx_Extensionmanager_Utility_FileHandling
+        * @var \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
+        * @inject
         */
        protected $fileHandlingUtility;
 
-
        /**
-        * @var Tx_Extensionmanager_Service_Management
+        * @var \TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService
+        * @inject
         */
        protected $managementService;
 
        /**
-        * @var Tx_Extensionmanager_Utility_Install
+        * @var \TYPO3\CMS\Extensionmanager\Utility\InstallUtility
+        * @inject
         */
        protected $installUtility;
 
        /**
-        * @param Tx_Extensionmanager_Utility_Install $installUtility
-        * @return void
-        */
-       public function injectInstallUtility(Tx_Extensionmanager_Utility_Install $installUtility) {
-               $this->installUtility = $installUtility;
-       }
-
-       /**
-        * Dependency injection of the Extension Repository
-        *
-        * @param Tx_Extensionmanager_Domain_Repository_ExtensionRepository $extensionRepository
-        * @return void
-        */
-       public function injectExtensionRepository(Tx_Extensionmanager_Domain_Repository_ExtensionRepository $extensionRepository) {
-               $this->extensionRepository = $extensionRepository;
-       }
-
-       /**
-        * @param Tx_Extensionmanager_Utility_FileHandling $fileHandlingUtility
-        * @return void
-        */
-       public function injectFileHandlingUtility(Tx_Extensionmanager_Utility_FileHandling $fileHandlingUtility) {
-               $this->fileHandlingUtility = $fileHandlingUtility;
-       }
-
-       /**
-        * @param Tx_Extensionmanager_Service_Management $managementService
-        * @return void
-        */
-       public function injectManagementService(Tx_Extensionmanager_Service_Management $managementService) {
-               $this->managementService = $managementService;
-       }
-
-       /**
-        * @var Tx_Extensionmanager_Utility_Download
+        * @var \TYPO3\CMS\Extensionmanager\Utility\DownloadUtility
+        * @inject
         */
        protected $downloadUtility;
 
-       /**
-        * @param Tx_Extensionmanager_Utility_Download $downloadUtility
-        * @return void
-        */
-       public function injectDownloadUtility(Tx_Extensionmanager_Utility_Download $downloadUtility) {
-               $this->downloadUtility = $downloadUtility;
-       }
-
        /**
         * Check extension dependencies
         *
-        * @throws Exception
-        * @return void
+        * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
+        * @throws \Exception
         */
-       public function checkDependenciesAction() {
-               if (!$this->request->hasArgument('extension')) {
-                       throw new Exception('Required argument extension not set.', 1334433342);
-               }
-               $extensionUid = $this->request->getArgument('extension');
-               /** @var $extension Tx_Extensionmanager_Domain_Model_Extension */
-               $extension = $this->extensionRepository->findByUid(intval($extensionUid));
-
-               $dependencyTypes = $this->managementService->getAndResolveDependencies($extension);
+       public function checkDependenciesAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension) {
                $message = '';
-               if (count($dependencyTypes) > 0) {
-                               // @todo translate and beautify
-                       $message = 'The following dependencies have to be resolved before installation:<br /><br />';
-                       foreach ($dependencyTypes as $dependencyType => $dependencies) {
-                               $message .= '<h3>Extensions marked for ' . $dependencyType . ':</h3>';
-                               foreach ($dependencies as $extensionKey => $dependency) {
-                                       $message .= $extensionKey . '<br />';
+               $title = '';
+               $hasDependencies = FALSE;
+               $hasErrors = FALSE;
+               try {
+                       $dependencyTypes = $this->managementService->getAndResolveDependencies($extension);
+                       if (count($dependencyTypes) > 0) {
+                               $hasDependencies = TRUE;
+                               $message = $this->translate('downloadExtension.dependencies.headline');
+                               foreach ($dependencyTypes as $dependencyType => $dependencies) {
+                                       $extensions = '';
+                                       foreach ($dependencies as $extensionKey => $dependency) {
+                                               $extensions .= htmlspecialchars($extensionKey) . '<br />';
+                                       }
+                                       $message .= $this->translate('downloadExtension.dependencies.typeHeadline',
+                                               array(
+                                                       $this->translate('downloadExtension.dependencyType.' . $dependencyType),
+                                                       $extensions
+                                               )
+                                       );
                                }
-                               $message .= 'Shall these dependencies be resolved automatically?';
+                               $title = $this->translate('downloadExtension.dependencies.resolveAutomatically');
                        }
+                       $this->view->assign('dependencies', $dependencyTypes);
+               } catch (\Exception $e) {
+                       $hasErrors = TRUE;
+                       $title = $this->translate('downloadExtension.dependencies.errorTitle');
+                       $message = $e->getMessage();
                }
-               $this->view->assign('dependencies', $dependencyTypes)
+               $this->view->assign('extension', $extension)
+                               ->assign('hasDependencies', $hasDependencies)
+                               ->assign('hasErrors', $hasErrors)
+                               ->assign('message', $message)
+                               ->assign('title', $title);
+       }
+
+       /**
+        * Install an extension from TER action
+        *
+        * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
+        * @param string $downloadPath
+        */
+       public function installFromTerAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension, $downloadPath) {
+               list($result, $errorMessage) = $this->installFromTer($extension, $downloadPath);
+               $this->view
+                       ->assign('result', $result)
                        ->assign('extension', $extension)
-                       ->assign('message', $message);
+                       ->assign('errorMessage', $errorMessage);
        }
 
        /**
-        * Install an extension from TER
+        * Action for installing a distribution -
+        * redirects directly to configuration after installing
         *
-        * @throws Tx_Extensionmanager_Exception_ExtensionManager
+        * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
         * @return void
         */
-       public function installFromTerAction() {
-               $result = FALSE;
-               $errorMessage = '';
-               try {
-                       if (!$this->request->hasArgument('extension')) {
-                               throw new Tx_Extensionmanager_Exception_ExtensionManager('Required argument extension not set.', 1334433342);
-                       }
-                       $extensionUid = $this->request->getArgument('extension');
-
-                       if ($this->request->hasArgument('downloadPath')) {
-                               $this->downloadUtility->setDownloadPath($this->request->getArgument('downloadPath'));
-                       }
-
-                       /** @var $extension Tx_Extensionmanager_Domain_Model_Extension */
-                       $extension = $this->extensionRepository->findByUid(intval($extensionUid));
-                       $this->prepareExtensionForImport($extension);
-                       $result = $this->managementService->resolveDependenciesAndInstall($extension);
-               } catch (Tx_Extensionmanager_Exception_ExtensionManager $e) {
-                       $errorMessage = $e->getMessage();
+       public function installDistributionAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension) {
+               list($result, $errorMessage) = $this->installFromTer($extension);
+               if ($errorMessage) {
+                       // @TODO: write Template
+                       $this->view
+                               ->assign('result', $result)
+                               ->assign('errorMessage', $errorMessage);
+               } else {
+                       $this->redirect(
+                               'showConfigurationForm',
+                               'Configuration',
+                               NULL,
+                               array(
+                                       'extension' => $this->installUtility->enrichExtensionWithDetails($extension->getExtensionKey())
+                               )
+                       );
                }
-               $this->view->assign('result', $result)
-                       ->assign('extension', $extension)
-                       ->assign('errorMessage', $errorMessage);
        }
 
        /**
@@ -171,12 +151,12 @@ class Tx_Extensionmanager_Controller_DownloadController extends Tx_Extensionmana
         * Uninstalls the extension if it is already loaded (case: update)
         * and reloads the caches.
         *
-        * @param Tx_Extensionmanager_Domain_Model_Extension $extension
+        * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
         * @return void
         */
-       protected function prepareExtensionForImport(Tx_Extensionmanager_Domain_Model_Extension $extension) {
-               if (t3lib_extMgm::isLoaded($extension->getExtensionKey())) {
-                       t3lib_extMgm::unloadExtension($extension->getExtensionKey());
+       protected function prepareExtensionForImport(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension) {
+               if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded($extension->getExtensionKey())) {
+                       \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::unloadExtension($extension->getExtensionKey());
                        $this->installUtility->reloadCaches();
                }
        }
@@ -191,12 +171,11 @@ class Tx_Extensionmanager_Controller_DownloadController extends Tx_Extensionmana
         */
        protected function updateExtensionAction() {
                $extensionKey = $this->request->getArgument('extension');
-               /** @var $highestTerVersionExtension Tx_Extensionmanager_Domain_Model_Extension */
+               /** @var $highestTerVersionExtension \TYPO3\CMS\Extensionmanager\Domain\Model\Extension */
                $highestTerVersionExtension = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
                $this->prepareExtensionForImport($highestTerVersionExtension);
                $result = $this->managementService->resolveDependenciesAndInstall($highestTerVersionExtension);
-               $this->view->assign('result', $result)
-                       ->assign('extension', $highestTerVersionExtension);
+               $this->view->assign('result', $result)->assign('extension', $highestTerVersionExtension);
        }
 
        /**
@@ -210,15 +189,35 @@ class Tx_Extensionmanager_Controller_DownloadController extends Tx_Extensionmana
                $extensionKey = $this->request->getArgument('extension');
                $version = $this->request->getArgument('integerVersion');
                $updateComments = array();
-               /** @var $updatableVersion Tx_Extensionmanager_Domain_Model_Extension */
+               /** @var $updatableVersion \TYPO3\CMS\Extensionmanager\Domain\Model\Extension */
                $updatableVersions = $this->extensionRepository->findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $version);
                foreach ($updatableVersions as $updatableVersion) {
                        $updateComments[$updatableVersion->getVersion()] = $updatableVersion->getUpdateComment();
                }
-               $this->view
-                       ->assign('updateComments', $updateComments)
-                       ->assign('extensionKey', $extensionKey);
+               $this->view->assign('updateComments', $updateComments)->assign('extensionKey', $extensionKey);
        }
 
+       /**
+        * Install an action from TER
+        * Downloads the extension, resolves dependencies and installs it
+        *
+        * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
+        * @param string $downloadPath
+        * @return array
+        */
+       protected function installFromTer(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension, $downloadPath = 'Local') {
+               $result = FALSE;
+               $errorMessage = '';
+               try {
+                       $this->downloadUtility->setDownloadPath($downloadPath);
+                       $this->prepareExtensionForImport($extension);
+                       $result = $this->managementService->resolveDependenciesAndInstall($extension);
+               } catch (\TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException $e) {
+                       $errorMessage = $e->getMessage();
+               }
+               return array($result, $errorMessage);
+       }
 }
+
+
 ?>
\ No newline at end of file