[BUGFIX] Use correct value for overwriteExistingFiles
[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\Resource\Folder;
20 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Lang\LanguageService;
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 = array();
68 $markup[] = '<div class="element-browser-section element-browser-createfolder">';
69 $markup[] = '<form action="' . htmlspecialchars($formAction)
70 . '" method="post" name="editform" enctype="multipart/form-data">';
71 $markup[] = '<h3>' . htmlspecialchars($lang->sL('LLL:EXT:lang/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="row form-group"><div class="col-xs-8">';
77 $markup[] = '<input class="form-control" type="text" name="file[newfolder][' . $a . '][data]" />';
78 $markup[] = '<input type="hidden" name="file[newfolder][' . $a . '][target]" value="'
79 . htmlspecialchars($folderObject->getCombinedIdentifier()) . '" />';
80
81 // Make footer of upload form, including the submit button:
82 $redirectValue = $this->parameterProvider->getScriptUrl() . GeneralUtility::implodeArrayForUrl(
83 '',
84 $this->parameterProvider->getUrlParameters(
85 ['identifier' => $folderObject->getCombinedIdentifier()]
86 )
87 );
88 $markup[] = '<input type="hidden" name="redirect" value="' . htmlspecialchars($redirectValue) . '" />';
89 $markup[] = '</div><div class="col-xs-4">';
90 $markup[] = '<input class="btn btn-default" type="submit" name="submit" value="'
91 . htmlspecialchars($lang->sL('LLL:EXT:lang/locallang_core.xlf:create_folder.submit')) . '" />';
92
93 $markup[] = '</div></div></form>';
94 $markup[] = '</div>';
95
96 return implode(LF, $markup);
97 }
98
99 /**
100 * Makes an upload form for uploading files to the filemount the user is browsing.
101 * The files are uploaded to the tce_file.php script in the core which will handle the upload.
102 *
103 * @param Folder $folderObject
104 * @param string[] $allowedExtensions
105 *
106 * @return string HTML for an upload form.
107 */
108 public function uploadForm(Folder $folderObject, array $allowedExtensions)
109 {
110 if (!$folderObject->checkActionPermission('write')) {
111 return '';
112 }
113 // Read configuration of upload field count
114 $userSetting = $this->getBackendUser()->getTSConfigVal('options.folderTree.uploadFieldsInLinkBrowser');
115 $count = isset($userSetting) ? (int)$userSetting : 1;
116 if ($count === 0) {
117 return '';
118 }
119
120 $count = (int)$count === 0 ? 1 : (int)$count;
121 // Create header, showing upload path:
122 $header = $folderObject->getIdentifier();
123 $lang = $this->getLanguageService();
124 // Create a list of allowed file extensions with the readable format "youtube, vimeo" etc.
125 $fileExtList = array();
126 foreach ($allowedExtensions as $fileExt) {
127 if (GeneralUtility::verifyFilenameAgainstDenyPattern('.' . $fileExt)) {
128 $fileExtList[] = '<span class="label label-success">'
129 . strtoupper(htmlspecialchars($fileExt)) . '</span>';
130 }
131 }
132 $formAction = BackendUtility::getModuleUrl('tce_file');
133 $combinedIdentifier = $folderObject->getCombinedIdentifier();
134 $markup = array();
135 $markup[] = '<div class="element-browser-section element-browser-upload">';
136 $markup[] = ' <form action="' . htmlspecialchars($formAction)
137 . '" method="post" name="editform" enctype="multipart/form-data">';
138 $markup[] = ' <h3>' . htmlspecialchars($lang->sL('LLL:EXT:lang/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="file[upload][' . $a . '][target]" value="'
148 . htmlspecialchars($combinedIdentifier) . '" />';
149 $markup[] = '<input type="hidden" name="file[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="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/locallang_core.xlf:cm.allowedFileExtensions')) . '<br/>';
161 $markup[] = ' </label>';
162 $markup[] = ' <div class="form-control">';
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/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/locallang_core.xlf:file_upload.php.submit')) . '" />';
176
177 $markup[] = ' </form>';
178 $markup[] = '</div>';
179
180 $code = implode(LF, $markup);
181
182 // Add online media
183 // Create a list of allowed file extensions in a readable format "youtube, vimeo" etc.
184 $fileExtList = array();
185 $onlineMediaFileExt = OnlineMediaHelperRegistry::getInstance()->getSupportedFileExtensions();
186 foreach ($onlineMediaFileExt as $fileExt) {
187 if (GeneralUtility::verifyFilenameAgainstDenyPattern('.' . $fileExt)
188 && (empty($allowedExtensions) || in_array($fileExt, $allowedExtensions, true))
189 ) {
190 $fileExtList[] = '<span class="label label-success">'
191 . strtoupper(htmlspecialchars($fileExt)) . '</span>';
192 }
193 }
194 if (!empty($fileExtList)) {
195 $formAction = BackendUtility::getModuleUrl('online_media');
196
197 $markup = array();
198 $markup[] = '<div class="element-browser-section element-browser-mediaurls">';
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:lang/locallang_core.xlf:online_media.new_media')) . ':</h3>';
202 $markup[] = '<p><strong>' . htmlspecialchars($lang->getLL('path')) . ':</strong>' . htmlspecialchars($header) . '</p>';
203 $markup[] = '<div class="row form-group"><div class="col-xs-8">';
204 $markup[] = '<input type="hidden" name="file[newMedia][0][target]" value="'
205 . htmlspecialchars($folderObject->getCombinedIdentifier()) . '" />';
206 $markup[] = '<input type="hidden" name="file[newMedia][0][allowed]" value="'
207 . htmlspecialchars(implode(',', $allowedExtensions)) . '" />';
208 $markup[] = '<input type="text" name="file[newMedia][0][url]" class="form-control" placeholder="'
209 . htmlspecialchars($lang->sL('LLL:EXT:lang/locallang_core.xlf:online_media.new_media.placeholder')) . '" />';
210 $markup[] = '</div><div class="col-xs-4">';
211 $markup[] = '<button class="btn btn-default">'
212 . htmlspecialchars($lang->sL('LLL:EXT:lang/locallang_core.xlf:online_media.new_media.submit')) . '</button>';
213
214 $markup[] = '</div></div>';
215 $markup[] = '<div class="form-group">';
216 $markup[] = ' <label>';
217 $markup[] = $lang->sL('LLL:EXT:lang/locallang_core.xlf:online_media.new_media.allowedProviders') . '<br/>';
218 $markup[] = ' </label>';
219 $markup[] = ' <div class="form-control">';
220 $markup[] = implode(' ', $fileExtList);
221 $markup[] = ' </div>';
222 $markup[] = '</div>';
223 $markup[] = '<input type="hidden" name="redirect" value="' . htmlspecialchars($redirectValue) . '" />';
224 $markup[] = '</form>';
225 $markup[] = '</div>';
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 . GeneralUtility::implodeArrayForUrl('', $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:filelist/Resources/Private/Language/locallang.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 }