[BUGFIX] Fix undefined Tree error in FolderBrowser
[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.registerDragDropHandlers();
50 }');
51 }
52
53 /**
54 * @return void
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 array($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('TBE folder selector');
103 $content .= $this->doc->getFlashMessages();
104
105 // Putting the parts together, side by side:
106 $content .= '
107
108 <!--
109 Wrapper table for folder tree / folder list:
110 -->
111 <table border="0" cellpadding="0" cellspacing="0" id="typo3-EBfiles">
112 <tr>
113 <td class="c-wCell" valign="top"><h3>' . $this->getLanguageService()->getLL('folderTree', true) . ':</h3>' . $tree . '</td>
114 <td class="c-wCell" valign="top">' . $folders . '</td>
115 </tr>
116 </table>
117 ';
118
119 // Adding create folder if applicable:
120 if ($selectedFolder) {
121 $content .= GeneralUtility::makeInstance(FolderUtilityRenderer::class, $this)->createFolder($selectedFolder);
122 }
123
124 // Add some space
125 $content .= '<br /><br />';
126
127 // Ending page, returning content:
128 $content .= $this->doc->endPage();
129 return $this->doc->insertStylesAndJS($content);
130 }
131
132 /**
133 * @param Folder $parentFolder
134 * @return string HTML code
135 */
136 protected function renderFolders(Folder $parentFolder)
137 {
138 if (!$parentFolder->checkActionPermission('read')) {
139 return '';
140 }
141 $content = '';
142 $lang = $this->getLanguageService();
143 $folders = $parentFolder->getSubfolders();
144 $folderIdentifier = $parentFolder->getCombinedIdentifier();
145
146 // Create headline (showing number of folders):
147 $content .= '<h3>' . sprintf($lang->getLL('folders', true) . ' (%s):', count($folders)) . '</h3>';
148
149 $titleLength = (int)$this->getBackendUser()->uc['titleLen'];
150 // Create the header of current folder:
151 $folderIcon = '<a href="#" data-folder-id="' . htmlspecialchars($folderIdentifier) . '" data-close="1">';
152 $folderIcon .= $this->iconFactory->getIcon('apps-filetree-folder-default', Icon::SIZE_SMALL);
153 $folderIcon .= htmlspecialchars(GeneralUtility::fixed_lgd_cs($parentFolder->getName(), $titleLength));
154 $folderIcon .= '</a>';
155 $content .= $folderIcon . '<br />';
156
157 $lines = array();
158 // Traverse the folder list:
159 foreach ($folders as $subFolder) {
160 $subFolderIdentifier = $subFolder->getCombinedIdentifier();
161 // Create folder icon:
162 $icon = '<span style="width: 16px; height: 16px; display: inline-block;"></span>';
163 $icon .= '<span title="' . htmlspecialchars($subFolder->getName()) . '">' . $this->iconFactory->getIcon('apps-filetree-folder-default', Icon::SIZE_SMALL) . '</span>';
164 // Create links for adding the folder:
165 $aTag = '<a href="#" data-folder-id="' . htmlspecialchars($folderIdentifier) . '" data-close="0">';
166 $aTag_alt = '<a href="#" data-folder-id="' . htmlspecialchars($folderIdentifier) . '" data-close="1">';
167 if (strstr($subFolderIdentifier, ',') || strstr($subFolderIdentifier, '|')) {
168 // In case an invalid character is in the filepath, display error message:
169 $errorMessage = sprintf($lang->getLL('invalidChar', true), ', |');
170 $aTag = '<a href="#" class="t3js-folderIdError" data-message="' . $errorMessage . '">';
171 }
172 $aTag_e = '</a>';
173 // Combine icon and folderpath:
174 $foldernameAndIcon = $aTag_alt . $icon . htmlspecialchars(GeneralUtility::fixed_lgd_cs($subFolder->getName(), $titleLength)) . $aTag_e;
175 $lines[] = '
176 <tr class="bgColor4">
177 <td nowrap="nowrap">' . $foldernameAndIcon . '&nbsp;</td>
178 <td>' . $aTag . '<span title="' . $lang->getLL('addToList', true) . '">' . $this->iconFactory->getIcon('actions-edit-add', Icon::SIZE_SMALL)->render() . '</span>' . $aTag_e . '</td>
179 <td>&nbsp;</td>
180 </tr>';
181 $lines[] = '
182 <tr>
183 <td colspan="3"><span style="width: 1px; height: 3px; display: inline-block;"></span></td>
184 </tr>';
185 }
186 // Wrap all the rows in table tags:
187 $content .= '
188
189 <!--
190 Folder listing
191 -->
192 <table border="0" cellpadding="0" cellspacing="1" id="typo3-folderList">
193 ' . implode('', $lines) . '
194 </table>';
195
196 return $content;
197 }
198
199 /**
200 * @return string[] Array of body-tag attributes
201 */
202 protected function getBodyTagAttributes()
203 {
204 return [
205 'data-mode' => 'folder'
206 ];
207 }
208
209 /**
210 * @param array $values Array of values to include into the parameters
211 * @return string[] Array of parameters which have to be added to URLs
212 */
213 public function getUrlParameters(array $values)
214 {
215 return [
216 'mode' => 'folder',
217 'expandFolder' => isset($values['identifier']) ? $values['identifier'] : $this->expandFolder,
218 'bparams' => $this->bparams
219 ];
220 }
221
222 /**
223 * @param array $values Values to be checked
224 * @return bool Returns TRUE if the given values match the currently selected item
225 */
226 public function isCurrentlySelectedItem(array $values)
227 {
228 return false;
229 }
230
231 /**
232 * Returns the URL of the current script
233 *
234 * @return string
235 */
236 public function getScriptUrl()
237 {
238 return $this->thisScript;
239 }
240 }