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\ResourceFactory;
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(ResourceFactory::class)->getStorageObject((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($parameterArray['itemFormElName'], $isPublic ? 1 : 0, 0, 1, implode('', $parameterArray['fieldChangeFunc']));
95        $checkboxId = $parameterArray['itemFormElID'] . '_1';
96        $html = [];
97        $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
98        $html[] = $fieldInformationHtml;
99        $html[] =   '<div class="form-wizards-wrap">';
100        $html[] =       '<div class="form-wizards-element">';
101        $html[] =           '<div class="checkbox checkbox-type-toggle">';
102        $html[] =               '<input type="checkbox"';
103        $html[] =                   ' class="checkbox-input"';
104        $html[] =                   ' value="1"';
105        $html[] =                   ' data-formengine-input-name="' . htmlspecialchars($parameterArray['itemFormElName'], ENT_QUOTES) . '"';
106        $html[] =                   ' id="' . htmlspecialchars($checkboxId, ENT_QUOTES) . '"';
107        $html[] =                   $checkboxParameters;
108        $html[] =                   $isPublic ? ' checked="checked"' : '';
109        $html[] =               '/>';
110        $html[] =               '<label class="checkbox-label" for="' . htmlspecialchars($checkboxId, ENT_QUOTES) . '">';
111        $html[] =                   '<span class="checkbox-label-text">' . $this->appendValueToLabelInDebugMode('&nbsp;', $isPublicAsString) . '</span>';
112        $html[] =               '</label>';
113        $html[] =               '<input type="hidden"';
114        $html[] =                   ' name="' . htmlspecialchars($parameterArray['itemFormElName'], ENT_QUOTES) . '"';
115        $html[] =                   ' value="' . $isPublicAsString . '"';
116        $html[] =               ' />';
117        $html[] =           '</div>';
118        $html[] =       '</div>';
119        $html[] =   '</div>';
120        $html[] = '</div>';
121        $resultArray['html'] = implode(LF, $html);
122        return $resultArray;
123    }
124
125    /**
126     * @return LanguageService
127     */
128    protected function getLanguageService(): LanguageService
129    {
130        return $GLOBALS['LANG'];
131    }
132}
133