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