TYPO3 CMS  TYPO3_8-7
FolderBrowser.php
Go to the documentation of this file.
1 <?php
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 
24 
29 {
38  protected $expandFolder;
39 
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 
56  protected function initVariables()
57  {
58  parent::initVariables();
59  $this->expandFolder = GeneralUtility::_GP('expandFolder');
60  }
61 
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 
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:
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 
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 
203  protected function getBodyTagAttributes()
204  {
205  return [
206  'data-mode' => 'folder'
207  ];
208  }
209 
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 
227  public function isCurrentlySelectedItem(array $values)
228  {
229  return false;
230  }
231 
237  public function getScriptUrl()
238  {
239  return $this->thisScript;
240  }
241 }
static makeInstance($className,... $constructorArguments)
getSubfolders($start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive=false)
Definition: Folder.php:272
static fixed_lgd_cs($string, $chars, $appendString='...')