Skip to content
Snippets Groups Projects
Commit a097c288 authored by Dominik Beyer's avatar Dominik Beyer
Browse files

[TASK] Overrite number of likes per news entry

parent 687abd41
No related branches found
No related tags found
1 merge request!47[TASK] Overrite number of likes per news entry
...@@ -629,6 +629,29 @@ class NewsRepository extends AbstractRepository { ...@@ -629,6 +629,29 @@ class NewsRepository extends AbstractRepository {
return $query->execute(); 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 { protected function getConnection(): Connection {
return GeneralUtility::makeInstance(ConnectionPool::class) return GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('pages'); ->getConnectionForTable('pages');
......
...@@ -28,6 +28,7 @@ namespace SGalinski\SgNews\Domain\Service; ...@@ -28,6 +28,7 @@ namespace SGalinski\SgNews\Domain\Service;
use SGalinski\SgNews\Domain\Model\Category; use SGalinski\SgNews\Domain\Model\Category;
use SGalinski\SgNews\Domain\Model\News; use SGalinski\SgNews\Domain\Model\News;
use SGalinski\SgNews\Domain\Repository\NewsRepository;
use TYPO3\CMS\Core\Resource\FileRepository; use TYPO3\CMS\Core\Resource\FileRepository;
use TYPO3\CMS\Core\SingletonInterface; use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
...@@ -49,6 +50,8 @@ class NewsService implements SingletonInterface { ...@@ -49,6 +50,8 @@ class NewsService implements SingletonInterface {
* @param Category $category * @param Category $category
* @return array * @return array
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Driver\Exception
*/ */
public function getMetaDataForNews(News $news, Category $category): array { public function getMetaDataForNews(News $news, Category $category): array {
$newsId = $news->getUid(); $newsId = $news->getUid();
...@@ -72,6 +75,12 @@ class NewsService implements SingletonInterface { ...@@ -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( $newsRecord = array_merge(
[ [
'category' => $category, 'category' => $category,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment