Skip to content

Commit

Permalink
Add SendNotificationEmailToAddressesGenericEvent generic event action
Browse files Browse the repository at this point in the history
It was necessary to change `ScenarioGenericEventInterface->createEvent` method
to `createEvents()` and to change its return value to array
of `EventInterface` objects.

remp/crm#1123
  • Loading branch information
Adam Zoldak committed Mar 18, 2021
1 parent 86fbccf commit e700347
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
96 changes: 96 additions & 0 deletions src/Scenarios/SendNotificationEmailToAddressesGenericEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Crm\RempMailerModule\Scenarios;

use Crm\ApplicationModule\Criteria\ScenarioParams\StringLabeledArrayParam;
use Crm\ApplicationModule\DataRow;
use Crm\PaymentsModule\Repository\PaymentsRepository;
use Crm\RempMailerModule\Repositories\MailTemplatesRepository;
use Crm\ScenariosModule\Events\ScenarioGenericEventInterface;
use Crm\UsersModule\Events\NotificationEvent;
use Crm\UsersModule\Repository\UsersRepository;
use League\Event\Emitter;

class SendNotificationEmailToAddressesGenericEvent implements ScenarioGenericEventInterface
{
private $usersRepository;

private $paymentsRepository;

private $mailTemplatesRepository;

private $emitter;

private $allowedMailTypeCodes = [];

public function __construct(
UsersRepository $usersRepository,
PaymentsRepository $paymentsRepository,
Emitter $emitter,
MailTemplatesRepository $mailTemplatesRepository
) {
$this->emitter = $emitter;
$this->usersRepository = $usersRepository;
$this->paymentsRepository = $paymentsRepository;
$this->mailTemplatesRepository = $mailTemplatesRepository;
}

public function addAllowedMailTypeCodes(string ...$mailTypeCodes): void
{
foreach ($mailTypeCodes as $mailTypeCode) {
$this->allowedMailTypeCodes[$mailTypeCode] = $mailTypeCode;
}
}

public function getLabel(): string
{
return 'Send notification email to addresses';
}

public function getParams(): array
{
$mailTemplates = $this->mailTemplatesRepository->all($this->allowedMailTypeCodes);

$mailTemplateOptions = [];
foreach ($mailTemplates as $mailTemplate) {
$mailTemplateOptions[$mailTemplate->code] = $mailTemplate->name;
}

return [
new StringLabeledArrayParam('email_addresses', 'Email addresses', [], 'and', true),
new StringLabeledArrayParam('email_codes', 'Email codes', $mailTemplateOptions, 'and'),
];
}

public function createEvents($options, $params): array
{
$templateParams = [];

$user = $this->usersRepository->find($params->user_id);
$payment = isset($params->payment_id) ? $this->paymentsRepository->find($params->payment_id) : null;

if ($user) {
$templateParams['user'] = $user->toArray();
}
if ($payment) {
$templateParams['payment'] = $payment->toArray();
}

$events = [];
foreach ($options['email_addresses']->selection as $emailAddress) {
$userRow = new DataRow([
'email' => $emailAddress,
]);

foreach ($options['email_codes']->selection as $emailCode) {
$events[] = new NotificationEvent(
$this->emitter,
$userRow,
$emailCode,
$templateParams
);
}
}
return $events;
}
}
9 changes: 9 additions & 0 deletions src/config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ services:
mailUserSubscriptionsRepository: Crm\RempMailerModule\Repositories\MailUserSubscriptionsRepository
mailTypeCategoriesRepository: Crm\RempMailerModule\Repositories\MailTypeCategoriesRepository
mailLogsRepository: Crm\RempMailerModule\Repositories\MailLogsRepository

sendNotificationEmailGenericEvent:
class: Crm\RempMailerModule\Scenarios\SendNotificationEmailToAddressesGenericEvent
setup:
- addAllowedMailTypeCodes('system', 'system_optional')

scenariosGenericEventsManager:
setup:
- register('send_notification_email_to_addresses', @sendNotificationEmailGenericEvent)

0 comments on commit e700347

Please sign in to comment.