From b0c24cabd9a3e799e82a823812219af261994480 Mon Sep 17 00:00:00 2001 From: Fritz Michael Gschwantner Date: Wed, 2 Oct 2024 13:00:20 +0200 Subject: [PATCH] Track whether filtering was applied (#263) --- src/Criteria/NewsCriteriaBuilder.php | 30 +++++++++++++++---- src/EventListener/NewsListener.php | 19 ++++++++---- .../CategoryFilteringNotAppliedException.php | 15 ++++++++++ 3 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 src/Exception/CategoryFilteringNotAppliedException.php diff --git a/src/Criteria/NewsCriteriaBuilder.php b/src/Criteria/NewsCriteriaBuilder.php index 2409df2..f3e1d2b 100644 --- a/src/Criteria/NewsCriteriaBuilder.php +++ b/src/Criteria/NewsCriteriaBuilder.php @@ -13,6 +13,7 @@ namespace Codefog\NewsCategoriesBundle\Criteria; use Codefog\HasteBundle\Model\DcaRelationsModel; +use Codefog\NewsCategoriesBundle\Exception\CategoryFilteringNotAppliedException; use Codefog\NewsCategoriesBundle\Exception\CategoryNotFoundException; use Codefog\NewsCategoriesBundle\Exception\NoNewsException; use Codefog\NewsCategoriesBundle\FrontendModule\CumulativeFilterModule; @@ -66,6 +67,8 @@ public function getCriteriaForArchiveModule(array $archives, int $begin, int $en $this->setRegularListCriteria($criteria, $module); } catch (NoNewsException) { return null; + } catch (CategoryFilteringNotAppliedException $e) { + // noop } return $criteria; @@ -74,7 +77,7 @@ public function getCriteriaForArchiveModule(array $archives, int $begin, int $en /** * Get the criteria for list module. */ - public function getCriteriaForListModule(array $archives, bool|null $featured, Module $module): NewsCriteria|null + public function getCriteriaForListModule(array $archives, bool|null $featured, Module $module, bool $throwOnFilteringNotApplied = false): NewsCriteria|null { $criteria = new NewsCriteria($this->framework, $this->tokenChecker); @@ -91,10 +94,14 @@ public function getCriteriaForListModule(array $archives, bool|null $featured, M $this->setRelatedListCriteria($criteria, $module); } else { // Set the regular list criteria - $this->setRegularListCriteria($criteria, $module); + $this->setRegularListCriteria($criteria, $module, $throwOnFilteringNotApplied); } } catch (NoNewsException) { return null; + } catch (CategoryFilteringNotAppliedException $e) { + if ($throwOnFilteringNotApplied) { + throw $e; + } } return $criteria; @@ -116,6 +123,8 @@ public function getCriteriaForMenuModule(array $archives, Module $module): NewsC $this->setRegularListCriteria($criteria, $module); } catch (NoNewsException) { return null; + } catch (CategoryFilteringNotAppliedException $e) { + // noop } return $criteria; @@ -129,9 +138,12 @@ public function getCriteriaForMenuModule(array $archives, Module $module): NewsC */ private function setRegularListCriteria(NewsCriteria $criteria, Module $module): void { + $filteringApplied = false; + // Filter by default categories if (!empty($default = StringUtil::deserialize($module->news_filterDefault, true))) { $criteria->setDefaultCategories($default); + $filteringApplied = true; } // Filter by multiple active categories @@ -165,13 +177,13 @@ private function setRegularListCriteria(NewsCriteria $criteria, Module $module): } } } - } - return; + $filteringApplied = true; + } } - // Filter by active category - if ($module->news_filterCategories) { + elseif ($module->news_filterCategories) { + /** @var Input $input */ $input = $this->framework->getAdapter(Input::class); $param = $this->manager->getParameterName(); @@ -184,8 +196,14 @@ private function setRegularListCriteria(NewsCriteria $criteria, Module $module): } $criteria->setCategory($category->id, (bool) $module->news_filterPreserve, (bool) $module->news_includeSubcategories); + + $filteringApplied = true; } } + + if (!$filteringApplied) { + throw new CategoryFilteringNotAppliedException(); + } } /** diff --git a/src/EventListener/NewsListener.php b/src/EventListener/NewsListener.php index 060421e..43552a8 100644 --- a/src/EventListener/NewsListener.php +++ b/src/EventListener/NewsListener.php @@ -14,6 +14,7 @@ use Codefog\NewsCategoriesBundle\Criteria\NewsCriteria; use Codefog\NewsCategoriesBundle\Criteria\NewsCriteriaBuilder; +use Codefog\NewsCategoriesBundle\Exception\CategoryFilteringNotAppliedException; use Codefog\NewsCategoriesBundle\Exception\CategoryNotFoundException; use Contao\CoreBundle\DependencyInjection\Attribute\AsHook; use Contao\CoreBundle\Exception\PageNotFoundException; @@ -30,8 +31,12 @@ public function __construct(private readonly NewsCriteriaBuilder $searchBuilder) #[AsHook('newsListCountItems')] public function onNewsListCountItems(array $archives, bool|null $featured, ModuleNewsList $module): int { - if (null === ($criteria = $this->getCriteria($archives, $featured, $module))) { - return 0; + try { + if (null === ($criteria = $this->getCriteria($archives, $featured, $module))) { + return 0; + } + } catch (CategoryFilteringNotAppliedException $e) { + return false; } return NewsModel::countBy($criteria->getColumns(), $criteria->getValues()); @@ -43,8 +48,12 @@ public function onNewsListCountItems(array $archives, bool|null $featured, Modul #[AsHook('newsListFetchItems')] public function onNewsListFetchItems(array $archives, bool|null $featured, int $limit, int $offset, ModuleNewsList $module): Collection|null { - if (null === ($criteria = $this->getCriteria($archives, $featured, $module))) { - return null; + try { + if (null === ($criteria = $this->getCriteria($archives, $featured, $module))) { + return null; + } + } catch (CategoryFilteringNotAppliedException $e) { + return false; } $criteria->setLimit($limit); @@ -60,7 +69,7 @@ public function onNewsListFetchItems(array $archives, bool|null $featured, int $ private function getCriteria(array $archives, bool|null $featured, ModuleNewsList $module): NewsCriteria|null { try { - $criteria = $this->searchBuilder->getCriteriaForListModule($archives, $featured, $module); + $criteria = $this->searchBuilder->getCriteriaForListModule($archives, $featured, $module, true); } catch (CategoryNotFoundException $e) { throw new PageNotFoundException($e->getMessage(), 0, $e); } diff --git a/src/Exception/CategoryFilteringNotAppliedException.php b/src/Exception/CategoryFilteringNotAppliedException.php new file mode 100644 index 0000000..aec8295 --- /dev/null +++ b/src/Exception/CategoryFilteringNotAppliedException.php @@ -0,0 +1,15 @@ + + * @license MIT + */ + +namespace Codefog\NewsCategoriesBundle\Exception; + +class CategoryFilteringNotAppliedException extends \RuntimeException +{ +}