Commit 3579043f authored by Stefan Busemann's avatar Stefan Busemann
Browse files

Merge branch 'feature/add-import-of-members' into 'develop'

[FEATURE] Add importer for members

Closes t3o_membership#42

See merge request !438
parents 5fab081f 158b2ff7
Pipeline #12467 passed with stages
in 7 minutes and 20 seconds
<?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(
$this->queryBuilder->expr()->neq('external_identifier', '""')
)
->set('hidden', 1)
->execute();
$this->queryBuilder = $this->queryBuilder->getConnection()->createQueryBuilder();
$warnings = false;
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']);
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;
}
}
......@@ -13,6 +13,12 @@ services:
command: 't3o:fetchplatinummembers'
description: 'Fetch TYPO3 platinum members for import in randombanners'
T3o\T3orgLayout\Command\FetchMembersCommand:
tags:
- name: 'console.command'
command: 't3o:fetchmembers'
description: 'Fetch TYPO3 members for import in t3o_membership'
T3o\T3orgLayout\Event\ModifyDetailViewVariablesEvent:
tags:
- name: event.listener
......
<?php
$columns = [
'external_identifier' => [
'label' => 'External identifier (my.typo3.org)',
'exclude' => 1,
'config' => [
'type' => 'input',
'readOnly' => true,
],
],
];
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
'tx_t3omembership_domain_model_member',
$columns
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'tx_t3omembership_domain_model_member',
'external_identifier',
'',
'after:hidden'
);
<?php
$columns = [
'external_identifier' => [
'label' => 'External identifier (my.typo3.org)',
'exclude' => 1,
'config' => [
'type' => 'input',
],
],
];
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
'tx_t3omembership_domain_model_membership',
$columns
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'tx_t3omembership_domain_model_membership',
'external_identifier',
'',
'after:hidden'
);
# cat=PlatinumMembers; type=string; label=Authentication Bearer for importing members from my.typo3.org
# cat=API; type=string; label=Authentication Bearer for importing members from my.typo3.org
BearerToken =
# cat=PlatinumMembers; type=int; label=PID for storing randombanner records after import
BannerStorage =
# cat=Members; type=int; label=PID for storing t3o_membership records after import
MemberStorage =
......@@ -27,3 +27,11 @@ CREATE TABLE tx_randombanners_domain_model_banner (
external_identifier varchar(255) DEFAULT '' NOT NULL,
external_logo varchar(1024) DEFAULT '' NOT NULL
);
CREATE TABLE tx_t3omembership_domain_model_member (
external_identifier varchar(255) DEFAULT '' NOT NULL
);
CREATE TABLE tx_t3omembership_domain_model_membership (
external_identifier varchar(255) DEFAULT '' NOT NULL
);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment