1bc68c6276cdf6e83e22f7e80225d7bcf8b7cdc2
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / ViewHelpers / DownloadExtensionViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\ViewHelpers;
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\Extbase\Utility\LocalizationUtility;
18 use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
19
20 /**
21 * view helper
22 * @internal
23 */
24 class DownloadExtensionViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper
25 {
26 /**
27 * @var string
28 */
29 protected $tagName = 'form';
30
31 /**
32 * @var \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility
33 */
34 protected $configurationUtility;
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Service\ExtensionService
38 */
39 protected $extensionService;
40
41 /**
42 * @param \TYPO3\CMS\Extbase\Service\ExtensionService $extensionService
43 */
44 public function injectExtensionService(\TYPO3\CMS\Extbase\Service\ExtensionService $extensionService)
45 {
46 $this->extensionService = $extensionService;
47 }
48
49 /**
50 * @param \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility $configurationUtility
51 */
52 public function injectConfigurationUtility(\TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility $configurationUtility)
53 {
54 $this->configurationUtility = $configurationUtility;
55 }
56
57 /**
58 * Initialize arguments.
59 */
60 public function initializeArguments()
61 {
62 parent::initializeArguments();
63 $this->registerArgument('extension', Extension::class, '', true);
64 $this->registerTagAttribute('enctype', 'string', 'MIME type with which the form is submitted');
65 $this->registerTagAttribute('method', 'string', 'Transfer type (GET or POST)');
66 $this->registerTagAttribute('name', 'string', 'Name of form');
67 $this->registerTagAttribute('onreset', 'string', 'JavaScript: On reset of the form');
68 $this->registerTagAttribute('onsubmit', 'string', 'JavaScript: On submit of the form');
69 $this->registerUniversalTagAttributes();
70 }
71
72 /**
73 * Renders a download link
74 *
75 * @return string the rendered a tag
76 */
77 public function render()
78 {
79 /** @var Extension $extension */
80 $extension = $this->arguments['extension'];
81 $installPaths = Extension::returnAllowedInstallPaths();
82 if (empty($installPaths)) {
83 return '';
84 }
85 $pathSelector = '<ul class="is-hidden">';
86 foreach ($installPaths as $installPathType => $installPath) {
87 $pathSelector .= '<li>
88 <input type="radio" id="' . htmlspecialchars($extension->getExtensionKey()) . '-downloadPath-' . htmlspecialchars($installPathType) . '" name="' . htmlspecialchars($this->getFieldNamePrefix()) . '[downloadPath]" class="downloadPath" value="' . htmlspecialchars($installPathType) . '" ' . ($installPathType === 'Local' ? 'checked="checked"' : '') . ' />
89 <label for="' . htmlspecialchars($extension->getExtensionKey()) . '-downloadPath-' . htmlspecialchars($installPathType) . '">' . htmlspecialchars($installPathType) . '</label>
90 </li>';
91 }
92 $pathSelector .= '</ul>';
93 $uriBuilder = $this->controllerContext->getUriBuilder();
94 $action = 'checkDependencies';
95 $uriBuilder->reset();
96 $uriBuilder->setFormat('json');
97 $uri = $uriBuilder->uriFor($action, [
98 'extension' => (int)$extension->getUid()
99 ], 'Download');
100 $this->tag->addAttribute('data-href', $uri);
101
102 $automaticInstallation = $this->configurationUtility->getCurrentConfiguration('extensionmanager')['automaticInstallation']['value'];
103 $labelKeySuffix = $automaticInstallation ? '' : '.downloadOnly';
104 $label = '
105 <div class="btn-group">
106 <button
107 title="' . LocalizationUtility::translate('extensionList.downloadViewHelper.submit' . $labelKeySuffix, 'extensionmanager') . '"
108 type="submit"
109 class="btn btn-default"
110 value="' . LocalizationUtility::translate('extensionList.downloadViewHelper.submit' . $labelKeySuffix, 'extensionmanager') . '"
111 >
112 <span class="t3-icon fa fa-cloud-download"></span>
113 </button>
114 </div>';
115
116 $this->tag->setContent($label . $pathSelector);
117 $this->tag->addAttribute('class', 'download');
118 return '<div id="' . htmlspecialchars($extension->getExtensionKey()) . '-downloadFromTer" class="downloadFromTer">' . $this->tag->render() . '</div>';
119 }
120
121 /**
122 * Get the field name prefix
123 *
124 * @return string
125 */
126 protected function getFieldNamePrefix()
127 {
128 if ($this->hasArgument('fieldNamePrefix')) {
129 return $this->arguments['fieldNamePrefix'];
130 } else {
131 return $this->getDefaultFieldNamePrefix();
132 }
133 }
134
135 /**
136 * Retrieves the default field name prefix for this form
137 *
138 * @return string default field name prefix
139 */
140 protected function getDefaultFieldNamePrefix()
141 {
142 $request = $this->controllerContext->getRequest();
143 if ($this->hasArgument('extensionName')) {
144 $extensionName = $this->arguments['extensionName'];
145 } else {
146 $extensionName = $request->getControllerExtensionName();
147 }
148 if ($this->hasArgument('pluginName')) {
149 $pluginName = $this->arguments['pluginName'];
150 } else {
151 $pluginName = $request->getPluginName();
152 }
153 if ($extensionName !== null && $pluginName != null) {
154 return $this->extensionService->getPluginNamespace($extensionName, $pluginName);
155 } else {
156 return '';
157 }
158 }
159 }