Completed
Push — master ( a9a357...5d1efe )
by Tim
02:14
created

DefaultEventConstraintsListener::getTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace HDNET\Calendarize\EventListener;
4
5
use HDNET\Calendarize\Domain\Model\PluginConfiguration;
6
use HDNET\Calendarize\Event\IndexRepositoryDefaultConstraintEvent;
7
use HDNET\Calendarize\Register;
8
use HDNET\Calendarize\Utility\HelperUtility;
9
use TYPO3\CMS\Core\Utility\MathUtility;
10
11
class DefaultEventConstraintsListener
12
{
13
    public function __invoke(IndexRepositoryDefaultConstraintEvent $event)
14
    {
15
        if (!empty($event->getIndexTypes()) && !\in_array(Register::UNIQUE_REGISTER_KEY, $event->getIndexTypes(), true)) {
16
            return;
17
        }
18
19
        $table = 'sys_category_record_mm';
20
        $db = HelperUtility::getDatabaseConnection($table);
21
        $q = $db->createQueryBuilder();
22
23
        $additionalSlotArguments = $event->getAdditionalSlotArguments();
24
25
        $categoryIds = [];
26
        if (isset($additionalSlotArguments['contentRecord']['uid']) && MathUtility::canBeInterpretedAsInteger($additionalSlotArguments['contentRecord']['uid'])) {
27
            $rows = $q->select('uid_local')
28
                ->from($table)
29
                ->where(
30
                    $q->expr()->andX(
31
                        $q->expr()->eq('tablenames', $q->quote('tt_content')),
32
                        $q->expr()->eq('fieldname', $q->quote('categories')),
33
                        $q->expr()->eq('uid_foreign', $q->createNamedParameter($additionalSlotArguments['contentRecord']['uid']))
34
                    )
35
                )
36
                ->execute()
37
                ->fetchAll();
38
39
            foreach ($rows as $row) {
40
                $categoryIds[] = (int)$row['uid_local'];
41
            }
42
        }
43
44
        if (isset($additionalSlotArguments['settings']['pluginConfiguration']) && $additionalSlotArguments['settings']['pluginConfiguration'] instanceof PluginConfiguration) {
45
            /** @var PluginConfiguration $pluginConfiguration */
46
            $pluginConfiguration = $additionalSlotArguments['settings']['pluginConfiguration'];
47
            $categories = $pluginConfiguration->getCategories();
48
            foreach ($categories as $category) {
49
                $categoryIds[] = $category->getUid();
50
            }
51
        }
52
53
        if (empty($categoryIds)) {
54
            return;
55
        }
56
57
        $q->resetQueryParts();
58
        $rows = $q->select('uid_foreign')
59
            ->from('sys_category_record_mm')
60
            ->where(
61
                $q->expr()->in('uid_local', $categoryIds),
62
                $q->expr()->eq('tablenames', $q->quote($this->getTableName())),
63
                $q->expr()->eq('fieldname', $q->quote('categories'))
64
            )
65
            ->execute()
66
            ->fetchAll();
67
68
        $indexIds = $event->getIndexIds();
69
        foreach ($rows as $row) {
70
            $indexIds[] = (int)$row['uid_foreign'];
71
        }
72
73
        $indexIds[] = -1;
74
        $event->setIndexIds($indexIds);
75
    }
76
77
    /**
78
     * Table name.
79
     *
80
     * Note: This complete class is for the Event Model of the calendarize extension.
81
     * If you use a own model with special search criteria you have to register your
82
     * own custom Slot. If you only want the category logic for your model, you can
83
     * easily register a own slot that is based on this class. Thean you only have
84
     * to overwrite the tableName property.
85
     *
86
     * @return string
87
     */
88
    protected function getTableName()
89
    {
90
        $config = Register::getDefaultCalendarizeConfiguration();
91
92
        return $config['tableName'];
93
    }
94
}
95