224745e8424ce13176f6fd9f6921d26707b36a7b
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Service / UserFileMountService.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Service;
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
17 use TYPO3\CMS\Core\Messaging\FlashMessage;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Service class for implementing the user filemounts,
22 * used for BE_USER (\TYPO3\CMS\Core\Authentication\BackendUserAuthentication)
23 * and TCEforms hooks
24 *
25 * Note: This is now also used by sys_file_category table (fieldname "folder")!
26 */
27 class UserFileMountService
28 {
29 /**
30 * User function for sys_filemounts (the userfilemounts)
31 * to render a dropdown for selecting a folder
32 * of a selected mount
33 *
34 * @param array $PA the array with additional configuration options.
35 * @return string The HTML code for the TCEform field
36 */
37 public function renderTceformsSelectDropdown(&$PA)
38 {
39 // If working for sys_filemounts table
40 $storageUid = (int)$PA['row']['base'][0];
41 if (!$storageUid) {
42 // If working for sys_file_collection table
43 $storageUid = (int)$PA['row']['storage'][0];
44 }
45 if ($storageUid > 0) {
46 /** @var $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */
47 $storageRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class);
48 /** @var $storage \TYPO3\CMS\Core\Resource\ResourceStorage */
49 $storage = $storageRepository->findByUid($storageUid);
50 if ($storage === null) {
51 /** @var \TYPO3\CMS\Core\Messaging\FlashMessageService $flashMessageService */
52 $flashMessageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class);
53 $queue = $flashMessageService->getMessageQueueByIdentifier();
54 $queue->enqueue(new FlashMessage('Storage #' . $storageUid . ' does not exist. No folder is currently selectable.', '', FlashMessage::ERROR));
55 if (empty($PA['items'])) {
56 $PA['items'][] = [
57 $PA['row'][$PA['field']],
58 $PA['row'][$PA['field']]
59 ];
60 }
61 } elseif ($storage->isBrowsable()) {
62 $rootLevelFolders = [];
63
64 $fileMounts = $storage->getFileMounts();
65 if (!empty($fileMounts)) {
66 foreach ($fileMounts as $fileMountInfo) {
67 $rootLevelFolders[] = $fileMountInfo['folder'];
68 }
69 } else {
70 $rootLevelFolders[] = $storage->getRootLevelFolder();
71 }
72
73 foreach ($rootLevelFolders as $rootLevelFolder) {
74 $folderItems = $this->getSubfoldersForOptionList($rootLevelFolder);
75 foreach ($folderItems as $item) {
76 $PA['items'][] = [
77 $item->getIdentifier(),
78 $item->getIdentifier()
79 ];
80 }
81 }
82 } else {
83 /** @var \TYPO3\CMS\Core\Messaging\FlashMessageService $flashMessageService */
84 $flashMessageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class);
85 $queue = $flashMessageService->getMessageQueueByIdentifier();
86 $queue->enqueue(new FlashMessage('Storage "' . $storage->getName() . '" is not browsable. No folder is currently selectable.', '', FlashMessage::WARNING));
87 if (empty($PA['items'])) {
88 $PA['items'][] = [
89 $PA['row'][$PA['field']],
90 $PA['row'][$PA['field']]
91 ];
92 }
93 }
94 } else {
95 $PA['items'][] = ['', 'Please choose a FAL mount from above first.'];
96 }
97 }
98
99 /**
100 * Simple function to make a hierarchical subfolder request into
101 * a "flat" option list
102 *
103 * @param \TYPO3\CMS\Core\Resource\Folder $parentFolder
104 * @param int $level a limiter
105 * @return \TYPO3\CMS\Core\Resource\Folder[]
106 */
107 protected function getSubfoldersForOptionList(\TYPO3\CMS\Core\Resource\Folder $parentFolder, $level = 0)
108 {
109 $level++;
110 // hard break on recursion
111 if ($level > 99) {
112 return [];
113 }
114 $allFolderItems = [$parentFolder];
115 $subFolders = $parentFolder->getSubfolders();
116 foreach ($subFolders as $subFolder) {
117 try {
118 $subFolderItems = $this->getSubfoldersForOptionList($subFolder, $level);
119 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderReadPermissionsException $e) {
120 $subFolderItems = [];
121 }
122 $allFolderItems = array_merge($allFolderItems, $subFolderItems);
123 }
124 return $allFolderItems;
125 }
126 }