From a097c288a76648f45dadcd3954f7450d6e38ec1c Mon Sep 17 00:00:00 2001
From: Dominik Beyer <dominik.beyer@sgalinski.de>
Date: Tue, 26 Jul 2022 12:50:00 +0200
Subject: [PATCH] [TASK] Overrite number of likes per news entry

---
 Classes/Domain/Repository/NewsRepository.php | 23 ++++++++++++++++++++
 Classes/Domain/Service/NewsService.php       |  9 ++++++++
 2 files changed, 32 insertions(+)

diff --git a/Classes/Domain/Repository/NewsRepository.php b/Classes/Domain/Repository/NewsRepository.php
index 9ab663b..1878c99 100644
--- a/Classes/Domain/Repository/NewsRepository.php
+++ b/Classes/Domain/Repository/NewsRepository.php
@@ -629,6 +629,29 @@ class NewsRepository extends AbstractRepository {
 		return $query->execute();
 	}
 
+	/**
+	 * Sums up the number of likes per news entry for all existing translations.
+	 *
+	 * @param int $uid
+	 * @return int The like count
+	 * @throws \Doctrine\DBAL\DBALException
+	 * @throws \Doctrine\DBAL\Driver\Exception
+	 */
+	public function sumLikes(int $uid): int {
+		$connection = $this->getConnection();
+		$qb = $connection->createQueryBuilder();
+		$constraints = [];
+
+		$constraints[] = $qb->expr()->eq('uid', $qb->createNamedParameter($uid, Connection::PARAM_INT));
+		$constraints[] = $qb->expr()->eq('l10n_source', $qb->createNamedParameter($uid, Connection::PARAM_INT));
+
+		 $qb->addSelectLiteral($qb->expr()->sum('tx_sgnews_likes', 'sum_likes'))
+			 ->from('pages', 'pages')
+			 ->where($qb->expr()->or(...$constraints));
+
+		return $qb->execute()->fetchOne();
+	}
+
 	protected function getConnection(): Connection {
 		return GeneralUtility::makeInstance(ConnectionPool::class)
 			->getConnectionForTable('pages');
diff --git a/Classes/Domain/Service/NewsService.php b/Classes/Domain/Service/NewsService.php
index ec24fca..cf7a3da 100644
--- a/Classes/Domain/Service/NewsService.php
+++ b/Classes/Domain/Service/NewsService.php
@@ -28,6 +28,7 @@ namespace SGalinski\SgNews\Domain\Service;
 
 use SGalinski\SgNews\Domain\Model\Category;
 use SGalinski\SgNews\Domain\Model\News;
+use SGalinski\SgNews\Domain\Repository\NewsRepository;
 use TYPO3\CMS\Core\Resource\FileRepository;
 use TYPO3\CMS\Core\SingletonInterface;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -49,6 +50,8 @@ class NewsService implements SingletonInterface {
 	 * @param Category $category
 	 * @return array
 	 * @throws \InvalidArgumentException
+	 * @throws \Doctrine\DBAL\DBALException
+	 * @throws \Doctrine\DBAL\Driver\Exception
 	 */
 	public function getMetaDataForNews(News $news, Category $category): array {
 		$newsId = $news->getUid();
@@ -72,6 +75,12 @@ class NewsService implements SingletonInterface {
 			];
 		}
 
+		// Overwrite the number of likes with the one from the default news entry and add possible likes from translated
+		// entries to fix broken data in older instances.
+		$newsRepository = GeneralUtility::makeInstance(NewsRepository::class);
+		$totalLikes = $newsRepository->sumLikes($newsId);
+		$news->setLikes($totalLikes);
+
 		$newsRecord = array_merge(
 			[
 				'category' => $category,
-- 
GitLab