TYPO3 CMS  TYPO3_7-6
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.ajaxID = "sc_alt_file_navframe_expandtoggle";
50  Tree.registerDragDropHandlers();
51  }');
52  }
53 
57  protected function initVariables()
58  {
59  parent::initVariables();
60  $this->expandFolder = GeneralUtility::_GP('expandFolder');
61  }
62 
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 
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:
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 
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 
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 }
getSubfolders($start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive=false)
Definition: Folder.php:273
static fixed_lgd_cs($string, $chars, $appendString='...')