From d7fb57e964a25a43e3a25aef795aefc9f3a86f49 Mon Sep 17 00:00:00 2001
From: Stefan Galinski <stefan@sgalinski.de>
Date: Wed, 13 Dec 2017 23:57:42 +0100
Subject: [PATCH] [FEATURE] The whole foo should work now (let's start the fine
 tuning)

---
 Classes/Controller/JoblistController.php      | 30 +++++++++----------
 Classes/Domain/Model/JobApplication.php       |  3 +-
 Configuration/TypoScript/Common/setup.txt     | 11 +++++++
 .../Private/Templates/Joblist/ApplyForm.html  | 23 ++++++++++++--
 .../SgMail/ApplicationMail/template.html      |  2 +-
 ext_localconf.php                             |  7 ++++-
 6 files changed, 55 insertions(+), 21 deletions(-)
 create mode 100644 Configuration/TypoScript/Common/setup.txt

diff --git a/Classes/Controller/JoblistController.php b/Classes/Controller/JoblistController.php
index 09652a84..4bbd2975 100644
--- a/Classes/Controller/JoblistController.php
+++ b/Classes/Controller/JoblistController.php
@@ -107,11 +107,13 @@ class JoblistController extends ActionController {
 	/**
 	 * Renders the application form with an optional job
 	 *
-	 * @param JobApplication|NULL $applyData
-	 * @param string|NULL $error
-	 * @param string|NULL $folderName
+	 * @param JobApplication $applyData
+	 * @param string $error
+	 * @param string $folderName
+	 * @param int $uid
+	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
 	 */
-	public function applyFormAction(JobApplication $applyData = NULL, $error = NULL, $folderName = NULL) {
+	public function applyFormAction(JobApplication $applyData = NULL, $error = NULL, $folderName = NULL, $uid = NULL) {
 		if ($error !== NULL && $error !== '') {
 			$this->view->assign('internalError', $error);
 		}
@@ -121,11 +123,10 @@ class JoblistController extends ActionController {
 		}
 		$this->view->assign('folderName', $folderName);
 
-		$jobId = $this->request->getArguments()['uid'];
 		$jobData = NULL;
-		if (!empty($jobId)) {
+		if (!$uid !== NULL) {
 			/** @var Job $jobData */
-			$jobData = $this->jobRepository->findByUid($jobId);
+			$jobData = $this->jobRepository->findByUid($uid);
 			$this->view->assign('job', $jobData);
 		}
 
@@ -195,7 +196,7 @@ class JoblistController extends ActionController {
 		$resourceFactory = $this->objectManager->get(ResourceFactory::class);
 		$newName = date('Ymd-His') . '_' . $applicationData->getFirstName();
 		$storage = $resourceFactory->getStorageObject(1);
-		$applicationFilePath = str_replace('/', '', $storage->getConfiguration()['basePath']) . $folderName .
+		$applicationFilePath = PATH_site . $storage->getConfiguration()['basePath'] . 'JobApplication/' . $folderName .
 			'/' . $newName . '.csv';
 		$this->writeApplicationFile($applicationData, $applicationFilePath);
 	}
@@ -244,14 +245,13 @@ class JoblistController extends ActionController {
 			
 			$mailService->sendEmail();
 
-			// redirect to the given page id from the flexform
-			$redirectPageId = (int) $this->configurationManager->getConfiguration(
-				ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK
-			)['settings']['redirectPage'];
+			$redirectPageUid = (int) $this->settings['redirectPage'];
+			if ($redirectPageUid) {
+				$url = $this->configurationManager->getContentObject()->getTypoLink_URL($redirectPageUid);
+				$this->redirectToUri($url);
+			}
 
-			$uriBuilder = $this->uriBuilder;
-			$uri = $uriBuilder->reset()->setTargetPageUid($redirectPageId)->build();
-			$this->redirectToUri($uri);
+			$this->redirect('applyForm');
 
 		} catch (\Exception $exception) {
 			$this->forward('applyForm', NULL, NULL, ['applyData' => $applyData, 'error' => $exception->getMessage()]);
diff --git a/Classes/Domain/Model/JobApplication.php b/Classes/Domain/Model/JobApplication.php
index 85180460..a4a624bd 100644
--- a/Classes/Domain/Model/JobApplication.php
+++ b/Classes/Domain/Model/JobApplication.php
@@ -37,7 +37,6 @@ class JobApplication extends AbstractEntity {
 	 * An internal id for external purposes
 	 *
 	 * @var string $jobId
-	 * @validate NotEmpty
 	 */
 	protected $jobId = '';
 
@@ -45,7 +44,7 @@ class JobApplication extends AbstractEntity {
 	 * @var string $gender
 	 * @validate NotEmpty
 	 */
-	protected $gender = '';
+	protected $gender = 0;
 
 	/**
 	 * @var string $firstName
diff --git a/Configuration/TypoScript/Common/setup.txt b/Configuration/TypoScript/Common/setup.txt
new file mode 100644
index 00000000..dbe4f434
--- /dev/null
+++ b/Configuration/TypoScript/Common/setup.txt
@@ -0,0 +1,11 @@
+config.tx_extbase {
+	persistence {
+		classes {
+			SGalinski\SgJobs\Domain\Model\JobApplication {
+				mapping {
+					tableName = tx_sgjobs_domain_model_job_application
+				}
+			}
+		}
+	}
+}
diff --git a/Resources/Private/Templates/Joblist/ApplyForm.html b/Resources/Private/Templates/Joblist/ApplyForm.html
index 098f06f5..a92f9324 100644
--- a/Resources/Private/Templates/Joblist/ApplyForm.html
+++ b/Resources/Private/Templates/Joblist/ApplyForm.html
@@ -4,7 +4,25 @@
 
 <f:section name="main">
 	<f:form action="apply" controller="Joblist" method="post" name="applyData" object="{applyData}" enctype="multipart/form-data">
-		<f:form.hidden name="folderName" value="{folderName}" />
+		<input type="hidden" name="tx_sgjobs_jobapplication[folderName]" value="{folderName}" />
+
+		<f:comment><!-- To output all validation errors
+		<f:form.validationResults>
+		  <f:if condition="{validationResults.flattenedErrors}">
+		    <ul class="errors">
+		      <f:for each="{validationResults.flattenedErrors}" as="errors" key="propertyPath">
+		        <li>{propertyPath}
+		          <ul>
+		          <f:for each="{errors}" as="error">
+		            <li>{error.code}: {error}</li>
+		          </f:for>
+		          </ul>
+		        </li>
+		      </f:for>
+		    </ul>
+		  </f:if>
+		</f:form.validationResults>
+		--></f:comment>
 
 		<f:if condition="{internalError}">
 			<div class="errormessage">
@@ -14,7 +32,8 @@
 
 		<f:if condition="{job}">
 			<p>
-				<f:form.hidden value="{applyData.jobId}" property="jobId" />
+				<input type="hidden" name="tx_sgjobs_jobapplication[applyData][jobId]" value="{job.jobId}" />
+				<input type="hidden" name="tx_sgjobs_jobapplication[applyData][jobTitle]" value="{job.title}" />
 				<label for="apply-title"><f:translate key="frontend.apply.title" /></label>
 				<span id="apply-title">{job.title}</span>
 			</p>
diff --git a/Resources/Private/Templates/SgMail/ApplicationMail/template.html b/Resources/Private/Templates/SgMail/ApplicationMail/template.html
index bfdad9ec..e1133597 100644
--- a/Resources/Private/Templates/SgMail/ApplicationMail/template.html
+++ b/Resources/Private/Templates/SgMail/ApplicationMail/template.html
@@ -20,4 +20,4 @@ Mobil: {application.mobile}
 
 E-Mail Adresse: {application.email}
 
-Nachricht: {application.message}
\ No newline at end of file
+Nachricht: {application.message}
diff --git a/ext_localconf.php b/ext_localconf.php
index b2b1f99c..adc9537d 100644
--- a/ext_localconf.php
+++ b/ext_localconf.php
@@ -1,5 +1,11 @@
 <?php
 
+$extPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('sg_jobs');
+
+// common typoscript configuration
+$tsPath = $extPath . 'Configuration/TypoScript/Common/';
+\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTypoScriptSetup(file_get_contents($tsPath . 'setup.txt'));
+
 // Configure frontend plugins
 \TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
 	'SGalinski.sg_jobs',
@@ -34,7 +40,6 @@
 );
 
 if (TYPO3_MODE === 'BE') {
-	$extPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('sg_jobs');
 	$tsPath = $extPath . 'Configuration/TypoScript/Backend/';
 
 	\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTypoScriptConstants(
-- 
GitLab