From 22dc81fef43e9912616edce0f052fee22a01bfde Mon Sep 17 00:00:00 2001
From: Stefan Galinski <stefan@sgalinski.de>
Date: Thu, 14 Dec 2017 00:52:32 +0100
Subject: [PATCH] [BUGFIX] Fix some issues with the validation

---
 Classes/Controller/JoblistController.php         | 16 +++++++++++++---
 Classes/Domain/Model/JobApplication.php          |  6 +++---
 .../UploadedFileReferenceConverter.php           | 10 ++++++++--
 .../Private/Templates/Joblist/ApplyForm.html     |  7 +++----
 4 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/Classes/Controller/JoblistController.php b/Classes/Controller/JoblistController.php
index 3a0fd069..d9eae01b 100644
--- a/Classes/Controller/JoblistController.php
+++ b/Classes/Controller/JoblistController.php
@@ -109,16 +109,24 @@ class JoblistController extends ActionController {
 	 *
 	 * @param JobApplication $applyData
 	 * @param string $error
-	 * @param string $folderName
 	 * @param int $uid
+	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentNameException
 	 */
-	public function applyFormAction(JobApplication $applyData = NULL, $error = NULL, $folderName = NULL, $uid = NULL) {
+	public function applyFormAction(JobApplication $applyData = NULL, $error = NULL, $uid = NULL) {
 		if ($error !== NULL && $error !== '') {
 			$this->view->assign('internalError', $error);
+			$this->request->setArgument('error', NULL);
 		}
 
+		$folderName = NULL;
+		try {
+			$folderName = $this->request->getArgument('folderName');
+		} catch (\Exception $exception) {
+			// nope
+		}
 		if ($folderName === NULL) {
 			$folderName = md5(uniqid('sgjobs-', TRUE));
+			$this->request->setArgument('folderName', $folderName);
 		}
 		$this->view->assign('folderName', $folderName);
 
@@ -157,13 +165,15 @@ class JoblistController extends ActionController {
 	 * @return void
 	 * @throws NoSuchArgumentException
 	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
+	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
 	 */
 	protected function initializeApplyAction() {
 		try {
 			$uniqueFolderName = $this->request->getArgument('folderName');
 		} catch (NoSuchArgumentException $exception) {
 			$exceptionMessage = 'Eine Datei konnte nicht hochgeladen werden. Ist diese eventuell zu groß?';
-			$this->forward('applyForm', NULL, NULL, ['error' => $exceptionMessage]);
+			$this->redirect('applyForm', NULL, NULL, ['error' => $exceptionMessage]);
+			exit;
 		}
 		$propertyMappingConfiguration = $this->arguments->getArgument('applyData')->getPropertyMappingConfiguration();
 
diff --git a/Classes/Domain/Model/JobApplication.php b/Classes/Domain/Model/JobApplication.php
index 08d31bc3..73872e87 100644
--- a/Classes/Domain/Model/JobApplication.php
+++ b/Classes/Domain/Model/JobApplication.php
@@ -125,19 +125,19 @@ class JobApplication extends AbstractEntity {
 
 	/**
 	 * @var \SGalinski\SgJobs\Domain\Model\FileReference $coverLetter
-	 *
+	 * @validate NotEmpty
 	 */
 	protected $coverLetter;
 
 	/**
 	 * @var \SGalinski\SgJobs\Domain\Model\FileReference $cv
-	 *
+	 * @validate NotEmpty
 	 */
 	protected $cv;
 
 	/**
 	 * @var \SGalinski\SgJobs\Domain\Model\FileReference $certificate
-	 *
+	 * @validate NotEmpty
 	 */
 	protected $certificate;
 
diff --git a/Classes/Property/TypeConverter/UploadedFileReferenceConverter.php b/Classes/Property/TypeConverter/UploadedFileReferenceConverter.php
index fe99a5be..d727da82 100644
--- a/Classes/Property/TypeConverter/UploadedFileReferenceConverter.php
+++ b/Classes/Property/TypeConverter/UploadedFileReferenceConverter.php
@@ -328,7 +328,7 @@ class UploadedFileReferenceConverter implements TypeConverterInterface {
 	 * Import a resource and respect configuration given for properties
 	 *
 	 * @param array $uploadInfo
-	 * @return \SGalinski\SgJobs\Domain\Model\FileReference
+	 * @return \SGalinski\SgJobs\Domain\Model\FileReference|NULL
 	 * @throws \InvalidArgumentException
 	 * @throws TypeConverterException
 	 * @throws \Exception
@@ -336,7 +336,8 @@ class UploadedFileReferenceConverter implements TypeConverterInterface {
 	 * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException
 	 * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException
 	 */
-	protected function importUploadedResource(array $uploadInfo): FileReference {
+	protected function importUploadedResource(array $uploadInfo) {
+		$uploadedFile = NULL;
 		$filePathInfo = PathUtility::pathinfo($uploadInfo['name']);
 		$fileName = $this->getTargetUploadFileName();
 		$finalFileName = $fileName . '.' . strtolower($filePathInfo['extension']);
@@ -372,6 +373,7 @@ class UploadedFileReferenceConverter implements TypeConverterInterface {
 			$uploadFolderId = $this->getUploadFolder();
 			// Security protection to not allow manipulations outside of this specific folder
 			if (strpos($uploadFolderId, '/JobApplication/') !== FALSE) {
+				debug($uploadFolderId . '/' . $finalFileName);
 				try {
 					$uploadedFile = $this->resourceFactory->retrieveFileOrFolderObject(
 						$uploadFolderId . '/' . $finalFileName
@@ -385,6 +387,10 @@ class UploadedFileReferenceConverter implements TypeConverterInterface {
 			}
 		}
 
+		if (!$uploadedFile) {
+			return NULL;
+		}
+
 		return $this->createFileReferenceFromFalFileObject($uploadedFile);
 	}
 
diff --git a/Resources/Private/Templates/Joblist/ApplyForm.html b/Resources/Private/Templates/Joblist/ApplyForm.html
index b727984e..02263c2e 100644
--- a/Resources/Private/Templates/Joblist/ApplyForm.html
+++ b/Resources/Private/Templates/Joblist/ApplyForm.html
@@ -31,12 +31,11 @@
 		</f:if>
 
 		<f:if condition="{job}">
-			<p>
+			<div>
 				<f:form.hidden name="jobId" value="{job.jobId}" />
 				<f:form.hidden property="jobTitle" value="{job.title}" />
-				<label for="apply-title"><f:translate key="frontend.apply.title" /></label>
-				<span id="apply-title">{job.title}</span>
-			</p>
+				<h1>Bewerbung für <span id="apply-title">{job.title}</span></h1>
+			</div>
 		</f:if>
 
 		<p>
-- 
GitLab