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

Optimize error and redirect handling #14

Merged
merged 5 commits into from
Aug 28, 2024
Merged
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
86 changes: 64 additions & 22 deletions Classes/Controller/JobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
use FGTCLB\AcademicJobs\Event\AfterSaveJobEvent;
use FGTCLB\AcademicJobs\Property\TypeConverter\JobAvatarImageUploadConverter;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Backend\Routing\UriBuilder as BackendUriBuilder;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;
use TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter;
use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
use TYPO3\CMS\Extbase\Service\ImageService;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Backend\Routing\UriBuilder as BackendUriBuilder;
use TYPO3\CMS\Core\Messaging\FlashMessage;

class JobController extends ActionController
{
Expand All @@ -38,8 +38,18 @@ public function indexAction(): ResponseInterface
return $this->htmlResponse();
}

public function showAction(Job $job): ResponseInterface
public function showAction(?Job $job = null): ResponseInterface
{
if ($job === null) {
$this->addFlashMessage(
$this->translateAlert('job_not_found.body', 'Job not found.'),
null,
FlashMessage::ERROR,
true
);
return $this->htmlResponse();
}

$title = $job->getTitle();
$description = strip_tags($job->getDescription());
$image = $this->getImageUri($job->getImage());
Expand Down Expand Up @@ -162,28 +172,52 @@ public function saveJobAction(Job $job): void
$this->jobRepository->add($job);
$this->persistenceManager->persistAll();

$successMessageTitle = LocalizationUtility::translate('tx_academicjobs.fe.alert.job_created.title', 'AcademicJobs');
$successMessageBody = LocalizationUtility::translate('tx_academicjobs.fe.alert.job_created.body', 'AcademicJobs');
$this->addFlashMessage(
(string)$successMessageBody,
(string)$successMessageTitle,
FlashMessage::OK,
true
);
$uid = $job->getUid();

if ($uid === null) {
$this->addFlashMessage(
$this->translateAlert('job_not_created.body', 'Something went wrong.'),
$this->translateAlert('job_not_created.title', 'Job not created'),
FlashMessage::ERROR,
true
);
$this->redirect('newJobForm');
}

$afterSaveJobEvent = new AfterSaveJobEvent($job);
$this->eventDispatcher->dispatch($afterSaveJobEvent);

$uid = $job->getUid();

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

// We only need to create messages if we stay on the same page
if ($listPid === null) {
if ($mailWasSent) {
$this->addFlashMessage(
$this->translateAlert('job_created.body', 'Job created and email sent.'),
$this->translateAlert('job_created.title', 'Job created'),
FlashMessage::OK,
true
);
} else {
$this->addFlashMessage(
$this->translateAlert('job_created_no_email.body', 'Job created, but email could not be sent.'),
$this->translateAlert('job_created_no_email.title', 'Job created'),
FlashMessage::WARNING,
true
);
}
}

$this->redirect('list');
if ($listPid !== null) {
$uri = $this->uriBuilder->setTargetPageUid($listPid)->build();
$this->redirectToUri($uri);
} else {
$this->redirect('list');
}
}

public function sendEmail(int $recordId): void
public function sendEmail(int $recordId): bool
{
$url = $this->buildUrl($recordId);

Expand All @@ -192,7 +226,8 @@ public function sendEmail(int $recordId): void
$mail->from($this->settings['email']['senderEmail']);
$mail->subject($this->settings['email']['subject']);
$mail->text('A new job has been posted. Please check the TYPO3 backend: ' . $url);
$mail->send();

return $mail->send();
}

public function buildUrl(int $recordId): string
Expand All @@ -203,13 +238,20 @@ public function buildUrl(int $recordId): string
[
'edit' => [
'tx_academicjobs_domain_model_job' => [
$recordId => 'edit'
]
$recordId => 'edit',
],
],
'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI'),
]
);

return GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST') . $path;
}

private function translateAlert(
string $alert,
string $missing = 'Missing translation!'
): string {
return LocalizationUtility::translate('tx_academicjobs.fe.alert.' . $alert, 'AcademicJobs') ?? $missing;
}
}
8 changes: 4 additions & 4 deletions Classes/Domain/Model/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
namespace FGTCLB\AcademicJobs\Domain\Model;

use TYPO3\CMS\Extbase\Annotation\ORM\Cascade;
use TYPO3\CMS\Extbase\Annotation\ORM\Lazy;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;

class Job extends AbstractEntity
{
Expand Down Expand Up @@ -217,11 +215,13 @@ public function setType(int $type): void
$this->type = $type;
}

public function getContact(): ?Contact {
public function getContact(): ?Contact
{
return $this->contact;
}

public function setContact(Contact $contact): void {
public function setContact(Contact $contact): void
{
$this->contact = $contact;
}

Expand Down
5 changes: 5 additions & 0 deletions Configuration/TypoScript/constants.typoscript
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ plugin.tx_academicjobs {
# cat=plugin.tx_academicjobs_createjob/file; type=string; label=Path to template layouts (FE)
layoutRootPath = EXT:academic_jobs/Resources/Private/Layouts/
}

persistence {
# cat=plugin.tx_academicjobs_createjob//a; type=string; label=Default storage PID
storagePid =
}

# cat=plugin.tx_academicjobs_createjob//a; type=string; label=Default detail page PID
detailPid =
# cat=plugin.tx_academicjobs_createjob//a; type=string; label=Default list page PID
listPid =

email {
from =
recipientEmail =
Expand Down
1 change: 1 addition & 0 deletions Configuration/TypoScript/setup.typoscript
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ plugin.tx_academicjobs {
}
settings {
detailPid = {$plugin.tx_academicjobs.detailPid}
listPid = {$plugin.tx_academicjobs.listPid}

email {
recipientEmail = {$plugin.tx_academicjobs.email.recipientEmail}
Expand Down
Loading
Loading