From dffc4257b6e5c49d154c63633d2ec814fc7a4528 Mon Sep 17 00:00:00 2001
From: Stefan Galinski <stefan@sgalinski.de>
Date: Mon, 1 Nov 2021 17:10:38 +0100
Subject: [PATCH] [BUGFIX] Fix totally stupid job teaser repository filter
 integration (tons of side effects)

---
 Classes/Controller/JobTeaserController.php  |  4 +--
 Classes/Domain/Repository/JobRepository.php | 39 +++++++++------------
 2 files changed, 19 insertions(+), 24 deletions(-)

diff --git a/Classes/Controller/JobTeaserController.php b/Classes/Controller/JobTeaserController.php
index f735d212..b6400c30 100644
--- a/Classes/Controller/JobTeaserController.php
+++ b/Classes/Controller/JobTeaserController.php
@@ -54,7 +54,7 @@ class JobTeaserController extends ActionController {
 	 */
 	public function indexAction(): void {
 		$allowedLocations = [];
-		if (!empty($this->settings['locations'])) {
+		if ($this->settings['locations'] !== '') {
 			$allowedLocations = GeneralUtility::trimExplode(',', $this->settings['locations']);
 		}
 		$totalAmountOfOffers = $this->jobRepository->countAll($allowedLocations);
@@ -65,7 +65,7 @@ class JobTeaserController extends ActionController {
 
 		$featuredOffers = $this->jobRepository->findByFeaturedOffer($allowedLocations);
 		$this->view->assign('totalAmountOfOffers', $totalAmountOfOffers);
-		$this->view->assign('filteredLocations', !empty($allowedLocations));
+		$this->view->assign('filteredLocations', (bool) count($allowedLocations));
 		$this->view->assign('featuredOffers', $featuredOffers);
 	}
 }
diff --git a/Classes/Domain/Repository/JobRepository.php b/Classes/Domain/Repository/JobRepository.php
index cda7e430..60d82819 100644
--- a/Classes/Domain/Repository/JobRepository.php
+++ b/Classes/Domain/Repository/JobRepository.php
@@ -26,7 +26,6 @@ namespace SGalinski\SgJobs\Domain\Repository;
  *  This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
 
-use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
 use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
 use TYPO3\CMS\Extbase\Persistence\QueryInterface;
@@ -49,7 +48,6 @@ class JobRepository extends Repository {
 
 	public function __construct(ObjectManagerInterface $objectManager) {
 		parent::__construct($objectManager);
-		// Appearently some of the extbase classes still need the objectManager
 		$querySettings = $objectManager->get(Typo3QuerySettings::class);
 		$querySettings->setRespectStoragePage(TRUE);
 		$this->setDefaultQuerySettings($querySettings);
@@ -77,7 +75,7 @@ class JobRepository extends Repository {
 	 * @param int $limit
 	 * @param int $offset
 	 * @return mixed
-	 * @throws \InvalidArgumentException
+	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
 	 */
 	public function findBackendJobs($recordPageId, array $filters = [], $limit = 0, $offset = 0) {
 		$query = $this->createQuery();
@@ -111,7 +109,7 @@ class JobRepository extends Repository {
 			return $query->matching($query->logicalAnd($constraints))->execute();
 		}
 
-		if(\count($constraints) > 0) {
+		if (\count($constraints) > 0) {
 			return $query->matching($constraints[0])->execute();
 		}
 
@@ -146,7 +144,7 @@ class JobRepository extends Repository {
 
 		$constraints = [];
 
-		if ($jobIds !== NULL && \is_array($jobIds) && \count($jobIds)) {
+		if (\is_array($jobIds) && \count($jobIds)) {
 			$companyConstraints = [];
 			foreach ($jobIds as $jobId) {
 				if ($jobId) {
@@ -261,7 +259,6 @@ class JobRepository extends Repository {
 	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
 	 */
 	public function countAll(array $companies = []): int {
-		/** @var QueryInterface $query */
 		$query = $this->createQuery();
 		$constraints = [];
 
@@ -284,32 +281,30 @@ class JobRepository extends Repository {
 	 * Gets the featured jobs filtered by companies
 	 *
 	 * @param array $companies
-	 * @return mixed
+	 * @param int $limit
+	 * @return array|ExtbaseQueryResultInterface
 	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
 	 */
-	public function findByFeaturedOffer(array $companies = []) {
-		/** @var QueryInterface $query */
+	public function findByFeaturedOffer(array $companies = [], int $limit = 3) {
 		$query = $this->createQuery();
-		$constraints = [];
 
 		$storagePageIds = $query->getQuerySettings()->getStoragePageIds();
-		if (empty($storagePageIds)) {
+		if (count($storagePageIds) <= 0) {
 			// if no record storage page has been selected in the plugin, ignore it
 			$query->getQuerySettings()->setRespectStoragePage(FALSE);
 		}
 
-		if (\count($companies) !== 0) {
-			$constraints[] = $query->in('company', $companies);
-			$query->setOrderings(
-				[
-					'featured_offer' => QueryInterface::ORDER_DESCENDING
-				]
-			);
-			$query->setLimit(3);
-		} else {
-			$constraints[] = $query->equals('featured_offer', TRUE);
+		if (\count($companies)) {
+			$query->matching($query->in('company', $companies));
 		}
 
-		return $query->matching($query->logicalAnd($constraints))->execute();
+		$query->setOrderings(
+			[
+				'featured_offer' => QueryInterface::ORDER_DESCENDING
+			]
+		);
+
+		$query->setLimit($limit);
+		return $query->execute();
 	}
 }
-- 
GitLab