Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Extend AfterSaveJobEvent to make mail sending controllable #17

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions Classes/Controller/JobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,18 @@ public function saveJobAction(Job $job): void
$this->redirect('newJobForm');
}

$afterSaveJobEvent = new AfterSaveJobEvent($job);
$this->eventDispatcher->dispatch($afterSaveJobEvent);
$afterSaveJobEvent = new AfterSaveJobEvent($this->request, $job, $this->settings);
$shouldSendMail = $this->eventDispatcher->dispatch($afterSaveJobEvent)->getSendMail();

$listPid = $this->settings['listPid'] ? (int)$this->settings['listPid'] : null;
$mailWasSent = $this->sendEmail($uid);
$mailWasSent = false;
if ($shouldSendMail) {
$mailWasSent = $this->sendEmail($uid);
}

// We only need to create messages if we stay on the same page
if ($listPid === null) {
// We only need to create messages if we stay on the same page and mail should have been sent.
// @todo This does not make sense in all projects.
if ($shouldSendMail && $listPid === null) {
if ($mailWasSent) {
$this->addFlashMessage(
$this->translateAlert('job_created.body', 'Job created and email sent.'),
Expand Down
81 changes: 77 additions & 4 deletions Classes/Event/AfterSaveJobEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,92 @@

namespace FGTCLB\AcademicJobs\Event;

use FGTCLB\AcademicJobs\Controller\JobController;
use FGTCLB\AcademicJobs\Domain\Model\Job;
use Psr\Http\Message\ServerRequestInterface;

/**
* Event is fired after {@see JobController::newJobFormAction()} job form has been
* processed and persisted in {@see JobController::saveJobAction()} and can be used
* as pure notification to process additional tasks or to control if the default
* job saved mail should be sent or not {@see self::setSendMail()}.
*/
final class AfterSaveJobEvent
{
private Job $job;
private bool $sendMail = true;

public function __construct(Job $job)
{
$this->job = $job;
/**
* @param array<string, mixed> $pluginSettings
*/
public function __construct(
private readonly ServerRequestInterface $request,
private readonly Job $job,
private readonly array $pluginSettings = [],
private ?int $listPid = null,
) {
}

public function getJob(): Job
{
return $this->job;
}

/**
* Current request, extbase request in TYPO3 v11 (decorated)
* and the enriched direct request object in TYPO3 v12 and
* newer. Consuming code needs to take care of this on their
* own.
*
* @return ServerRequestInterface
*/
public function getRequest(): ServerRequestInterface
{
return $this->request;
}

/**
* Return the plugin settings (pi_flexform, ...) as array.
*
* @return array<string, mixed>
*/
public function getPluginSettings(): array
{
return $this->pluginSettings;
}

/**
* Short circuit access to listPid plugin settings.
*/
public function getListPid(): ?int
{
return $this->listPid;
}

/**
* Set pageId of page to redirect (list page).
*
* Can be set to `null` to redirect to form page.
*/
public function setListPid(?int $listPid): self
{
$this->listPid = $listPid;
return $this;
}

/**
* States if default job saved email should be sent or not.
*/
public function getSendMail(): bool
{
return $this->sendMail;
}

/**
* Set if default job saved email should be sent or not.
*/
public function setSendMail(bool $sendMail): AfterSaveJobEvent
{
$this->sendMail = $sendMail;
return $this;
}
}