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