1<?php
2
3declare(strict_types=1);
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
18namespace TYPO3\CMS\Backend\Form\Element;
19
20use TYPO3\CMS\Core\Localization\LanguageService;
21use TYPO3\CMS\Core\Messaging\FlashMessage;
22use TYPO3\CMS\Core\Messaging\FlashMessageService;
23use TYPO3\CMS\Core\Resource\Exception\InvalidPathException;
24use TYPO3\CMS\Core\Resource\StorageRepository;
25use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27/**
28 * Special type="user" element used in sys_file_storage is_public field
29 *
30 * @internal
31 */
32class UserSysFileStorageIsPublicElement extends AbstractFormElement
33{
34    /**
35     * Default field information enabled for this element.
36     *
37     * @var array
38     */
39    protected $defaultFieldInformation = [
40        'tcaDescription' => [
41            'renderType' => 'tcaDescription',
42        ],
43    ];
44
45    /**
46     * There are some edge cases where "is_public" can never be marked as true in the BE,
47     * for instance for storage located outside the document root or
48     * for storages driven by special driver such as Flickr, ...
49     *
50     * @return array As defined in initializeResultArray() of AbstractNode
51     */
52    public function render(): array
53    {
54        $row = $this->data['databaseRow'];
55        $parameterArray = $this->data['parameterArray'];
56        $isPublic = (bool)$GLOBALS['TCA']['sys_file_storage']['columns']['is_public']['config']['default'];
57
58        if ($this->data['command'] === 'edit') {
59            // Make sure the storage object can be retrieved which is not the case when new storage.
60            $lang = $this->getLanguageService();
61            $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
62            $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
63            try {
64                $storage = GeneralUtility::makeInstance(StorageRepository::class)->findByUid((int)$row['uid']);
65                $storageRecord = $storage->getStorageRecord();
66                $isPublic = $storage->isPublic() && $storageRecord['is_public'];
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            } catch (InvalidPathException $e) {
79                $message = GeneralUtility::makeInstance(
80                    FlashMessage::class,
81                    $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:filestorage.invalidpathexception.message'),
82                    $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:filestorage.invalidpathexception.title'),
83                    FlashMessage::ERROR
84                );
85                $defaultFlashMessageQueue->enqueue($message);
86            }
87        }
88
89        $isPublicAsString = $isPublic ? '1' : '0';
90        $fieldInformationResult = $this->renderFieldInformation();
91        $fieldInformationHtml = $fieldInformationResult['html'];
92        $resultArray = $this->mergeChildReturnIntoExistingResult($this->initializeResultArray(), $fieldInformationResult, false);
93
94        $checkboxParameters = $this->checkBoxParams(
95            $parameterArray['itemFormElName'],
96            $isPublic ? 1 : 0,
97            0,
98            1,
99            $parameterArray['fieldChangeFunc'] ?? []
100        );
101        $checkboxId = $parameterArray['itemFormElID'] . '_1';
102        $html = [];
103        $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
104        $html[] = $fieldInformationHtml;
105        $html[] =   '<div class="form-wizards-wrap">';
106        $html[] =       '<div class="form-wizards-element">';
107        $html[] =           '<div class="form-check form-switch">';
108        $html[] =               '<input type="checkbox"';
109        $html[] =                   ' class="form-check-input"';
110        $html[] =                   ' value="1"';
111        $html[] =                   ' data-formengine-input-name="' . htmlspecialchars($parameterArray['itemFormElName'], ENT_QUOTES) . '"';
112        $html[] =                   ' id="' . htmlspecialchars($checkboxId, ENT_QUOTES) . '"';
113        $html[] =                   $checkboxParameters;
114        $html[] =                   $isPublic ? ' checked="checked"' : '';
115        $html[] =               '/>';
116        $html[] =               '<label class="form-check-label" for="' . htmlspecialchars($checkboxId, ENT_QUOTES) . '">';
117        $html[] =                   '<span class="form-check-label-text">' . $this->appendValueToLabelInDebugMode('&nbsp;', $isPublicAsString) . '</span>';
118        $html[] =               '</label>';
119        $html[] =               '<input type="hidden"';
120        $html[] =                   ' name="' . htmlspecialchars($parameterArray['itemFormElName'], ENT_QUOTES) . '"';
121        $html[] =                   ' value="' . $isPublicAsString . '"';
122        $html[] =               ' />';
123        $html[] =           '</div>';
124        $html[] =       '</div>';
125        $html[] =   '</div>';
126        $html[] = '</div>';
127        $resultArray['html'] = implode(LF, $html);
128        return $resultArray;
129    }
130
131    /**
132     * @return LanguageService
133     */
134    protected function getLanguageService(): LanguageService
135    {
136        return $GLOBALS['LANG'];
137    }
138}
139