Skip to content

Commit

Permalink
[FEATURE] Allow attachments in custom notifications via TypoScript
Browse files Browse the repository at this point in the history
The existing possibility to add attachment to emails sent using the
extension has been extended, so attachments configured in TypoScript
now also can be applied to custom notifications. The configuration
options are the same as for emails sent by the registration process.

Example:

```
customNotifications {
    thanksForParticipation {
        title = Thank you message
        template = ThanksForParticipation.html
        subject = Thank you for participation in event "{event.title}"
        attachments {
          user {
            fromFiles {
              1 = fileadmin/test.pdf
            }
            fromEventProperty {
              1 = files
              2 = image
            }
            iCalFile = 1
          }
        }
    }
}
```
  • Loading branch information
derhansen committed Dec 7, 2023
1 parent 4f54239 commit e4837c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
34 changes: 25 additions & 9 deletions Classes/Service/Notification/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace DERHANSEN\SfEventMgt\Service\Notification;

use DERHANSEN\SfEventMgt\Domain\Model\Dto\CustomNotification;
use DERHANSEN\SfEventMgt\Domain\Model\Registration;
use DERHANSEN\SfEventMgt\Service\ICalendarService;
use DERHANSEN\SfEventMgt\Utility\MessageType;
Expand Down Expand Up @@ -62,26 +63,31 @@ public function getAttachments(
array $settings,
Registration $registration,
int $messageType,
string $messageRecipient
string $messageRecipient,
?CustomNotification $customNotification = null
): array {
$attachments = [];
$settingPath = $this->getSettingsPath($messageType);
$attachmentSettings = $settings['notification'][$settingPath]['attachments'][$messageRecipient] ?? [];

if (isset($settings['notification'][$settingPath]['attachments'][$messageRecipient])) {
if ($customNotification) {
$attachmentSettings = $settings['notification']['customNotifications'][$customNotification->getTemplate()]['attachments'][$messageRecipient] ?? [];
}

if (!empty($attachmentSettings)) {
// Attachments globally from TypoScript
$config = $settings['notification'][$settingPath]['attachments'][$messageRecipient];
$attachments = $this->getFileAttachments($config);
$attachments = $this->getFileAttachments($attachmentSettings);

// Attachments from Event properties
$eventAttachments = $this->getObjectAttachments(
$config['fromEventProperty'] ?? [],
$attachmentSettings['fromEventProperty'] ?? [],
$registration->getEvent()
);
$attachments = array_merge($attachments, $eventAttachments);

// Attachments from Registration properties
$registrationAttachments = $this->getObjectAttachments(
$config['fromRegistrationProperty'] ?? [],
$attachmentSettings['fromRegistrationProperty'] ?? [],
$registration
);
$attachments = array_merge($attachments, $registrationAttachments);
Expand All @@ -108,13 +114,19 @@ public function getICalAttachment(
array $settings,
Registration $registration,
int $messageType,
string $messageRecipient
string $messageRecipient,
?CustomNotification $customNotification = null
): string {
$file = '';
$settingPath = $this->getSettingsPath($messageType);

if (isset($settings['notification'][$settingPath]['attachments'][$messageRecipient]['iCalFile']) &&
(bool)$settings['notification'][$settingPath]['attachments'][$messageRecipient]['iCalFile']) {
$attachICalFile = (bool)($settings['notification'][$settingPath]['attachments'][$messageRecipient]['iCalFile'] ?? false);

if ($customNotification) {
$attachICalFile = (bool)($settings['notification']['customNotifications'][$customNotification->getTemplate()]['attachments'][$messageRecipient]['iCalFile'] ?? false);
}

if ($attachICalFile) {
$file = GeneralUtility::tempnam(
'event-' . $registration->getEvent()->getUid() . '-',
'.ics'
Expand Down Expand Up @@ -188,6 +200,10 @@ protected function getObjectAttachments(array $propertyNames, AbstractEntity $ob
protected function getAttachmentsFromProperty(AbstractEntity $object, string $propertyName): array
{
$attachments = [];
if ($propertyName === '') {
return $attachments;
}

$property = $object->_getProperty($propertyName);

if ($property instanceof ObjectStorage) {
Expand Down
6 changes: 4 additions & 2 deletions Classes/Service/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,17 @@ public function sendUserMessage(
$settings,
$registration,
$type,
MessageRecipient::USER
MessageRecipient::USER,
$customNotification
);

// Get iCal attachment if configured
$iCalAttachment = $this->attachmentService->getICalAttachment(
$settings,
$registration,
$type,
MessageRecipient::USER
MessageRecipient::USER,
$customNotification
);

if ($iCalAttachment !== '') {
Expand Down

0 comments on commit e4837c5

Please sign in to comment.