FolderUtilityRenderer.php 12.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
namespace TYPO3\CMS\Recordlist\View;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19
use TYPO3\CMS\Core\Localization\LanguageService;
20
21
22
23
24
25
26
27
use TYPO3\CMS\Core\Resource\Folder;
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Recordlist\Tree\View\LinkParameterProviderInterface;

/**
 * Renders utility forms used in the views for files/folders of Element and Link Browser
 */
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class FolderUtilityRenderer
{
    /**
     * @var LinkParameterProviderInterface
     */
    protected $parameterProvider;

    /**
     * @param LinkParameterProviderInterface $parameterProvider
     */
    public function __construct(LinkParameterProviderInterface $parameterProvider)
    {
        $this->parameterProvider = $parameterProvider;
    }

    /**
     * For TBE: Makes a form for creating new folders in the filemount the user is browsing.
     * The folder creation request is sent to the tce_file.php script in the core which will handle the creation.
     *
     * @param Folder $folderObject Absolute filepath on server in which to create the new folder.
     *
     * @return string HTML for the create folder form.
     */
    public function createFolder(Folder $folderObject)
    {
        if (!$folderObject->checkActionPermission('write')) {
            return '';
        }
        $backendUser = $this->getBackendUser();
        if (!$backendUser->isAdmin() && !$backendUser->getTSConfigVal('options.createFoldersInEB')) {
            return '';
        }
        // Don't show Folder-create form if it's denied
        if ($backendUser->getTSConfigVal('options.folderTree.hideCreateFolder')) {
            return '';
        }
        $lang = $this->getLanguageService();
65
66

        $formAction = BackendUtility::getModuleUrl('tce_file');
67
        $markup = [];
68
69
        $markup[] = '<form action="' . htmlspecialchars($formAction)
            . '" 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
73
            . htmlspecialchars($folderObject->getIdentifier()) . '</p>';

74
        $a = 1;
75
76
        $markup[] = '<div class="form-group">';
        $markup[] = '<div class="input-group">';
77
        $markup[] = '<input class="form-control" type="text" name="data[newfolder][' . $a . '][data]" />';
78
79
80
81
82
        $markup[] = '<span class="input-group-btn">';
        $markup[] = '<input class="btn btn-default" type="submit" name="submit" value="'
            . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:create_folder.submit')) . '" />';
        $markup[] = '</span>';
        $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
88
89
90
91
92
        $redirectValue = $this->parameterProvider->getScriptUrl() . GeneralUtility::implodeArrayForUrl(
            '',
            $this->parameterProvider->getUrlParameters(
                ['identifier' => $folderObject->getCombinedIdentifier()]
            )
        );
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    }

    /**
     * Makes an upload form for uploading files to the filemount the user is browsing.
     * The files are uploaded to the tce_file.php script in the core which will handle the upload.
     *
     * @param Folder $folderObject
     * @param string[] $allowedExtensions
     *
     * @return string HTML for an upload form.
     */
    public function uploadForm(Folder $folderObject, array $allowedExtensions)
    {
        if (!$folderObject->checkActionPermission('write')) {
            return '';
        }
        // Read configuration of upload field count
        $userSetting = $this->getBackendUser()->getTSConfigVal('options.folderTree.uploadFieldsInLinkBrowser');
        $count = isset($userSetting) ? (int)$userSetting : 1;
        if ($count === 0) {
            return '';
        }

        $count = (int)$count === 0 ? 1 : (int)$count;
        // Create header, showing upload path:
        $header = $folderObject->getIdentifier();
        $lang = $this->getLanguageService();
        // 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
130
                $fileExtList[] = '<span class="label label-success">'
                    . 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
143
144
145
146
            $markup[] = '<div class="form-group">';
            $markup[] = '<span class="btn btn-default btn-file">';
            $markup[] = '<input type="file" multiple="multiple" name="upload_' . $a . '[]" size="50" />';
            $markup[] = '</span>';
            $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
152
153
154
        $redirectValue = $this->parameterProvider->getScriptUrl() . GeneralUtility::implodeArrayForUrl(
            '',
            $this->parameterProvider->getUrlParameters(['identifier' => $combinedIdentifier])
        );
155
        $markup[] = '<input type="hidden" name="data[upload][1][redirect]" value="' . htmlspecialchars($redirectValue) . '" />';
156

157
        if (!empty($fileExtList)) {
158
159
            $markup[] = '<div class="form-group">';
            $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
164
165
            $markup[] = implode(' ', $fileExtList);
            $markup[] = '    </div>';
            $markup[] = '</div>';
166
        }
167

168
169
        $markup[] = '<div class="checkbox">';
        $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
173
174
        $markup[] = '    </label>';
        $markup[] = '</div>';
        $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
182
        // Add online media
        // Create a list of allowed file extensions in a readable format "youtube, vimeo" etc.
183
        $fileExtList = [];
184
185
        $onlineMediaFileExt = OnlineMediaHelperRegistry::getInstance()->getSupportedFileExtensions();
        foreach ($onlineMediaFileExt as $fileExt) {
186
            if (GeneralUtility::verifyFilenameAgainstDenyPattern('.' . $fileExt)
187
188
                && (empty($allowedExtensions) || in_array($fileExt, $allowedExtensions, true))
            ) {
189
190
                $fileExtList[] = '<span class="label label-success">'
                    . strtoupper(htmlspecialchars($fileExt)) . '</span>';
191
192
193
            }
        }
        if (!empty($fileExtList)) {
194
195
            $formAction = BackendUtility::getModuleUrl('online_media');

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
213
            $markup[] = '</div>';
            $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
218
            $markup[] = '</label>';
            $markup[] = '<div>';
219
            $markup[] = implode(' ', $fileExtList);
220
            $markup[] = '</div>';
221
            $markup[] = '</div>';
222
            $markup[] = '<input type="hidden" name="redirect" value="' . htmlspecialchars($redirectValue) . '" />';
223
224
225
            $markup[] = '</form>';

            $code .= implode(LF, $markup);
226
227
228
229
230
        }

        return $code;
    }

Markus Klein's avatar
Markus Klein committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
    /**
     * Get the HTML data required for the file search field of the TYPO3 Element Browser.
     *
     * @param string $searchWord
     * @return string HTML data required for the search field in the file list of the Element Browser
     */
    public function getFileSearchField($searchWord)
    {
        $action = $this->parameterProvider->getScriptUrl()
            . GeneralUtility::implodeArrayForUrl('', $this->parameterProvider->getUrlParameters([]));

        $markup = [];
        $markup[] = '<form method="post" action="' . htmlspecialchars($action) . '" style="padding-bottom: 15px;">';
        $markup[] = '   <div class="input-group">';
        $markup[] = '       <input class="form-control" type="text" name="searchWord" value="'
            . htmlspecialchars($searchWord) . '">';
        $markup[] = '       <span class="input-group-btn">';
        $markup[] = '           <button class="btn btn-default" type="submit">'
            . htmlspecialchars(
                $this->getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:search')
            )
            . '</button>';
        $markup[] = '       </span>';
        $markup[] = '   </div>';
        $markup[] = '</form>';
        return implode(LF, $markup);
    }

259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
    /**
     * @return LanguageService
     */
    protected function getLanguageService()
    {
        return $GLOBALS['LANG'];
    }

    /**
     * @return BackendUserAuthentication
     */
    protected function getBackendUser()
    {
        return $GLOBALS['BE_USER'];
    }
274
}