From 8f67515eeb0623a83f285721410166301f61cc6f Mon Sep 17 00:00:00 2001 From: Torsten Oppermann <torsten@sgalinski.de> Date: Fri, 28 Jul 2017 15:20:44 +0200 Subject: [PATCH] [TASK] Filter for news categories --- Classes/Controller/BackendController.php | 2 +- Classes/Service/Backend/Utility.php | 25 +++++++++++++++++-- .../Private/Partials/Backend/Filter.html | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Classes/Controller/BackendController.php b/Classes/Controller/BackendController.php index 8facb6e..a9afd3a 100644 --- a/Classes/Controller/BackendController.php +++ b/Classes/Controller/BackendController.php @@ -82,7 +82,7 @@ class BackendController extends ActionController { // retrieve next site root id $siteRootId = Utility::getSiteRoot((int) GeneralUtility::_GP('id')); $categories = Utility::getCategoriesForSiteRoot($siteRootId); - $news = Utility::getAllNewsByCategories($categories); + $news = Utility::getAllNewsByCategories($categories, $filters); $this->view->assign('docHeader', $this->docHeaderComponent->docHeaderContent()); $this->view->assign('pageUid', $pageUid); diff --git a/Classes/Service/Backend/Utility.php b/Classes/Service/Backend/Utility.php index 9514def..a14fef7 100644 --- a/Classes/Service/Backend/Utility.php +++ b/Classes/Service/Backend/Utility.php @@ -131,18 +131,39 @@ class Utility { } /** + * Get all news for the given categories (all if filter is empty) + * * @param array $categories + * @param array $filters * @return array * @throws \InvalidArgumentException * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException */ - public static function getAllNewsByCategories(array $categories = []) { + public static function getAllNewsByCategories(array $categories = [], array $filters = []) { $objectManager = GeneralUtility::makeInstance(ObjectManager::class); /** @var NewsRepository $newsRepository */ $newsRepository = $objectManager->get(NewsRepository::class); + // filter by category if set + if (!empty($filters['filters']['categories'])) { + foreach ($categories as $key => $value) { + if (is_array($filters['filters']['categories'])) { + $unset = TRUE; + foreach($filters['filters']['categories'] as $category) { + if ($key == (int) $category) { + $unset = FALSE; + } + } + + if ($unset) { + unset($categories[$key]); + } + } + } + } + $categoryIds = []; - foreach($categories as $key => $value) { + foreach ($categories as $key => $value) { $categoryIds[] = $key; } diff --git a/Resources/Private/Partials/Backend/Filter.html b/Resources/Private/Partials/Backend/Filter.html index 5bae71a..5339597 100644 --- a/Resources/Private/Partials/Backend/Filter.html +++ b/Resources/Private/Partials/Backend/Filter.html @@ -6,7 +6,7 @@ <f:translate key="backend.filters.category" /> <small><f:translate key="backend.filters.categories.description" /></small> </label> - <f:form.select class="form-control" multiple="1" size="4" property="categories" options="{categories}" id="filter-category" /> + <f:form.select class="form-control" multiple="1" size="4" property="filters.categories" options="{categories}" id="filter-category" /> </div> </div> <div class="col-xs-4"> -- GitLab