Skip to content

Commit

Permalink
Merge pull request #5946 from christianbeeznest/ofaj-22151-6
Browse files Browse the repository at this point in the history
Internal: Fix translations for event reminders based on user locale - refs BT#22151
  • Loading branch information
christianbeeznest authored Nov 29, 2024
2 parents 330c056 + 5abe422 commit 96a3270
Showing 1 changed file with 27 additions and 32 deletions.
59 changes: 27 additions & 32 deletions src/CoreBundle/Command/SendEventRemindersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ protected function configure(): void
$this
->setDescription('Send notification messages to users that have reminders from events in their agenda.')
->addOption('debug', null, InputOption::VALUE_NONE, 'Enable debug mode')
->setHelp('This command sends notifications to users who have pending reminders for calendar events.')
;
->setHelp('This command sends notifications to users who have pending reminders for calendar events.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand Down Expand Up @@ -94,30 +93,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$eventDetails = $this->generateEventDetails($event);
$messageSubject = \sprintf('Reminder for event: %s', $event->getTitle());
$messageContent = implode(PHP_EOL, $eventDetails);

$initialSentRemindersCount = $sentRemindersCount;

if ('personal' === $eventType) {
$creator = $event->getResourceNode()->getCreator();
if ($creator) {
$this->messageHelper->sendMessageSimple($creator->getId(), $messageSubject, $messageContent, $senderId);
if ($debug) {
error_log("Message sent to creator ID: {$creator->getId()} for personal event: ".$event->getTitle());
}
$sentRemindersCount++;
$this->sendReminderMessage($creator, $event, $senderId, $debug, $io, $sentRemindersCount);
}

$resourceLinks = $event->getResourceNode()->getResourceLinks();
if (!$resourceLinks->isEmpty()) {
foreach ($resourceLinks as $link) {
if ($user = $link->getUser()) {
$this->messageHelper->sendMessageSimple($user->getId(), $messageSubject, $messageContent, $senderId);
if ($debug) {
error_log("Message sent to user ID: {$user->getId()} for personal event: ".$event->getTitle());
}
$sentRemindersCount++;
$this->sendReminderMessage($user, $event, $senderId, $debug, $io, $sentRemindersCount);
}
}
}
Expand All @@ -135,25 +124,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
case 'global':
foreach ($event->getResourceNode()->getResourceLinks() as $link) {
if ($user = $link->getUser()) {
$this->messageHelper->sendMessageSimple($user->getId(), $messageSubject, $messageContent, $senderId);
if ($debug) {
error_log("Message sent to user ID: {$user->getId()} for global event: ".$event->getTitle());
}
$sentRemindersCount++;
$this->sendReminderMessage($user, $event, $senderId, $debug, $io, $sentRemindersCount);
}
}

break;

case 'course':
if ($course = $resourceLink->getCourse()) {
$users = $this->courseRepository->getSubscribedUsers($course)->getQuery()->getResult();
foreach ($users as $user) {
$this->messageHelper->sendMessageSimple($user->getId(), $messageSubject, $messageContent, $senderId);
if ($debug) {
error_log("Message sent to user ID: {$user->getId()} for course event: ".$event->getTitle());
}
$sentRemindersCount++;
$this->sendReminderMessage($user, $event, $senderId, $debug, $io, $sentRemindersCount);
}
}

Expand Down Expand Up @@ -186,11 +166,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

foreach ($usersToNotify as $user) {
$this->messageHelper->sendMessageSimple($user->getId(), $messageSubject, $messageContent, $senderId);
if ($debug) {
error_log("Message sent to user ID: {$user->getId()} ({$user->getUsername()}) for session event: {$event->getTitle()}");
}
$sentRemindersCount++;
$this->sendReminderMessage($user, $event, $senderId, $debug, $io, $sentRemindersCount);
}
}

Expand All @@ -215,6 +191,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

private function sendReminderMessage(User $user, CCalendarEvent $event, int $senderId, bool $debug, SymfonyStyle $io, int &$sentRemindersCount): void
{
$locale = $user->getLocale() ?: 'en';
$this->translator->setLocale($locale);

$messageSubject = $this->translator->trans('Reminder for event : %s', ['%s' => $event->getTitle()]);
$messageContent = implode(PHP_EOL, $this->generateEventDetails($event));

$this->messageHelper->sendMessageSimple($user->getId(), $messageSubject, $messageContent, $senderId);

if ($debug) {
error_log("Message sent to user ID: {$user->getId()} for event: {$event->getTitle()}");
error_log("Message Subject: {$messageSubject}");
error_log("Message Content: {$messageContent}");
}

$sentRemindersCount++;
}

private function getFirstAdminId(): int
{
$admin = $this->entityManager->getRepository(User::class)->findOneBy([]);
Expand All @@ -234,13 +229,13 @@ private function generateEventDetails(CCalendarEvent $event): array
} else {
$details[] = \sprintf(
'<p class="small">%s</p>',
\sprintf($this->translator->trans('From %s'), $event->getStartDate()->format('Y-m-d H:i:s'))
$this->translator->trans('From %s', ['%s' => $event->getStartDate()->format('Y-m-d H:i:s')])
);

if ($event->getEndDate()) {
$details[] = \sprintf(
'<p class="small">%s</p>',
\sprintf($this->translator->trans('Until %s'), $event->getEndDate()->format('Y-m-d H:i:s'))
$this->translator->trans('Until %s', ['%s' => $event->getEndDate()->format('Y-m-d H:i:s')])
);
}
}
Expand Down

0 comments on commit 96a3270

Please sign in to comment.