‪TYPO3CMS  11.5
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;
32 
39 {
43 
45  {
46  $this->iconFactory = ‪$iconFactory ?? GeneralUtility::makeInstance(IconFactory::class);
47  $this->treeProvider = GeneralUtility::makeInstance(FileStorageTreeProvider::class);
48  $this->resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
49  }
50 
57  public function ‪fetchDataAction(ServerRequestInterface $request): ResponseInterface
58  {
59  $parentIdentifier = $request->getQueryParams()['parent'] ?? null;
60  if ($parentIdentifier) {
61  $currentDepth = (int)($request->getQueryParams()['currentDepth'] ?? 1);
62  $parentIdentifier = rawurldecode($parentIdentifier);
63  $folder = $this->resourceFactory->getFolderObjectFromCombinedIdentifier($parentIdentifier);
64  $items = $this->treeProvider->getSubfoldersRecursively($folder, $currentDepth + 1);
65  } else {
66  $items = $this->treeProvider->getRootNodes($this->‪getBackendUser());
67  }
68  $items = array_map(function (array $item) {
69  return $this->‪prepareItemForOutput($item);
70  }, $items);
71  return new ‪JsonResponse($items);
72  }
73 
81  public function ‪filterDataAction(ServerRequestInterface $request): ResponseInterface
82  {
83  $search = $request->getQueryParams()['q'] ?? '';
84  $foundFolders = $this->treeProvider->getFilteredTree($this->‪getBackendUser(), $search);
85 
86  $items = [];
87  foreach ($foundFolders as $folder) {
88  if (!$folder instanceof ‪Folder) {
89  continue;
90  }
91  $storage = $folder->getStorage();
92  $itemsInRootLine = [];
93 
94  // Go back the root folder structure until the root folder
95  $nextFolder = $folder;
96  $isParent = false;
97  do {
98  $itemsInRootLine[$nextFolder->getCombinedIdentifier()] = array_merge(
99  $this->treeProvider->prepareFolderInformation($nextFolder),
100  [
101  'expanded' => $isParent,
102  ]
103  );
104  $isParent = true;
105  try {
106  $nextFolder = $nextFolder->getParentFolder();
108  $nextFolder = null;
109  }
110  } while ($nextFolder instanceof ‪Folder && $nextFolder->‪getIdentifier() !== '/');
111  // Add the storage / sys_filemount itself
112  $storageData = $this->treeProvider->prepareFolderInformation(
113  $storage->getRootLevelFolder(true),
114  $storage->getName()
115  );
116  $storageData = array_merge($storageData, [
117  'depth' => 0,
118  'expanded' => true,
119  'siblingsCount' => 1,
120  'siblingsPosition' => 1,
121  ]);
122  $itemsInRootLine[$storage->getUid() . ':/'] = $storageData;
123 
124  $itemsInRootLine = array_reverse($itemsInRootLine);
125  $depth = 0;
126  foreach ($itemsInRootLine as $k => $itm) {
127  $itm['depth'] = $depth++;
128  $items[$k] = $itm;
129  }
130  }
131 
132  ksort($items);
133  // Make sure siblingsCount and siblingsPosition works
134  $finalItems = [];
135  $items = array_values($items);
136  foreach ($items as $item) {
137  $stateIdentifier = $item['stateIdentifier'];
138  $parentIdentifier = $item['parentIdentifier'];
139  $siblings = array_filter($items, static function ($itemInArray) use ($parentIdentifier) {
140  if ($itemInArray['parentIdentifier'] === $parentIdentifier) {
141  return true;
142  }
143  return false;
144  });
145  $positionFound = false;
146  $siblingsBeforeInSameDepth = array_filter($siblings, static function ($itemInArray) use ($stateIdentifier, &$positionFound): bool {
147  if ($itemInArray['stateIdentifier'] === $stateIdentifier) {
148  $positionFound = true;
149  return false;
150  }
151  return !$positionFound;
152  });
153  $item['siblingsCount'] = count($siblings);
154  $item['siblingsPosition'] = count($siblingsBeforeInSameDepth) + 1;
155  $finalItems[] = $this->‪prepareItemForOutput($item);
156  }
157  // now lets do the siblingsCount
158  return new ‪JsonResponse($finalItems);
159  }
160 
167  protected function ‪prepareItemForOutput(array $item): array
168  {
169  $folder = $item['resource'];
170  $isStorage = $item['itemType'] !== 'sys_file';
171  if ($isStorage && !$folder->getStorage()->isOnline()) {
172  $item['name'] .= ' (' . $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_mod_file.xlf:sys_file_storage.isOffline') . ')';
173  }
174  $icon = $this->iconFactory->getIconForResource($folder, ‪Icon::SIZE_SMALL, null, $isStorage ? ['mount-root' => true] : []);
175  $item['icon'] = $icon->getIdentifier();
176  $item['overlayIcon'] = $icon->getOverlayIcon() ? $icon->getOverlayIcon()->getIdentifier() : '';
177  unset($item['resource']);
178  return $item;
179  }
180 
182  {
183  return ‪$GLOBALS['BE_USER'];
184  }
185 
187  {
188  return ‪$GLOBALS['LANG'];
189  }
190 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\getLanguageService
‪getLanguageService()
Definition: TreeController.php:186
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException
Definition: InsufficientFolderAccessPermissionsException.php:23
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\$resourceFactory
‪ResourceFactory $resourceFactory
Definition: TreeController.php:42
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\$iconFactory
‪IconFactory $iconFactory
Definition: TreeController.php:40
‪TYPO3\CMS\Backend\Tree\FileStorageTreeProvider
Definition: FileStorageTreeProvider.php:39
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\$treeProvider
‪FileStorageTreeProvider $treeProvider
Definition: TreeController.php:41
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\prepareItemForOutput
‪array prepareItemForOutput(array $item)
Definition: TreeController.php:167
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController
Definition: TreeController.php:39
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\filterDataAction
‪ResponseInterface filterDataAction(ServerRequestInterface $request)
Definition: TreeController.php:81
‪TYPO3\CMS\Backend\Controller\FileStorage
Definition: TreeController.php:18
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\fetchDataAction
‪ResponseInterface fetchDataAction(ServerRequestInterface $request)
Definition: TreeController.php:57
‪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
‪string getIdentifier()
Definition: Folder.php:160
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\__construct
‪__construct(IconFactory $iconFactory=null)
Definition: TreeController.php:44
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\getBackendUser
‪getBackendUser()
Definition: TreeController.php:181