Completed
Pull Request — master (#248)
by Pascale
03:56
created

EventSearch   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 141
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setIdsByCustomSearch() 0 33 3
C setIdsByGeneral() 0 63 10
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\Utility\HelperUtility;
13
use TYPO3\CMS\Core\Utility\MathUtility;
14
15
/**
16
 * Event search service.
17
 */
18
class EventSearch
19
{
20
    /**
21
     * Table name.
22
     *
23
     * Note: This complete class is for the Event Model of the calendarize extension.
24
     * If you use a own model with special search criteria you have to register your
25
     * own custom Slot. If you only want the category logic for your model, you can
26
     * easily register a own slot that is based on this class. Thean you only have
27
     * to overwrite the tableName property.
28
     *
29
     * @var string
30
     */
31
    protected $tableName = 'tx_calendarize_domain_model_event';
32
33
    /**
34
     * Check if we can reduce the amount of results.
35
     *
36
     * @signalClass \HDNET\Calendarize\Domain\Repository\IndexRepository
37
     * @signalName findBySearchPre
38
     *
39
     * @param array          $indexIds
40
     * @param \DateTime|null $startDate
41
     * @param \DateTime|null $endDate
42
     * @param array          $customSearch
43
     * @param bool           $emptyPreResult
44
     * @param array          $additionalSlotArguments
45
     * @param array          $indexTypes
46
     *
47
     * @return array|void
48
     */
49
    public function setIdsByCustomSearch(
50
        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...
51
        \DateTime $startDate,
52
        \DateTime $endDate,
53
        array $customSearch,
54
        array $indexTypes,
55
        bool $emptyPreResult,
56
        array $additionalSlotArguments
57
    ) {
58
        if (!\in_array('Event', $indexTypes, true)) {
59
            return;
60
        }
61
62
        // @todo Filter here for $customSearch['categories'] and take also care of the fullText
63
        // ?tx_calendarize_calendar[customSearch][categories]=1
64
        // https://github.com/lochmueller/calendarize/issues/89
65
66
        if (!isset($customSearch['fullText'])) {
67
            return;
68
        }
69
70
        $eventRepository = HelperUtility::create(EventRepository::class);
71
72
        return [
73
            'indexIds' => $eventRepository->getIdsBySearchTerm($customSearch['fullText']),
74
            'startDate' => $startDate,
75
            'endDate' => $endDate,
76
            'customSearch' => $customSearch,
77
            'indexTypes' => $indexTypes,
78
            'emptyPreResult' => $emptyPreResult,
79
            'additionalSlotArguments' => $additionalSlotArguments,
80
        ];
81
    }
82
83
    /**
84
     * Set ids by general.
85
     *
86
     * @signalClass \HDNET\Calendarize\Domain\Repository\IndexRepository
87
     * @signalName getDefaultConstraints
88
     *
89
     * @param array $indexIds
90
     * @param array $indexTypes
91
     * @param array $additionalSlotArguments
92
     *
93
     * @return array|null
94
     */
95
    public function setIdsByGeneral(array $indexIds, array $indexTypes, array $additionalSlotArguments)
96
    {
97
        if (!\in_array('Event', $indexTypes, true)) {
98
            return;
99
        }
100
101
        $table = 'sys_category_record_mm';
102
        $db = HelperUtility::getDatabaseConnection($table);
103
        $q = $db->createQueryBuilder();
104
105
        $categoryIds = [];
106
        if (isset($additionalSlotArguments['contentRecord']['uid']) && MathUtility::canBeInterpretedAsInteger($additionalSlotArguments['contentRecord']['uid'])) {
107
            $rows = $q->select('uid_local')
108
                ->from($table)
109
                ->where(
110
                    $q->expr()->andX(
111
                        $q->expr()->eq('tablenames', 'tt_content'),
112
                        $q->expr()->eq('uid_foreign', $q->createNamedParameter($additionalSlotArguments['contentRecord']['uid']))
113
                    )
114
                )
115
                ->execute()
116
                ->fetchAll();
117
118
            foreach ($rows as $row) {
119
                $categoryIds[] = (int) $row['uid_local'];
120
            }
121
        }
122
123
        if (isset($additionalSlotArguments['settings']['pluginConfiguration']) && $additionalSlotArguments['settings']['pluginConfiguration'] instanceof PluginConfiguration) {
124
            /** @var PluginConfiguration $pluginConfiguration */
125
            $pluginConfiguration = $additionalSlotArguments['settings']['pluginConfiguration'];
126
            $categories = $pluginConfiguration->getCategories();
127
            foreach ($categories as $category) {
128
                $categoryIds[] = $category->getUid();
129
            }
130
        }
131
132
        if (empty($categoryIds)) {
133
            return;
134
        }
135
136
        $q->resetQueryParts();
137
        $rows = $q->select('uid_foreign')
138
            ->from('sys_category_record_mm')
139
            ->where(
140
                $q->expr()->andX(
141
                    $q->expr()->eq('tablenames', $this->tableName),
142
                    $q->expr()->in('uid', $categoryIds)
143
                )
144
            )
145
            ->execute()
146
            ->fetchAll();
147
148
        foreach ($rows as $row) {
149
            $indexIds[] = (int) $row['uid_foreign'];
150
        }
151
152
        return [
153
            'indexIds' => $indexIds,
154
            'indexTypes' => $indexTypes,
155
            'additionalSlotArguments' => $additionalSlotArguments,
156
        ];
157
    }
158
}
159