From 2e16bea0de169cd333b15cfd86d7c0e70b84d7e8 Mon Sep 17 00:00:00 2001
From: Stefan Galinski <stefan@sgalinski.de>
Date: Tue, 2 Jan 2018 15:58:49 +0100
Subject: [PATCH] [FEATURE] Cleanups and add the title / description for each
 single view

---
 Classes/Controller/JoblistController.php | 38 +++++++++++++++++-------
 1 file changed, 28 insertions(+), 10 deletions(-)

diff --git a/Classes/Controller/JoblistController.php b/Classes/Controller/JoblistController.php
index 27619b3b..07f2fc0b 100644
--- a/Classes/Controller/JoblistController.php
+++ b/Classes/Controller/JoblistController.php
@@ -76,7 +76,7 @@ class JoblistController extends ActionController {
 	 * @return void
 	 * @throws \InvalidArgumentException
 	 */
-	public function indexAction(array $filters = [], $jobId = null) {
+	public function indexAction(array $filters = [], $jobId = NULL) {
 		if ($filters) {
 			$this->view->assign('selectedCountry', $filters['filterCountry']);
 			$this->view->assign('selectedCompany', $filters['filterCompany']);
@@ -94,7 +94,16 @@ class JoblistController extends ActionController {
 		$jobLimit = (int) $this->settings['jobLimit'];
 
 		if ($jobId) {
-			$jobs = [$this->jobRepository->findByUid($jobId)];
+			/** @var Job $job */
+			$job = $this->jobRepository->findByUid($jobId);
+			if (!$job) {
+				throw new \InvalidArgumentException('Given Job Id is invalid!');
+			}
+
+			$GLOBALS['TSFE']->page['titlebyextension'] = $job->getTitle();
+			$GLOBALS['TSFE']->page['description'] = substr($job->getDescription(), 0, 200);
+
+			$jobs = [$job];
 			$numberOfPages = 1;
 		} else {
 			// pagination logic
@@ -123,6 +132,7 @@ class JoblistController extends ActionController {
 	 * @param JobApplication $applyData
 	 * @param string $error
 	 * @param int $uid
+	 * @throws \InvalidArgumentException
 	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentNameException
 	 */
 	public function applyFormAction(JobApplication $applyData = NULL, $error = NULL, $uid = NULL) {
@@ -135,19 +145,25 @@ class JoblistController extends ActionController {
 		try {
 			$folderName = $this->request->getArgument('folderName');
 		} catch (\Exception $exception) {
-			// nope
+			// this happens for the initial call, but works for any follow-up call as the form validation
+			// throws you back to this one if something has failed
 		}
+
 		if ($folderName === NULL) {
 			$folderName = md5(uniqid('sgjobs-', TRUE));
 			$this->request->setArgument('folderName', $folderName);
 		}
 		$this->view->assign('folderName', $folderName);
 
-		$jobData = NULL;
+		$job = NULL;
 		if (!$uid !== NULL) {
-			/** @var Job $jobData */
-			$jobData = $this->jobRepository->findByUid($uid);
-			$this->view->assign('job', $jobData);
+			/** @var Job $job */
+			$job = $this->jobRepository->findByUid($uid);
+			if ($job) {
+				$GLOBALS['TSFE']->page['titlebyextension'] = $job->getTitle();
+				$GLOBALS['TSFE']->page['description'] = substr($job->getDescription(), 0, 200);
+			}
+			$this->view->assign('job', $job);
 		}
 
 		// display country options
@@ -164,8 +180,8 @@ class JoblistController extends ActionController {
 		if ($applyData === NULL) {
 			/** @noinspection CallableParameterUseCaseInTypeContextInspection */
 			$applyData = $this->objectManager->get(JobApplication::class);
-			if ($jobData) {
-				$applyData->setJobId($jobData->getJobId());
+			if ($job) {
+				$applyData->setJobId($job->getJobId());
 			}
 		}
 
@@ -238,8 +254,11 @@ class JoblistController extends ActionController {
 	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
 	 * @throws \InvalidArgumentException
 	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentNameException
+	 * @throws NoSuchArgumentException
 	 */
 	public function applyAction(JobApplication $applyData) {
+		$folderName = $this->request->getArgument('folderName');
+
 		try {
 			$applyData->setPid($GLOBALS['TSFE']->id);
 
@@ -258,7 +277,6 @@ class JoblistController extends ActionController {
 				$this->jobApplicationRepository->update($applyData);
 			}
 
-			$folderName = $this->request->getArgument('folderName');
 			$this->submitApplicationFiles($applyData, $folderName);
 
 			/** @noinspection PhpMethodParametersCountMismatchInspection */
-- 
GitLab