Completed
Push — master ( a07bcc...e28719 )
by Tim
08:54
created

EventSearch::setIdsByCustomSearch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
cc 3
nc 3
nop 7
1
<?php
2
3
/**
4
 * Event search service.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\Slots;
9
10
use HDNET\Calendarize\Domain\Model\PluginConfiguration;
11
use HDNET\Calendarize\Domain\Repository\EventRepository;
12
use HDNET\Calendarize\Register;
13
use HDNET\Calendarize\Utility\HelperUtility;
14
use TYPO3\CMS\Core\Utility\MathUtility;
15
16
/**
17
 * Event search service.
18
 */
19
class EventSearch
20
{
21
22
    /**
23
     * Check if we can reduce the amount of results.
24
     *
25
     * @signalClass \HDNET\Calendarize\Domain\Repository\IndexRepository
26
     * @signalName findBySearchPre
27
     *
28
     * @param array          $indexIds
29
     * @param \DateTime|null $startDate
30
     * @param \DateTime|null $endDate
31
     * @param array          $customSearch
32
     * @param bool           $emptyPreResult
33
     * @param array          $additionalSlotArguments
34
     * @param array          $indexTypes
35
     *
36
     * @return array|void
37
     */
38
    public function setIdsByCustomSearch(
39
        array $indexIds,
0 ignored issues
show
Unused Code introduced by
The parameter $indexIds is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
        \DateTime $startDate = null,
41
        \DateTime $endDate = null,
42
        array $customSearch = [],
43
        array $indexTypes = [],
44
        bool $emptyPreResult = false,
45
        array $additionalSlotArguments = []
46
    ) {
47
        if (!\in_array($this->getUniqueRegisterKey(), $indexTypes, true)) {
48
            return;
49
        }
50
51
        // @todo Filter here for $customSearch['categories'] and take also care of the fullText
52
        // ?tx_calendarize_calendar[customSearch][categories]=1
53
        // https://github.com/lochmueller/calendarize/issues/89
54
55
        if (!isset($customSearch['fullText'])) {
56
            return;
57
        }
58
59
        $eventRepository = HelperUtility::create(EventRepository::class);
60
61
        return [
62
            'indexIds' => $eventRepository->getIdsBySearchTerm($customSearch['fullText']),
63
            'startDate' => $startDate,
64
            'endDate' => $endDate,
65
            'customSearch' => $customSearch,
66
            'indexTypes' => $indexTypes,
67
            'emptyPreResult' => $emptyPreResult,
68
            'additionalSlotArguments' => $additionalSlotArguments,
69
        ];
70
    }
71
72
    /**
73
     * Set ids by general.
74
     *
75
     * @signalClass \HDNET\Calendarize\Domain\Repository\IndexRepository
76
     * @signalName getDefaultConstraints
77
     *
78
     * @param array $indexIds
79
     * @param array $indexTypes
80
     * @param array $additionalSlotArguments
81
     *
82
     * @return array|null
83
     */
84
    public function setIdsByGeneral(array $indexIds, array $indexTypes, array $additionalSlotArguments)
85
    {
86
        if (!\in_array($this->getUniqueRegisterKey(), $indexTypes, true)) {
87
            return;
88
        }
89
90
        $table = 'sys_category_record_mm';
91
        $db = HelperUtility::getDatabaseConnection($table);
92
        $q = $db->createQueryBuilder();
93
94
        $categoryIds = [];
95
        if (isset($additionalSlotArguments['contentRecord']['uid']) && MathUtility::canBeInterpretedAsInteger($additionalSlotArguments['contentRecord']['uid'])) {
96
            $rows = $q->select('uid_local')
97
                ->from($table)
98
                ->where(
99
                    $q->expr()->andX(
100
                        $q->expr()->eq('tablenames', $q->quote('tt_content')),
101
                        $q->expr()->eq('uid_foreign', $q->createNamedParameter($additionalSlotArguments['contentRecord']['uid']))
102
                    )
103
                )
104
                ->execute()
105
                ->fetchAll();
106
107
            foreach ($rows as $row) {
108
                $categoryIds[] = (int) $row['uid_local'];
109
            }
110
        }
111
112
        if (isset($additionalSlotArguments['settings']['pluginConfiguration']) && $additionalSlotArguments['settings']['pluginConfiguration'] instanceof PluginConfiguration) {
113
            /** @var PluginConfiguration $pluginConfiguration */
114
            $pluginConfiguration = $additionalSlotArguments['settings']['pluginConfiguration'];
115
            $categories = $pluginConfiguration->getCategories();
116
            foreach ($categories as $category) {
117
                $categoryIds[] = $category->getUid();
118
            }
119
        }
120
121
        if (empty($categoryIds)) {
122
            return;
123
        }
124
125
        $q->resetQueryParts();
126
        $rows = $q->select('uid_foreign')
127
            ->from('sys_category_record_mm')
128
            ->where(
129
                $q->expr()->in('uid_local', $categoryIds),
130
                $q->expr()->eq('tablenames', $q->quote($this->getTableName()))
131
            )
132
            ->execute()
133
            ->fetchAll();
134
135
        foreach ($rows as $row) {
136
            $indexIds[] = (int) $row['uid_foreign'];
137
        }
138
139
        return [
140
            'indexIds' => $indexIds,
141
            'indexTypes' => $indexTypes,
142
            'additionalSlotArguments' => $additionalSlotArguments,
143
        ];
144
    }
145
146
    /**
147
     * Table name.
148
     *
149
     * Note: This complete class is for the Event Model of the calendarize extension.
150
     * If you use a own model with special search criteria you have to register your
151
     * own custom Slot. If you only want the category logic for your model, you can
152
     * easily register a own slot that is based on this class. Thean you only have
153
     * to overwrite the tableName property.
154
     *
155
     * @return string
156
     */
157
    protected function getTableName()
158
    {
159
        $config = Register::getDefaultCalendarizeConfiguration();
160
        return $config['tableName'];
161
    }
162
163
    /**
164
     * Unique register key.
165
     *
166
     * @return string
167
     */
168
    protected function getUniqueRegisterKey()
169
    {
170
        $config = Register::getDefaultCalendarizeConfiguration();
171
        return $config['uniqueRegisterKey'];
172
    }
173
}
174