[TASK] Move language labels to correct extension(s)
[Packages/TYPO3.CMS.git] / typo3 / sysext / recordlist / Classes / View / FolderUtilityRenderer.php
1 <?php
2 namespace TYPO3\CMS\Recordlist\View;
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\Backend\Routing\UriBuilder;
18 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19 use TYPO3\CMS\Core\Localization\LanguageService;
20 use TYPO3\CMS\Core\Resource\Folder;
21 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\HttpUtility;
24 use TYPO3\CMS\Recordlist\Tree\View\LinkParameterProviderInterface;
25
26 /**
27 * Renders utility forms used in the views for files/folders of Element and Link Browser
28 * @internal
29 */
30 class FolderUtilityRenderer
31 {
32 /**
33 * @var LinkParameterProviderInterface
34 */
35 protected $parameterProvider;
36
37 /**
38 * @param LinkParameterProviderInterface $parameterProvider
39 */
40 public function __construct(LinkParameterProviderInterface $parameterProvider)
41 {
42 $this->parameterProvider = $parameterProvider;
43 }
44
45 /**
46 * For TBE: Makes a form for creating new folders in the filemount the user is browsing.
47 * The folder creation request is sent to the tce_file.php script in the core which will handle the creation.
48 *
49 * @param Folder $folderObject Absolute filepath on server in which to create the new folder.
50 *
51 * @return string HTML for the create folder form.
52 */
53 public function createFolder(Folder $folderObject)
54 {
55 $backendUser = $this->getBackendUser();
56 $userTsConfig = $backendUser->getTSConfig();
57 $lang = $this->getLanguageService();
58
59 if (!$folderObject->checkActionPermission('write')
60 || !$backendUser->isAdmin() && !($userTsConfig['options.']['createFoldersInEB'] ?? false)
61 || $userTsConfig['options.']['folderTree.']['hideCreateFolder'] ?? false
62 ) {
63 // Do not show create folder form if it is denied
64 return '';
65 }
66
67 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
68 $formAction = (string)$uriBuilder->buildUriFromRoute('tce_file');
69 $markup = [];
70 $markup[] = '<form action="' . htmlspecialchars($formAction)
71 . '" method="post" name="editform" enctype="multipart/form-data">';
72 $markup[] = '<h3>' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:create_folder.title')) . ':</h3>';
73 $markup[] = '<p><strong>' . htmlspecialchars($lang->getLL('path')) . ':</strong>'
74 . htmlspecialchars($folderObject->getIdentifier()) . '</p>';
75
76 $a = 1;
77 $markup[] = '<div class="form-group">';
78 $markup[] = '<div class="input-group">';
79 $markup[] = '<input class="form-control" type="text" name="data[newfolder][' . $a . '][data]" />';
80 $markup[] = '<span class="input-group-btn">';
81 $markup[] = '<input class="btn btn-default" type="submit" name="submit" value="'
82 . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:create_folder.submit')) . '" />';
83 $markup[] = '</span>';
84 $markup[] = '</div>';
85 $markup[] = '<input type="hidden" name="data[newfolder][' . $a . '][target]" value="'
86 . htmlspecialchars($folderObject->getCombinedIdentifier()) . '" />';
87
88 // Make footer of upload form, including the submit button:
89 $redirectValue = $this->parameterProvider->getScriptUrl() . HttpUtility::buildQueryString(
90 $this->parameterProvider->getUrlParameters(
91 ['identifier' => $folderObject->getCombinedIdentifier()]
92 ),
93 '&'
94 );
95 $markup[] = '<input type="hidden" name="data[newfolder][' . $a . '][redirect]" value="' . htmlspecialchars($redirectValue) . '" />';
96
97 $markup[] = '</div></form>';
98
99 return implode(LF, $markup);
100 }
101
102 /**
103 * Makes an upload form for uploading files to the filemount the user is browsing.
104 * The files are uploaded to the tce_file.php script in the core which will handle the upload.
105 *
106 * @param Folder $folderObject
107 * @param string[] $allowedExtensions
108 *
109 * @return string HTML for an upload form.
110 */
111 public function uploadForm(Folder $folderObject, array $allowedExtensions)
112 {
113 if (!$folderObject->checkActionPermission('write')) {
114 return '';
115 }
116 // Read configuration of upload field count
117 $count = (int)($this->getBackendUser()->getTSConfig()['options.']['folderTree.']['uploadFieldsInLinkBrowser'] ?? 1);
118 if ($count === 0) {
119 return '';
120 }
121
122 // Create header, showing upload path:
123 $header = $folderObject->getIdentifier();
124 $lang = $this->getLanguageService();
125 // Create a list of allowed file extensions with the readable format "youtube, vimeo" etc.
126 $fileExtList = [];
127 foreach ($allowedExtensions as $fileExt) {
128 if (GeneralUtility::verifyFilenameAgainstDenyPattern('.' . $fileExt)) {
129 $fileExtList[] = '<span class="label label-success">'
130 . strtoupper(htmlspecialchars($fileExt)) . '</span>';
131 }
132 }
133 /** @var UriBuilder $uriBuilder */
134 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
135 $formAction = (string)$uriBuilder->buildUriFromRoute('tce_file');
136 $combinedIdentifier = $folderObject->getCombinedIdentifier();
137 $markup = [];
138 $markup[] = '<form action="' . htmlspecialchars($formAction)
139 . '" method="post" name="editform" enctype="multipart/form-data">';
140 $markup[] = ' <h3>' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:file_upload.php.pagetitle')) . ':</h3>';
141 $markup[] = ' <p><strong>' . htmlspecialchars($lang->getLL('path')) . ':</strong>' . htmlspecialchars($header) . '</p>';
142 // Traverse the number of upload fields:
143 for ($a = 1; $a <= $count; $a++) {
144 $markup[] = '<div class="form-group">';
145 $markup[] = '<span class="btn btn-default btn-file">';
146 $markup[] = '<input type="file" multiple="multiple" name="upload_' . $a . '[]" size="50" />';
147 $markup[] = '</span>';
148 $markup[] = '</div>';
149 $markup[] = '<input type="hidden" name="data[upload][' . $a . '][target]" value="'
150 . htmlspecialchars($combinedIdentifier) . '" />';
151 $markup[] = '<input type="hidden" name="data[upload][' . $a . '][data]" value="' . $a . '" />';
152 }
153 $redirectValue = $this->parameterProvider->getScriptUrl() . HttpUtility::buildQueryString(
154 $this->parameterProvider->getUrlParameters(['identifier' => $combinedIdentifier]),
155 '&'
156 );
157 $markup[] = '<input type="hidden" name="data[upload][1][redirect]" value="' . htmlspecialchars($redirectValue) . '" />';
158
159 if (!empty($fileExtList)) {
160 $markup[] = '<div class="form-group">';
161 $markup[] = ' <label>';
162 $markup[] = htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.allowedFileExtensions')) . '<br/>';
163 $markup[] = ' </label>';
164 $markup[] = ' <div>';
165 $markup[] = implode(' ', $fileExtList);
166 $markup[] = ' </div>';
167 $markup[] = '</div>';
168 }
169
170 $markup[] = '<div class="checkbox">';
171 $markup[] = ' <label>';
172 $markup[] = ' <input type="checkbox" name="overwriteExistingFiles" id="overwriteExistingFiles" value="replace" />';
173 $markup[] = htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_misc.xlf:overwriteExistingFiles'));
174 $markup[] = ' </label>';
175 $markup[] = '</div>';
176 $markup[] = '<input class="btn btn-default" type="submit" name="submit" value="'
177 . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:file_upload.php.submit')) . '" />';
178
179 $markup[] = '</form>';
180
181 $code = implode(LF, $markup);
182
183 // Add online media
184 // Create a list of allowed file extensions in a readable format "youtube, vimeo" etc.
185 $fileExtList = [];
186 $onlineMediaFileExt = OnlineMediaHelperRegistry::getInstance()->getSupportedFileExtensions();
187 foreach ($onlineMediaFileExt as $fileExt) {
188 if (GeneralUtility::verifyFilenameAgainstDenyPattern('.' . $fileExt)
189 && (empty($allowedExtensions) || in_array($fileExt, $allowedExtensions, true))
190 ) {
191 $fileExtList[] = '<span class="label label-success">'
192 . strtoupper(htmlspecialchars($fileExt)) . '</span>';
193 }
194 }
195 if (!empty($fileExtList)) {
196 $formAction = (string)$uriBuilder->buildUriFromRoute('online_media');
197
198 $markup = [];
199 $markup[] = '<form action="' . htmlspecialchars($formAction)
200 . '" method="post" name="editform1" id="typo3-addMediaForm" enctype="multipart/form-data">';
201 $markup[] = '<h3>' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media')) . ':</h3>';
202 $markup[] = '<p><strong>' . htmlspecialchars($lang->getLL('path')) . ':</strong>' . htmlspecialchars($header) . '</p>';
203 $markup[] = '<div class="form-group">';
204 $markup[] = '<input type="hidden" name="data[newMedia][0][target]" value="'
205 . htmlspecialchars($folderObject->getCombinedIdentifier()) . '" />';
206 $markup[] = '<input type="hidden" name="data[newMedia][0][allowed]" value="'
207 . htmlspecialchars(implode(',', $allowedExtensions)) . '" />';
208 $markup[] = '<div class="input-group">';
209 $markup[] = '<input type="text" name="data[newMedia][0][url]" class="form-control" placeholder="'
210 . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media.placeholder')) . '" />';
211 $markup[] = '<div class="input-group-btn">';
212 $markup[] = '<button class="btn btn-default">'
213 . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media.submit')) . '</button>';
214 $markup[] = '</div>';
215 $markup[] = '</div>';
216 $markup[] = '<div class="form-group">';
217 $markup[] = '<label>';
218 $markup[] = $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media.allowedProviders') . '<br/>';
219 $markup[] = '</label>';
220 $markup[] = '<div>';
221 $markup[] = implode(' ', $fileExtList);
222 $markup[] = '</div>';
223 $markup[] = '</div>';
224 $markup[] = '<input type="hidden" name="redirect" value="' . htmlspecialchars($redirectValue) . '" />';
225 $markup[] = '</form>';
226
227 $code .= implode(LF, $markup);
228 }
229
230 return $code;
231 }
232
233 /**
234 * Get the HTML data required for the file search field of the TYPO3 Element Browser.
235 *
236 * @param string $searchWord
237 * @return string HTML data required for the search field in the file list of the Element Browser
238 */
239 public function getFileSearchField($searchWord)
240 {
241 $action = $this->parameterProvider->getScriptUrl()
242 . HttpUtility::buildQueryString($this->parameterProvider->getUrlParameters([]), '&');
243
244 $markup = [];
245 $markup[] = '<form method="post" action="' . htmlspecialchars($action) . '" style="padding-bottom: 15px;">';
246 $markup[] = ' <div class="input-group">';
247 $markup[] = ' <input class="form-control" type="text" name="searchWord" value="'
248 . htmlspecialchars($searchWord) . '">';
249 $markup[] = ' <span class="input-group-btn">';
250 $markup[] = ' <button class="btn btn-default" type="submit">'
251 . htmlspecialchars(
252 $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:search')
253 )
254 . '</button>';
255 $markup[] = ' </span>';
256 $markup[] = ' </div>';
257 $markup[] = '</form>';
258 return implode(LF, $markup);
259 }
260
261 /**
262 * @return LanguageService
263 */
264 protected function getLanguageService()
265 {
266 return $GLOBALS['LANG'];
267 }
268
269 /**
270 * @return BackendUserAuthentication
271 */
272 protected function getBackendUser()
273 {
274 return $GLOBALS['BE_USER'];
275 }
276 }