Passed
Push — master ( e0a47f...b7e4d9 )
by
unknown
19:48
created

FormEngineUtility::databaseRowCompatibility()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 19
rs 9.2222
cc 6
nc 5
nop 1
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Backend\Form\Utility;
17
18
use TYPO3\CMS\Backend\Utility\BackendUtility;
19
use TYPO3\CMS\Core\Cache\CacheManager;
20
use TYPO3\CMS\Core\Imaging\Icon;
21
use TYPO3\CMS\Core\Imaging\IconFactory;
22
use TYPO3\CMS\Core\Utility\ArrayUtility;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Core\Utility\PathUtility;
25
26
/**
27
 * This is a static, internal and intermediate helper class for various
28
 * FormEngine related tasks.
29
 *
30
 * This class was introduced to help disentangling FormEngine and
31
 * its sub classes. It MUST NOT be used in other extensions and will
32
 * change or vanish without further notice.
33
 *
34
 * @internal
35
 * @todo: These helpers are target to be dropped if further FormEngine refactoring is done
36
 */
37
class FormEngineUtility
38
{
39
    /**
40
     * Whitelist that allows TCA field configuration to be overridden by TSconfig
41
     *
42
     * @see overrideFieldConf()
43
     * @var array
44
     */
45
    protected static $allowOverrideMatrix = [
46
        'input' => ['size', 'max', 'readOnly'],
47
        'text' => ['cols', 'rows', 'wrap', 'max', 'readOnly'],
48
        'check' => ['cols', 'readOnly'],
49
        'select' => ['size', 'autoSizeMax', 'maxitems', 'minitems', 'readOnly', 'treeConfig'],
50
        'group' => ['size', 'autoSizeMax', 'max_size', 'maxitems', 'minitems', 'readOnly'],
51
        'inline' => ['appearance', 'behaviour', 'foreign_label', 'foreign_selector', 'foreign_unique', 'maxitems', 'minitems', 'size', 'autoSizeMax', 'symmetric_label', 'readOnly'],
52
        'imageManipulation' => ['ratios', 'cropVariants']
53
    ];
54
55
    /**
56
     * Overrides the TCA field configuration by TSconfig settings.
57
     *
58
     * Example TSconfig: TCEform.<table>.<field>.config.appearance.useSortable = 1
59
     * This overrides the setting in $GLOBALS['TCA'][<table>]['columns'][<field>]['config']['appearance']['useSortable'].
60
     *
61
     * @param array $fieldConfig $GLOBALS['TCA'] field configuration
62
     * @param array $TSconfig TSconfig
63
     * @return array Changed TCA field configuration
64
     * @internal
65
     */
66
    public static function overrideFieldConf($fieldConfig, $TSconfig)
67
    {
68
        if (is_array($TSconfig)) {
0 ignored issues
show
introduced by
The condition is_array($TSconfig) is always true.
Loading history...
69
            $TSconfig = GeneralUtility::removeDotsFromTS($TSconfig);
70
            $type = $fieldConfig['type'] ?? '';
71
            if (isset($TSconfig['config']) && is_array($TSconfig['config']) && is_array(static::$allowOverrideMatrix[$type])) {
72
                // Check if the keys in TSconfig['config'] are allowed to override TCA field config:
73
                foreach ($TSconfig['config'] as $key => $_) {
74
                    if (!in_array($key, static::$allowOverrideMatrix[$type], true)) {
75
                        unset($TSconfig['config'][$key]);
76
                    }
77
                }
78
                // Override $GLOBALS['TCA'] field config by remaining TSconfig['config']:
79
                if (!empty($TSconfig['config'])) {
80
                    ArrayUtility::mergeRecursiveWithOverrule($fieldConfig, $TSconfig['config']);
81
                }
82
            }
83
        }
84
        return $fieldConfig;
85
    }
86
87
    /**
88
     * Returns TSconfig for given table and row
89
     *
90
     * @param string $table The table name
91
     * @param array $row The table row - Must at least contain the "uid" value, even if "NEW..." string.
92
     *                   The "pid" field is important as well, negative values will be interpreted as pointing to a record from the same table.
93
     * @param string $field Optionally specify the field name as well. In that case the TSconfig for this field is returned.
94
     * @return mixed The TSconfig values - probably in an array
95
     * @internal
96
     */
97
    public static function getTSconfigForTableRow($table, $row, $field = '')
98
    {
99
        $runtimeCache = GeneralUtility::makeInstance(CacheManager::class)->getCache('runtime');
100
        $cache = $runtimeCache->get('formEngineUtilityTsConfigForTableRow') ?: [];
101
        $cacheIdentifier = $table . ':' . $row['uid'];
102
        if (!isset($cache[$cacheIdentifier])) {
103
            $cache[$cacheIdentifier] = BackendUtility::getTCEFORM_TSconfig($table, $row);
104
            $runtimeCache->set('formEngineUtilityTsConfigForTableRow', $cache);
105
        }
106
        if ($field && isset($cache[$cacheIdentifier][$field])) {
107
            return $cache[$cacheIdentifier][$field];
108
        }
109
        return $cache[$cacheIdentifier];
110
    }
111
112
    /**
113
     * Renders the $icon, supports a filename for skinImg or sprite-icon-name
114
     *
115
     * @param string $icon The icon passed, could be a file-reference or a sprite Icon name
116
     * @param string $alt Alt attribute of the icon returned
117
     * @param string $title Title attribute of the icon return
118
     * @return string A tag representing to show the asked icon
119
     * @internal
120
     */
121
    public static function getIconHtml($icon, $alt = '', $title = '')
122
    {
123
        $icon = (string)$icon;
124
        $absoluteFilePath = GeneralUtility::getFileAbsFileName($icon);
125
        if (!empty($absoluteFilePath) && is_file($absoluteFilePath)) {
126
            return '<img'
127
                . ' src="' . htmlspecialchars(PathUtility::getAbsoluteWebPath($absoluteFilePath)) . '"'
128
                . ' alt="' . htmlspecialchars($alt) . '" '
129
                . ($title ? 'title="' . htmlspecialchars($title) . '"' : '')
130
                . ' />';
131
        }
132
133
        $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
134
        return '<span title="' . htmlspecialchars($title) . '">'
135
            . $iconFactory->getIcon($icon, Icon::SIZE_SMALL)->render()
136
            . '</span>';
137
    }
138
}
139