1<?php
2declare(strict_types = 1);
3namespace 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
18use TYPO3\CMS\Core\Localization\LanguageService;
19use TYPO3\CMS\Core\Messaging\FlashMessage;
20use TYPO3\CMS\Core\Messaging\FlashMessageService;
21use TYPO3\CMS\Core\Resource\Exception\InvalidPathException;
22use TYPO3\CMS\Core\Resource\ResourceFactory;
23use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25/**
26 * Special type="user" element used in sys_file_storage is_public field
27 *
28 * @internal
29 */
30class 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        $isPublicAsString = $isPublic ? '1' : '0';
88        $fieldInformationResult = $this->renderFieldInformation();
89        $fieldInformationHtml = $fieldInformationResult['html'];
90        $resultArray = $this->mergeChildReturnIntoExistingResult($this->initializeResultArray(), $fieldInformationResult, false);
91
92        $checkboxParameters = $this->checkBoxParams($parameterArray['itemFormElName'], $isPublic, 0, 1, implode('', $parameterArray['fieldChangeFunc']));
93        $checkboxId = $parameterArray['itemFormElID'] . '_1';
94        $html = [];
95        $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
96        $html[] = $fieldInformationHtml;
97        $html[] =   '<div class="form-wizards-wrap">';
98        $html[] =       '<div class="form-wizards-element">';
99        $html[] =           '<div class="checkbox checkbox-type-toggle">';
100        $html[] =               '<input type="checkbox"';
101        $html[] =                   ' class="checkbox-input"';
102        $html[] =                   ' value="1"';
103        $html[] =                   ' data-formengine-input-name="' . htmlspecialchars($parameterArray['itemFormElName'], ENT_QUOTES) . '"';
104        $html[] =                   ' id="' . htmlspecialchars($checkboxId, ENT_QUOTES) . '"';
105        $html[] =                   $checkboxParameters;
106        $html[] =                   $isPublic ? ' checked="checked"' : '';
107        $html[] =               '/>';
108        $html[] =               '<label class="checkbox-label" for="' . htmlspecialchars($checkboxId, ENT_QUOTES) . '">';
109        $html[] =                   '<span class="checkbox-label-text">' . $this->appendValueToLabelInDebugMode('&nbsp;', $isPublicAsString) . '</span>';
110        $html[] =               '</label>';
111        $html[] =               '<input type="hidden"';
112        $html[] =                   ' name="' . htmlspecialchars($parameterArray['itemFormElName'], ENT_QUOTES) . '"';
113        $html[] =                   ' value="' . $isPublicAsString . '"';
114        $html[] =               ' />';
115        $html[] =           '</div>';
116        $html[] =       '</div>';
117        $html[] =   '</div>';
118        $html[] = '</div>';
119        $resultArray['html'] = implode(LF, $html);
120        return $resultArray;
121    }
122
123    /**
124     * @return LanguageService
125     */
126    protected function getLanguageService(): LanguageService
127    {
128        return $GLOBALS['LANG'];
129    }
130}
131