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