48e0d4aad6aeb8dbe4e2f3ed300ba60988774124
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Service / UserStorageCapabilityService.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\Imaging\Icon;
18 use TYPO3\CMS\Core\Imaging\IconFactory;
19 use TYPO3\CMS\Core\Localization\LanguageService;
20 use TYPO3\CMS\Core\Messaging\FlashMessage;
21 use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
22 use TYPO3\CMS\Core\Messaging\FlashMessageService;
23 use TYPO3\CMS\Core\Resource\Exception\InvalidPathException;
24 use TYPO3\CMS\Core\Resource\ResourceFactory;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27 /**
28 * Utility class to render capabilities of the storage.
29 */
30 class UserStorageCapabilityService
31 {
32 /**
33 * UserFunc function for rendering field "is_public".
34 * There are some edge cases where "is_public" can never be marked as true in the BE,
35 * for instance for storage located outside the document root or
36 * for storages driven by special driver such as Flickr, ...
37 *
38 * @param array $propertyArray the array with additional configuration options.
39 * @return string
40 */
41 public function renderIsPublic(array $propertyArray)
42 {
43 $isPublic = $GLOBALS['TCA']['sys_file_storage']['columns']['is_public']['config']['default'];
44 $fileRecord = $propertyArray['row'];
45
46 // Makes sure the storage object can be retrieved which is not the case when new storage.
47 if ((int)$propertyArray['row']['uid'] > 0) {
48 /** @var LanguageService $lang */
49 $lang = $GLOBALS['LANG'];
50 /** @var $flashMessageService FlashMessageService */
51 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
52 /** @var $defaultFlashMessageQueue FlashMessageQueue */
53 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
54 try {
55 $storage = ResourceFactory::getInstance()->getStorageObject($fileRecord['uid']);
56 $storageRecord = $storage->getStorageRecord();
57 $isPublic = $storage->isPublic() && $storageRecord['is_public'];
58 } catch (InvalidPathException $e) {
59 $message = GeneralUtility::makeInstance(
60 FlashMessage::class,
61 $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:filestorage.invalidpathexception.message'),
62 $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:filestorage.invalidpathexception.title'),
63 FlashMessage::ERROR
64 );
65 $defaultFlashMessageQueue->enqueue($message);
66 }
67
68 // Display a warning to the BE User in case settings is not inline with storage capability.
69 if ($storageRecord['is_public'] && !$storage->isPublic()) {
70 $message = GeneralUtility::makeInstance(
71 FlashMessage::class,
72 $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:warning.message.storage_is_no_public'),
73 $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:warning.header.storage_is_no_public'),
74 FlashMessage::WARNING
75 );
76 $defaultFlashMessageQueue->enqueue($message);
77 }
78 }
79
80 return $this->renderFileInformationContent($fileRecord, $isPublic);
81 }
82
83 /**
84 * Renders a HTML block containing the checkbox for field "is_public".
85 *
86 * @param array $fileRecord
87 * @param bool $isPublic
88 * @return string
89 */
90 protected function renderFileInformationContent(array $fileRecord, $isPublic)
91 {
92 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
93 $iconChecked = $iconFactory->getIcon('actions-check', Icon::SIZE_SMALL)->render('inline');
94 $iconUnchecked = $iconFactory->getIcon('empty-empty', Icon::SIZE_SMALL)->render('inline');
95
96 $template = '
97 <div class="checkbox checkbox-type-icon-toggle">
98 <input type="checkbox" id="filestorage-ispublic" onclick="document.editform[\'data[sys_file_storage][{uid}][is_public]\'].value=this.checked?(document.editform[\'data[sys_file_storage][{uid}][is_public]\'].value|1):(document.editform[\'data[sys_file_storage][{uid}][is_public]\'].value&0);TBE_EDITOR.fieldChanged(\'sys_file_storage\',\'{uid}\',\'is_public\',\'data[sys_file_storage][{uid}][is_public]\');" class="checkbox-input" value="1" name="data[sys_file_storage][{uid}][is_public]_0" %s />
99 <label class="checkbox-label" for="filestorage-ispublic">
100 <span class="checkbox-label-icon">
101 <span class="checkbox-label-icon-checked">' . $iconChecked . '</span>
102 <span class="checkbox-label-icon-unchecked">' . $iconUnchecked . '</span>
103 </span>
104 <span class="checkbox-label-text">&nbsp;</span>
105 </label>
106 <input type="hidden" name="data[sys_file_storage][{uid}][is_public]" value="1">
107 </div>
108 ';
109
110 $content = sprintf(
111 $template,
112 $isPublic ? 'checked="checked"' : ''
113 );
114
115 return str_replace('{uid}', $fileRecord['uid'], $content);
116 }
117 }