[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / recordlist / Classes / Browser / FolderBrowser.php
1 <?php
2 namespace TYPO3\CMS\Recordlist\Browser;
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\Tree\View\ElementBrowserFolderTreeView;
18 use TYPO3\CMS\Core\Imaging\Icon;
19 use TYPO3\CMS\Core\Resource\Folder;
20 use TYPO3\CMS\Core\Resource\ResourceFactory;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Recordlist\Tree\View\LinkParameterProviderInterface;
23 use TYPO3\CMS\Recordlist\View\FolderUtilityRenderer;
24
25 /**
26 * Browser for folders
27 */
28 class FolderBrowser extends AbstractElementBrowser implements ElementBrowserInterface, LinkParameterProviderInterface
29 {
30 /**
31 * When you click a folder name/expand icon to see the content of a certain file folder,
32 * this value will contain the path of the expanded file folder.
33 * If the value is NOT set, then it will be restored from the module session data.
34 * Example value: "/www/htdocs/typo3/32/3dsplm/fileadmin/css/"
35 *
36 * @var string|null
37 */
38 protected $expandFolder;
39
40 /**
41 * Adds additional JavaScript modules
42 */
43 protected function initialize()
44 {
45 parent::initialize();
46 $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Recordlist/BrowseFolders');
47 $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/LegacyTree', 'function() {
48 DragDrop.table = "folders";
49 Tree.registerDragDropHandlers();
50 }');
51 }
52
53 /**
54 * Checks for an additional request parameter
55 */
56 protected function initVariables()
57 {
58 parent::initVariables();
59 $this->expandFolder = GeneralUtility::_GP('expandFolder');
60 }
61
62 /**
63 * Session data for this class can be set from outside with this method.
64 *
65 * @param mixed[] $data Session data array
66 * @return array[] Session data and boolean which indicates that data needs to be stored in session because it's changed
67 */
68 public function processSessionData($data)
69 {
70 if ($this->expandFolder !== null) {
71 $data['expandFolder'] = $this->expandFolder;
72 $store = true;
73 } else {
74 $this->expandFolder = $data['expandFolder'];
75 $store = false;
76 }
77 return [$data, $store];
78 }
79
80 /**
81 * @return string HTML content
82 */
83 public function render()
84 {
85 $selectedFolder = null;
86 if ($this->expandFolder) {
87 $selectedFolder = ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier($this->expandFolder);
88 }
89
90 // Create folder tree:
91 /** @var ElementBrowserFolderTreeView $folderTree */
92 $folderTree = GeneralUtility::makeInstance(ElementBrowserFolderTreeView::class);
93 $folderTree->setLinkParameterProvider($this);
94 $tree = $folderTree->getBrowsableTree();
95
96 $folders = '';
97 if ($selectedFolder) {
98 $folders = $this->renderFolders($selectedFolder);
99 }
100
101 $this->initDocumentTemplate();
102 $content = $this->doc->startPage(htmlspecialchars($this->getLanguageService()->getLL('folderSelector')));
103
104 // Putting the parts together, side by side:
105 $markup = [];
106 $markup[] = '<!-- Wrapper table for folder tree / filelist: -->';
107 $markup[] = '<div class="element-browser">';
108 $markup[] = ' <div class="element-browser-panel element-browser-main">';
109 $markup[] = ' <div class="element-browser-main-sidebar">';
110 $markup[] = ' <div class="element-browser-body">';
111 $markup[] = ' <h3>' . htmlspecialchars($this->getLanguageService()->getLL('folderTree')) . ':</h3>';
112 $markup[] = ' ' . $tree;
113 $markup[] = ' </div>';
114 $markup[] = ' </div>';
115 $markup[] = ' <div class="element-browser-main-content">';
116 $markup[] = ' <div class="element-browser-body">';
117 $markup[] = ' ' . $this->doc->getFlashMessages();
118 $markup[] = ' ' . $folders;
119 if ($selectedFolder) {
120 $markup[] = ' ' . GeneralUtility::makeInstance(FolderUtilityRenderer::class, $this)->createFolder($selectedFolder);
121 }
122 $markup[] = ' </div>';
123 $markup[] = ' </div>';
124 $markup[] = ' </div>';
125 $markup[] = '</div>';
126 $content .= implode('', $markup);
127
128 // Ending page, returning content:
129 $content .= $this->doc->endPage();
130 return $this->doc->insertStylesAndJS($content);
131 }
132
133 /**
134 * @param Folder $parentFolder
135 * @return string HTML code
136 */
137 protected function renderFolders(Folder $parentFolder)
138 {
139 if (!$parentFolder->checkActionPermission('read')) {
140 return '';
141 }
142 $content = '';
143 $lang = $this->getLanguageService();
144 $folders = $parentFolder->getSubfolders();
145 $folderIdentifier = $parentFolder->getCombinedIdentifier();
146
147 // Create headline (showing number of folders):
148 $content .= '<h3>' . sprintf(htmlspecialchars($lang->getLL('folders')) . ' (%s):', count($folders)) . '</h3>';
149
150 $titleLength = (int)$this->getBackendUser()->uc['titleLen'];
151 // Create the header of current folder:
152 $folderIcon = '<a href="#" data-folder-id="' . htmlspecialchars($folderIdentifier) . '" data-close="1">';
153 $folderIcon .= $this->iconFactory->getIcon('apps-filetree-folder-default', Icon::SIZE_SMALL);
154 $folderIcon .= htmlspecialchars(GeneralUtility::fixed_lgd_cs($parentFolder->getName(), $titleLength));
155 $folderIcon .= '</a>';
156 $content .= $folderIcon . '<br />';
157
158 $lines = [];
159 // Traverse the folder list:
160 foreach ($folders as $subFolder) {
161 $subFolderIdentifier = $subFolder->getCombinedIdentifier();
162 // Create folder icon:
163 $icon = '<span style="width: 16px; height: 16px; display: inline-block;"></span>';
164 $icon .= '<span title="' . htmlspecialchars($subFolder->getName()) . '">' . $this->iconFactory->getIcon('apps-filetree-folder-default', Icon::SIZE_SMALL) . '</span>';
165 // Create links for adding the folder:
166 $aTag = '<a href="#" data-folder-id="' . htmlspecialchars($folderIdentifier) . '" data-close="0">';
167 $aTag_alt = '<a href="#" data-folder-id="' . htmlspecialchars($folderIdentifier) . '" data-close="1">';
168 if (strstr($subFolderIdentifier, ',') || strstr($subFolderIdentifier, '|')) {
169 // In case an invalid character is in the filepath, display error message:
170 $errorMessage = sprintf(htmlspecialchars($lang->getLL('invalidChar')), ', |');
171 $aTag = '<a href="#" class="t3js-folderIdError" data-message="' . $errorMessage . '">';
172 }
173 $aTag_e = '</a>';
174 // Combine icon and folderpath:
175 $foldernameAndIcon = $aTag_alt . $icon . htmlspecialchars(GeneralUtility::fixed_lgd_cs($subFolder->getName(), $titleLength)) . $aTag_e;
176 $lines[] = '
177 <tr>
178 <td class="nowrap">' . $foldernameAndIcon . '&nbsp;</td>
179 <td>' . $aTag . '<span title="' . htmlspecialchars($lang->getLL('addToList')) . '">' . $this->iconFactory->getIcon('actions-add', Icon::SIZE_SMALL)->render() . '</span>' . $aTag_e . '</td>
180 <td>&nbsp;</td>
181 </tr>';
182 $lines[] = '
183 <tr>
184 <td colspan="3"><span style="width: 1px; height: 3px; display: inline-block;"></span></td>
185 </tr>';
186 }
187 // Wrap all the rows in table tags:
188 $content .= '
189
190 <!--
191 Folder listing
192 -->
193 <table border="0" cellpadding="0" cellspacing="1" id="typo3-folderList">
194 ' . implode('', $lines) . '
195 </table>';
196
197 return $content;
198 }
199
200 /**
201 * @return string[] Array of body-tag attributes
202 */
203 protected function getBodyTagAttributes()
204 {
205 return [
206 'data-mode' => 'folder'
207 ];
208 }
209
210 /**
211 * @param array $values Array of values to include into the parameters
212 * @return string[] Array of parameters which have to be added to URLs
213 */
214 public function getUrlParameters(array $values)
215 {
216 return [
217 'mode' => 'folder',
218 'expandFolder' => isset($values['identifier']) ? $values['identifier'] : $this->expandFolder,
219 'bparams' => $this->bparams
220 ];
221 }
222
223 /**
224 * @param array $values Values to be checked
225 * @return bool Returns TRUE if the given values match the currently selected item
226 */
227 public function isCurrentlySelectedItem(array $values)
228 {
229 return false;
230 }
231
232 /**
233 * Returns the URL of the current script
234 *
235 * @return string
236 */
237 public function getScriptUrl()
238 {
239 return $this->thisScript;
240 }
241 }