Completed
Push — master ( 7c2b21...ca3b7a )
by Tim
01:59
created

DatabaseRecordLinkBuilder::getIndexForEventUid()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.4746
c 0
b 0
f 0
cc 7
nc 20
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HDNET\Calendarize\Typolink;
6
7
use HDNET\Calendarize\Domain\Repository\IndexRepository;
8
use HDNET\Calendarize\Register;
9
use TYPO3\CMS\Core\Utility\ClassNamingUtility;
10
use TYPO3\CMS\Core\Utility\GeneralUtility;
11
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
12
use TYPO3\CMS\Extbase\Object\ObjectManager;
13
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
14
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
15
use TYPO3\CMS\Frontend\Typolink\UnableToLinkException;
16
17
/**
18
 * DatabaseRecordLinkBuilder.
19
 */
20
class DatabaseRecordLinkBuilder extends \TYPO3\CMS\Frontend\Typolink\DatabaseRecordLinkBuilder
21
{
22
    public function build(array &$linkDetails, string $linkText, string $target, array $conf): array
23
    {
24
        if (isset($linkDetails['identifier']) && \in_array($linkDetails['identifier'], $this->getEventTables(), true)) {
25
            $eventId = $linkDetails['uid'];
26
            $defaultPid = (int) ($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_calendarize.']['settings.']['defaultDetailPid'] ?? 0);
27
            if ($defaultPid <= 0) {
28
                throw new \Exception('You have to configure calendarize:defaultDetailPid to use the linkhandler function');
29
            }
30
31
            $typoScriptConfiguration = [
32
                'parameter' => $defaultPid,
33
                'additionalParams' => '&tx_calendarize_calendar[index]=' . $this->getIndexForEventUid($linkDetails['identifier'], $eventId),
34
            ];
35
36
            $localContentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
37
            $localContentObjectRenderer->parameters = $this->contentObjectRenderer->parameters;
38
            $link = $localContentObjectRenderer->typoLink($linkText, $typoScriptConfiguration);
39
40
            $this->contentObjectRenderer->lastTypoLinkLD = $localContentObjectRenderer->lastTypoLinkLD;
41
            $this->contentObjectRenderer->lastTypoLinkUrl = $localContentObjectRenderer->lastTypoLinkUrl;
42
            $this->contentObjectRenderer->lastTypoLinkTarget = $localContentObjectRenderer->lastTypoLinkTarget;
43
44
            // nasty workaround so typolink stops putting a link together, there is a link already built
45
            throw new UnableToLinkException('', 1491130170, null, $link);
46
        }
47
48
        parent::build($linkDetails, $linkText, $target, $conf);
49
    }
50
51
    protected function getIndexForEventUid($table, $uid): int
52
    {
53
        $indexRepository = (new ObjectManager())->get(IndexRepository::class);
54
        $register = Register::getRegister();
55
56
        $event = null;
57
        foreach ($register as $key => $value) {
58
            if ($value['tableName'] === $table) {
59
                $repositoryName = ClassNamingUtility::translateModelNameToRepositoryName($value['modelName']);
60
                if (\class_exists($repositoryName)) {
61
                    $objectManager = new ObjectManager();
62
                    $repository = $objectManager->get($repositoryName);
63
                    $event = $repository->findByUid($uid);
64
                }
65
            }
66
        }
67
68
        if (!($event instanceof DomainObjectInterface)) {
69
            return 0;
70
        }
71
72
        $fetchEvent = $indexRepository->findByEventTraversing($event, true, false, 1)->toArray();
73
        if (\count($fetchEvent) <= 0) {
74
            $fetchEvent = $indexRepository->findByEventTraversing($event, false, true, 1, QueryInterface::ORDER_DESCENDING)->toArray();
75
        }
76
77
        if (empty($fetchEvent)) {
78
            return 0;
79
        }
80
81
        return (int) $fetchEvent[0]->getUid();
82
    }
83
84
    protected function getEventTables(): array
85
    {
86
        static $tables;
87
        if (!\is_array($tables)) {
88
            $tables = \array_map(function ($config) {
89
                return $config['tableName'];
90
            }, GeneralUtility::makeInstance(Register::class)->getRegister());
91
        }
92
93
        return $tables;
94
    }
95
}
96