From ff8dff6312053e341b1308da32cd1ac138a2bb7e Mon Sep 17 00:00:00 2001
From: Sergiu-Lucian Petrica <sergiu@die-lobby.de>
Date: Wed, 13 Dec 2017 11:40:03 +0200
Subject: [PATCH] Implemented redirection to form when exception is thrown,
 with a generic error showing to the user + logging error message

---
 Classes/Controller/JoblistController.php      | 21 +++++++++++--------
 Resources/Private/Language/de.locallang.xlf   |  4 ++++
 Resources/Private/Language/locallang.xlf      |  3 +++
 .../Private/Templates/Joblist/ApplyForm.html  |  6 +++++-
 4 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/Classes/Controller/JoblistController.php b/Classes/Controller/JoblistController.php
index aa774101..390c05e3 100644
--- a/Classes/Controller/JoblistController.php
+++ b/Classes/Controller/JoblistController.php
@@ -28,7 +28,7 @@ namespace SGalinski\SgJobs\Controller;
 
 use SGalinski\SgJobs\Domain\Model\JobApplication;
 use SGalinski\SgMail\Service\MailTemplateService;
-use TYPO3\CMS\Core\Messaging\FlashMessage;
+use TYPO3\CMS\Core\Log\LogLevel;
 use TYPO3\CMS\Core\Resource\DuplicationBehavior;
 use TYPO3\CMS\Core\Resource\ResourceFactory;
 use TYPO3\CMS\Core\Utility\File\ExtendedFileUtility;
@@ -92,13 +92,20 @@ class JoblistController extends ActionController {
 	 * Renders the application form with an optional job
 	 *
 	 * @param JobApplication $applyData
+	 * @param string $error
 	 */
-	public function applyFormAction(JobApplication $applyData = NULL) {
+	public function applyFormAction(JobApplication $applyData = NULL, $error = NULL) {
 		if ($this->request->getOriginalRequest()) {
 			$uploadedFiles = $this->request->getOriginalRequest()->getArguments()['uploadedFiles'];
 			$this->view->assign('uploadedFiles', $uploadedFiles);
 		}
 
+		if ($error !== NULL) {
+			$logger = GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager')->getLogger(__CLASS__);
+			$logger->log(LogLevel::ALERT, $error, $applyData);
+			$this->view->assign('error', 1);
+		}
+
 		$jobId = $this->request->getArguments()['jobData']['uid'];
 		if (!empty($jobId)) {
 			$jobData = $this->jobRepository->findByUid($jobId);
@@ -146,11 +153,9 @@ class JoblistController extends ActionController {
 				->setTargetPageUid($redirectPageId)
 				->build();
 			$this->redirectToUri($uri);
-
 		} catch (\Exception $exception) {
 			// possible errors, because of wrong mails (maybe log that somewhere? Does this makes sense?)
-			GeneralUtility::makeInstance(FlashMessage::class, $exception->getMessage());
-			$this->redirect('applyFormAction');
+			$this->redirect('applyForm', NULL, NULL, ['error' => $exception->getMessage()]);
 		}
 	}
 
@@ -178,8 +183,7 @@ class JoblistController extends ActionController {
 			$this->handleFileUpload('certificates', $uniqueFolderName);
 		} catch (\Exception $exception) {
 			// possible errors, because of wrong mails
-			GeneralUtility::makeInstance(FlashMessage::class, $exception->getMessage());
-			$this->redirect('applyFormAction');
+			$this->redirect('applyForm', NULL, NULL, ['error' => $exception->getMessage()]);
 		}
 		$propertyMappingConfiguration = $this->arguments->getArgument('applyData')->getPropertyMappingConfiguration();
 		$propertyMappingConfiguration->forProperty('coverLetter')->allowAllProperties();
@@ -363,8 +367,7 @@ class JoblistController extends ActionController {
 			fputcsv($file, $dataToInsertArr);
 			fclose($file);
 		} catch (\RuntimeException $exception) {
-			GeneralUtility::makeInstance(FlashMessage::class, $exception->getMessage());
-			$this->redirect('applyFormAction');
+			$this->redirect('applyForm', NULL, NULL, ['error' => $exception->getMessage()]);
 		}
 	}
 
diff --git a/Resources/Private/Language/de.locallang.xlf b/Resources/Private/Language/de.locallang.xlf
index 79f2e5e4..2ab24b4c 100644
--- a/Resources/Private/Language/de.locallang.xlf
+++ b/Resources/Private/Language/de.locallang.xlf
@@ -210,6 +210,10 @@
 			<source>Female</source>
 			<target>Weiblich</target>
 		</trans-unit>
+		<trans-unit id="frontend.apply.error.general" approved="yes">
+			<source>An error has occured</source>
+			<target>Ein Fehler ist aufgetreten</target>
+		</trans-unit>
 		<trans-unit id="frontend.applyNow" approved="yes">
 			<source>Apply online now</source>
 			<target>Jetzt online bewerben</target>
diff --git a/Resources/Private/Language/locallang.xlf b/Resources/Private/Language/locallang.xlf
index 126f2fb8..1b95de31 100644
--- a/Resources/Private/Language/locallang.xlf
+++ b/Resources/Private/Language/locallang.xlf
@@ -159,6 +159,9 @@
 		<trans-unit id="frontend.apply.gender.female">
 			<source>Female</source>
 		</trans-unit>
+		<trans-unit id="frontend.apply.error.general">
+			<source>An error has occured</source>
+		</trans-unit>
 		<trans-unit id="frontend.applyNow">
 			<source>Apply online now</source>
 		</trans-unit>
diff --git a/Resources/Private/Templates/Joblist/ApplyForm.html b/Resources/Private/Templates/Joblist/ApplyForm.html
index 0d3eca44..2fabe81b 100644
--- a/Resources/Private/Templates/Joblist/ApplyForm.html
+++ b/Resources/Private/Templates/Joblist/ApplyForm.html
@@ -1,8 +1,12 @@
 <f:layout name="Default" />
 
 <f:section name="main">
-	<f:flashMessages />
 	<f:form action="apply" controller="Joblist" method="post" objectName="applyData" object="{applyData}" enctype="multipart/form-data">
+		<f:if condition="{error}">
+			<div class="errormessage">
+				<f:translate key="frontend.apply.error.general" />
+			</div>
+		</f:if>
 		<f:if condition="{job}">
 			<f:form.hidden value="{job.jobId}" property="jobId" />
 			<label for="apply-title"><f:translate key="frontend.apply.title" /></label>
-- 
GitLab