Skip to content
Snippets Groups Projects
Commit 4e469a6d authored by Michael Kessler's avatar Michael Kessler
Browse files

[TASK] Combine overview controller functions with filters

parent a2028028
No related branches found
No related tags found
1 merge request!43[FEATURE] Enable filtering by all filters
......@@ -28,7 +28,6 @@ namespace SGalinski\SgNews\Controller;
use SGalinski\SgNews\Domain\Model\Category;
use SGalinski\SgNews\Domain\Model\News;
use SGalinski\SgNews\Domain\Model\Tag;
use SGalinski\SgNews\Domain\Repository\CategoryRepository;
use SGalinski\SgNews\Domain\Repository\NewsRepository;
use SGalinski\SgNews\Domain\Repository\TagRepository;
......@@ -37,7 +36,6 @@ use SGalinski\SgNews\Service\HeaderMetaDataService;
use TYPO3\CMS\Core\Http\ImmediateResponseException;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
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;
use TYPO3\CMS\Frontend\Controller\ErrorController;
......@@ -108,70 +106,12 @@ class OverviewController extends AbstractController {
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
*/
public function overviewAction(array $newsFilter = [], int $currentPageBrowserPage = 0) {
if ((bool) $this->settings['enableFilter'] === TRUE) {
$this->overviewWithAllFilters([], [], $newsFilter, $currentPageBrowserPage);
} else {
switch ((int) $this->settings['groupBy']) {
case 1:
$this->overviewWithCategories([], [], $newsFilter, $currentPageBrowserPage);
break;
case 2:
$this->overviewWithTags([], [], $newsFilter, $currentPageBrowserPage);
break;
default:
$this->forward('overviewWithoutCategories', NULL, NULL, $this->request->getArguments());
break;
}
}
$this->setupGridColumns();
}
/**
* Assign the grid column classes to the frontend
*
* @return void
*/
protected function setupGridColumns(): void {
if (!isset($this->settings['gridColumns'])) {
$this->view->assign('gridColumnClasses', 'col-md-4 col-sm-6 col-xs-12');
return;
}
$columnAmount = (int) $this->settings['gridColumns'];
$columnClasses = '';
switch ($columnAmount) {
case 4:
$columnClasses .= 'col-lg-3 ';
case 3:
$columnClasses .= 'col-md-4 ';
case 2:
$columnClasses .= 'col-sm-6 ';
default:
$columnClasses .= 'col-xs-12';
if ((int) $this->settings['groupBy'] === 0 && (bool) $this->settings['enableFilter'] === FALSE) {
$this->forward('overviewWithoutCategories', NULL, NULL, $this->request->getArguments());
}
$this->view->assign('gridColumnClasses', $columnClasses);
}
/**
* Renders the news overview by all filters
*
* @param array $newsByFilters
* @param array $allNews
* @param array $newsFilter
* @param int $currentPageBrowserPage
* @return void
* @throws \InvalidArgumentException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws ImmediateResponseException
* @throws \TYPO3\CMS\Core\Error\Http\PageNotFoundException
*/
protected function overviewWithAllFilters(
array $newsByFilters = [], array $allNews = [], array $newsFilter = [], int $currentPageBrowserPage = 0
) {
// Setup settings
$startTime = (int) $this->settings['starttime'];
$endTime = (int) $this->settings['endtime'];
......@@ -182,6 +122,9 @@ class OverviewController extends AbstractController {
$sortDirection = $configurationService->getConfiguration('sortDirection', $this->settings);
$this->tagRepository->setDefaultOrderings(['sorting' => QueryInterface::ORDER_ASCENDING]);
$this->categoryRepository->setDefaultOrderings(['sorting' => QueryInterface::ORDER_ASCENDING]);
$useAllFilters = (bool) $this->settings['enableFilter'];
$isCategoryFiltered = $useAllFilters || (int) $this->settings['groupBy'] === 1;
$isTagFiltered = $useAllFilters || (int) $this->settings['groupBy'] === 2;
// Get tag pid
$tagPid = (int) $this->settings['tagPid'];
......@@ -197,8 +140,11 @@ class OverviewController extends AbstractController {
$categories = $this->categoryRepository->findAll()->toArray();
}
// Get category restrictions
// Apply category restrictions
$categoryRestrictions = GeneralUtility::intExplode(',', $this->settings['categoryRestrictions'], TRUE);
if (!$isCategoryFiltered) {
$categoryRestrictions = [];
}
if (count($categoryRestrictions) > 0) {
foreach ($categories as $key => $category) {
$categoryId = $category->getUid();
......@@ -220,8 +166,11 @@ class OverviewController extends AbstractController {
}
}
// Get tag restrictions
// Apply tag restrictions
$tagRestrictions = GeneralUtility::intExplode(',', $this->settings['tagRestrictions'], TRUE);
if (!$isTagFiltered) {
$tagRestrictions = [];
}
if (count($tagRestrictions) > 0) {
foreach ($tags as $key => $tag) {
if (!in_array($tag->getUid(), $tagRestrictions, TRUE)) {
......@@ -234,20 +183,24 @@ class OverviewController extends AbstractController {
$categoryIds = [];
if ($newsFilter['category']) {
$categoryIds = [(int) $newsFilter['category']];
} else {
} elseif ($isCategoryFiltered) {
foreach ($categories as $category) {
$categoryIds[] = $category->getUid();
}
} else {
$categoryIds = NULL;
}
// Get tag ids or use the one in the filter
$tagIds = [];
if ($newsFilter['tag']) {
$tagIds = [(int) $newsFilter['tag']];
} else {
} elseif ($isTagFiltered) {
foreach ($tags as $tag) {
$tagIds[] = $tag->getUid();
}
} else {
$tagIds = NULL;
}
// Get all news by category and tag ids
......@@ -255,20 +208,57 @@ class OverviewController extends AbstractController {
$categoryIds, $newsLimit, $offset, $sortBy, $tagIds, $startTime, $endTime, $sortDirection
);
// Process news result query into meta data
$newsMetaData = [];
// Process news result query based on filters
$allNews = [];
$newsItems = [];
$areCategoriesCreated = FALSE;
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$categoryId = $newsEntry->getPid();
if (!isset($categoriesById[$categoryId])) {
$categoriesById[$categoryId] = $this->categoryRepository->findByUid($categoryId);
}
$category = $categoriesById[$categoryId];
if (!$category) {
// Category isn't visible.
continue;
$newsCategory = $this->categoryRepository->findOriginalLanguageById(
$newsEntry->getPid()
) ?? $this->categoryRepository->findByUid($newsEntry->getPid());
$newsCategoryId = $newsCategory->getUid();
$newsMetaData = $this->getMetaDataForNews($newsEntry, $newsCategory);
if ((int) $this->settings['groupBy'] === 1) {
if (!$areCategoriesCreated) {
// Add all required categories
foreach ($categories as $category) {
/** @var Category $category */
$newsItems[$category->getUid()] = [
'record' => $category,
'recordId' => $category->getUid(),
'recordType' => 'category',
'newsMetaData' => [],
'newsCount' => 0
];
}
$areCategoriesCreated = TRUE;
}
$newsItems[$newsCategoryId]['newsMetaData'][] = $newsMetaData;
$newsItems[$newsCategoryId]['newsCount'] += 1;
} else {
foreach ($tags as $tag) {
$tagId = $tag->getUid();
if (!isset($newsItems[$tagId])) {
$newsItems[$tagId] = [
'record' => $tag,
'recordId' => $tagId,
'recordType' => 'tag',
'newsMetaData' => [],
'newsCount' => 0
];
}
if ($newsEntry->getTags()->contains($tag)) {
$newsItems[$tagId]['newsMetaData'][] = $newsMetaData;
$newsItems[$tagId]['newsCount'] += 1;
}
}
}
$newsMetaData[] = $this->getMetaDataForNews($newsEntry, $category);
$allNews[] = $newsMetaData;
}
$this->highlightBestFitNews($categoryIds, $tagIds);
......@@ -295,9 +285,49 @@ class OverviewController extends AbstractController {
$this->view->assign('tags', $tags);
$this->view->assign('categories', $categories);
$this->view->assign('numberOfPages', $numberOfPages);
$this->view->assign('newsItems', $newsByTag);
$this->view->assign('groupBy', $this->settings['groupBy']);
$this->view->assign('allNews', $newsMetaData);
$this->view->assign('newsItems', $newsItems);
$this->view->assign('allNews', $allNews);
switch ($this->settings['groupBy']) {
case 1:
$this->view->assign('groupBy', 'category');
$this->view->assign('categoryTabs', TRUE);
break;
case 2:
$this->view->assign('groupBy', 'tag');
$this->view->assign('tagTabs', TRUE);
break;
default:
$this->view->assign('groupBy', 'none');
}
}
/**
* Assign the grid column classes to the frontend
*
* @return void
*/
protected function setupGridColumns(): void {
if (!isset($this->settings['gridColumns'])) {
$this->view->assign('gridColumnClasses', 'col-md-4 col-sm-6 col-xs-12');
return;
}
$columnAmount = (int) $this->settings['gridColumns'];
$columnClasses = '';
switch ($columnAmount) {
case 4:
$columnClasses .= 'col-lg-3 ';
case 3:
$columnClasses .= 'col-md-4 ';
case 2:
$columnClasses .= 'col-sm-6 ';
default:
$columnClasses .= 'col-xs-12';
}
$this->view->assign('gridColumnClasses', $columnClasses);
}
/**
......@@ -336,366 +366,6 @@ class OverviewController extends AbstractController {
}
}
/**
* Renders the news overview grouped by categories
*
* @param array $newsByCategory
* @param array $allNews
* @param array $newsFilter
* @param int $currentPageBrowserPage
* @return void
* @throws \InvalidArgumentException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws ImmediateResponseException
* @throws \TYPO3\CMS\Core\Error\Http\PageNotFoundException
*/
protected function overviewWithCategories(
array $newsByCategory = [], array $allNews = [], array $newsFilter = [], int $currentPageBrowserPage = 0
) {
$newsLimitPerCategory = (int) $this->settings['newsLimit'];
$this->categoryRepository->setDefaultOrderings(['sorting' => Query::ORDER_ASCENDING]);
$offset = $this->calculatePaginationOffset($currentPageBrowserPage, $newsLimitPerCategory);
if ($this->settings['onlyNewsWithinThisPageSection']) {
$categories = $this->categoryRepository->findCategoriesInRootLine($GLOBALS['TSFE']->id);
} else {
$categories = $this->categoryRepository->findAll()->toArray();
}
if (count($categories) <= 0) {
return;
}
$categoryRestrictions = GeneralUtility::intExplode(',', $this->settings['categoryRestrictions'], TRUE);
if (count($categoryRestrictions) > 0) {
foreach ($categories as $key => $category) {
$categoryId = $category->getUid();
// older version compatibility with selection of categories in translations and so on
$categoryIdTranslated = $categoryId;
if ($category->_getProperty('_languageUid') > 0) {
$originalLangCategory = $this->categoryRepository->findOriginalLanguageById($categoryId);
if ($originalLangCategory) {
$categoryIdTranslated = $originalLangCategory->getUid();
}
}
if (!in_array($categoryId, $categoryRestrictions, TRUE) &&
!in_array($categoryIdTranslated, $categoryRestrictions, TRUE)
) {
unset($categories[$key]);
}
}
}
if (count($categories) <= 0) {
return;
}
$startTime = (int) $this->settings['starttime'];
$endTime = (int) $this->settings['endtime'];
$configurationService = GeneralUtility::makeInstance(ConfigurationService::class);
$sortBy = $configurationService->getConfiguration('sortBy', $this->settings);
$sortDirection = $configurationService->getConfiguration('sortDirection', $this->settings);
$categoryIds = [];
$categoriesById = [];
$newsMetaData = [];
foreach ($categories as $category) {
/** @var Category $category */
$categoryId = $category->getUid();
$categoryIdsForSelect = [$categoryId];
$categoryIds[] = $category->getUid();
$categoriesById[$categoryId] = $category;
if ($category->_getProperty('_languageUid') > 0) {
$originalLangCategory = $this->categoryRepository->findOriginalLanguageById($category->getUid());
if ($originalLangCategory) {
$originalLangCategoryId = $originalLangCategory->getUid();
$categoryIdsForSelect[] = $originalLangCategoryId;
$categoryIds[] = $originalLangCategoryId;
$categoriesById[$originalLangCategoryId] = $originalLangCategory;
}
}
$tagIds = NULL;
if ($newsFilter['tag']) {
$tagIds = [(int) $newsFilter['tag']];
}
foreach ($categoryIdsForSelect as $categoryIdsForSelectId) {
$news = $this->newsRepository->findAllSortedNewsByCategories(
[$categoryIdsForSelectId], $newsLimitPerCategory, $offset, $sortBy, $tagIds, $startTime, $endTime,
$sortDirection
);
$newsMetaData[$categoryIdsForSelectId] = [];
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$category = $categoriesById[$categoryIdsForSelectId];
if (!$category) {
// Category isn't visible.
continue;
}
$data = $this->getMetaDataForNews($newsEntry, $category);
$newsMetaData[$categoryIdsForSelectId][] = $data;
}
}
}
$maxNewsPerCategory = 0;
foreach ($categoriesById as $categoryId => $category) {
if (!isset($newsMetaData[$categoryId]) || count($newsMetaData[$categoryId]) <= 0) {
// Hide empty categories.
continue;
}
/** @var Category $category */
if (isset($newsByCategory[$categoryId])) {
/** @var Category $category */
$newsByCategory[$categoryId]['newsMetaData'] =
array_merge($newsByCategory[$categoryId]['newsMetaData'], $newsMetaData[$categoryId], $newsFilter);
} else {
$newsByCategory[$categoryId] = [
'record' => $category,
'recordId' => $categoryId,
'recordType' => 'category',
'newsMetaData' => $newsMetaData[$categoryId],
'newsCount' => count($newsMetaData[$categoryId])
];
}
$maxNewsPerCategory = \max($maxNewsPerCategory, \count($newsByCategory[$categoryId]['newsMetaData']));
}
$tagIds = NULL;
if ($newsFilter['tag']) {
$tagIds = [(int) $newsFilter['tag']];
}
if ($newsFilter['category']) {
$categoryIds = [(int) $newsFilter['category']];
}
$news = $this->newsRepository->findAllSortedNewsByCategories(
$categoryIds, $newsLimitPerCategory, $offset, $sortBy, $tagIds, $startTime, $endTime, $sortDirection
);
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$categoryId = $newsEntry->getPid();
$category = $categoriesById[$categoryId];
if (!$category) {
// Category isn't visible.
continue;
}
$data = $this->getMetaDataForNews($newsEntry, $category);
$allNews[] = $data;
}
$this->highlightBestFitNews($categoryIds);
$newsCount = $this->newsRepository->newsCountByCategories($categoryIds, $tagIds, $startTime, $endTime);
$numberOfPages = (int) ($newsLimitPerCategory <= 0 ? 0 : ceil($newsCount / $newsLimitPerCategory));
if ($numberOfPages !== 0 && $currentPageBrowserPage >= $numberOfPages) {
/** @var ErrorController $errorController */
$errorController = GeneralUtility::makeInstance(ErrorController::class);
$response = $errorController->pageNotFoundAction(
$GLOBALS['TYPO3_REQUEST'],
'The requested page does not exist',
['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
);
throw new ImmediateResponseException($response);
}
// find all tags
$tagPid = $GLOBALS['TSFE']->id;
if ($this->settings['onlyNewsWithinThisPageSection']) {
$tags = $this->tagRepository->findTagsInRootLine($tagPid);
} else {
$tags = $this->tagRepository->findAll()->toArray();
}
// remember selection of the filter values, if any
$selectedCategory = $this->categoryRepository->findByUid((int) $newsFilter['category']);
$selectedTag = $this->tagRepository->findByUid((int) $newsFilter['tag']);
$this->view->assign('selectedCategory', $selectedCategory);
$this->view->assign('selectedTag', $selectedTag);
$this->view->assign('tags', $tags);
$this->view->assign('categories', $categories);
$this->view->assign('numberOfPages', $numberOfPages);
$this->view->assign('newsItems', $newsByCategory);
$this->view->assign('groupBy', 'category');
$this->view->assign('allNews', $allNews);
$this->view->assign('categoryTabs', TRUE);
}
/**
* Renders the news overview grouped by tags
*
* @param array $newsByTag
* @param array $allNews
* @param array $newsFilter
* @param int $currentPageBrowserPage
* @return void
* @throws \InvalidArgumentException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws ImmediateResponseException
* @throws \TYPO3\CMS\Core\Error\Http\PageNotFoundException
*/
protected function overviewWithTags(
array $newsByTag = [], array $allNews = [], array $newsFilter = [], int $currentPageBrowserPage = 0
) {
$startTime = (int) $this->settings['starttime'];
$endTime = (int) $this->settings['endtime'];
$newsLimitPerTag = (int) $this->settings['newsLimit'];
$offset = $this->calculatePaginationOffset($currentPageBrowserPage, $newsLimitPerTag);
$tagPid = (int) $this->settings['tagPid'];
if (!$tagPid) {
$tagPid = $GLOBALS['TSFE']->id;
}
$configurationService = GeneralUtility::makeInstance(ConfigurationService::class);
$sortBy = $configurationService->getConfiguration('sortBy', $this->settings);
$sortDirection = $configurationService->getConfiguration('sortDirection', $this->settings);
$categoryIds = NULL;
if ($newsFilter['category']) {
$categoryIds = [(int) $newsFilter['category']];
}
$this->tagRepository->setDefaultOrderings(['sorting' => QueryInterface::ORDER_ASCENDING]);
if ($this->settings['onlyNewsWithinThisPageSection']) {
$tags = $this->tagRepository->findTagsInRootLine($tagPid);
} else {
$tags = $this->tagRepository->findAll()->toArray();
}
$tagRestrictions = GeneralUtility::intExplode(',', $this->settings['tagRestrictions'], TRUE);
if (count($tagRestrictions) > 0) {
foreach ($tags as $key => $tag) {
if (!in_array($tag->getUid(), $tagRestrictions, TRUE)) {
unset($tags[$key]);
}
}
}
// Get news by tag id
$tagIds = [];
$tagsById = [];
$categoriesById = [];
$newsMetaData = [];
foreach ($tags as $tag) {
/** @var $tag Tag */
$tagId = $tag->getUid();
$tagIds[] = $tagId;
$tagsById[$tagId] = $tag;
$news = $this->newsRepository->findAllSortedNewsByCategories(
$categoryIds, $newsLimitPerTag, $offset, $sortBy, [$tagId], $startTime, $endTime, $sortDirection
);
$newsMetaData[$tagId] = [];
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$categoryId = $newsEntry->getPid();
if (!isset($categoriesById[$categoryId])) {
$categoriesById[$categoryId] = $this->categoryRepository->findByUid($categoryId);
}
$category = $categoriesById[$categoryId];
if (!$category) {
// Category isn't visible.
continue;
}
$newsMetaData[$tagId][] = $this->getMetaDataForNews($newsEntry, $category);
}
}
// Process news by tag id
$maxNewsPerTag = 0;
foreach ($tagsById as $tagId => $tag) {
if (\count($newsMetaData[$tagId]) <= 0) {
// Hide empty tags.
continue;
}
if (isset($newsByTag[$tagId])) {
$newsByTag[$tagId]['newsMetaData'] =
\array_merge($newsByTag[$tagId]['newsMetaData'], $newsMetaData[$tagId]);
} else {
$newsByTag[$tagId] = [
'record' => $tag,
'recordId' => $tagId,
'recordType' => 'tag',
'newsMetaData' => $newsMetaData[$tagId],
'newsCount' => \count($newsMetaData[$tagId])
];
}
$maxNewsPerTag = \max($maxNewsPerTag, \count($newsByTag[$tagId]['newsMetaData']));
}
// Get all news by tags.
if ($newsFilter['tag']) {
$tagIds = [(int) $newsFilter['tag']];
}
$news = $this->newsRepository->findAllSortedNewsByCategories(
$categoryIds, $newsLimitPerTag, $offset, $sortBy, $tagIds, $startTime, $endTime, $sortDirection
);
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$categoryId = $newsEntry->getPid();
if (!isset($categoriesById[$categoryId])) {
$categoriesById[$categoryId] = $this->categoryRepository->findByUid($categoryId);
}
/** @var Category $category */
$category = $categoriesById[$categoryId];
if (!$category) {
// Category isn't visible.
continue;
}
$allNews[] = $this->getMetaDataForNews($newsEntry, $category);
}
$this->highlightBestFitNews([], $tagIds);
// Check to achieve less Ajax calls.
$newsCount = $this->newsRepository->newsCountByCategories([], $tagIds, $startTime, $endTime);
$numberOfPages = (int) ($newsLimitPerTag <= 0 ? 0 : ceil($newsCount / $newsLimitPerTag));
if ($numberOfPages !== 0 && $currentPageBrowserPage >= $numberOfPages) {
/** @var ErrorController $errorController */
$errorController = GeneralUtility::makeInstance(ErrorController::class);
$response = $errorController->pageNotFoundAction(
$GLOBALS['TYPO3_REQUEST'],
'The requested page does not exist',
['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
);
throw new ImmediateResponseException($response);
}
if ($this->settings['onlyNewsWithinThisPageSection']) {
$categories = $this->categoryRepository->findCategoriesInRootLine($GLOBALS['TSFE']->id);
} else {
$categories = $this->categoryRepository->findAll()->toArray();
}
// remember selection of the filter values, if any
$selectedTag = $this->tagRepository->findByUid((int) $newsFilter['tag']);
$selectedCategory = $this->categoryRepository->findByUid((int) $newsFilter['category']);
$this->view->assign('selectedCategory', $selectedCategory);
$this->view->assign('selectedTag', $selectedTag);
$this->view->assign('tags', $tags);
$this->view->assign('categories', $categories);
$this->view->assign('numberOfPages', $numberOfPages);
$this->view->assign('newsItems', $newsByTag);
$this->view->assign('groupBy', 'tag');
$this->view->assign('allNews', $allNews);
$this->view->assign('tagTabs', TRUE);
}
/**
* Initialize the overviewWithoutCategoriesAction to set the currentPageBrowserPage parameter
*
......
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