From 4a1c7b022db95a350ab2d34435e25552e5a1f4ba Mon Sep 17 00:00:00 2001
From: Tim Wagner <tim.wagner@sgalinski.de>
Date: Wed, 19 Aug 2020 15:41:58 +0200
Subject: [PATCH] [BUGFIX] Clean up and fix job list filtering

---
 .../Domain/Repository/CompanyRepository.php   | 86 +++++--------------
 Resources/Private/Language/de.locallang.xlf   |  4 +
 Resources/Private/Language/locallang.xlf      |  3 +
 Resources/Private/Language/zh.locallang.xlf   |  4 +
 Resources/Private/Partials/Filter.html        | 14 ++-
 5 files changed, 44 insertions(+), 67 deletions(-)

diff --git a/Classes/Domain/Repository/CompanyRepository.php b/Classes/Domain/Repository/CompanyRepository.php
index 4b418613..031a5b55 100644
--- a/Classes/Domain/Repository/CompanyRepository.php
+++ b/Classes/Domain/Repository/CompanyRepository.php
@@ -52,32 +52,19 @@ class CompanyRepository extends Repository {
 	 *
 	 * @param int $pageUid
 	 * @return mixed
-	 * @throws \InvalidArgumentException
+	 * @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
 	 */
 	public function getAllCountries($pageUid) {
-		/** @var QueryInterface $query */
-		$query = $this->createQuery();
-		$querySettings = $query->getQuerySettings();
-		$querySettings->setStoragePageIds([$pageUid]);
-		$querySettings->setLanguageUid(
-			GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('language', 'id', 0)
-		);
+		$result = $this->getAllCompanies($pageUid)->toArray();
 
-		$query->setQuerySettings($querySettings);
-
-		$query->setOrderings(
-			[
-				'sorting' => QueryInterface::ORDER_ASCENDING,
-			]
-		);
-
-		$result = $query->execute()->toArray();
-
-		$countryArray = [''];
+		$countryArray = [];
 		/** @var Company $company */
 		foreach ($result as $company) {
 			$countryName = $company->getCountry();
-			$countryArray[$countryName] = $countryName;
+			if ($countryName !== '') {
+				// The filtering needs this specific key for some reason.
+				$countryArray[$countryName] = $countryName;
+			}
 		}
 
 		return $countryArray;
@@ -88,68 +75,42 @@ class CompanyRepository extends Repository {
 	 *
 	 * @param int $pageUid
 	 * @return mixed
-	 * @throws \InvalidArgumentException
+	 * @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
 	 */
 	public function getAllCities($pageUid) {
-		/** @var QueryInterface $query */
-		$query = $this->createQuery();
-		$querySettings = $query->getQuerySettings();
-		$querySettings->setStoragePageIds([$pageUid]);
-		$querySettings->setLanguageUid(
-			GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('language', 'id', 0)
-		);
-
-		$query->setQuerySettings($querySettings);
+		$result = $this->getAllCompanies($pageUid)->toArray();
 
-		$query->setOrderings(
-			[
-				'sorting' => QueryInterface::ORDER_ASCENDING,
-			]
-		);
-
-		$result = $query->execute()->toArray();
-
-		$cityArray = [''];
+		$cityArray = [];
 		/** @var Company $company */
 		foreach ($result as $company) {
 			$cityName = $company->getCity();
-			$cityArray[$cityName] = $cityName;
+			if ($cityName !== '') {
+				// The filtering needs this specific key for some reason.
+				$cityArray[$cityName] = $cityName;
+			}
 		}
 
 		return $cityArray;
 	}
 
 	/**
-	 * Returns all companies filtered by page id
+	 * Returns all company names filtered by page id
 	 *
 	 * @param int $pageUid
 	 * @return mixed
-	 * @throws \InvalidArgumentException
+	 * @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
 	 */
 	public function getAllCompanyNames($pageUid) {
-		/** @var QueryInterface $query */
-		$query = $this->createQuery();
-		$querySettings = $query->getQuerySettings();
-		$querySettings->setStoragePageIds([$pageUid]);
-		$querySettings->setLanguageUid(
-			GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('language', 'id', 0)
-		);
-
-		$query->setQuerySettings($querySettings);
-
-		$query->setOrderings(
-			[
-				'sorting' => QueryInterface::ORDER_ASCENDING,
-			]
-		);
-
-		$result = $query->execute()->toArray();
+		$result = $this->getAllCompanies($pageUid)->toArray();
 
-		$companyArray = [''];
+		$companyArray = [];
 		/** @var Company $company */
 		foreach ($result as $company) {
 			$companyName = $company->getName();
-			$companyArray[$companyName] = $companyName;
+			if ($companyName !== '') {
+				// The filtering needs this specific key for some reason.
+				$companyArray[$companyName] = $companyName;
+			}
 		}
 
 		return $companyArray;
@@ -160,10 +121,9 @@ class CompanyRepository extends Repository {
 	 *
 	 * @param int $pageUid
 	 * @return mixed
-	 * @throws \InvalidArgumentException
+	 * @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
 	 */
 	public function getAllCompanies($pageUid) {
-		/** @var QueryInterface $query */
 		$query = $this->createQuery();
 		$querySettings = $query->getQuerySettings();
 		$querySettings->setStoragePageIds([$pageUid]);
diff --git a/Resources/Private/Language/de.locallang.xlf b/Resources/Private/Language/de.locallang.xlf
index b56f493b..fb81d5f7 100644
--- a/Resources/Private/Language/de.locallang.xlf
+++ b/Resources/Private/Language/de.locallang.xlf
@@ -366,6 +366,10 @@
 				<source><![CDATA[Location]]></source>
 				<target><![CDATA[Standort]]></target>
 			</trans-unit>
+			<trans-unit id="frontend.filter.selections.all" approved="yes">
+				<source><![CDATA[All]]></source>
+				<target><![CDATA[Alle]]></target>
+			</trans-unit>
 			<trans-unit id="frontend.filter.submit" approved="yes">
 				<source><![CDATA[Filter]]></source>
 				<target><![CDATA[Filtern]]></target>
diff --git a/Resources/Private/Language/locallang.xlf b/Resources/Private/Language/locallang.xlf
index 22309a35..e94e3d70 100644
--- a/Resources/Private/Language/locallang.xlf
+++ b/Resources/Private/Language/locallang.xlf
@@ -276,6 +276,9 @@
 			<trans-unit id="frontend.filter.locations">
 				<source><![CDATA[Location]]></source>
 			</trans-unit>
+			<trans-unit id="frontend.filter.selections.all">
+				<source><![CDATA[All]]></source>
+			</trans-unit>
 			<trans-unit id="frontend.filter.submit">
 				<source><![CDATA[Filter]]></source>
 			</trans-unit>
diff --git a/Resources/Private/Language/zh.locallang.xlf b/Resources/Private/Language/zh.locallang.xlf
index cc59f7cb..ce92a164 100644
--- a/Resources/Private/Language/zh.locallang.xlf
+++ b/Resources/Private/Language/zh.locallang.xlf
@@ -213,6 +213,10 @@
 				<source><![CDATA[Location]]></source>
 				<target><![CDATA[地点]]></target>
 			</trans-unit>
+			<trans-unit id="frontend.filter.selections.all" approved="yes">
+				<source><![CDATA[All]]></source>
+				<target><![CDATA[]]></target>
+			</trans-unit>
 			<trans-unit id="frontend.location" approved="yes">
 				<source><![CDATA[Location]]></source>
 				<target><![CDATA[地点]]></target>
diff --git a/Resources/Private/Partials/Filter.html b/Resources/Private/Partials/Filter.html
index 6a10b6a6..52335513 100644
--- a/Resources/Private/Partials/Filter.html
+++ b/Resources/Private/Partials/Filter.html
@@ -18,13 +18,15 @@
 							property="filterCountry"
 							optionValueField="value"
 							options="{countries}"
+							prependOptionLabel="{f:translate(key: 'frontend.filter.selections.all')}"
+							prependOptionValue="0"
 							id="filter-countries"/>
 					</div>
 				</div>
 
 				<div class="col-md-6 col-sm-6 col-cs-12">
 					<div class="sgjobs-filter-bar-form-control">
-						<label for="filter-countries">
+						<label for="filter-locations">
 							<f:translate key="frontend.filter.locations"/>
 						</label>
 						<f:form.select
@@ -33,7 +35,9 @@
 							value="{selectedLocation}"
 							property="filterLocation"
 							optionValueField="value"
-							options="{companies}"
+							options="{cities}"
+							prependOptionLabel="{f:translate(key: 'frontend.filter.selections.all')}"
+							prependOptionValue="0"
 							id="filter-locations"/>
 					</div>
 				</div>
@@ -52,6 +56,8 @@
 							property="filterCompany"
 							optionValueField="value"
 							options="{companies}"
+							prependOptionLabel="{f:translate(key: 'frontend.filter.selections.all')}"
+							prependOptionValue="0"
 							id="filter-companies"/>
 					</div>
 				</div>
@@ -69,7 +75,7 @@
 							optionLabelField="title"
 							optionValueField="uid"
 							options="{departments}"
-							prependOptionLabel=""
+							prependOptionLabel="{f:translate(key: 'frontend.filter.selections.all')}"
 							prependOptionValue="0"
 							id="filter-departments"/>
 					</div>
@@ -90,7 +96,7 @@
 							optionLabelField="title"
 							optionValueField="uid"
 							options="{experienceLevels}"
-							prependOptionLabel=""
+							prependOptionLabel="{f:translate(key: 'frontend.filter.selections.all')}"
 							prependOptionValue="0"
 							id="filter-experienceLevels"/>
 					</div>
-- 
GitLab