Skip to content

Commit

Permalink
[FEATURE] Added ModifyDownloadRegistrationCsvEvent PSR-14 event
Browse files Browse the repository at this point in the history
Refs #1164
  • Loading branch information
derhansen committed Sep 17, 2023
1 parent 6c8455c commit 5bf4cd5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
57 changes: 57 additions & 0 deletions Classes/Event/ModifyDownloadRegistrationCsvEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace DERHANSEN\SfEventMgt\Event;

/**
* This event is triggered before the registration CSV download is initiated. Listerners can use this event
* to set the CSV content and the CSV filename
*/
final class ModifyDownloadRegistrationCsvEvent
{
public function __construct(
protected string $csvContent,
protected string $downloadFilename,
protected readonly int $eventUid,
protected readonly array $settings
) {
}

public function getCsvContent(): string
{
return $this->csvContent;
}

public function setCsvContent(string $csvContent): void
{
$this->csvContent = $csvContent;
}

public function getDownloadFilename(): string
{
return $this->downloadFilename;
}

public function setDownloadFilename(string $downloadFilename): void
{
$this->downloadFilename = $downloadFilename;
}

public function getEventUid(): int
{
return $this->eventUid;
}

public function getSettings(): array
{
return $this->settings;
}
}
27 changes: 22 additions & 5 deletions Classes/Service/ExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,44 @@
use DERHANSEN\SfEventMgt\Domain\Model\Registration;
use DERHANSEN\SfEventMgt\Domain\Repository\EventRepository;
use DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository;
use DERHANSEN\SfEventMgt\Event\ModifyDownloadRegistrationCsvEvent;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Utility\CsvUtility;
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;

class ExportService
{
protected RegistrationRepository $registrationRepository;
protected EventRepository $eventRepository;
protected EventDispatcherInterface $eventDispatcher;

public function __construct(RegistrationRepository $registrationRepository, EventRepository $eventRepository)
{
public function __construct(
RegistrationRepository $registrationRepository,
EventRepository $eventRepository,
EventDispatcherInterface $eventDispatcher
) {
$this->registrationRepository = $registrationRepository;
$this->eventRepository = $eventRepository;
$this->eventDispatcher = $eventDispatcher;
}

/**
* Initiates the CSV downloads for registrations of the given event uid
*/
public function downloadRegistrationsCsv(int $eventUid, array $settings = []): void
{
$content = $this->exportRegistrationsCsv($eventUid, $settings);
header('Content-Disposition: attachment; filename="event_' . $eventUid . '_reg_' . date('dmY_His') . '.csv"');
$modifyDownloadRegistrationCsvEvent = new ModifyDownloadRegistrationCsvEvent(
$this->exportRegistrationsCsv($eventUid, $settings),
'event_' . $eventUid . '_reg_' . date('dmY_His') . '.csv',
$eventUid,
$settings
);
$this->eventDispatcher->dispatch($modifyDownloadRegistrationCsvEvent);

$content = $modifyDownloadRegistrationCsvEvent->getCsvContent();
$filename = $modifyDownloadRegistrationCsvEvent->getDownloadFilename();

header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: text/csv');
header('Content-Length: ' . strlen($content));
header('Expires: 0');
Expand All @@ -54,7 +71,7 @@ public function exportRegistrationsCsv(int $eventUid, array $settings = []): str
$registrationFieldData = [];
$fieldsArray = array_map('trim', explode(',', ($settings['fields'] ?? '')));

if (in_array('registration_fields', $fieldsArray)) {
if (in_array('registration_fields', $fieldsArray, true)) {
$hasRegistrationFields = true;
$registrationFieldData = $this->getRegistrationFieldData($eventUid);
$fieldsArray = array_diff($fieldsArray, ['registration_fields']);
Expand Down
4 changes: 4 additions & 0 deletions Documentation/ForDevelopers/Events/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,7 @@ Notification Service
Registration Service
--------------------
* :php:`AfterRegistrationMovedFromWaitlist`

Export Service
--------------
* :php:`ModifyDownloadRegistrationCsvEvent`
5 changes: 4 additions & 1 deletion Tests/Unit/Service/ExportServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use DERHANSEN\SfEventMgt\Domain\Repository\EventRepository;
use DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository;
use DERHANSEN\SfEventMgt\Service\ExportService;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

Expand Down Expand Up @@ -127,7 +128,9 @@ public function exportServiceWorksWithDifferentFormattedTypoScriptValues(
);

$eventRepository = $this->getMockBuilder(EventRepository::class)->disableOriginalConstructor()->getMock();
$exportService = new ExportService($registrationRepository, $eventRepository);
$eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)
->disableOriginalConstructor()->getMock();
$exportService = new ExportService($registrationRepository, $eventRepository, $eventDispatcher);

$returnValue = $exportService->exportRegistrationsCsv($uid, $fields);
self::assertSame($expected, $returnValue);
Expand Down

0 comments on commit 5bf4cd5

Please sign in to comment.