[BUGFIX] Adjust itemsProcFunc for sys_filemounts
[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 // If working for sys_filemounts table
39 $storageUid = (int)$PA['row']['base'][0];
40 if (!$storageUid) {
41 // If working for sys_file_collection table
42 $storageUid = (int)$PA['row']['storage'];
43 }
44 if ($storageUid > 0) {
45 /** @var $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */
46 $storageRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class);
47 /** @var $storage \TYPO3\CMS\Core\Resource\ResourceStorage */
48 $storage = $storageRepository->findByUid($storageUid);
49 if ($storage === NULL) {
50 /** @var \TYPO3\CMS\Core\Messaging\FlashMessageService $flashMessageService */
51 $flashMessageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class);
52 $queue = $flashMessageService->getMessageQueueByIdentifier();
53 $queue->enqueue(new FlashMessage('Storage #' . $storageUid . ' does not exist. No folder is currently selectable.', '', FlashMessage::ERROR));
54 if (empty($PA['items'])) {
55 $PA['items'][] = array(
56 $PA['row'][$PA['field']],
57 $PA['row'][$PA['field']]
58 );
59 }
60 } elseif ($storage->isBrowsable()) {
61 $rootLevelFolders = array();
62
63 $fileMounts = $storage->getFileMounts();
64 if (!empty($fileMounts)) {
65 foreach ($fileMounts as $fileMountInfo) {
66 $rootLevelFolders[] = $fileMountInfo['folder'];
67 }
68 } else {
69 $rootLevelFolders[] = $storage->getRootLevelFolder();
70 }
71
72 foreach ($rootLevelFolders as $rootLevelFolder) {
73 $folderItems = $this->getSubfoldersForOptionList($rootLevelFolder);
74 foreach ($folderItems as $item) {
75 $PA['items'][] = array(
76 $item->getIdentifier(),
77 $item->getIdentifier()
78 );
79 }
80 }
81 } else {
82 /** @var \TYPO3\CMS\Core\Messaging\FlashMessageService $flashMessageService */
83 $flashMessageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class);
84 $queue = $flashMessageService->getMessageQueueByIdentifier();
85 $queue->enqueue(new FlashMessage('Storage "' . $storage->getName() . '" is not browsable. No folder is currently selectable.', '', FlashMessage::WARNING));
86 if (empty($PA['items'])) {
87 $PA['items'][] = array(
88 $PA['row'][$PA['field']],
89 $PA['row'][$PA['field']]
90 );
91 }
92 }
93 } else {
94 $PA['items'][] = array('', 'Please choose a FAL mount from above first.');
95 }
96 }
97
98 /**
99 * Simple function to make a hierarchical subfolder request into
100 * a "flat" option list
101 *
102 * @param \TYPO3\CMS\Core\Resource\Folder $parentFolder
103 * @param int $level a limiter
104 * @return \TYPO3\CMS\Core\Resource\Folder[]
105 */
106 protected function getSubfoldersForOptionList(\TYPO3\CMS\Core\Resource\Folder $parentFolder, $level = 0) {
107 $level++;
108 // hard break on recursion
109 if ($level > 99) {
110 return array();
111 }
112 $allFolderItems = array($parentFolder);
113 $subFolders = $parentFolder->getSubfolders();
114 foreach ($subFolders as $subFolder) {
115 try {
116 $subFolderItems = $this->getSubfoldersForOptionList($subFolder, $level);
117 } catch(\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderReadPermissionsException $e) {
118 $subFolderItems = array();
119 }
120 $allFolderItems = array_merge($allFolderItems, $subFolderItems);
121 }
122 return $allFolderItems;
123 }
124
125 }