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\FieldWizard;
19
20use TYPO3\CMS\Backend\Form\AbstractNode;
21use TYPO3\CMS\Core\Imaging\Icon;
22use TYPO3\CMS\Core\Imaging\IconFactory;
23use TYPO3\CMS\Core\Localization\LanguageService;
24use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26/**
27 * Render list of tables and link to element browser,
28 * typically used with type=group and internal_type=db.
29 */
30class TableList extends AbstractNode
31{
32    /**
33     * Render table buttons
34     *
35     * @return array
36     */
37    public function render(): array
38    {
39        $languageService = $this->getLanguageService();
40        $result = $this->initializeResultArray();
41
42        $parameterArray = $this->data['parameterArray'];
43        $config = $parameterArray['fieldConf']['config'];
44        $itemName = $parameterArray['itemFormElName'];
45
46        if (empty($config['allowed']) || !is_string($config['allowed'])) {
47            // No handling if the field has no, or funny "allowed" settings.
48            return $result;
49        }
50
51        $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
52        $allowed = GeneralUtility::trimExplode(',', $config['allowed'], true);
53        $allowedTablesHtml = [];
54        foreach ($allowed as $tableName) {
55            if ($tableName === '*') {
56                $label = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.allTables');
57                $allowedTablesHtml[] = '<span>';
58                $allowedTablesHtml[] =  htmlspecialchars($label);
59                $allowedTablesHtml[] = '</span>';
60            } else {
61                $label = $languageService->sL($GLOBALS['TCA'][$tableName]['ctrl']['title']);
62                $icon = $iconFactory->getIconForRecord($tableName, [], Icon::SIZE_SMALL)->render();
63                if ((bool)($config['fieldControl']['elementBrowser']['disabled'] ?? false)) {
64                    $allowedTablesHtml[] = '<span class="tablelist-item-nolink">';
65                    $allowedTablesHtml[] =  $icon;
66                    $allowedTablesHtml[] =  htmlspecialchars($label);
67                    $allowedTablesHtml[] = '</span>';
68                } else {
69                    $allowedTablesHtml[] = '<a href="#" class="btn btn-default t3js-element-browser" data-mode="db" data-params="' . htmlspecialchars($itemName . '|||' . $tableName) . '">';
70                    $allowedTablesHtml[] =  $icon;
71                    $allowedTablesHtml[] =  htmlspecialchars($label);
72                    $allowedTablesHtml[] = '</a>';
73                }
74            }
75        }
76
77        $html = [];
78        $html[] = '<div class="help-block">';
79        $html[] =   implode(LF, $allowedTablesHtml);
80        $html[] = '</div>';
81
82        $result['html'] = implode(LF, $html);
83        return $result;
84    }
85
86    /**
87     * @return LanguageService
88     */
89    protected function getLanguageService()
90    {
91        return $GLOBALS['LANG'];
92    }
93}
94