diff --git a/Classes/Backend/CategoryWarningDrawer.php b/Classes/Backend/CategoryWarningDrawer.php index d367c102a106c88183729f1649cdf67eb610a23d..efcfa95223503c1e4f53d4d829ebc0867bf65501 100644 --- a/Classes/Backend/CategoryWarningDrawer.php +++ b/Classes/Backend/CategoryWarningDrawer.php @@ -26,35 +26,34 @@ namespace SGalinski\SgNews\Backend; use SGalinski\SgNews\Domain\Model\Category; +use SGalinski\SgNews\Domain\Model\News; use TYPO3\CMS\Backend\Controller\PageLayoutController; use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Localization\LanguageService; +use TYPO3\CMS\Core\Messaging\AbstractMessage; use TYPO3\CMS\Core\Messaging\FlashMessage; use TYPO3\CMS\Core\Messaging\FlashMessageRendererResolver; use TYPO3\CMS\Core\Utility\GeneralUtility; /** - * + * This class handles a warning message if the news is not located in a news category. It is implmeneted as a hook for + * $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/db_layout.php']['drawHeaderHook'] in ext_localconf.php */ class CategoryWarningDrawer { - - /** - * @var LanguageService - */ - protected $languageService; - - public function __construct(LanguageService $languageService) { - $this->languageService = $languageService; - } - /** + * Renders the error message, if the given $parentObj is a news page and has no category as parent page + * * @param array|NULL $params * @param PageLayoutController|NULL $parentObj * @return string - * @throws \Doctrine\DBAL\Driver\Exception + * @throws \Doctrine\DBAL\Driver\Exception|\Doctrine\DBAL\DBALException */ - public function render(array $params = null, PageLayoutController $parentObj = null): string { + public function render(array $params = NULL, PageLayoutController $parentObj = NULL): string { + if (!$parentObj || $parentObj->pageinfo['doktype'] !== News::DOK_TYPE_NEWS) { + return ''; + } + $parentPage = $parentObj->pageinfo['pid']; $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) ->getQueryBuilderForTable('pages'); @@ -67,22 +66,25 @@ class CategoryWarningDrawer { ) )->execute() ->fetchOne(); + if ($parentDoktype === Category::DOK_TYPE_CATEGORY) { return ''; } - $message = GeneralUtility::makeInstance(FlashMessage::class, - $this->languageService->sL( - 'LLL:EXT:sg_news/Resources/Private/Language/locallang_backend.xlf:categoryErrorMessage' - ), - $this->languageService->sL( + $languageService = GeneralUtility::makeInstance(LanguageService::class); + $message = GeneralUtility::makeInstance( + FlashMessage::class, + $languageService->sL( + 'LLL:EXT:sg_news/Resources/Private/Language/locallang_backend.xlf:categoryErrorMessage' + ), + $languageService->sL( 'LLL:EXT:sg_news/Resources/Private/Language/locallang_backend.xlf:categoryErrorMessageHeader' ), - FlashMessage::ERROR, - true + AbstractMessage::ERROR, + TRUE ); return GeneralUtility::makeInstance(FlashMessageRendererResolver::class) - ->resolve() - ->render([$message]); + ->resolve() + ->render([$message]); } } 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 7fe59bad9bcab870e00d8ce1172b1bf92ca26fbb..7795775e49aa699bc94cbea34805395cf3a21c21 100644 --- a/Classes/Domain/Service/NewsService.php +++ b/Classes/Domain/Service/NewsService.php @@ -27,6 +27,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\Database\Connection; use TYPO3\CMS\Core\Database\ConnectionPool; @@ -51,6 +52,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(); @@ -74,6 +77,7 @@ class NewsService implements SingletonInterface { ]; } + // If sg_seo is loaded, add the jsonld schema data from the field if filled $customSchemaJson = ''; if (ExtensionManagementUtility::isLoaded('sg_seo')) { $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) @@ -91,6 +95,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, diff --git a/ext_localconf.php b/ext_localconf.php index 90d6777afe5364c5aec3aef97531a806b3ce68b7..d6d221088675ee9426d710f9b9cef7e7249dccef 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -98,7 +98,7 @@ call_user_func( $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem']['sg_news'] = \SGalinski\SgNews\Hooks\PageLayoutView\PluginRenderer::class; $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/db_layout.php']['drawHeaderHook'][] - = \SGalinski\SgNews\Backend\CategoryWarningDrawer::class . '->render'; + = \SGalinski\SgNews\Backend\CategoryWarningDrawer::class . '->render'; // Xclasses $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\TYPO3\CMS\Core\Page\PageRenderer::class] =