Completed
Pull Request — master (#450)
by
unknown
02:09
created

EventSearch::setIdsByCustomSearch()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.7217
c 0
b 0
f 0
cc 6
nc 4
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\Autoloader\Annotation\SignalClass;
11
use HDNET\Autoloader\Annotation\SignalName;
12
use HDNET\Calendarize\Domain\Model\PluginConfiguration;
13
use HDNET\Calendarize\Domain\Repository\EventRepository;
14
use HDNET\Calendarize\Register;
15
use HDNET\Calendarize\Utility\HelperUtility;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use TYPO3\CMS\Core\Utility\MathUtility;
18
use TYPO3\CMS\Extbase\Object\ObjectManager;
19
20
/**
21
 * Event search service.
22
 */
23
class EventSearch
24
{
25
    /**
26
     * Check if we can reduce the amount of results.
27
     *
28
     * @SignalClass(value="HDNET\Calendarize\Domain\Repository\IndexRepository")
29
     * @SignalName(value="findBySearchPre")
30
     *
31
     * @param array          $indexIds
32
     * @param \DateTime|null $startDate
33
     * @param \DateTime|null $endDate
34
     * @param array          $customSearch
35
     * @param array          $indexTypes
36
     * @param bool           $emptyPreResult
37
     * @param array          $additionalSlotArguments
38
     *
39
     * @return array|void
40
     */
41
    public function setIdsByCustomSearch(
42
        array $indexIds,
43
        \DateTime $startDate = null,
44
        \DateTime $endDate = null,
45
        array $customSearch = [],
46
        array $indexTypes = [],
47
        bool $emptyPreResult = false,
48
        array $additionalSlotArguments = []
49
    ) {
50
        if (!\in_array($this->getUniqueRegisterKey(), $indexTypes, true)) {
51
            return;
52
        }
53
54
        // @todo Filter here for $customSearch['categories'] and take also care of the fullText
55
        // ?tx_calendarize_calendar[customSearch][categories]=1
56
        // https://github.com/lochmueller/calendarize/issues/89
57
58
        if (!isset($customSearch['fullText']) || !$customSearch['fullText']) {
59
            return;
60
        }
61
        /** @var EventRepository $eventRepository */
62
        $eventRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(EventRepository::class);
63
        $searchTermHits = $eventRepository->getIdsBySearchTerm($customSearch['fullText']);
64
        if ($searchTermHits && count($searchTermHits)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $searchTermHits 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...
65
            $indexIds['tx_calendarize_domain_model_event'] = $searchTermHits;
66
        }
67
        return [
68
            'indexIds' => $indexIds,
69
            'startDate' => $startDate,
70
            'endDate' => $endDate,
71
            'customSearch' => $customSearch,
72
            'indexTypes' => $indexTypes,
73
            'emptyPreResult' => $emptyPreResult,
74
            'additionalSlotArguments' => $additionalSlotArguments,
75
        ];
76
    }
77
78
    /**
79
     * Set ids by general.
80
     *
81
     * @SignalClass \HDNET\Calendarize\Domain\Repository\IndexRepository
82
     * @SignalName getDefaultConstraints
83
     *
84
     * @param array $indexIds
85
     * @param array $indexTypes
86
     * @param array $additionalSlotArguments
87
     *
88
     * @return array|null
89
     */
90
    public function setIdsByGeneral(array $indexIds, array $indexTypes, array $additionalSlotArguments)
91
    {
92
        if (!\in_array($this->getUniqueRegisterKey(), $indexTypes, true)) {
93
            return;
94
        }
95
96
        $table = 'sys_category_record_mm';
97
        $db = HelperUtility::getDatabaseConnection($table);
98
        $q = $db->createQueryBuilder();
99
100
        $categoryIds = [];
101
        if (isset($additionalSlotArguments['contentRecord']['uid']) && MathUtility::canBeInterpretedAsInteger($additionalSlotArguments['contentRecord']['uid'])) {
102
            $rows = $q->select('uid_local')
103
                ->from($table)
104
                ->where(
105
                    $q->expr()->andX(
106
                        $q->expr()->eq('tablenames', $q->quote('tt_content')),
107
                        $q->expr()->eq('fieldname', $q->quote('categories')),
108
                        $q->expr()->eq('uid_foreign', $q->createNamedParameter($additionalSlotArguments['contentRecord']['uid']))
109
                    )
110
                )
111
                ->execute()
112
                ->fetchAll();
113
114
            foreach ($rows as $row) {
115
                $categoryIds[] = (int)$row['uid_local'];
116
            }
117
        }
118
119
        if (isset($additionalSlotArguments['settings']['pluginConfiguration']) && $additionalSlotArguments['settings']['pluginConfiguration'] instanceof PluginConfiguration) {
120
            /** @var PluginConfiguration $pluginConfiguration */
121
            $pluginConfiguration = $additionalSlotArguments['settings']['pluginConfiguration'];
122
            $categories = $pluginConfiguration->getCategories();
123
            foreach ($categories as $category) {
124
                $categoryIds[] = $category->getUid();
125
            }
126
        }
127
128
        if (empty($categoryIds)) {
129
            return;
130
        }
131
132
        $q->resetQueryParts();
133
        $rows = $q->select('uid_foreign')
134
            ->from('sys_category_record_mm')
135
            ->where(
136
                $q->expr()->in('uid_local', $categoryIds),
137
                $q->expr()->eq('tablenames', $q->quote($this->getTableName())),
138
                $q->expr()->eq('fieldname', $q->quote('categories'))
139
            )
140
            ->execute()
141
            ->fetchAll();
142
143
        foreach ($rows as $row) {
144
            $indexIds[] = (int)$row['uid_foreign'];
145
        }
146
147
        $indexIds[] = -1;
148
149
        return [
150
            'indexIds' => $indexIds,
151
            'indexTypes' => $indexTypes,
152
            'additionalSlotArguments' => $additionalSlotArguments,
153
        ];
154
    }
155
156
    /**
157
     * Table name.
158
     *
159
     * Note: This complete class is for the Event Model of the calendarize extension.
160
     * If you use a own model with special search criteria you have to register your
161
     * own custom Slot. If you only want the category logic for your model, you can
162
     * easily register a own slot that is based on this class. Thean you only have
163
     * to overwrite the tableName property.
164
     *
165
     * @return string
166
     */
167
    protected function getTableName()
168
    {
169
        $config = Register::getDefaultCalendarizeConfiguration();
170
171
        return $config['tableName'];
172
    }
173
174
    /**
175
     * Unique register key.
176
     *
177
     * @return string
178
     */
179
    protected function getUniqueRegisterKey()
180
    {
181
        $config = Register::getDefaultCalendarizeConfiguration();
182
183
        return $config['uniqueRegisterKey'];
184
    }
185
}
186