From 2bf9f89f0f0594a985c6a85333e32f8b34192c6d Mon Sep 17 00:00:00 2001
From: Kevin Ditscheid <kevin.ditscheid@sgalinski.de>
Date: Tue, 9 Jan 2024 17:18:44 +0100
Subject: [PATCH] [BUGFIX] Fix wrongly named property getter so validation
 doesn't crash in the applyAction

---
 Classes/Controller/JoblistController.php      | 27 ++++-----------
 Classes/Domain/Model/Job.php                  | 15 ++++----
 Classes/Domain/Model/JobApplication.php       | 12 +++++--
 Classes/Domain/Repository/JobRepository.php   |  2 +-
 Configuration/SgMail/ApplicantMail.php        |  2 +-
 Configuration/SgMail/ApplicationMail.php      |  4 +--
 .../TCA/tx_sgjobs_domain_model_job.php        |  1 +
 .../Private/Partials/ApplyFormSchema.html     |  4 +--
 .../Private/Templates/Backend/Index.html      |  2 +-
 .../Private/Templates/Joblist/ApplyForm.html  | 34 +++++++++----------
 10 files changed, 51 insertions(+), 52 deletions(-)

diff --git a/Classes/Controller/JoblistController.php b/Classes/Controller/JoblistController.php
index 9637bb2b..483c525b 100644
--- a/Classes/Controller/JoblistController.php
+++ b/Classes/Controller/JoblistController.php
@@ -532,19 +532,6 @@ class JoblistController extends ActionController {
 	 * @throws NoSuchArgumentException
 	 */
 	protected function initializeApplyAction(): void {
-		$propertyMappingConfiguration = $this->arguments->getArgument('applyData')->getPropertyMappingConfiguration();
-		$propertyMappingConfiguration->forProperty('job')->allowAllProperties();
-
-		// remove the job validator
-		/** @var ConjunctionValidator $validator */
-		$validator = $this->arguments->getArgument('applyData')->getValidator();
-		foreach ($validator->getValidators() as $subValidator) {
-			/** @var GenericObjectValidator $subValidatorSub */
-			foreach ($subValidator->getValidators() as $subValidatorSub) {
-				$subValidatorsSub = $subValidatorSub->getPropertyValidators();
-				unset($subValidatorsSub['job']);
-			}
-		}
 	}
 
 	/**
@@ -565,11 +552,11 @@ class JoblistController extends ActionController {
 			$job = $applyData->getJob();
 			if ($job) {
 				$applyData->setJobId($job->getJobId());
-				foreach ($job->getCompanies() as $company) {
+				foreach ($job->getCompany() as $company) {
 					/** @var Company $company */
 					$applyData->setCompany($company);
 				}
-			} elseif (!$applyData->getCompany()) {
+			} elseif (!$applyData->getFirstCompany()) {
 				/** @var Company $company */
 				$company = $this->companyRepository->findByUid(
 					(int) $_POST['tx_sgjobs_jobapplication']['applyData']['company']
@@ -581,8 +568,8 @@ class JoblistController extends ActionController {
 
 			// look for a configured default job, in case of unsolicited application
 			$applyData->setPid($GLOBALS['TSFE']->id);
-			if ((!$job || !$applyData->getJobId()) && $applyData->getCompany()) {
-				$applyData->setJobId($applyData->getCompany()->getJobId());
+			if ((!$job || !$applyData->getJobId()) && $applyData->getFirstCompany()) {
+				$applyData->setJobId($applyData->getFirstCompany()->getJobId());
 			}
 
 			if ($applyData->_isNew()) {
@@ -627,7 +614,7 @@ class JoblistController extends ActionController {
 			if ($contact !== NULL) {
 				$mailService->setToAddresses($contact->getEmail());
 			} else {
-				$company = $applyData->getCompany();
+				$company = $applyData->getFirstCompany();
 				if (($company !== NULL) && $company->getContact() !== NULL) {
 					$mailService->setToAddresses($company->getContact()->getEmail());
 				}
@@ -730,8 +717,8 @@ class JoblistController extends ActionController {
 	 */
 	protected function getApplicationMailMarkers(JobApplication $applyData): array {
 		$location = '';
-		if ($applyData->getCompany() !== NULL) {
-			$location = $applyData->getCompany()->getCity();
+		if ($applyData->getFirstCompany() !== NULL) {
+			$location = $applyData->getFirstCompany()->getCity();
 		}
 
 		return [
diff --git a/Classes/Domain/Model/Job.php b/Classes/Domain/Model/Job.php
index cb2976f8..a9fbba2a 100644
--- a/Classes/Domain/Model/Job.php
+++ b/Classes/Domain/Model/Job.php
@@ -107,7 +107,7 @@ class Job extends AbstractEntity {
 	 * @var ObjectStorage<Company>
 	 */
 	#[Lazy]
-	protected $company;
+	protected $company = NULL;
 
 	/**
 	 * @var ObjectStorage<Job>
@@ -287,25 +287,28 @@ class Job extends AbstractEntity {
 	}
 
 	/**
-	 * @return Company|NULL
+	 * @return ObjectStorage|NULL
 	 */
 	public function getCompany() {
 		if ($this->company instanceof LazyLoadingProxy) {
 			$this->company->_loadRealInstance();
 		}
 
-		return $this->company->offsetGet(0);
+		return $this->company;
 	}
 
 	/**
-	 * @return ObjectStorage
+	 * @return Company|NULL
 	 */
-	public function getCompanies(): ObjectStorage {
+	public function getFirstCompany(): ?Company {
 		if ($this->company instanceof LazyLoadingProxy) {
 			$this->company->_loadRealInstance();
 		}
 
-		return $this->company;
+		if ($this->company->count() === 0) {
+			return NULL;
+		}
+		return $this->company->offsetGet(0);
 	}
 
 	/**
diff --git a/Classes/Domain/Model/JobApplication.php b/Classes/Domain/Model/JobApplication.php
index afbf630c..a19e4a26 100644
--- a/Classes/Domain/Model/JobApplication.php
+++ b/Classes/Domain/Model/JobApplication.php
@@ -53,7 +53,7 @@ class JobApplication extends AbstractEntity {
 	 * @var ObjectStorage<Company>
 	 */
 	#[Lazy]
-	protected $company;
+	protected $company = NULL;
 
 	/**
 	 * @var Job $job
@@ -477,13 +477,21 @@ class JobApplication extends AbstractEntity {
 	}
 
 	/**
-	 * @return Company|NULL
+	 * @return ObjectStorage|NULL
 	 */
 	public function getCompany() {
 		if ($this->company instanceof LazyLoadingProxy) {
 			$this->company->_loadRealInstance();
 		}
 
+		return $this->company;
+	}
+
+	public function getFirstCompany(): ?Company {
+		if ($this->company instanceof LazyLoadingProxy) {
+			$this->company->_loadRealInstance();
+		}
+
 		if ($this->company->count() === 0) {
 			return NULL;
 		}
diff --git a/Classes/Domain/Repository/JobRepository.php b/Classes/Domain/Repository/JobRepository.php
index 7c1acccd..ca99220f 100644
--- a/Classes/Domain/Repository/JobRepository.php
+++ b/Classes/Domain/Repository/JobRepository.php
@@ -339,7 +339,7 @@ class JobRepository extends Repository {
 			$query->getQuerySettings()->setRespectStoragePage(FALSE);
 		}
 
-		$company = $job->getCompany();
+		$company = $job->getFirstCompany();
 		$department = $job->getDepartment();
 
 		if ($limit > 0) {
diff --git a/Configuration/SgMail/ApplicantMail.php b/Configuration/SgMail/ApplicantMail.php
index f83256bc..03717825 100644
--- a/Configuration/SgMail/ApplicantMail.php
+++ b/Configuration/SgMail/ApplicantMail.php
@@ -26,7 +26,7 @@ EOT
 			'description' => 'LLL:EXT:sg_jobs/Resources/Private/Language/locallang.xlf:application_mail.marker.jobtitle'
 		],
 		[
-			'marker' => 'application.company.location',
+			'marker' => 'application.firstCompany.location',
 			'type' => MailTemplateService::MARKER_TYPE_STRING,
 			'value' => 'München',
 			'description' => 'LLL:EXT:sg_jobs/Resources/Private/Language/locallang.xlf:application_mail.marker.location'
diff --git a/Configuration/SgMail/ApplicationMail.php b/Configuration/SgMail/ApplicationMail.php
index a0e9197c..36e187c5 100644
--- a/Configuration/SgMail/ApplicationMail.php
+++ b/Configuration/SgMail/ApplicationMail.php
@@ -8,7 +8,7 @@ return [
 	'description' => 'LLL:EXT:sg_jobs/Resources/Private/Language/locallang.xlf:mail.application.description',
 	'subject' => 'Eine neue Bewerbung',
 	'templateContent' => <<<EOT
-Eine neue {f:if(condition: '{application.jobTitle}', then: 'Bewerbung als <b>{application.jobTitle}</b> für den Standort <b>{application.company.city}</b>.', else: 'Initiativbewerbung für den Standort <b>{application.company.city}</b>.')}
+Eine neue {f:if(condition: '{application.jobTitle}', then: 'Bewerbung als <b>{application.jobTitle}</b> für den Standort <b>{application.firstCompany.city}</b>.', else: 'Initiativbewerbung für den Standort <b>{application.firstCompany.city}</b>.')}
 
 Geschlecht: {f:translate(key: 'frontend.apply.gender.{application.gender}', extensionName: 'SgJobs')}
 
@@ -51,7 +51,7 @@ EOT
 			'description' => 'LLL:EXT:sg_jobs/Resources/Private/Language/locallang.xlf:application_mail.marker.jobtitle'
 		],
 		[
-			'marker' => 'application.company.location',
+			'marker' => 'application.firstCompany.location',
 			'type' => MailTemplateService::MARKER_TYPE_STRING,
 			'value' => 'München',
 			'description' => 'LLL:EXT:sg_jobs/Resources/Private/Language/locallang.xlf:application_mail.marker.location'
diff --git a/Configuration/TCA/tx_sgjobs_domain_model_job.php b/Configuration/TCA/tx_sgjobs_domain_model_job.php
index dc086e23..b434ac1c 100644
--- a/Configuration/TCA/tx_sgjobs_domain_model_job.php
+++ b/Configuration/TCA/tx_sgjobs_domain_model_job.php
@@ -311,6 +311,7 @@ $columns = [
 				'foreign_table' => 'tx_sgjobs_domain_model_company',
 				'foreign_table_where' => ' AND tx_sgjobs_domain_model_company.sys_language_uid IN (0,-1) ORDER BY tx_sgjobs_domain_model_company.sorting ASC',
 				'minitems' => 1,
+				'multiple' => 1,
 				'fieldControl' => [
 					'editPopup' => [
 						'disabled' => FALSE,
diff --git a/Resources/Private/Partials/ApplyFormSchema.html b/Resources/Private/Partials/ApplyFormSchema.html
index ab35b14a..00efd97b 100644
--- a/Resources/Private/Partials/ApplyFormSchema.html
+++ b/Resources/Private/Partials/ApplyFormSchema.html
@@ -76,8 +76,8 @@
 
 		"hiringOrganization": {leftBrace}
 			"@type": "Organization",
-			"name": "{job.company.name}",
-			"sameAs": "{f:uri.typolink(parameter: job.company.identifyingUrl, absolute: 'TRUE')}",
+			"name": "{job.firstCompany.name}",
+			"sameAs": "{f:uri.typolink(parameter: job.firstCompany.identifyingUrl, absolute: 'TRUE')}",
 			"department": {leftBrace}
 				"@type": "Organization",
 				"name": "{job.department.title}"
diff --git a/Resources/Private/Templates/Backend/Index.html b/Resources/Private/Templates/Backend/Index.html
index 03f38698..b2f26076 100644
--- a/Resources/Private/Templates/Backend/Index.html
+++ b/Resources/Private/Templates/Backend/Index.html
@@ -46,7 +46,7 @@
 											</td>
 											<td>
 												<be:link.editRecord uid="{job.uid}" table="tx_sgjobs_domain_model_job">
-													<span>{job.title} - <sg:backend.companyLabel uid="{job.uid}" companyraw="{job.company}"></sg:backend.companyLabel> </span>
+													<span>{job.title} - <sg:backend.companyLabel uid="{job.uid}" companyraw="{job.firstCompany}"></sg:backend.companyLabel> </span>
 												</be:link.editRecord>
 											</td>
 											<td nowrap="nowrap" class="col-control">
diff --git a/Resources/Private/Templates/Joblist/ApplyForm.html b/Resources/Private/Templates/Joblist/ApplyForm.html
index 68110fb6..99443c5e 100644
--- a/Resources/Private/Templates/Joblist/ApplyForm.html
+++ b/Resources/Private/Templates/Joblist/ApplyForm.html
@@ -91,13 +91,13 @@
 							<f:if condition="!{job.hideApplyByPostal}">
 								<p>
 									<f:format.raw><f:translate key="frontend.job.via.post" /></f:format.raw><br>
-									{job.company.name}<br>
+									{job.firstCompany.name}<br>
 									<f:if condition="{job.contact}">
 										<f:then>
 											{job.contact.title} {job.contact.firstName} {job.contact.lastName}<br>
 										</f:then>
 										<f:else>
-											{job.company.contact.title} {job.company.contact.firstName} {job.company.contact.lastName}<br>
+											{job.firstCompany.contact.title} {job.firstCompany.contact.firstName} {job.firstCompany.contact.lastName}<br>
 										</f:else>
 									</f:if>
 									<f:if condition="{job.contact} && {job.contact.street}">
@@ -112,25 +112,25 @@
 											</f:if>
 										</f:then>
 										<f:else>
-											<f:if condition="{job.company.contact} && {job.company.contact.street}">
+											<f:if condition="{job.firstCompany.contact} && {job.firstCompany.contact.street}">
 												<f:then>
-													{job.company.contact.street}<br>
-													<f:if condition="{job.company.contact.state}">
-														{job.company.contact.state}<br>
+													{job.firstCompany.contact.street}<br>
+													<f:if condition="{job.firstCompany.contact.state}">
+														{job.firstCompany.contact.state}<br>
 													</f:if>
-													{job.company.contact.zip} {job.company.contact.city}
-													<f:if condition="{job.company.contact.country}">
-														<br>{job.company.contact.country}
+													{job.firstCompany.contact.zip} {job.firstCompany.contact.city}
+													<f:if condition="{job.firstCompany.contact.country}">
+														<br>{job.firstCompany.contact.country}
 													</f:if>
 												</f:then>
 												<f:else>
-													{job.company.street}<br>
-													<f:if condition="{job.company.state}">
-														{job.company.state}<br>
+													{job.firstCompany.street}<br>
+													<f:if condition="{job.firstCompany.state}">
+														{job.firstCompany.state}<br>
 													</f:if>
-													{job.company.zip} {job.company.city}
-													<f:if condition="{job.company.country}">
-														<br>{job.company.country}
+													{job.firstCompany.zip} {job.firstCompany.city}
+													<f:if condition="{job.firstCompany.country}">
+														<br>{job.firstCompany.country}
 													</f:if>
 												</f:else>
 											</f:if>
@@ -149,7 +149,7 @@
 											<f:format.html parseFuncTSPath="lib.parseFunc"><a href="mailto:{job.contact.email}"><f:translate key="frontend.emailContact"/></a></f:format.html>
 										</f:then>
 										<f:else>
-											<f:format.html parseFuncTSPath="lib.parseFunc"><a href="mailto:{job.company.contact.email}"><f:translate key="frontend.emailContact"/></a></f:format.html>
+											<f:format.html parseFuncTSPath="lib.parseFunc"><a href="mailto:{job.firstCompany.contact.email}"><f:translate key="frontend.emailContact"/></a></f:format.html>
 										</f:else>
 									</f:if>
 								</p>
@@ -170,7 +170,7 @@
 								<f:render section="contactBox" arguments="{contact: job.contact, hideApplyByEmail: job.hideApplyByEmail}" />
 							</f:then>
 							<f:else>
-								<f:render section="contactBox" arguments="{contact: job.company.contact, hideApplyByEmail: job.hideApplyByEmail}" />
+								<f:render section="contactBox" arguments="{contact: job.firstCompany.contact, hideApplyByEmail: job.hideApplyByEmail}" />
 							</f:else>
 						</f:if>
 						<f:if condition="{job.attachment}">
-- 
GitLab