Skip to content
Snippets Groups Projects
Commit a8132bc7 authored by Fabian Galinski's avatar Fabian Galinski :pouting_cat:
Browse files

[BUGFIX] Cleanup and a fix, where some news are appearing twice in the list after scrollbrowse

parent 8d404893
No related branches found
No related tags found
No related merge requests found
......@@ -33,6 +33,7 @@ use SGalinski\SgNews\Service\HeaderMetaDataService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\Query;
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
/**
* Controller that handles the overview page of categories and their news
......@@ -209,7 +210,7 @@ class OverviewController extends AbstractController {
];
}
$maxNewsPerCategory = max($maxNewsPerCategory, count($newsByCategory[$categoryId]['newsMetaData']));
$maxNewsPerCategory = \max($maxNewsPerCategory, \count($newsByCategory[$categoryId]['newsMetaData']));
}
$tagIds = NULL;
......@@ -241,6 +242,7 @@ class OverviewController extends AbstractController {
if ($maxNewsPerCategory < $newsLimitPerCategory && count($allNews) < $newsLimitPerCategory) {
$nextPage = $currentPageBrowserPage + 1;
if ($nextPage <= $numberOfPages) {
$this->setPageBrowserPage($nextPage);
$this->overviewWithCategories($newsByCategory, $allNews, $newsFilter, $nextPage);
return;
}
......@@ -281,15 +283,21 @@ class OverviewController extends AbstractController {
protected function overviewWithTags(
array $newsByTag = [], array $allNews = [], array $newsFilter = [], $currentPageBrowserPage = 0
) {
$startTime = (int) $this->settings['starttime'];
$endTime = (int) $this->settings['endtime'];
$newsLimitPerTag = (int) $this->settings['newsLimit'];
$this->tagRepository->setDefaultOrderings(['sorting' => Query::ORDER_ASCENDING]);
$offset = $this->calculatePaginationOffset($currentPageBrowserPage, $newsLimitPerTag);
$tagPid = (int) $this->settings['tagPid'];
if (!$tagPid) {
$tagPid = $GLOBALS['TSFE']->id;
}
$categoryIds = NULL;
if ($newsFilter['category']) {
$categoryIds = [(int) $newsFilter['category']];
}
$this->tagRepository->setDefaultOrderings(['sorting' => QueryInterface::ORDER_ASCENDING]);
if ($this->settings['onlyNewsWithinThisPageSection']) {
/** @noinspection PhpUndefinedMethodInspection */
$tags = $this->tagRepository->findByPid($tagPid);
......@@ -297,24 +305,17 @@ class OverviewController extends AbstractController {
$tags = $this->tagRepository->findAll();
}
$startTime = (int) $this->settings['starttime'];
$endTime = (int) $this->settings['endtime'];
// Get news by tag id
$tagIds = [];
$tagsById = [];
$categoriesById = [];
$newsMetaData = [];
foreach ($tags as $tag) {
/** @var $tag Tag */
$tagId = $tag->getUid();
$tagIds[] = $tagId;
$tagsById[$tagId] = $tag;
$categoryIds = NULL;
if ($newsFilter['category']) {
$categoryIds = [(int) $newsFilter['category']];
}
$news = $this->newsRepository->findAllSortedNewsByCategories(
$categoryIds, $newsLimitPerTag, $offset, $this->settings['sortBy'], [$tagId], $startTime, $endTime
);
......@@ -331,11 +332,11 @@ class OverviewController extends AbstractController {
continue;
}
$data = $this->getMetaDataForNews($newsEntry, $category);
$newsMetaData[$tagId][] = $data;
$newsMetaData[$tagId][] = $this->getMetaDataForNews($newsEntry, $category);
}
}
// Process news by tag id
$maxNewsPerTag = 0;
foreach ($tagsById as $tagId => $tag) {
if (\count($newsMetaData[$tagId]) <= 0) {
......@@ -345,7 +346,7 @@ class OverviewController extends AbstractController {
if (isset($newsByTag[$tagId])) {
$newsByTag[$tagId]['newsMetaData'] =
array_merge($newsByTag[$tagId]['newsMetaData'], $newsMetaData[$tagId]);
\array_merge($newsByTag[$tagId]['newsMetaData'], $newsMetaData[$tagId]);
} else {
$newsByTag[$tagId] = [
'record' => $tag,
......@@ -356,13 +357,10 @@ class OverviewController extends AbstractController {
];
}
$maxNewsPerTag = max($maxNewsPerTag, count($newsByTag[$tagId]['newsMetaData']));
$maxNewsPerTag = \max($maxNewsPerTag, \count($newsByTag[$tagId]['newsMetaData']));
}
$categoryIds = NULL;
if ($newsFilter['category']) {
$categoryIds = [(int) $newsFilter['category']];
}
// Get all news by tags.
$news = $this->newsRepository->findAllSortedNewsByCategories(
$categoryIds, $newsLimitPerTag, $offset, $this->settings['sortBy'], $tagIds, $startTime, $endTime
);
......@@ -378,18 +376,19 @@ class OverviewController extends AbstractController {
continue;
}
$data = $this->getMetaDataForNews($newsEntry, $category);
$allNews[] = $data;
$allNews[] = $this->getMetaDataForNews($newsEntry, $category);
}
$this->highlightBestFitNews([], $tagIds);
// Check to achieve less Ajax calls.
$newsCount = $this->newsRepository->newsCountByCategories([], $tagIds, $startTime, $endTime);
$numberOfPages = ($newsLimitPerTag <= 0 ? 0 : ceil($newsCount / $newsLimitPerTag));
// Redo this function, until one variable get the amount of newsLimitPerTag. Reduces the amount of ajax calls. Needed because of languagevisibility.
if ($maxNewsPerTag < $newsLimitPerTag && count($allNews) < $newsLimitPerTag) {
if ($maxNewsPerTag < $newsLimitPerTag && \count($allNews) < $newsLimitPerTag) {
$nextPage = $currentPageBrowserPage + 1;
if ($nextPage <= $numberOfPages) {
$this->setPageBrowserPage($nextPage);
$this->overviewWithTags($newsByTag, $allNews, $newsFilter, $nextPage);
return;
}
......@@ -515,6 +514,7 @@ class OverviewController extends AbstractController {
if (count($newsMetaData) < $newsPerPage) {
$nextPage = $currentPageBrowserPage + 1;
if ($nextPage <= $numberOfPages) {
$this->setPageBrowserPage($nextPage);
$this->overviewWithoutCategoriesAction($newsMetaData, $newsFilter, $nextPage);
return;
}
......@@ -533,6 +533,19 @@ class OverviewController extends AbstractController {
$this->view->assign('categories', $categories->toArray());
$this->view->assign('numberOfPages', $numberOfPages);
$this->view->assign('newsMetaData', $newsMetaData);
}
/**
* Sets the pagebrowser page to the given new page.
*
* @param int $newPage
* @return void
*/
protected function setPageBrowserPage($newPage) {
if (!isset($_GET['tx_sgnews_pagebrowser']['currentPage'])) {
return;
}
$_GET['tx_sgnews_pagebrowser']['currentPage'] = $newPage;
}
}
......@@ -106,8 +106,8 @@ class PageBrowserController extends ActionController {
}
$pageLinks = [];
$start = max($this->currentPage - $this->pagesBefore, 0);
$end = min($this->numberOfPages, $this->currentPage + $this->pagesAfter + 1);
$start = \max($this->currentPage - $this->pagesBefore, 0);
$end = \min($this->numberOfPages, $this->currentPage + $this->pagesAfter + 1);
for ($i = $start; $i < $end; $i++) {
$pageLinks[] = [
'number' => $i + 1,
......
......@@ -55,20 +55,20 @@ class NewsRepository extends AbstractRepository {
$query = $this->createQuery();
$constraints = [];
if ($categoryIds !== NULL && is_array($categoryIds) && count($categoryIds)) {
if ($categoryIds !== NULL && \is_array($categoryIds) && \count($categoryIds)) {
$constraints[] = $query->in('pid', $categoryIds);
}
if ($tagIds !== NULL && is_array($tagIds)) {
if ($tagIds !== NULL && \is_array($tagIds)) {
$tagConstraints = [];
foreach ($tagIds as $tagId) {
if ($tagId) {
$tagConstraints[] = $query->contains('tags', $tagId);
}
}
if (count($tagConstraints) > 1) {
if (\count($tagConstraints) > 1) {
$constraints[] = $query->logicalOr($tagConstraints);
} elseif (count($tagConstraints)) {
} elseif (\count($tagConstraints)) {
$constraints[] = $tagConstraints[0];
}
}
......@@ -85,7 +85,7 @@ class NewsRepository extends AbstractRepository {
$constraints[] = $query->lessThanOrEqual('lastUpdated', $endTimeObject);
}
if (count($constraints)) {
if (\count($constraints)) {
$query->matching($query->logicalAnd($constraints));
}
......
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