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