[BUGFIX] Respect all filemounts in file collection
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Service / UserFileMountService.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Benjamin Mack <benni@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 use TYPO3\CMS\Core\Messaging\FlashMessage;
30 use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32 /**
33 * Service class for implementing the user filemounts,
34 * used for BE_USER (\TYPO3\CMS\Core\Authentication\BackendUserAuthentication)
35 * and TCEforms hooks
36 *
37 * Note: This is now also used by sys_file_category table (fieldname "folder")!
38 *
39 * @author Benjamin Mack <benni@typo3.org>
40 */
41 class UserFileMountService {
42
43 /**
44 * User function for sys_filemounts (the userfilemounts)
45 * to render a dropdown for selecting a folder
46 * of a selected mount
47 *
48 * @param array $PA the array with additional configuration options.
49 * @param \TYPO3\CMS\Backend\Form\FormEngine $tceformsObj Parent object
50 * @return string The HTML code for the TCEform field
51 */
52 public function renderTceformsSelectDropdown(&$PA, &$tceformsObj) {
53 // If working for sys_filemounts table
54 $storageUid = (int)$PA['row']['base'];
55 if (!$storageUid) {
56 // If working for sys_file_collection table
57 $storageUid = (int)$PA['row']['storage'];
58 }
59 if ($storageUid > 0) {
60 /** @var $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */
61 $storageRepository = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
62 /** @var $storage \TYPO3\CMS\Core\Resource\ResourceStorage */
63 $storage = $storageRepository->findByUid($storageUid);
64 if ($storage->isBrowsable()) {
65 $rootLevelFolders = array();
66
67 $fileMounts = $storage->getFileMounts();
68 if (!empty($fileMounts)) {
69 foreach ($fileMounts as $fileMountInfo) {
70 $rootLevelFolders[] = $fileMountInfo['folder'];
71 }
72 } else {
73 $rootLevelFolders[] = $storage->getRootLevelFolder();
74 }
75
76 foreach ($rootLevelFolders as $rootLevelFolder) {
77 $folderItems = $this->getSubfoldersForOptionList($rootLevelFolder);
78 foreach ($folderItems as $item) {
79 $PA['items'][] = array(
80 $item->getIdentifier(),
81 $item->getIdentifier()
82 );
83 }
84 }
85 } else {
86 /** @var \TYPO3\CMS\Core\Messaging\FlashMessageService $flashMessageService */
87 $flashMessageService = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessageService');
88 $queue = $flashMessageService->getMessageQueueByIdentifier();
89 $queue->enqueue(new FlashMessage('Storage "' . $storage->getName() . '" is not browsable. No folder is currently selectable.', '', FlashMessage::WARNING));
90 if (!count($PA['items'])) {
91 $PA['items'][] = array(
92 $PA['row'][$PA['field']],
93 $PA['row'][$PA['field']]
94 );
95 }
96 }
97 } else {
98 $PA['items'][] = array('', 'Please choose a FAL mount from above first.');
99 }
100 }
101
102 /**
103 * Simple function to make a hierarchical subfolder request into
104 * a "flat" option list
105 *
106 * @param \TYPO3\CMS\Core\Resource\Folder $parentFolder
107 * @param integer $level a limiter
108 * @return \TYPO3\CMS\Core\Resource\Folder[]
109 */
110 protected function getSubfoldersForOptionList(\TYPO3\CMS\Core\Resource\Folder $parentFolder, $level = 0) {
111 $level++;
112 // hard break on recursion
113 if ($level > 99) {
114 return array();
115 }
116 $allFolderItems = array($parentFolder);
117 $subFolders = $parentFolder->getSubfolders();
118 foreach ($subFolders as $subFolder) {
119 $subFolderItems = $this->getSubfoldersForOptionList($subFolder, $level);
120 $allFolderItems = array_merge($allFolderItems, $subFolderItems);
121 }
122 return $allFolderItems;
123 }
124
125 }