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