88 lines
2.9 KiB
PHP
88 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Mummert\KsNossenerlandBundle\Command;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Doctrine\DBAL\Connection;
|
|
use Mummert\KsNossenerlandBundle\Service\SoapClientService;
|
|
|
|
class ExportEventsCommand extends Command
|
|
{
|
|
private $connection;
|
|
private $soapClientService;
|
|
private const BATCH_SIZE = 200;
|
|
|
|
public function __construct(Connection $connection, SoapClientService $soapClientService)
|
|
{
|
|
parent::__construct();
|
|
$this->connection = $connection;
|
|
$this->soapClientService = $soapClientService;
|
|
}
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this->setHelp('Dieses Kommando exportiert zukünftige Events nach EVLKS und löscht nicht mehr vorhandene.');
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$today = (new \DateTimeImmutable('today'))->getTimestamp();
|
|
|
|
$exportedIds = [];
|
|
$exportCount = 0;
|
|
|
|
$lastId = 0;
|
|
|
|
while (true) {
|
|
$rows = $this->connection->executeQuery(
|
|
'SELECT * FROM tl_calendar_events
|
|
WHERE pid IN (1, 2, 3)
|
|
AND evlkscalendar != 1
|
|
AND ((endDate IS NOT NULL AND endDate >= ?) OR (endDate IS NULL AND startDate >= ?))
|
|
AND id > ?
|
|
ORDER BY id ASC
|
|
LIMIT ' . self::BATCH_SIZE,
|
|
[$today, $today, $lastId]
|
|
)->fetchAllAssociative();
|
|
|
|
if (empty($rows)) {
|
|
break;
|
|
}
|
|
|
|
foreach ($rows as $event) {
|
|
$response = $this->soapClientService->sendEventToSoapAPI($event);
|
|
if ($response) {
|
|
$exportedIds[] = (string) $event['id'];
|
|
$exportCount++;
|
|
}
|
|
|
|
$lastId = (int) $event['id'];
|
|
}
|
|
}
|
|
|
|
$remoteExternalIds = $this->soapClientService->fetchRemoteExternalIds();
|
|
$deletedCount = 0;
|
|
|
|
foreach ($remoteExternalIds as $externalId) {
|
|
if (!in_array($externalId, $exportedIds, true)) {
|
|
$response = $this->soapClientService->deleteEventByExternalId($externalId);
|
|
if ($response) {
|
|
$output->writeln("🗑️ gelöscht: $externalId");
|
|
$deletedCount++;
|
|
} else {
|
|
$output->writeln("❌ Fehler beim Löschen von Event $externalId");
|
|
}
|
|
}
|
|
}
|
|
|
|
$output->writeln('Export Summary:');
|
|
$output->writeln('----------------');
|
|
$output->writeln('Date: ' . date('Y-m-d H:i:s'));
|
|
$output->writeln("Number of exported events: $exportCount");
|
|
$output->writeln("Number of deleted events: $deletedCount");
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
} |