‪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;
26 use TYPO3\CMS\Core\Imaging\IconSize;
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 
54  public function ‪fetchDataAction(ServerRequestInterface $request): ResponseInterface
55  {
56  $parentIdentifier = $request->getQueryParams()['parent'] ?? null;
57  if ($parentIdentifier) {
58  $currentDepth = (int)($request->getQueryParams()['currentDepth'] ?? 1);
59  $parentIdentifier = rawurldecode($parentIdentifier);
60  $folder = $this->resourceFactory->getFolderObjectFromCombinedIdentifier($parentIdentifier);
61  $items = $this->treeProvider->getSubfoldersRecursively($folder, $currentDepth + 1);
62  } else {
63  $items = $this->treeProvider->getRootNodes($this->‪getBackendUser());
64  }
65  $items = array_map(function (array $item): array {
66  return $this->‪prepareItemForOutput($item);
67  }, $items);
68  return new ‪JsonResponse($items);
69  }
70 
76  public function ‪filterDataAction(ServerRequestInterface $request): ResponseInterface
77  {
78  $search = $request->getQueryParams()['q'] ?? '';
79  $foundFolders = $this->treeProvider->getFilteredTree($this->‪getBackendUser(), $search);
80 
81  $items = [];
82  foreach ($foundFolders as $folder) {
83  if (!$folder instanceof ‪Folder) {
84  continue;
85  }
86  $storage = $folder->getStorage();
87  $itemsInRootLine = [];
88 
89  // Go back the root folder structure until the root folder
90  $nextFolder = $folder;
91  $isParent = false;
92  do {
93  $itemsInRootLine[$nextFolder->getCombinedIdentifier()] = array_merge(
94  $this->treeProvider->prepareFolderInformation($nextFolder),
95  [
96  'expanded' => $isParent,
97  ]
98  );
99  $isParent = true;
100  try {
101  $nextFolder = $nextFolder->getParentFolder();
103  $nextFolder = null;
104  }
105  } while ($nextFolder instanceof ‪Folder && $nextFolder->‪getIdentifier() !== '/');
106  // Add the storage / sys_filemount itself
107  $storageData = $this->treeProvider->prepareFolderInformation(
108  $storage->getRootLevelFolder(true),
109  $storage->getName()
110  );
111  $storageData = array_merge($storageData, [
112  'depth' => 0,
113  'expanded' => true,
114  'siblingsCount' => 1,
115  'siblingsPosition' => 1,
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  // Make sure siblingsCount and siblingsPosition works
129  $finalItems = [];
130  $items = array_values($items);
131  foreach ($items as $item) {
132  $stateIdentifier = $item['stateIdentifier'];
133  $parentIdentifier = $item['parentIdentifier'];
134  $siblings = array_filter($items, static function (array $itemInArray) use ($parentIdentifier): bool {
135  if ($itemInArray['parentIdentifier'] === $parentIdentifier) {
136  return true;
137  }
138  return false;
139  });
140  $positionFound = false;
141  $siblingsBeforeInSameDepth = array_filter($siblings, static function (array $itemInArray) use ($stateIdentifier, &$positionFound): bool {
142  if ($itemInArray['stateIdentifier'] === $stateIdentifier) {
143  $positionFound = true;
144  return false;
145  }
146  return !$positionFound;
147  });
148  $item['siblingsCount'] = count($siblings);
149  $item['siblingsPosition'] = count($siblingsBeforeInSameDepth) + 1;
150  $finalItems[] = $this->‪prepareItemForOutput($item);
151  }
152  // now lets do the siblingsCount
153  return new ‪JsonResponse($finalItems);
154  }
155 
159  protected function ‪prepareItemForOutput(array $item): array
160  {
161  $folder = $item['resource'];
162  $isStorage = $item['itemType'] !== 'sys_file';
163  if ($isStorage && !$folder->getStorage()->isOnline()) {
164  $item['name'] .= ' (' . $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_mod_file.xlf:sys_file_storage.isOffline') . ')';
165  }
166  $icon = $this->iconFactory->getIconForResource($folder, IconSize::SMALL, null, $isStorage ? ['mount-root' => true] : []);
167  $item['icon'] = $icon->getIdentifier();
168  $item['overlayIcon'] = $icon->getOverlayIcon() ? $icon->getOverlayIcon()->getIdentifier() : '';
169  unset($item['resource']);
170  return $item;
171  }
172 
174  {
175  return ‪$GLOBALS['BE_USER'];
176  }
177 
179  {
180  return ‪$GLOBALS['LANG'];
181  }
182 }
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\getLanguageService
‪getLanguageService()
Definition: TreeController.php:178
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException
Definition: InsufficientFolderAccessPermissionsException.php:24
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\prepareItemForOutput
‪prepareItemForOutput(array $item)
Definition: TreeController.php:159
‪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
Definition: TreeController.php:39
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\fetchDataAction
‪fetchDataAction(ServerRequestInterface $request)
Definition: TreeController.php:54
‪TYPO3\CMS\Backend\Controller\FileStorage\TreeController\filterDataAction
‪filterDataAction(ServerRequestInterface $request)
Definition: TreeController.php:76
‪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:64
‪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:149
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪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:173