[!!!][TASK] Remove deprecated code from EXT:recordlist
[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 }
48
49 /**
50 * @return void
51 */
52 protected function initVariables()
53 {
54 parent::initVariables();
55 $this->expandFolder = GeneralUtility::_GP('expandFolder');
56 }
57
58 /**
59 * Session data for this class can be set from outside with this method.
60 *
61 * @param mixed[] $data Session data array
62 * @return array[] Session data and boolean which indicates that data needs to be stored in session because it's changed
63 */
64 public function processSessionData($data)
65 {
66 if ($this->expandFolder !== null) {
67 $data['expandFolder'] = $this->expandFolder;
68 $store = true;
69 } else {
70 $this->expandFolder = $data['expandFolder'];
71 $store = false;
72 }
73 return array($data, $store);
74 }
75
76 /**
77 * @return string HTML content
78 */
79 public function render()
80 {
81 $selectedFolder = null;
82 if ($this->expandFolder) {
83 $selectedFolder = ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier($this->expandFolder);
84 }
85
86 // Create folder tree:
87 /** @var ElementBrowserFolderTreeView $folderTree */
88 $folderTree = GeneralUtility::makeInstance(ElementBrowserFolderTreeView::class);
89 $folderTree->setLinkParameterProvider($this);
90 $tree = $folderTree->getBrowsableTree();
91
92 $folders = '';
93 if ($selectedFolder) {
94 $folders = $this->renderFolders($selectedFolder);
95 }
96
97 $this->initDocumentTemplate();
98 $content = $this->doc->startPage('TBE folder selector');
99 $content .= $this->doc->getFlashMessages();
100
101 // Putting the parts together, side by side:
102 $content .= '
103
104 <!--
105 Wrapper table for folder tree / folder list:
106 -->
107 <table border="0" cellpadding="0" cellspacing="0" id="typo3-EBfiles">
108 <tr>
109 <td class="c-wCell" valign="top"><h3>' . $this->getLanguageService()->getLL('folderTree', true) . ':</h3>' . $tree . '</td>
110 <td class="c-wCell" valign="top">' . $folders . '</td>
111 </tr>
112 </table>
113 ';
114
115 // Adding create folder if applicable:
116 if ($selectedFolder) {
117 $content .= GeneralUtility::makeInstance(FolderUtilityRenderer::class, $this)->createFolder($selectedFolder);
118 }
119
120 // Add some space
121 $content .= '<br /><br />';
122
123 // Ending page, returning content:
124 $content .= $this->doc->endPage();
125 return $this->doc->insertStylesAndJS($content);
126 }
127
128 /**
129 * @param Folder $parentFolder
130 * @return string HTML code
131 */
132 protected function renderFolders(Folder $parentFolder)
133 {
134 if (!$parentFolder->checkActionPermission('read')) {
135 return '';
136 }
137 $content = '';
138 $lang = $this->getLanguageService();
139 $folders = $parentFolder->getSubfolders();
140 $folderIdentifier = $parentFolder->getCombinedIdentifier();
141
142 // Create headline (showing number of folders):
143 $content .= '<h3>' . sprintf($lang->getLL('folders', true) . ' (%s):', count($folders)) . '</h3>';
144
145 $titleLength = (int)$this->getBackendUser()->uc['titleLen'];
146 // Create the header of current folder:
147 $folderIcon = '<a href="#" data-folder-id="' . htmlspecialchars($folderIdentifier) . '" data-close="1">';
148 $folderIcon .= $this->iconFactory->getIcon('apps-filetree-folder-default', Icon::SIZE_SMALL);
149 $folderIcon .= htmlspecialchars(GeneralUtility::fixed_lgd_cs($parentFolder->getName(), $titleLength));
150 $folderIcon .= '</a>';
151 $content .= $folderIcon . '<br />';
152
153 $lines = array();
154 // Traverse the folder list:
155 foreach ($folders as $subFolder) {
156 $subFolderIdentifier = $subFolder->getCombinedIdentifier();
157 // Create folder icon:
158 $icon = '<span style="width: 16px; height: 16px; display: inline-block;"></span>';
159 $icon .= '<span title="' . htmlspecialchars($subFolder->getName()) . '">' . $this->iconFactory->getIcon('apps-filetree-folder-default', Icon::SIZE_SMALL) . '</span>';
160 // Create links for adding the folder:
161 $aTag = '<a href="#" data-folder-id="' . htmlspecialchars($folderIdentifier) . '" data-close="0">';
162 $aTag_alt = '<a href="#" data-folder-id="' . htmlspecialchars($folderIdentifier) . '" data-close="1">';
163 if (strstr($subFolderIdentifier, ',') || strstr($subFolderIdentifier, '|')) {
164 // In case an invalid character is in the filepath, display error message:
165 $errorMessage = sprintf($lang->getLL('invalidChar', true), ', |');
166 $aTag = '<a href="#" class="t3js-folderIdError" data-message="' . $errorMessage . '">';
167 }
168 $aTag_e = '</a>';
169 // Combine icon and folderpath:
170 $foldernameAndIcon = $aTag_alt . $icon . htmlspecialchars(GeneralUtility::fixed_lgd_cs($subFolder->getName(), $titleLength)) . $aTag_e;
171 $lines[] = '
172 <tr class="bgColor4">
173 <td nowrap="nowrap">' . $foldernameAndIcon . '&nbsp;</td>
174 <td>' . $aTag . '<span title="' . $lang->getLL('addToList', true) . '">' . $this->iconFactory->getIcon('actions-edit-add', Icon::SIZE_SMALL)->render() . '</span>' . $aTag_e . '</td>
175 <td>&nbsp;</td>
176 </tr>';
177 $lines[] = '
178 <tr>
179 <td colspan="3"><span style="width: 1px; height: 3px; display: inline-block;"></span></td>
180 </tr>';
181 }
182 // Wrap all the rows in table tags:
183 $content .= '
184
185 <!--
186 Folder listing
187 -->
188 <table border="0" cellpadding="0" cellspacing="1" id="typo3-folderList">
189 ' . implode('', $lines) . '
190 </table>';
191
192 return $content;
193 }
194
195 /**
196 * @return string[] Array of body-tag attributes
197 */
198 protected function getBodyTagAttributes()
199 {
200 return [
201 'data-mode' => 'folder'
202 ];
203 }
204
205 /**
206 * @param array $values Array of values to include into the parameters
207 * @return string[] Array of parameters which have to be added to URLs
208 */
209 public function getUrlParameters(array $values)
210 {
211 return [
212 'mode' => 'folder',
213 'expandFolder' => isset($values['identifier']) ? $values['identifier'] : $this->expandFolder,
214 'bparams' => $this->bparams
215 ];
216 }
217
218 /**
219 * @param array $values Values to be checked
220 * @return bool Returns TRUE if the given values match the currently selected item
221 */
222 public function isCurrentlySelectedItem(array $values)
223 {
224 return false;
225 }
226
227 /**
228 * Returns the URL of the current script
229 *
230 * @return string
231 */
232 public function getScriptUrl()
233 {
234 return $this->thisScript;
235 }
236 }