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

[FEATURE] Repair the overview plugin for TYPO3 9. See description

- Group by Tags: Not respecting the rootline, if the option onlyNewsWithinThisPageSection is true.
- Group by Categories: Not resprecting the rootline, if the option onlyNewsWithinThisPageSection is true. Also not respecting the multi language behaviour of the TYPO3 9 pages table.
- No grouping:  Not respecting the rootline, if the option onlyNewsWithinThisPageSection is true.
parent 69cec781
No related branches found
No related tags found
No related merge requests found
...@@ -151,10 +151,13 @@ class OverviewController extends AbstractController { ...@@ -151,10 +151,13 @@ class OverviewController extends AbstractController {
$offset = $this->calculatePaginationOffset($currentPageBrowserPage, $newsLimitPerCategory); $offset = $this->calculatePaginationOffset($currentPageBrowserPage, $newsLimitPerCategory);
if ($this->settings['onlyNewsWithinThisPageSection']) { if ($this->settings['onlyNewsWithinThisPageSection']) {
/** @noinspection PhpUndefinedMethodInspection */ $categories = $this->categoryRepository->findCategoriesInRootLine($GLOBALS['TSFE']->id);
$categories = $this->categoryRepository->findByPid($GLOBALS['TSFE']->id);
} else { } else {
$categories = $this->categoryRepository->findAll(); $categories = $this->categoryRepository->findAll()->toArray();
}
if (count($categories) <= 0) {
return;
} }
$startTime = (int) $this->settings['starttime']; $startTime = (int) $this->settings['starttime'];
...@@ -170,13 +173,16 @@ class OverviewController extends AbstractController { ...@@ -170,13 +173,16 @@ class OverviewController extends AbstractController {
foreach ($categories as $category) { foreach ($categories as $category) {
/** @var $category Category */ /** @var $category Category */
$categoryId = $category->getUid(); $categoryId = $category->getUid();
$categoryIdsForSelect = [$categoryId];
$categoryIds[] = $category->getUid(); $categoryIds[] = $category->getUid();
$categoriesById[$categoryId] = $category; $categoriesById[$categoryId] = $category;
if ($category->_getProperty('_languageUid')>0) { if ($category->_getProperty('_languageUid') > 0) {
$originalLangCategory = $this->categoryRepository->findOriginalLanguageById($category->getUid()); $originalLangCategory = $this->categoryRepository->findOriginalLanguageById($category->getUid());
if ($originalLangCategory) { if ($originalLangCategory) {
$categoryIds[] = $originalLangCategory->getUid(); $originalLangCategoryId = $originalLangCategory->getUid();
$categoriesById[$originalLangCategory->getUid()] = $originalLangCategory; $categoryIdsForSelect[] = $originalLangCategoryId;
$categoryIds[] = $originalLangCategoryId;
$categoriesById[$originalLangCategoryId] = $originalLangCategory;
} }
} }
...@@ -184,28 +190,31 @@ class OverviewController extends AbstractController { ...@@ -184,28 +190,31 @@ class OverviewController extends AbstractController {
if ($newsFilter['tag']) { if ($newsFilter['tag']) {
$tagIds = [(int) $newsFilter['tag']]; $tagIds = [(int) $newsFilter['tag']];
} }
$news = $this->newsRepository->findAllSortedNewsByCategories( foreach ($categoryIdsForSelect as $categoryIdsForSelectId) {
[$categoryId], $newsLimitPerCategory, $offset, $sortBy, $tagIds, $startTime, $endTime, $sortDirection $news = $this->newsRepository->findAllSortedNewsByCategories(
); [$categoryIdsForSelectId], $newsLimitPerCategory, $offset, $sortBy, $tagIds, $startTime, $endTime, $sortDirection
$newsMetaData[$categoryId] = []; );
foreach ($news as $newsEntry) {
/** @var News $newsEntry */ $newsMetaData[$categoryIdsForSelectId] = [];
$categoryId = $newsEntry->getPid(); foreach ($news as $newsEntry) {
$category = $categoriesById[$categoryId]; /** @var News $newsEntry */
if (!$category) { $categoryId = $newsEntry->getPid();
// Category isn't visible. $category = $categoriesById[$categoryIdsForSelectId];
continue; if (!$category) {
} // Category isn't visible.
continue;
}
$data = $this->getMetaDataForNews($newsEntry, $category); $data = $this->getMetaDataForNews($newsEntry, $category);
$newsMetaData[$categoryId][] = $data; $newsMetaData[$categoryIdsForSelectId][] = $data;
}
} }
} }
$maxNewsPerCategory = 0; $maxNewsPerCategory = 0;
foreach ($categoriesById as $categoryId => $category) { foreach ($categoriesById as $categoryId => $category) {
if (\count($newsMetaData[$categoryId]) <= 0) { if (!isset($newsMetaData[$categoryId]) || count($newsMetaData[$categoryId]) <= 0) {
// Hide empty tags. // Hide empty categories.
continue; continue;
} }
...@@ -220,7 +229,7 @@ class OverviewController extends AbstractController { ...@@ -220,7 +229,7 @@ class OverviewController extends AbstractController {
'recordId' => $categoryId, 'recordId' => $categoryId,
'recordType' => 'category', 'recordType' => 'category',
'newsMetaData' => $newsMetaData[$categoryId], 'newsMetaData' => $newsMetaData[$categoryId],
'newsCount' => \count($newsMetaData[$categoryId]) 'newsCount' => count($newsMetaData[$categoryId])
]; ];
} }
...@@ -265,17 +274,16 @@ class OverviewController extends AbstractController { ...@@ -265,17 +274,16 @@ class OverviewController extends AbstractController {
// find all tags // find all tags
$tagPid = $GLOBALS['TSFE']->id; $tagPid = $GLOBALS['TSFE']->id;
if ($this->settings['onlyNewsWithinThisPageSection']) { if ($this->settings['onlyNewsWithinThisPageSection']) {
/** @noinspection PhpUndefinedMethodInspection */ $tags = $this->tagRepository->findTagsInRootLine($tagPid);
$tags = $this->tagRepository->findByPid($tagPid);
} else { } else {
$tags = $this->tagRepository->findAll(); $tags = $this->tagRepository->findAll()->toArray();
} }
// remember selection of the filter values, if any // remember selection of the filter values, if any
$selectedTag = $this->tagRepository->findByUid((int) $newsFilter['tag']); $selectedTag = $this->tagRepository->findByUid((int) $newsFilter['tag']);
$this->view->assign('selectedTag', $selectedTag); $this->view->assign('selectedTag', $selectedTag);
$this->view->assign('tags', $tags->toArray()); $this->view->assign('tags', $tags);
$this->view->assign('categories', $categories->toArray()); $this->view->assign('categories', $categories);
$this->view->assign('numberOfPages', $numberOfPages); $this->view->assign('numberOfPages', $numberOfPages);
$this->view->assign('newsItems', $newsByCategory); $this->view->assign('newsItems', $newsByCategory);
$this->view->assign('groupBy', 'category'); $this->view->assign('groupBy', 'category');
...@@ -318,10 +326,9 @@ class OverviewController extends AbstractController { ...@@ -318,10 +326,9 @@ class OverviewController extends AbstractController {
$this->tagRepository->setDefaultOrderings(['sorting' => QueryInterface::ORDER_ASCENDING]); $this->tagRepository->setDefaultOrderings(['sorting' => QueryInterface::ORDER_ASCENDING]);
if ($this->settings['onlyNewsWithinThisPageSection']) { if ($this->settings['onlyNewsWithinThisPageSection']) {
/** @noinspection PhpUndefinedMethodInspection */ $tags = $this->tagRepository->findTagsInRootLine($tagPid);
$tags = $this->tagRepository->findByPid($tagPid);
} else { } else {
$tags = $this->tagRepository->findAll(); $tags = $this->tagRepository->findAll()->toArray();
} }
// Get news by tag id // Get news by tag id
...@@ -414,17 +421,16 @@ class OverviewController extends AbstractController { ...@@ -414,17 +421,16 @@ class OverviewController extends AbstractController {
} }
if ($this->settings['onlyNewsWithinThisPageSection']) { if ($this->settings['onlyNewsWithinThisPageSection']) {
/** @noinspection PhpUndefinedMethodInspection */ $categories = $this->categoryRepository->findCategoriesInRootLine($GLOBALS['TSFE']->id);
$categories = $this->categoryRepository->findByPid($GLOBALS['TSFE']->id);
} else { } else {
$categories = $this->categoryRepository->findAll(); $categories = $this->categoryRepository->findAll()->toArray();
} }
// remember selection of the filter values, if any // remember selection of the filter values, if any
$selectedCategory = $this->categoryRepository->findByUid((int) $newsFilter['category']); $selectedCategory = $this->categoryRepository->findByUid((int) $newsFilter['category']);
$this->view->assign('selectedCategory', $selectedCategory); $this->view->assign('selectedCategory', $selectedCategory);
$this->view->assign('tags', $tags->toArray()); $this->view->assign('tags', $tags);
$this->view->assign('categories', $categories->toArray()); $this->view->assign('categories', $categories);
$this->view->assign('numberOfPages', $numberOfPages); $this->view->assign('numberOfPages', $numberOfPages);
$this->view->assign('newsItems', $newsByTag); $this->view->assign('newsItems', $newsByTag);
$this->view->assign('groupBy', 'tag'); $this->view->assign('groupBy', 'tag');
...@@ -472,54 +478,61 @@ class OverviewController extends AbstractController { ...@@ -472,54 +478,61 @@ class OverviewController extends AbstractController {
$newsPerPage = (int) $this->settings['newsLimit']; $newsPerPage = (int) $this->settings['newsLimit'];
$offset = $this->calculatePaginationOffset($currentPageBrowserPage, $newsPerPage); $offset = $this->calculatePaginationOffset($currentPageBrowserPage, $newsPerPage);
$newsCount = 0;
$startTime = (int) $this->settings['starttime']; $startTime = (int) $this->settings['starttime'];
$endTime = (int) $this->settings['endtime']; $endTime = (int) $this->settings['endtime'];
if ($this->settings['onlyNewsWithinThisPageSection']) { if ($this->settings['onlyNewsWithinThisPageSection']) {
/** @noinspection PhpUndefinedMethodInspection */ $categories = $this->categoryRepository->findCategoriesInRootLine($GLOBALS['TSFE']->id);
$categories = $this->categoryRepository->findByPid($GLOBALS['TSFE']->id); if (count($categories) > 0) {
$categoryIds = [];
$categoryIds = []; $categoriesById = [];
$categoriesById = []; /** @var QueryResult $categories */
/** @var QueryResult $categories */ foreach ($categories as $category) {
foreach ($categories as $category) { /** @var $category Category */
/** @var $category Category */ $categoriesById[$category->getUid()] = $category;
$categoriesById[$category->getUid()] = $category; $categoryIds[] = $category->getUid();
$categoryIds[] = $category->getUid(); if ($category->_getProperty('_languageUid') > 0) {
if ($category->_getProperty('_languageUid')>0) { $originalLangCategory = $this->categoryRepository->findOriginalLanguageById($category->getUid());
$originalLangCategory = $this->categoryRepository->findOriginalLanguageById($category->getUid()); if ($originalLangCategory) {
if ($originalLangCategory) { $categoryIds[] = $originalLangCategory->getUid();
$categoryIds[] = $originalLangCategory->getUid(); $categoriesById[$originalLangCategory->getUid()] = $originalLangCategory;
$categoriesById[$originalLangCategory->getUid()] = $originalLangCategory; }
} }
} }
}
// filter by category and tag if selected in the filter // filter by category and tag if selected in the filter
if ($newsFilter['category']) { if ($newsFilter['category']) {
$categoryIds = [(int) $newsFilter['category']]; $categoryIds = [(int) $newsFilter['category']];
} }
$tagIds = NULL;
if ($newsFilter['tag']) { $tagIds = NULL;
$tagIds = [(int) $newsFilter['tag']]; if ($newsFilter['tag']) {
} $tagIds = [(int) $newsFilter['tag']];
}
$newsCount = $this->newsRepository->newsCountByCategories($categoryIds, $tagIds, $startTime, $endTime); $newsCount = $this->newsRepository->newsCountByCategories($categoryIds, $tagIds, $startTime, $endTime);
}
} else { } else {
$newsCount = $this->newsRepository->countAll($startTime, $endTime); $newsCount = $this->newsRepository->countAll($startTime, $endTime);
$categoryIds = NULL; $categoryIds = NULL;
$categoriesById = []; $categoriesById = [];
$categories = $this->categoryRepository->findAll(); $categories = $this->categoryRepository->findAll()->toArray();
foreach ($categories as $category) { foreach ($categories as $category) {
/** @var $category Category */ /** @var $category Category */
$categoriesById[$category->getUid()] = $category; $categoriesById[$category->getUid()] = $category;
} }
} }
if ($newsCount <= 0) {
return;
}
// filter by category and tag if selected in the filter // filter by category and tag if selected in the filter
if ($newsFilter['category']) { if ($newsFilter['category']) {
$categoryIds = [(int) $newsFilter['category']]; $categoryIds = [(int) $newsFilter['category']];
} }
$tagIds = NULL; $tagIds = NULL;
if ($newsFilter['tag']) { if ($newsFilter['tag']) {
$tagIds = [(int) $newsFilter['tag']]; $tagIds = [(int) $newsFilter['tag']];
...@@ -551,14 +564,13 @@ class OverviewController extends AbstractController { ...@@ -551,14 +564,13 @@ class OverviewController extends AbstractController {
// find all tags // find all tags
$currentPageId = $GLOBALS['TSFE']->id; $currentPageId = $GLOBALS['TSFE']->id;
if ($this->settings['onlyNewsWithinThisPageSection']) { if ($this->settings['onlyNewsWithinThisPageSection']) {
/** @noinspection PhpUndefinedMethodInspection */ $tags = $this->tagRepository->findTagsInRootLine($currentPageId);
$tags = $this->tagRepository->findByPid($currentPageId);
} else { } else {
$tags = $this->tagRepository->findAll(); $tags = $this->tagRepository->findAll()->toArray();
} }
$this->view->assign('tags', $tags->toArray()); $this->view->assign('tags', $tags);
$this->view->assign('categories', $categories->toArray()); $this->view->assign('categories', $categories);
$this->view->assign('numberOfPages', $numberOfPages); $this->view->assign('numberOfPages', $numberOfPages);
$this->view->assign('newsMetaData', $newsMetaData); $this->view->assign('newsMetaData', $newsMetaData);
} }
......
...@@ -29,6 +29,7 @@ namespace SGalinski\SgNews\Domain\Repository; ...@@ -29,6 +29,7 @@ namespace SGalinski\SgNews\Domain\Repository;
use SGalinski\SgNews\Domain\Model\Category; use SGalinski\SgNews\Domain\Model\Category;
use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\RootlineUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper; use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
use TYPO3\CMS\Extbase\Persistence\QueryInterface; use TYPO3\CMS\Extbase\Persistence\QueryInterface;
...@@ -50,7 +51,7 @@ class CategoryRepository extends AbstractRepository { ...@@ -50,7 +51,7 @@ class CategoryRepository extends AbstractRepository {
* @param int $uid * @param int $uid
* @return Category|null * @return Category|null
*/ */
public function findOriginalLanguageById(int $uid): ?Category { public function findOriginalLanguageById(int $uid) {
$dataMapper = $this->objectManager->get(DataMapper::class); $dataMapper = $this->objectManager->get(DataMapper::class);
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages'); $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
$row = $queryBuilder->select('default.*') $row = $queryBuilder->select('default.*')
...@@ -67,4 +68,25 @@ class CategoryRepository extends AbstractRepository { ...@@ -67,4 +68,25 @@ class CategoryRepository extends AbstractRepository {
return NULL; return NULL;
} }
} }
/**
* Returns all categories, with given page id in it's rootline.
*
* @param int $pageId
*
* @return array
*/
public function findCategoriesInRootLine(int $pageId): array {
$matchedCategories = [];
$allCategories = $this->findAll();
foreach ($allCategories as $category) {
/** @var Category $category */
$rootLineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $category->getUid());
if (in_array($pageId, array_column($rootLineUtility->get(), 'uid'), TRUE)) {
$matchedCategories[] = $category;
}
}
return $matchedCategories;
}
} }
...@@ -26,8 +26,32 @@ namespace SGalinski\SgNews\Domain\Repository; ...@@ -26,8 +26,32 @@ namespace SGalinski\SgNews\Domain\Repository;
* This copyright notice MUST APPEAR in all copies of the script! * This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/ ***************************************************************/
use SGalinski\SgNews\Domain\Model\Tag;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\RootlineUtility;
/** /**
* Tag Repository * Tag Repository
*/ */
class TagRepository extends AbstractRepository { class TagRepository extends AbstractRepository {
/**
* Returns all categories, with given page id in it's rootline.
*
* @param int $pageId
*
* @return array
*/
public function findTagsInRootLine(int $pageId): array {
$matchedTags = [];
$allTags = $this->findAll();
foreach ($allTags as $tag) {
/** @var Tag $tag */
$rootLineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $tag->getPid());
if (in_array($pageId, array_column($rootLineUtility->get(), 'uid'), TRUE)) {
$matchedTags[] = $tag;
}
}
return $matchedTags;
}
} }
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