From 3beef7caf1ccf81f491ac925332caf6df0c9688e Mon Sep 17 00:00:00 2001
From: Torsten Oppermann <torsten@sgalinski.de>
Date: Thu, 16 Nov 2017 14:11:04 +0100
Subject: [PATCH] [TASK] Sgmail integration

---
 Classes/Controller/JoblistController.php      | 54 ++++++++++++++++---
 .../MailTemplates/ApplicationMail.php         |  6 +++
 Resources/Private/Language/de.locallang.xlf   |  8 +++
 Resources/Private/Language/locallang.xlf      |  6 +++
 Resources/Private/Partials/Job.html           |  1 +
 .../Private/Templates/Joblist/Apply.html      |  4 +-
 .../Private/Templates/Joblist/ApplyForm.html  |  2 +-
 7 files changed, 73 insertions(+), 8 deletions(-)

diff --git a/Classes/Controller/JoblistController.php b/Classes/Controller/JoblistController.php
index f7c3ff9b..9439b740 100644
--- a/Classes/Controller/JoblistController.php
+++ b/Classes/Controller/JoblistController.php
@@ -27,6 +27,9 @@ namespace SGalinski\SgJobs\Controller;
  ***************************************************************/
 
 use SGalinski\SgJobs\Service\BackendService;
+use SGalinski\SgMail\Service\MailTemplateService;
+use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException;
+use TYPO3\CMS\Core\Exception;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
 
@@ -34,6 +37,11 @@ use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
  * The joblist plugin controller
  */
 class JoblistController extends ActionController {
+	/**
+	 * @var \TYPO3\CMS\Extbase\Object\ObjectManager
+	 * @inject
+	 */
+	protected $objectManager;
 
 	/**
 	 * @var \SGalinski\SgJobs\Domain\Repository\CompanyRepository
@@ -60,17 +68,51 @@ class JoblistController extends ActionController {
 		// get all jobs for the next root page
 		$jobs = $this->jobRepository->findJobs(BackendService::getRootUidByPageUid($pageUid));
 		$this->view->assign('jobs', $jobs);
-
-		debug(BackendService::getRootUidByPageUid($pageUid));
-		debug($jobs);
 	}
 
-	public function applyFormAction() {
-
+	/**
+	 * @param string $jobTitle
+	 */
+	public function applyFormAction($jobTitle = '') {
+		$this->view->assign('jobTitle', $jobTitle);
 	}
 
+	/**
+	 * @param array $applyData
+	 * @return void
+	 */
+	public function applyAction(array $applyData = []) {
+		// get an instance of the mail service
+		/** @var MailTemplateService $mailService */
+		$mailService = $this->objectManager->get(
+			MailTemplateService::class, 'application_mail', 'sg_jobs', $this->getApplicationMailMarkers($applyData)
+		);
+		$mailService->setIgnoreMailQueue(TRUE);
+		$mailService->setToAddresses('torsten@sgalinski.de');
+		try {
+			$mailService->sendEmail();
+		} catch (Exception $exception) {
 
-	public function applyAction() {
+		}
+	}
 
+	/**
+	 * @param array $applyData
+	 * @return array
+	 */
+	private function getApplicationMailMarkers(array $applyData = []) {
+		return [
+			'jobtitle' => $applyData['jobTitle'],
+			'salutation' => $applyData['salutation'],
+			'firstname' => $applyData['firstName'],
+			'lastname' => $applyData['lastName'],
+			'street' => $applyData['street'],
+			'city' => $applyData['city'],
+			'country' => $applyData['country'],
+			'phone' => $applyData['phone'],
+			'mobile' => $applyData['mobile'],
+			'email' => $applyData['email'],
+			'message' => $applyData['message']
+		];
 	}
 }
diff --git a/Configuration/MailTemplates/ApplicationMail.php b/Configuration/MailTemplates/ApplicationMail.php
index fe0c359a..98f85ad5 100644
--- a/Configuration/MailTemplates/ApplicationMail.php
+++ b/Configuration/MailTemplates/ApplicationMail.php
@@ -42,6 +42,12 @@ return [
 			'value' => '99999',
 			'description' => 'LLL:EXT:sg_jobs/Resources/Private/Language/locallang.xlf:application_mail.marker.zip'
 		],
+		[
+			'marker' => 'city',
+			'type' => \SGalinski\SgMail\Service\MailTemplateService::MARKER_TYPE_STRING,
+			'value' => 'München',
+			'description' => 'LLL:EXT:sg_jobs/Resources/Private/Language/locallang.xlf:application_mail.marker.city'
+		],
 		[
 			'marker' => 'country',
 			'type' => \SGalinski\SgMail\Service\MailTemplateService::MARKER_TYPE_STRING,
diff --git a/Resources/Private/Language/de.locallang.xlf b/Resources/Private/Language/de.locallang.xlf
index b8860048..265c1665 100644
--- a/Resources/Private/Language/de.locallang.xlf
+++ b/Resources/Private/Language/de.locallang.xlf
@@ -9,6 +9,10 @@
 			<authorEmail>fabian@sgalinski.de</authorEmail>
 		</header>
 		<body>
+		<trans-unit id="application_mail.marker.city" approved="yes">
+			<source>City</source>
+			<target>Ort</target>
+		</trans-unit>
 		<trans-unit id="application_mail.marker.country" approved="yes">
 			<source>Country</source>
 			<target>Land</target>
@@ -162,6 +166,10 @@
 			<source>Street</source>
 			<target>Strasse</target>
 		</trans-unit>
+		<trans-unit id="frontend.apply.thank_you" approved="yes">
+			<source>Thank you for your application!</source>
+			<target>Vielen Dank für Ihre Bewerbung!</target>
+		</trans-unit>
 		<trans-unit id="frontend.apply.title" approved="yes">
 			<source>Job title</source>
 			<target>Job-Titel</target>
diff --git a/Resources/Private/Language/locallang.xlf b/Resources/Private/Language/locallang.xlf
index 5230763f..522b0fd4 100644
--- a/Resources/Private/Language/locallang.xlf
+++ b/Resources/Private/Language/locallang.xlf
@@ -9,6 +9,9 @@
 			<authorEmail>fabian@sgalinski.de</authorEmail>
 		</header>
 		<body>
+		<trans-unit id="application_mail.marker.city">
+			<source>City</source>
+		</trans-unit>
 		<trans-unit id="application_mail.marker.country">
 			<source>Country</source>
 		</trans-unit>
@@ -123,6 +126,9 @@
 		<trans-unit id="frontend.apply.street">
 			<source>Street</source>
 		</trans-unit>
+		<trans-unit id="frontend.apply.thank_you">
+			<source>Thank you for your application!</source>
+		</trans-unit>
 		<trans-unit id="frontend.apply.title">
 			<source>Job title</source>
 		</trans-unit>
diff --git a/Resources/Private/Partials/Job.html b/Resources/Private/Partials/Job.html
index 4e58f3b2..8f39a327 100644
--- a/Resources/Private/Partials/Job.html
+++ b/Resources/Private/Partials/Job.html
@@ -67,6 +67,7 @@
 
 <div>
 	<f:form action="applyForm" controller="Joblist">
+		<f:form.hidden value="{job.title}" property="jobTitle" />
 		<f:form.submit value="{f:translate(key:'frontend.applyNow')}" />
 	</f:form>
 </div>
diff --git a/Resources/Private/Templates/Joblist/Apply.html b/Resources/Private/Templates/Joblist/Apply.html
index b926622c..4fd20e33 100644
--- a/Resources/Private/Templates/Joblist/Apply.html
+++ b/Resources/Private/Templates/Joblist/Apply.html
@@ -1,5 +1,7 @@
 <f:layout name="Default" />
 
 <f:section name="main">
-
+	<p>
+		<f:translate key="frontend.apply.thank_you" />
+	</p>
 </f:section>
diff --git a/Resources/Private/Templates/Joblist/ApplyForm.html b/Resources/Private/Templates/Joblist/ApplyForm.html
index d8c68769..11a5e399 100644
--- a/Resources/Private/Templates/Joblist/ApplyForm.html
+++ b/Resources/Private/Templates/Joblist/ApplyForm.html
@@ -3,7 +3,7 @@
 <f:section name="main">
 	<f:form action="apply" controller="Joblist" method="post" objectName="applyData" object="{applyData}" enctype="multipart/form-data">
 		<label for="apply-jobTitle"><f:translate key="frontend.apply.title" /></label>
-		<f:form.textfield property="jobTitle" id="apply-jobTitle" data="{}" class="" />
+		<f:form.textfield property="jobTitle" id="apply-jobTitle" data="{}" class="" value="{jobTitle}"/>
 		<br />
 		<label for="apply-salutation"><f:translate key="frontend.apply.salutation" /></label>
 		<f:form.textfield property="salutation" id="apply-salutation" data="{}" class="" />
-- 
GitLab