‪TYPO3CMS  ‪main
TreeController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
28 use TYPO3\CMS\Core\Imaging\IconSize;
34 
41 {
45 
47  {
48  $this->iconFactory = ‪$iconFactory ?? GeneralUtility::makeInstance(IconFactory::class);
49  $this->treeProvider = GeneralUtility::makeInstance(FileStorageTreeProvider::class);
50  $this->resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
51  }
52 
56  public function ‪fetchDataAction(ServerRequestInterface $request): ResponseInterface
57  {
58  $parentIdentifier = $request->getQueryParams()['parent'] ?? null;
59  if ($parentIdentifier) {
60  $currentDepth = (int)($request->getQueryParams()['depth'] ?? 1);
61  $parentIdentifier = rawurldecode($parentIdentifier);
62  $folder = $this->resourceFactory->getFolderObjectFromCombinedIdentifier($parentIdentifier);
63  $items = $this->treeProvider->getSubfoldersRecursively($folder, $currentDepth + 1);
64  } else {
65  $items = $this->treeProvider->getRootNodes($this->‪getBackendUser());
66  }
67  $items = array_map(function (array $item): ‪FileTreeItem {
68  return $this->‪prepareItemForOutput($item);
69  }, $items);
70  return new ‪JsonResponse($items);
71  }
72 
78  public function ‪filterDataAction(ServerRequestInterface $request): ResponseInterface
79  {
80  $search = $request->getQueryParams()['q'] ?? '';
81  $foundFolders = $this->treeProvider->getFilteredTree($this->‪getBackendUser(), $search);
82 
83  $items = [];
84  foreach ($foundFolders as $folder) {
85  if (!$folder instanceof ‪Folder) {
86  continue;
87  }
88  $storage = $folder->getStorage();
89  $itemsInRootLine = [];
90 
91  // Go back the root folder structure until the root folder
92  $nextFolder = $folder;
93  $isParent = false;
94  do {
95  $itemsInRootLine[$nextFolder->getCombinedIdentifier()] = array_merge(
96  $this->treeProvider->prepareFolderInformation($nextFolder),
97  [
98  'expanded' => $isParent,
99  ]
100  );
101  $isParent = true;
102  try {
103  $nextFolder = $nextFolder->getParentFolder();
105  $nextFolder = null;
106  }
107  } while ($nextFolder instanceof ‪Folder && $nextFolder->‪getIdentifier() !== '/');
108  // Add the storage / sys_filemount itself
109  $storageData = $this->treeProvider->prepareFolderInformation(
110  $storage->getRootLevelFolder(true),
111  $storage->getName()
112  );
113  $storageData = array_merge($storageData, [
114  'depth' => 0,
115  'expanded' => true,
116  ]);
117  $itemsInRootLine[$storage->getUid() . ':/'] = $storageData;
118 
119  $itemsInRootLine = array_reverse($itemsInRootLine);
120  $depth = 0;
121  foreach ($itemsInRootLine as $k => $itm) {
122  $itm['depth'] = $depth++;
123  $items[$k] = $itm;
124  }
125  }
126 
127  ksort($items);
128  $finalItems = [];
129  $items = array_values($items);
130  foreach ($items as $item) {
131  $finalItems[] = $this->‪prepareItemForOutput($item);
132  }
133  return new ‪JsonResponse($finalItems);
134  }
135 
139  protected function ‪prepareItemForOutput(array $item): ‪FileTreeItem
140  {
141  $folder = $item['resource'];
142  $isStorage = $item['recordType'] !== 'sys_file';
143  if ($isStorage && !$folder->getStorage()->isOnline()) {
144  $item['name'] .= ' (' . $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_mod_file.xlf:sys_file_storage.isOffline') . ')';
145  }
146  $icon = $this->iconFactory->getIconForResource($folder, IconSize::SMALL, null, $isStorage ? ['mount-root' => true] : []);
147  $item['icon'] = $icon->getIdentifier();
148  $item['overlayIcon'] = $icon->getOverlayIcon() ? $icon->getOverlayIcon()->getIdentifier() : '';
149 
150  $treeItem = new ‪FileTreeItem(
151  item: new ‪TreeItem(
152  identifier: $item['identifier'],
153  parentIdentifier: (string)($item['parentIdentifier'] ?? ''),
154  recordType: (string)($item['recordType'] ?? ''),
155  name: (string)($item['name'] ?? ''),
156  ‪prefix: (string)($item['prefix'] ?? ''),
157  suffix: (string)($item['suffix'] ?? ''),
158  tooltip: (string)($item['tooltip'] ?? ''),
159  depth: (int)($item['depth'] ?? 0),
160  hasChildren: (bool)($item['hasChildren'] ?? false),
161  expanded: (bool)($item['expanded'] ?? false),
162  loaded: (bool)($item['loaded'] ?? false),
163  icon: $item['icon'],
164  overlayIcon: $item['overlayIcon'],
165  statusInformation: (array)($item['statusInformation'] ?? []),
166  labels: (array)($item['labels'] ?? []),
167  ),
168  pathIdentifier: (string)($item['pathIdentifier'] ?? ''),
169  storage: (int)($item['storage'] ?? 0),
170  resourceType: $isStorage ? 'storage' : 'folder',
171  );
172 
173  return $treeItem;
174  }
175 
177  {
178  return ‪$GLOBALS['BE_USER'];
179  }
180 
182  {
183  return ‪$GLOBALS['LANG'];
184  }
185 }
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\getLanguageService
‪getLanguageService()
Definition: TreeController.php:181
‪TYPO3\CMS\Backend\Dto\Tree\FileTreeItem
Definition: FileTreeItem.php:24
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException
Definition: InsufficientFolderAccessPermissionsException.php:23
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\prepareItemForOutput
‪prepareItemForOutput(array $item)
Definition: TreeController.php:139
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\$resourceFactory
‪ResourceFactory $resourceFactory
Definition: TreeController.php:44
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\$iconFactory
‪IconFactory $iconFactory
Definition: TreeController.php:42
‪TYPO3\CMS\Backend\Tree\FileStorageTreeProvider
Definition: FileStorageTreeProvider.php:39
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\$treeProvider
‪FileStorageTreeProvider $treeProvider
Definition: TreeController.php:43
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController
Definition: TreeController.php:41
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\fetchDataAction
‪fetchDataAction(ServerRequestInterface $request)
Definition: TreeController.php:56
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\filterDataAction
‪filterDataAction(ServerRequestInterface $request)
Definition: TreeController.php:78
‪TYPO3\CMS\Backend\Controller\FileStorage
Definition: TreeController.php:18
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:38
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:28
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Resource\Folder\getIdentifier
‪non empty string getIdentifier()
Definition: Folder.php:150
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Dto\Tree\TreeItem
Definition: TreeItem.php:27
‪TYPO3\CMS\Extbase\Security\prefix
‪@ prefix
Definition: HashScope.php:30
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\__construct
‪__construct(IconFactory $iconFactory=null)
Definition: TreeController.php:46
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\getBackendUser
‪getBackendUser()
Definition: TreeController.php:176