Completed
Push — master ( 5eeaf8...9b5d12 )
by
unknown
15:10
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
3
/***************************************************************
4
 *  Copyright notice
5
 *
6
 *  (c) 2017 AOE GmbH <[email protected]>
7
 *
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
28
29
/**
30
 * @package aoe_dbsequenzer
31
 */
32
class Tx_AoeDbsequenzer_TcaPostProcessor
33
{
34
    /**
35
     * Add overwrite-protection-field to TCA-fields of DB-tables which support overwrite-protection
36
     *
37
     * @param array $tca
38
     * @return array
39
     */
40
    public function postProcessTca(array $tca)
41
    {
42
        $GLOBALS['TCA'] = $tca;
43
44
        $columnConfig = [
45
            'tx_aoe_dbsquenzer_protectoverwrite_till' => [
46
                'label' => 'LLL:EXT:aoe_dbsequenzer/Resources/Private/Language/locallang_db.xml:protectoverwrite_till',
47
                'config' => [
48
                    'type' => 'user',
49
                    'userFunc' => 'Tx_AoeDbsequenzer_OverwriteProtectionService->renderInput',
50
                    'eval' => 'datetime'
51
                ]
52
            ]
53
        ];
54
        $columnName = 'tx_aoe_dbsquenzer_protectoverwrite_till';
55
56
        foreach ($this->getTcaTablesWithOverwriteProtectionSupport() as $table) {
57
            // add columnsConfig at END of TCA-configuration
58
            ExtensionManagementUtility::addTCAcolumns($table, $columnConfig);
59
            ExtensionManagementUtility::addToAllTCAtypes($table, $columnName);
60
61
            // move columnsConfig from END of TCA-configuration to BEGIN of TCA-configuration
62
            if(is_array($GLOBALS['TCA'][$table]['types'])) {
63
                foreach($GLOBALS['TCA'][$table]['types'] as &$tableTypeConfig) {
64
                    if(array_key_exists('showitem', $tableTypeConfig) && preg_match('/'.$columnName.'$/i', $tableTypeConfig['showitem'])) {
65
                        $showItems = &$tableTypeConfig['showitem'];
66
67
                        // 1. delete columnsConfig at END of TCA-configuration
68
                        $showItems = preg_replace('/,\s?'.$columnName.'/i', '', $showItems);
69
70
                        // 2. add columnsConfig at BEGIN of TCA-configuration
71
                        if(preg_match('/^--div--/i', $showItems)) {
72
                            // first entry is an tab
73
                            $firstColumnEntry = substr($showItems, 0, stripos($showItems, ',') + 1);
74
                            $showItems = str_replace($firstColumnEntry, '', $showItems);
75
                            $showItems = $firstColumnEntry . $columnName . ',' . $showItems;
76
                        } else {
77
                            // first entry is no tab
78
                            $showItems = $columnName . ',' . $showItems;
79
                        }
80
                    }
81
                }
82
            }
83
        }
84
85
        $tca = $GLOBALS['TCA'];
86
        return [$tca];
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    private function getTcaTablesWithOverwriteProtectionSupport()
93
    {
94
        $config = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['aoe_dbsequenzer']);
95
        if (isset($config['tables'])) {
96
            return explode(',', $config['tables']);
97
        }
98
        return [];
99
    }
100
}
101