Skip to content
Snippets Groups Projects
Verified Commit 4ae6b7bb authored by Kevin Ditscheid's avatar Kevin Ditscheid
Browse files

Merge branch 'master' into feature_customJSONLD

parents 4cfcbae4 c70b36ce
No related branches found
No related tags found
1 merge request!49[TASK] Add schemald from sg_seo
......@@ -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]);
}
}
......@@ -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');
......
......@@ -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,
......
......@@ -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] =
......
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