[BUGFIX] Fix folder element browser expand/collapse
[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 * @return void
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.ajaxID = "sc_alt_file_navframe_expandtoggle";
50 Tree.registerDragDropHandlers();
51 }');
52 }
53
54 /**
55 * @return void
56 */
57 protected function initVariables()
58 {
59 parent::initVariables();
60 $this->expandFolder = GeneralUtility::_GP('expandFolder');
61 }
62
63 /**
64 * Session data for this class can be set from outside with this method.
65 *
66 * @param mixed[] $data Session data array
67 * @return array[] Session data and boolean which indicates that data needs to be stored in session because it's changed
68 */
69 public function processSessionData($data)
70 {
71 if ($this->expandFolder !== null) {
72 $data['expandFolder'] = $this->expandFolder;
73 $store = true;
74 } else {
75 $this->expandFolder = $data['expandFolder'];
76 $store = false;
77 }
78 return [$data, $store];
79 }
80
81 /**
82 * @return string HTML content
83 */
84 public function render()
85 {
86 $selectedFolder = null;
87 if ($this->expandFolder) {
88 $selectedFolder = ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier($this->expandFolder);
89 }
90
91 // Create folder tree:
92 /** @var ElementBrowserFolderTreeView $folderTree */
93 $folderTree = GeneralUtility::makeInstance(ElementBrowserFolderTreeView::class);
94 $folderTree->setLinkParameterProvider($this);
95 $tree = $folderTree->getBrowsableTree();
96
97 $folders = '';
98 if ($selectedFolder) {
99 $folders = $this->renderFolders($selectedFolder);
100 }
101
102 $this->initDocumentTemplate();
103 $content = $this->doc->startPage('TBE folder selector');
104 $content .= $this->doc->getFlashMessages();
105
106 // Putting the parts together, side by side:
107 $content .= '
108
109 <!--
110 Wrapper table for folder tree / folder list:
111 -->
112 <table border="0" cellpadding="0" cellspacing="0" id="typo3-EBfiles">
113 <tr>
114 <td class="c-wCell" valign="top"><h3>' . $this->getLanguageService()->getLL('folderTree', true) . ':</h3>' . $tree . '</td>
115 <td class="c-wCell" valign="top">' . $folders . '</td>
116 </tr>
117 </table>
118 ';
119
120 // Adding create folder if applicable:
121 if ($selectedFolder) {
122 $content .= GeneralUtility::makeInstance(FolderUtilityRenderer::class, $this)->createFolder($selectedFolder);
123 }
124
125 // Add some space
126 $content .= '<br /><br />';
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($lang->getLL('folders', true) . ' (%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($lang->getLL('invalidChar', true), ', |');
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 class="bgColor4">
178 <td nowrap="nowrap">' . $foldernameAndIcon . '&nbsp;</td>
179 <td>' . $aTag . '<span title="' . $lang->getLL('addToList', true) . '">' . $this->iconFactory->getIcon('actions-edit-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 }