FetchMembersCommand.php 5.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace T3o\T3orgLayout\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use T3o\T3orgLayout\Service\FetchMembersService;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class FetchMembersCommand extends Command
{
    protected ?QueryBuilder $queryBuilder = null;

    protected ?FetchMembersService $fetchMembersService = null;

    protected array $extConfig = [];

    protected function initialize(InputInterface $input, OutputInterface $output)
    {
        $this->queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
            ->getQueryBuilderForTable('tx_t3omembership_domain_model_member');
        $this->extConfig = GeneralUtility::makeInstance(ExtensionConfiguration::class)
            ->get('t3org_layout');
        $this->fetchMembersService = GeneralUtility::makeInstance(FetchMembersService::class);
        $this->fetchMembersService->token = $this->extConfig['BearerToken'];
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $io->title('Importing members via API from my.typo3.org');

        if (!$this->extConfig['BearerToken']) {
            $io->error('No token for authentication to API found!');
            return 1;
        }

        $io->section('Fetching members...');
        $members = $this->fetchMembersService->getMembers();

        if ($members) {
            $io->success('Found ' . count($members) . ' members');
        } else {
            $io->warning('No members found. Please check the settings and API.');
            return 1;
        }

        $io->section('Importing members...');

        $this->queryBuilder
            ->update('tx_t3omembership_domain_model_member')
            ->where(
59
60
                $this->queryBuilder->expr()->neq('external_identifier', '""'),
                $this->queryBuilder->expr()->eq('pid', (int)$this->extConfig['MemberStorage'])
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
            )
            ->set('hidden', 1)
            ->execute();

        $this->queryBuilder = $this->queryBuilder->getConnection()->createQueryBuilder();

        foreach ($members as $member) {
            $this->queryBuilder = $this->queryBuilder->getConnection()->createQueryBuilder();
            $this->queryBuilder
                ->getRestrictions()
                ->removeByType(HiddenRestriction::class);

            $existingMember = $this->queryBuilder
                ->select('uid')
                ->from('tx_t3omembership_domain_model_member')
                ->where(
                    $this->queryBuilder->expr()->eq(
                        'external_identifier',
                        $this->queryBuilder->quote($member['identifier'])
                    )
                )
                ->execute()
                ->fetch();

            $this->queryBuilder = $this->queryBuilder->getConnection()->createQueryBuilder();

            if (!$existingMember) {
                // Prepare member date for DB insert
                $data = $member;
                $data['external_identifier'] = $data['identifier'];
                $data['url'] = $data['backlink'] ?? '';
                $data['hidden'] = 0;
                $data['pid'] = (int)$this->extConfig['MemberStorage'];
                $data['tstamp'] = time();
                $data['crdate'] = time();
                $data['membership'] = $this->getMembershipId($data['membership']);
97
                $data['email'] = $data['email'] ?? '';
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

                unset($data['votingEmail']);
                unset($data['identifier']);
                unset($data['backlink']);

                $this->queryBuilder
                    ->insert('tx_t3omembership_domain_model_member')
                    ->values($data)
                    ->execute();
            } else {
                $this->queryBuilder
                    ->update('tx_t3omembership_domain_model_member')
                    ->where(
                        $this->queryBuilder->expr()->eq(
                            'uid',
                            $existingMember['uid']
                        )
                    )
                    ->set('name', $member['name'])
                    ->set('email', $member['email'] ?? '')
                    ->set('url', $member['backlink'] ?? '')
                    ->set('membership', $this->getMembershipId($member['membership']))
                    ->set('hidden', 0)
                    ->set('tstamp', time())
                    ->execute();
            }

            $io->success('Imported member: ' . $member['name']);
        }

        $io->section('Flushing caches on page with UID 160');
        // Flush caches on page ID 160
        GeneralUtility::makeInstance(CacheManager::class)
            ->flushCachesInGroupByTags(
                'pages',
                ['pageId_160']
            );

        $io->success('Finished.');

        return 0;
    }

    private function getMembershipId(string $membership): int
    {
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
            ->getQueryBuilderForTable('tx_t3omembership_domain_model_membership');

        $membershipRecord = $queryBuilder
            ->select('uid')
            ->from('tx_t3omembership_domain_model_membership')
            ->where(
                $queryBuilder->expr()->eq('external_identifier', $queryBuilder->quote($membership))
            )
            ->execute()
            ->fetch();

        return $membershipRecord['uid'] ?? 0;
    }
}