Completed
Push — master ( 64542d...27c7de )
by
unknown
12:47
created

PlaceholderShadowColumnsResolver   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 3
A forNewPlaceholder() 0 5 1
B forTable() 0 31 9
1
<?php
2
3
declare(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
18
namespace TYPO3\CMS\Core\DataHandling;
19
20
use TYPO3\CMS\Core\Exception;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23
/**
24
 * Resolver for placeholder shadow columns to be used in workspace aware environments.
25
 *
26
 * Certain fields need to be "shadowed" - NEW and MOVE placeholder need to have values kept in sync
27
 * that are modified, like the "hidden" field (enable fields, slug fields etc).
28
 *
29
 * This class resolves the columns for a TCA table record that should be kept in sync.
30
 *
31
 * @see \TYPO3\CMS\Core\DataHandling\DataHandler::placeholderShadowing()
32
 */
33
class PlaceholderShadowColumnsResolver
34
{
35
    protected const CONTROL_COLUMNS = [
36
        'languageField',
37
        'transOrigPointerField',
38
        'translationSource',
39
        'type',
40
        'label'
41
    ];
42
43
    protected const FLAG_NONE = 0;
44
    protected const FLAG_APPLY_SYSTEM_COLUMNS = 1;
45
    protected const FLAG_APPLY_SLUG_COLUMNS = 2;
46
47
    /**
48
     * @var string
49
     */
50
    protected $tableName;
51
52
    /**
53
     * @var array
54
     */
55
    protected $tableConfiguration;
56
57
    /**
58
     * @var int|null
59
     */
60
    protected $flags;
61
62
    /**
63
     * @param string $tableName Name of the database table
64
     * @param array $tableConfiguration TCA configuration for $tableName
65
     * @throws Exception
66
     */
67
    public function __construct(string $tableName, array $tableConfiguration)
68
    {
69
        $this->tableName = $tableName;
70
        $this->tableConfiguration = $tableConfiguration;
71
72
        if (empty($this->tableName) || empty($this->tableConfiguration)) {
73
            throw new Exception('No table name and TCA given', 1574174231);
74
        }
75
    }
76
77
    /**
78
     * @param int|null $flags Custom flags to adjust resolving behavior
79
     * @return string[] Placeholder shadow column names
80
     */
81
    public function forNewPlaceholder(int $flags = null): array
82
    {
83
        $flags = $flags ?? self::FLAG_APPLY_SYSTEM_COLUMNS | self::FLAG_APPLY_SLUG_COLUMNS;
84
        $shadowColumnsList = $this->tableConfiguration['ctrl']['shadowColumnsForNewPlaceholders'] ?? '';
85
        return $this->forTable($shadowColumnsList, $flags);
86
    }
87
88
    protected function forTable(string $shadowColumnsList, int $flags = null): array
89
    {
90
        $shadowColumns = explode(',', $shadowColumnsList);
91
        $flags = $flags ?? self::FLAG_NONE;
92
93
        if ($flags & self::FLAG_APPLY_SYSTEM_COLUMNS) {
94
            foreach (self::CONTROL_COLUMNS as $controlColumn) {
95
                if (isset($this->tableConfiguration['ctrl'][$controlColumn])) {
96
                    $shadowColumns[] = $this->tableConfiguration['ctrl'][$controlColumn];
97
                }
98
            }
99
        }
100
        if ($flags & self::FLAG_APPLY_SLUG_COLUMNS) {
101
            $shadowColumns = array_merge(
102
                $shadowColumns,
103
                GeneralUtility::makeInstance(SlugEnricher::class)->resolveSlugFieldNames($this->tableName)
104
            );
105
        }
106
        foreach ($this->tableConfiguration['ctrl']['enablecolumns'] ?? [] as $enableColumn) {
107
            $shadowColumns[] = $enableColumn;
108
        }
109
110
        $shadowColumns = array_filter(
111
            array_map('trim', $shadowColumns),
112
            function (string $shadowColumn) {
113
                return !empty($shadowColumn) && $shadowColumn !== 'uid' && $shadowColumn !== 'pid'
114
                    && isset($this->tableConfiguration['columns'][$shadowColumn]);
115
            }
116
        );
117
        $shadowColumns = array_unique($shadowColumns);
118
        return $shadowColumns;
119
    }
120
}
121