Completed
Push — master ( 57f196...fb64ee )
by
unknown
15:57
created

getTcaTablesWithOverwriteProtectionSupport()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
namespace Aoe\AoeDbSequenzer;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use Aoe\AoeDbSequenzer\Service\OverwriteProtectionService;
29
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
30
31
/**
32
 * @package Aoe\AoeDbSequenzer
33
 */
34
class TcaPostProcessor
35
{
36
    /**
37
     * Add overwrite-protection-field to TCA-fields of DB-tables which support overwrite-protection
38
     *
39
     * @param array $tca
40
     * @return array
41
     */
42
    public function postProcessTca(array $tca)
43
    {
44
        $GLOBALS['TCA'] = $tca;
45
46
        $additionalColumns = [
47
            'tx_aoe_dbsquenzer_protectoverwrite_till' => [
48
                'label' => 'LLL:EXT:aoe_dbsequenzer/Resources/Private/Language/locallang_db.xml:protectoverwrite_till',
49
                'config' => [
50
                    'type' => 'user',
51
                    'userFunc' => 'Aoe\AoeDbSequenzer\Form\OverwriteTillElement->render',
52
                    'eval' => 'datetime'
53
                ]
54
            ],
55
            'tx_aoe_dbsquenzer_protectoverwrite_mode' => [
56
                'label' => 'LLL:EXT:aoe_dbsequenzer/Resources/Private/Language/locallang_db.xml:protected_mode',
57
                'config' => [
58
                    'type' => 'user',
59
                    'userFunc' => 'Aoe\AoeDbSequenzer\Form\OverwriteModeElement->render',
60
                ]
61
            ]
62
        ];
63
64
        $columnNames = [];
65
        $columnNames[] = OverwriteProtectionService::OVERWRITE_PROTECTION_TILL;
66
        $columnNames[] = OverwriteProtectionService::OVERWRITE_PROTECTION_MODE;
67
        $columnNamesStr = ' ' . implode(', ', $columnNames);
68
69
        $newFieldsString = '--palette--;LLL:EXT:aoe_dbsequenzer/Resources/Private/Language/locallang_db.xml:protectoverwrite_headline;tx_aoe_dbsequenzer';
70
        $newFieldsStringRegex = preg_quote($newFieldsString, '/');
71
72
        foreach ($this->getTcaTablesWithOverwriteProtectionSupport() as $table) {
73
            // add columnsConfig at END of TCA-configuration
74
            ExtensionManagementUtility::addTCAcolumns($table, $additionalColumns);
75
76
            ExtensionManagementUtility::addFieldsToPalette(
77
                $table,
78
                'tx_aoe_dbsequenzer',
79
                $columnNamesStr
80
            );
81
82
            ExtensionManagementUtility::addToAllTCAtypes(
83
                $table,
84
                $newFieldsString
85
            );
86
87
            // move columnsConfig from END of TCA-configuration to BEGIN of TCA-configuration
88
            if (is_array($GLOBALS['TCA'][$table]['types'])) {
89
                foreach ($GLOBALS['TCA'][$table]['types'] as &$tableTypeConfig) {
90
                    if (array_key_exists('showitem', $tableTypeConfig) &&
91
                        preg_match('/' . $newFieldsStringRegex . '$/i', $tableTypeConfig['showitem'])) {
92
93
                        $showItems = &$tableTypeConfig['showitem'];
94
95
                        // 1. delete columnsConfig at END of TCA-configuration
96
                        $showItems = preg_replace('/,\s?' . $newFieldsStringRegex . '/i', '', $showItems);
97
98
                        // 2. add columnsConfig at BEGIN of TCA-configuration
99
                        if (preg_match('/^--div--/i', $showItems)) {
100
                            // first entry is an tab
101
                            $firstColumnEntry = substr($showItems, 0, stripos($showItems, ',') + 1);
102
                            $showItems = str_replace($firstColumnEntry, '', $showItems);
103
                            $showItems = $firstColumnEntry . $newFieldsString . ',' . $showItems;
104
                        } else {
105
                            // first entry is no tab
106
                            $showItems = $newFieldsString . ',' . $showItems;
107
                        }
108
                    }
109
                }
110
            }
111
        }
112
113
        $tca = $GLOBALS['TCA'];
114
        return [$tca];
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    private function getTcaTablesWithOverwriteProtectionSupport()
121
    {
122
        $config = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['aoe_dbsequenzer']);
123
        if (isset($config['tables'])) {
124
            return explode(',', $config['tables']);
125
        }
126
        return [];
127
    }
128
}
129