From 2ac0611ee1b87f0e34049dd2e0899e4ee6b44c0c Mon Sep 17 00:00:00 2001
From: Kevin Ditscheid <kevin.ditscheid@sgalinski.de>
Date: Tue, 15 Mar 2022 09:54:05 +0100
Subject: [PATCH] [TASK] Add related news by tag or category

---
 Classes/Domain/Model/News.php                 | 26 ++++++++++++++++++-
 Classes/Domain/Repository/NewsRepository.php  | 25 ++++++++++++++++++
 .../TypoScript/Frontend/constants.typoscript  |  3 +++
 .../TypoScript/Frontend/setup.typoscript      |  3 +++
 4 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/Classes/Domain/Model/News.php b/Classes/Domain/Model/News.php
index f6e06c1..a3e51bd 100644
--- a/Classes/Domain/Model/News.php
+++ b/Classes/Domain/Model/News.php
@@ -26,8 +26,12 @@ namespace SGalinski\SgNews\Domain\Model;
  *  This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
 
+use SGalinski\SgNews\Domain\Repository\NewsRepository;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy;
 use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
+use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
+use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
 
 /**
  * News
@@ -97,14 +101,28 @@ class News extends CategoryAndNews {
 	 */
 	protected $contentFromAnotherPage = 0;
 
+	/**
+	 * @var ConfigurationManager
+	 */
+	protected $configurationManager;
+
+	/**
+	 * Inject the configuration manager
+	 *
+	 * @param ConfigurationManager $configurationManager
+	 */
+	public function injectConfigurationManager(ConfigurationManager $configurationManager) {
+		$this->configurationManager = $configurationManager;
+	}
+
 	/**
 	 * Constructor
 	 */
 	public function __construct() {
 		parent::__construct();
-		$this->relatedNews = new ObjectStorage();
 		$this->tags = new ObjectStorage();
 		$this->newsAuthor = new ObjectStorage();
+		$this->relatedNews = new ObjectStorage();
 	}
 
 	/**
@@ -182,6 +200,12 @@ class News extends CategoryAndNews {
 			$this->relatedNews->_loadRealInstance();
 		}
 
+		$settings = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS);
+		if ($settings['automaticRelatedNews'] && !$this->_isNew() && $this->relatedNews->count() === 0) {
+			$newsRepository = GeneralUtility::makeInstance(NewsRepository::class);
+			$this->relatedNews = $newsRepository->findRelated($this);
+		}
+
 		return $this->relatedNews;
 	}
 
diff --git a/Classes/Domain/Repository/NewsRepository.php b/Classes/Domain/Repository/NewsRepository.php
index 7bf40bf..60d5166 100644
--- a/Classes/Domain/Repository/NewsRepository.php
+++ b/Classes/Domain/Repository/NewsRepository.php
@@ -505,4 +505,29 @@ class NewsRepository extends AbstractRepository {
 		$query->matching($query->equals('uid', $uid));
 		return $query->execute()->getFirst();
 	}
+
+	/**
+	 * This method finds news related by Tag or Category to the given news record
+	 *
+	 * @param News $news The news to find related news to
+	 * @return QueryResultInterface
+	 */
+	public function findRelated(News $news) {
+		$query = $this->createQuery();
+		$query->getQuerySettings()->setRespectStoragePage(FALSE);
+		$contains = [];
+		$tags = $news->getTags();
+		if ($tags->count() > 0) {
+			foreach ($tags as $tag) {
+				$contains[] = $query->contains('tags', $tag);
+			}
+		} else {
+			$contains[] = $query->equals('pid', $news->getPid());
+		}
+
+		$query->matching(
+			$contains
+		);
+		return $query->execute();
+	}
 }
diff --git a/Configuration/TypoScript/Frontend/constants.typoscript b/Configuration/TypoScript/Frontend/constants.typoscript
index 34a9c4d..af0e2c0 100644
--- a/Configuration/TypoScript/Frontend/constants.typoscript
+++ b/Configuration/TypoScript/Frontend/constants.typoscript
@@ -19,5 +19,8 @@ plugin.tx_sgnews {
 
 		# sort direction (DESC / ASC)
 		sortDirection = DESC
+
+		# Enable fetching of related news by tag or category, if related news are not set manually
+		automaticRelatedNews = 1
 	}
 }
diff --git a/Configuration/TypoScript/Frontend/setup.typoscript b/Configuration/TypoScript/Frontend/setup.typoscript
index 9a52fd4..c85638a 100644
--- a/Configuration/TypoScript/Frontend/setup.typoscript
+++ b/Configuration/TypoScript/Frontend/setup.typoscript
@@ -109,6 +109,9 @@ plugin.tx_sgnews {
 
 		# The logo value for the structured data implementation (see single view template)
 		publisherLogo =
+
+		# Enable fetching of related news by tag or category, if related news are not set manually
+		automaticRelatedNews = {$plugin.tx_sgnews.settings.automaticRelatedNews}
 	}
 
 	features {
-- 
GitLab