Completed
Push — master ( c28fba...9b84d0 )
by Tim
15s queued 11s
created

IndexPreparationService::addSlugInformation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
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\Register;
12
use HDNET\Calendarize\Service\Url\SlugService;
13
use HDNET\Calendarize\Utility\HelperUtility;
14
use TYPO3\CMS\Backend\Utility\BackendUtility;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
17
/**
18
 * Helper class for the IndexService
19
 * Prepare the index.
20
 */
21
class IndexPreparationService extends AbstractService
22
{
23
    /**
24
     * @var SlugService
25
     */
26
    protected $slugService;
27
28
    public function __construct(SlugService $slugService)
29
    {
30
        $this->slugService = $slugService;
31
    }
32
33
    /**
34
     * Build the index for one element.
35
     *
36
     * @param string $configurationKey
37
     * @param string $tableName
38
     * @param int    $uid
39
     *
40
     * @return array
41
     */
42
    public function prepareIndex($configurationKey, $tableName, $uid)
43
    {
44
        $rawRecord = BackendUtility::getRecord($tableName, $uid);
45
        if (!$rawRecord) {
46
            return [];
47
        }
48
49
        $register = Register::getRegister();
50
        $fieldName = $register[$configurationKey]['fieldName'] ?? 'calendarize';
51
        $configurations = GeneralUtility::intExplode(',', $rawRecord[$fieldName], true);
52
53
        $transPointer = $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'] ?? false; // e.g. l10n_parent
54
        if ($transPointer && (int)$rawRecord[$transPointer] > 0) {
55
            $rawOriginalRecord = BackendUtility::getRecord($tableName, (int)$rawRecord[$transPointer]);
56
            $configurations = GeneralUtility::intExplode(',', $rawOriginalRecord[$fieldName], true);
57
        }
58
59
        $neededItems = [];
60
        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...
61
            $timeTableService = GeneralUtility::makeInstance(TimeTableService::class);
62
            $neededItems = $timeTableService->getTimeTablesByConfigurationIds($configurations);
63
            foreach ($neededItems as $key => $record) {
64
                $record['foreign_table'] = $tableName;
65
                $record['foreign_uid'] = $uid;
66
                $record['unique_register_key'] = $configurationKey;
67
68
                $this->prepareRecordForDatabase($record);
69
                $neededItems[$key] = $record;
70
            }
71
        }
72
73
        // Language information must be added before ctrl/enable information
74
        $this->addLanguageInformation($neededItems, $tableName, $rawRecord);
75
        $this->addEnableFieldInformation($neededItems, $tableName, $rawRecord);
76
        $this->addCtrlFieldInformation($neededItems, $tableName, $rawRecord);
77
        $this->addSlugInformation($neededItems, $configurationKey, $rawRecord);
78
79
        return $neededItems;
80
    }
81
82
    /**
83
     * Add the language information.
84
     *
85
     * @param array  $neededItems
86
     * @param string $tableName
87
     * @param array  $record
88
     */
89
    protected function addLanguageInformation(array &$neededItems, string $tableName, array $record): void
90
    {
91
        $languageField = $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] ?? false; // e.g. sys_language_uid
92
        $transPointer = $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'] ?? false; // e.g. l10n_parent
93
94
        if ($transPointer && (int)$record[$transPointer] > 0) {
95
            foreach ($neededItems as $key => $value) {
96
                $originalRecord = BackendUtility::getRecord($value['foreign_table'], $value['foreign_uid'], $transPointer);
97
98
                $searchFor = $value;
99
                $searchFor['foreign_uid'] = (int)$originalRecord[$transPointer];
100
101
                $db = HelperUtility::getDatabaseConnection(IndexerService::TABLE_NAME);
102
                $q = $db->createQueryBuilder();
103
                $where = [];
104
                foreach ($searchFor as $field => $val) {
105
                    if (\is_string($val)) {
106
                        $where[] = $q->expr()->eq($field, $q->quote($val));
107
                    } else {
108
                        $where[] = $q->expr()->eq($field, (int)$val);
109
                    }
110
                }
111
112
                $result = $q->select('uid')->from(IndexerService::TABLE_NAME)->andWhere(...$where)->execute()->fetch();
113
                if (isset($result['uid'])) {
114
                    $neededItems[$key]['l10n_parent'] = (int)$result['uid'];
115
                }
116
            }
117
        }
118
119
        if ($languageField && 0 !== (int)$record[$languageField]) {
120
            $language = (int)$record[$languageField];
121
            foreach (array_keys($neededItems) as $key) {
122
                $neededItems[$key]['sys_language_uid'] = $language;
123
            }
124
        }
125
    }
126
127
    /**
128
     * Add the enable field information.
129
     *
130
     * @param array  $neededItems
131
     * @param string $tableName
132
     * @param array  $record
133
     */
134
    protected function addEnableFieldInformation(array &$neededItems, $tableName, array $record)
135
    {
136
        $enableFields = $GLOBALS['TCA'][$tableName]['ctrl']['enablecolumns'] ?? [];
137
        if (!$enableFields) {
138
            return;
139
        }
140
141
        $addFields = [];
142
        if (isset($enableFields['disabled'])) {
143
            $addFields['hidden'] = (int)$record[$enableFields['disabled']];
144
        }
145
        if (isset($enableFields['starttime'])) {
146
            $addFields['starttime'] = (int)$record[$enableFields['starttime']];
147
        }
148
        if (isset($enableFields['endtime'])) {
149
            $addFields['endtime'] = (int)$record[$enableFields['endtime']];
150
        }
151
        if (isset($enableFields['fe_group'])) {
152
            $addFields['fe_group'] = (string)$record[$enableFields['fe_group']];
153
        }
154
155
        foreach ($neededItems as $key => $value) {
156
            $neededItems[$key] = array_merge($value, $addFields);
157
        }
158
    }
159
160
    /**
161
     * Add the ctrl field information.
162
     *
163
     * @param array  $neededItems
164
     * @param string $tableName
165
     * @param array  $record
166
     */
167
    protected function addCtrlFieldInformation(array &$neededItems, $tableName, array $record)
168
    {
169
        $ctrl = $GLOBALS['TCA'][$tableName]['ctrl'] ?? [];
170
        if (!$ctrl) {
171
            return;
172
        }
173
174
        $addFields = [];
175
        if (isset($ctrl['tstamp'])) {
176
            $addFields['tstamp'] = (int)$record[$ctrl['tstamp']];
177
        }
178
        if (isset($ctrl['crdate'])) {
179
            $addFields['crdate'] = (int)$record[$ctrl['crdate']];
180
        }
181
182
        foreach ($neededItems as $key => $value) {
183
            $neededItems[$key] = array_merge($value, $addFields);
184
        }
185
    }
186
187
    /**
188
     * Add slug to each index.
189
     *
190
     * @param array  $neededItems
191
     * @param string $uniqueRegisterKey
192
     * @param array  $record
193
     */
194
    protected function addSlugInformation(array &$neededItems, string $uniqueRegisterKey, array $record): void
195
    {
196
        $slugs = $this->slugService->generateSlugForItems($uniqueRegisterKey, $record, $neededItems);
197
        foreach ($neededItems as $key => $value) {
198
            $neededItems[$key] = array_merge($value, $slugs[$key] ?? []);
199
        }
200
    }
201
202
    /**
203
     * Prepare the record for the database insert.
204
     *
205
     * @param $record
206
     */
207
    protected function prepareRecordForDatabase(&$record)
208
    {
209
        foreach ($record as $key => $value) {
210
            if ($value instanceof \DateTimeInterface) {
211
                $record[$key] = $value->format('Y-m-d');
212
            } elseif (\is_bool($value) || 'start_time' === $key || 'end_time' === $key) {
213
                $record[$key] = (int)$value;
214
            } elseif (null === $value) {
215
                $record[$key] = '';
216
            }
217
        }
218
    }
219
}
220