Skip to content
Snippets Groups Projects
Commit c70b36ce authored by Stefan Galinski's avatar Stefan Galinski :video_game:
Browse files

Merge branch 'feature_sumLikes' into 'master'

[TASK] Overrite number of likes per news entry

See merge request !47
parents 1b6450f0 a097c288
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 {
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');
......
......@@ -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,
......
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