Completed
Push — master ( 6f5567...ee2824 )
by Tim
02:35
created

prepareRecordForDatabase()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 8.8333
c 0
b 0
f 0
cc 7
nc 5
nop 1
1
<?php
2
3
/**
4
 * Helper class for the IndexService
5
 * Prepare the index.
6
 */
7
declare(strict_types=1);
8
9
namespace HDNET\Calendarize\Service;
10
11
use HDNET\Calendarize\Register;
12
use HDNET\Calendarize\Utility\HelperUtility;
13
use TYPO3\CMS\Backend\Utility\BackendUtility;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
16
/**
17
 * Helper class for the IndexService
18
 * Prepare the index.
19
 */
20
class IndexPreparationService
21
{
22
    /**
23
     * Build the index for one element.
24
     *
25
     * @param string $configurationKey
26
     * @param string $tableName
27
     * @param int    $uid
28
     *
29
     * @return array
30
     */
31
    public function prepareIndex($configurationKey, $tableName, $uid)
32
    {
33
        $rawRecord = BackendUtility::getRecord($tableName, $uid);
34
        if (!$rawRecord) {
35
            return [];
36
        }
37
38
        $register = Register::getRegister();
39
        $fieldName = isset($register[$configurationKey]['fieldName']) ? $register[$configurationKey]['fieldName'] : 'calendarize';
40
        $configurations = GeneralUtility::intExplode(',', $rawRecord[$fieldName], true);
41
42
        $transPointer = $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'] ?? false; // e.g. l10n_parent
43
        if ($transPointer && (int)$rawRecord[$transPointer] > 0) {
44
            $rawOriginalRecord = BackendUtility::getRecord($tableName, (int)$rawRecord[$transPointer]);
45
            $configurations = GeneralUtility::intExplode(',', $rawOriginalRecord[$fieldName], true);
46
        }
47
48
        $neededItems = [];
49
        if ($configurations) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $configurations of type integer[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
50
            $timeTableService = GeneralUtility::makeInstance(TimeTableService::class);
51
            $neededItems = $timeTableService->getTimeTablesByConfigurationIds($configurations);
52
            foreach ($neededItems as $key => $record) {
53
                $record['foreign_table'] = $tableName;
54
                $record['foreign_uid'] = $uid;
55
                $record['unique_register_key'] = $configurationKey;
56
57
                $this->prepareRecordForDatabase($record);
58
                $neededItems[$key] = $record;
59
            }
60
        }
61
62
        $this->addEnableFieldInformation($neededItems, $tableName, $rawRecord);
63
        $this->addCtrlFieldInformation($neededItems, $tableName, $rawRecord);
64
        $this->addLanguageInformation($neededItems, $tableName, $rawRecord);
65
66
        return $neededItems;
67
    }
68
69
    /**
70
     * Add the language information.
71
     *
72
     * @param array  $neededItems
73
     * @param string $tableName
74
     * @param array  $record
75
     */
76
    protected function addLanguageInformation(array &$neededItems, $tableName, array $record)
77
    {
78
        $languageField = $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] ?? false; // e.g. sys_language_uid
79
        $transPointer = $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'] ?? false; // e.g. l10n_parent
80
81
        if ($transPointer && (int)$record[$transPointer] > 0) {
82
            foreach ($neededItems as $key => $value) {
83
                $originalRecord = BackendUtility::getRecord($value['foreign_table'], $value['foreign_uid']);
84
85
                $searchFor = $value;
86
                $searchFor['foreign_uid'] = (int)$originalRecord[$transPointer];
87
88
                $db = HelperUtility::getDatabaseConnection(IndexerService::TABLE_NAME);
89
                $q = $db->createQueryBuilder();
90
                $where = [];
91
                foreach ($searchFor as $field => $val) {
92
                    if (\is_string($val)) {
93
                        $where[] = $q->expr()->eq($field, $q->quote($val));
94
                    } else {
95
                        $where[] = $q->expr()->eq($field, (int)$val);
96
                    }
97
                }
98
99
                $result = $q->select('uid')->from(IndexerService::TABLE_NAME)->andWhere(...$where)->execute()->fetch();
100
                if (isset($result['uid'])) {
101
                    $neededItems[$key]['l10n_parent'] = (int)$result['uid'];
102
                }
103
            }
104
        }
105
106
        if ($languageField && 0 !== (int)$record[$languageField]) {
107
            $language = (int)$record[$languageField];
108
            foreach (array_keys($neededItems) as $key) {
109
                $neededItems[$key]['sys_language_uid'] = $language;
110
            }
111
        }
112
    }
113
114
    /**
115
     * Add the enable field information.
116
     *
117
     * @param array  $neededItems
118
     * @param string $tableName
119
     * @param array  $record
120
     */
121
    protected function addEnableFieldInformation(array &$neededItems, $tableName, array $record)
122
    {
123
        $enableFields = $GLOBALS['TCA'][$tableName]['ctrl']['enablecolumns'] ?? [];
124
        if (!$enableFields) {
125
            return;
126
        }
127
128
        $addFields = [];
129
        if (isset($enableFields['disabled'])) {
130
            $addFields['hidden'] = (int)$record[$enableFields['disabled']];
131
        }
132
        if (isset($enableFields['starttime'])) {
133
            $addFields['starttime'] = (int)$record[$enableFields['starttime']];
134
        }
135
        if (isset($enableFields['endtime'])) {
136
            $addFields['endtime'] = (int)$record[$enableFields['endtime']];
137
        }
138
        if (isset($enableFields['fe_group'])) {
139
            $addFields['fe_group'] = (string)$record[$enableFields['fe_group']];
140
        }
141
142
        foreach ($neededItems as $key => $value) {
143
            $neededItems[$key] = array_merge($value, $addFields);
144
        }
145
    }
146
147
    /**
148
     * Add the ctrl field information.
149
     *
150
     * @param array  $neededItems
151
     * @param string $tableName
152
     * @param array  $record
153
     */
154
    protected function addCtrlFieldInformation(array &$neededItems, $tableName, array $record)
155
    {
156
        $ctrl = $GLOBALS['TCA'][$tableName]['ctrl'] ?? [];
157
        if (!$ctrl) {
158
            return;
159
        }
160
161
        $addFields = [];
162
        if (isset($ctrl['tstamp'])) {
163
            $addFields['tstamp'] = (int) $record[$ctrl['tstamp']];
164
        }
165
        if (isset($ctrl['crdate'])) {
166
            $addFields['crdate'] = (int) $record[$ctrl['crdate']];
167
        }
168
169
        foreach ($neededItems as $key => $value) {
170
            $neededItems[$key] = array_merge($value, $addFields);
171
        }
172
    }
173
174
    /**
175
     * Prepare the record for the database insert.
176
     *
177
     * @param $record
178
     */
179
    protected function prepareRecordForDatabase(&$record)
180
    {
181
        foreach ($record as $key => $value) {
182
            if ($value instanceof \DateTimeInterface) {
183
                $record[$key] = $value->format('Y-m-d');
184
            } elseif (\is_bool($value) || 'start_time' === $key || 'end_time' === $key) {
185
                $record[$key] = (int)$value;
186
            } elseif (null === $value) {
187
                $record[$key] = '';
188
            }
189
        }
190
    }
191
}
192