From 294f23f09c07cd579e391402f5c095acb97cebcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BCrk?= Date: Tue, 18 Feb 2025 13:57:06 +0100 Subject: [PATCH] [TASK] Extend `AfterSaveJobEvent` to make mail sending controllable The provided `JobController` already dispatches the PSR-14 `AfterSaveJobEvent` in the `saveJobAction()` as notification event. The aforementioned event is extended and ... * Adds the current plugin settings to allow event listeners to process additional tasks based on replaced or extended plugin settings (pi_flexform) in projects, which are not egitbale as generic solutiion or quick project related implementation. * Adds `listPid` as direct option to be read or set and allow to base the further handling (redirect) more dynamically on project needs, implementing following method signatures: - `setListPid(?int $listPid): self` - `getListPid(): ?int` * Adds the current request object to the event to mitigate the need to fetch it from `$GLOBALS['TYPO3_REQUEST`] and missing extbase/current state anyway and follow TYPO3 recommendation and practice to pass it down. --- Classes/Controller/JobController.php | 14 +++-- Classes/Event/AfterSaveJobEvent.php | 81 ++++++++++++++++++++++++++-- 2 files changed, 86 insertions(+), 9 deletions(-) diff --git a/Classes/Controller/JobController.php b/Classes/Controller/JobController.php index 42772e4..142662a 100644 --- a/Classes/Controller/JobController.php +++ b/Classes/Controller/JobController.php @@ -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.'), diff --git a/Classes/Event/AfterSaveJobEvent.php b/Classes/Event/AfterSaveJobEvent.php index 5716e55..82ca293 100644 --- a/Classes/Event/AfterSaveJobEvent.php +++ b/Classes/Event/AfterSaveJobEvent.php @@ -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 $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 + */ + 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; + } }