Completed
Push — master ( ed4971...ad2e84 )
by Tim
02:09
created

IndexPreparationService::prepareIndex()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 8.5341
c 0
b 0
f 0
cc 6
nc 5
nop 3
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\Utility\HelperUtility;
12
use TYPO3\CMS\Backend\Utility\BackendUtility;
13
use TYPO3\CMS\Core\Utility\GeneralUtility;
14
15
/**
16
 * Helper class for the IndexService
17
 * Prepare the index.
18
 */
19
class IndexPreparationService
20
{
21
    /**
22
     * Build the index for one element.
23
     *
24
     * @param string $configurationKey
25
     * @param string $tableName
26
     * @param int    $uid
27
     *
28
     * @return array
29
     */
30
    public function prepareIndex($configurationKey, $tableName, $uid)
31
    {
32
        $rawRecord = BackendUtility::getRecord($tableName, $uid);
33
        if (!$rawRecord) {
34
            return [];
35
        }
36
        $configurations = GeneralUtility::intExplode(',', $rawRecord['calendarize'], true);
37
38
        $transPointer = $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'] ?? false; // e.g. l10n_parent
39
        if ($transPointer && (int) $rawRecord[$transPointer] > 0) {
40
            $rawOriginalRecord = BackendUtility::getRecord($tableName, (int) $rawRecord[$transPointer]);
41
            $configurations = GeneralUtility::intExplode(',', $rawOriginalRecord['calendarize'], true);
42
        }
43
44
        $neededItems = [];
45
        if ($configurations) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $configurations of type array 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...
46
            $timeTableService = GeneralUtility::makeInstance(TimeTableService::class);
47
            $neededItems = $timeTableService->getTimeTablesByConfigurationIds($configurations);
48
            foreach ($neededItems as $key => $record) {
49
                $record['foreign_table'] = $tableName;
50
                $record['foreign_uid'] = $uid;
51
                $record['unique_register_key'] = $configurationKey;
52
53
                // UTC fix
54
                $record['start_date'] = \DateTime::createFromFormat(
55
                    'Y-m-d H:i:s',
56
                    $record['start_date']->format('Y-m-d') . ' 00:00:00',
57
                    new \DateTimeZone('UTC')
58
                );
59
                $record['end_date'] = \DateTime::createFromFormat(
60
                    'Y-m-d H:i:s',
61
                    $record['end_date']->format('Y-m-d') . ' 00:00:00',
62
                    new \DateTimeZone('UTC')
63
                );
64
65
                $this->prepareRecordForDatabase($record);
66
                $neededItems[$key] = $record;
67
            }
68
        }
69
70
        $this->addEnableFieldInformation($neededItems, $tableName, $rawRecord);
71
        $this->addLanguageInformation($neededItems, $tableName, $rawRecord);
72
73
        // @todo Handle Workspace IDs?
74
75
        return $neededItems;
76
    }
77
78
    /**
79
     * Add the language information.
80
     *
81
     * @param array  $neededItems
82
     * @param string $tableName
83
     * @param array  $record
84
     */
85
    protected function addLanguageInformation(array &$neededItems, $tableName, array $record)
86
    {
87
        $languageField = $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] ?? false; // e.g. sys_language_uid
88
        $transPointer = $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'] ?? false; // e.g. l10n_parent
89
90
        if ($transPointer && (int) $record[$transPointer] > 0) {
91
            foreach ($neededItems as $key => $value) {
92
                $originalRecord = BackendUtility::getRecord($value['foreign_table'], $value['foreign_uid']);
93
94
                $searchFor = $value;
95
                $searchFor['foreign_uid'] = (int) $originalRecord[$transPointer];
96
97
                $db = HelperUtility::getDatabaseConnection(IndexerService::TABLE_NAME);
98
                $q = $db->createQueryBuilder();
99
                $where = [];
100
                foreach ($searchFor as $field => $val) {
101
                    if (\is_string($val)) {
102
                        $where[] = $q->expr()->eq($field, $q->quote($val));
103
                    } else {
104
                        $where[] = $q->expr()->eq($field, (int) $val);
105
                    }
106
                }
107
108
                $result = $q->select('uid')->from(IndexerService::TABLE_NAME)->andWhere(...$where)->execute()->fetch();
109
                if (isset($result['uid'])) {
110
                    $neededItems[$key]['l10n_parent'] = (int) $result['uid'];
111
                }
112
            }
113
        }
114
115
        if ($languageField && 0 !== (int) $record[$languageField]) {
116
            $language = (int) $record[$languageField];
117
            foreach (\array_keys($neededItems) as $key) {
118
                $neededItems[$key]['sys_language_uid'] = $language;
119
            }
120
        }
121
    }
122
123
    /**
124
     * Add the enable field information.
125
     *
126
     * @param array  $neededItems
127
     * @param string $tableName
128
     * @param array  $record
129
     */
130
    protected function addEnableFieldInformation(array &$neededItems, $tableName, array $record)
131
    {
132
        $enableFields = $GLOBALS['TCA'][$tableName]['ctrl']['enablecolumns'] ?? [];
133
        if (!$enableFields) {
134
            return;
135
        }
136
137
        $addFields = [];
138
        if (isset($enableFields['disabled'])) {
139
            $addFields['hidden'] = (int) $record[$enableFields['disabled']];
140
        }
141
        if (isset($enableFields['starttime'])) {
142
            $addFields['starttime'] = (int) $record[$enableFields['starttime']];
143
        }
144
        if (isset($enableFields['endtime'])) {
145
            $addFields['endtime'] = (int) $record[$enableFields['endtime']];
146
        }
147
        if (isset($enableFields['fe_group'])) {
148
            $addFields['fe_group'] = (string) $record[$enableFields['fe_group']];
149
        }
150
151
        foreach ($neededItems as $key => $value) {
152
            $neededItems[$key] = \array_merge($value, $addFields);
153
        }
154
    }
155
156
    /**
157
     * Prepare the record for the database insert.
158
     *
159
     * @param $record
160
     */
161
    protected function prepareRecordForDatabase(&$record)
162
    {
163
        foreach ($record as $key => $value) {
164
            if ($value instanceof \DateTimeInterface) {
165
                $record[$key] = $value->getTimestamp();
166
            } elseif (\is_bool($value) || 'start_time' === $key || 'end_time' === $key) {
167
                $record[$key] = (int) $value;
168
            } elseif (null === $value) {
169
                $record[$key] = '';
170
            }
171
        }
172
    }
173
}
174