[TASK] Switch UserStorageCapabilityService to a renderType
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / UserSysFileStorageIsPublicElement.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Form\Element;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Localization\LanguageService;
19 use TYPO3\CMS\Core\Messaging\FlashMessage;
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 * Special type="user" element used in sys_file_storage is_public field
27 *
28 * @internal
29 */
30 class UserSysFileStorageIsPublicElement extends AbstractFormElement
31 {
32 /**
33 * Default field information enabled for this element.
34 *
35 * @var array
36 */
37 protected $defaultFieldInformation = [
38 'tcaDescription' => [
39 'renderType' => 'tcaDescription',
40 ],
41 ];
42
43 /**
44 * There are some edge cases where "is_public" can never be marked as true in the BE,
45 * for instance for storage located outside the document root or
46 * for storages driven by special driver such as Flickr, ...
47 *
48 * @return array As defined in initializeResultArray() of AbstractNode
49 */
50 public function render(): array
51 {
52 $row = $this->data['databaseRow'];
53 $parameterArray = $this->data['parameterArray'];
54 $isPublic = (bool)$GLOBALS['TCA']['sys_file_storage']['columns']['is_public']['config']['default'];
55
56 if ($this->data['command'] === 'edit') {
57 // Make sure the storage object can be retrieved which is not the case when new storage.
58 $lang = $this->getLanguageService();
59 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
60 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
61 try {
62 $storage = ResourceFactory::getInstance()->getStorageObject((int)$row['uid']);
63 $storageRecord = $storage->getStorageRecord();
64 $isPublic = $storage->isPublic() && $storageRecord['is_public'];
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 } catch (InvalidPathException $e) {
77 $message = GeneralUtility::makeInstance(
78 FlashMessage::class,
79 $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:filestorage.invalidpathexception.message'),
80 $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:filestorage.invalidpathexception.title'),
81 FlashMessage::ERROR
82 );
83 $defaultFlashMessageQueue->enqueue($message);
84 }
85 }
86
87 $fieldInformationResult = $this->renderFieldInformation();
88 $fieldInformationHtml = $fieldInformationResult['html'];
89 $resultArray = $this->mergeChildReturnIntoExistingResult($this->initializeResultArray(), $fieldInformationResult, false);
90
91 $checkboxParameters = $this->checkBoxParams($parameterArray['itemFormElName'], $isPublic, 0, 1, implode('', $parameterArray['fieldChangeFunc']));
92 $checkboxId = $parameterArray['itemFormElID'] . '_1';
93 $html = [];
94 $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
95 $html[] = $fieldInformationHtml;
96 $html[] = '<div class="form-wizards-wrap">';
97 $html[] = '<div class="form-wizards-element">';
98 $html[] = '<div class="checkbox checkbox-type-toggle">';
99 $html[] = '<input type="checkbox"';
100 $html[] = ' class="checkbox-input"';
101 $html[] = ' value="1"';
102 $html[] = ' data-formengine-input-name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
103 $html[] = ' id="' . htmlspecialchars($checkboxId) . '"';
104 $html[] = $checkboxParameters;
105 $html[] = $isPublic ? ' checked="checked"' : '';
106 $html[] = '/>';
107 $html[] = '<label class="checkbox-label" for="' . htmlspecialchars($checkboxId) . '">';
108 $html[] = '<span class="checkbox-label-text">' . $this->appendValueToLabelInDebugMode('&nbsp;', $isPublic ? '1' : '0') . '</span>';
109 $html[] = '</label>';
110 $html[] = '<input type="hidden"';
111 $html[] = ' name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
112 $html[] = ' value="' . htmlspecialchars((string)$isPublic) . '"';
113 $html[] = ' />';
114 $html[] = '</div>';
115 $html[] = '</div>';
116 $html[] = '</div>';
117 $html[] = '</div>';
118 $resultArray['html'] = implode(LF, $html);
119 return $resultArray;
120 }
121
122 /**
123 * @return LanguageService
124 */
125 protected function getLanguageService(): LanguageService
126 {
127 return $GLOBALS['LANG'];
128 }
129 }