diff --git a/Classes/Domain/Repository/NewsRepository.php b/Classes/Domain/Repository/NewsRepository.php index 9ab663bf11f6bf8d1d1ced9c27cacb8dd2462b97..1878c99525317b32fc68bf0ce61550b4907f757a 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 ec24fca72e1d16fcc8048187c3aefc2a801bc8da..cf7a3dab15ed9706297c4e89a94e62c774775f3a 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,