From 02cedc8d1b9b7a68d9d2f2f402aee4a1a107afc6 Mon Sep 17 00:00:00 2001 From: fritzmg Date: Mon, 26 Feb 2024 20:51:10 +0000 Subject: [PATCH] track whether filterin was applied --- src/Criteria/NewsCriteriaBuilder.php | 29 +++++++++++++++---- src/EventListener/NewsListener.php | 19 ++++++++---- .../CategoryFilteringNotAppliedException.php | 15 ++++++++++ 3 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 src/Exception/CategoryFilteringNotAppliedException.php diff --git a/src/Criteria/NewsCriteriaBuilder.php b/src/Criteria/NewsCriteriaBuilder.php index 0dc395f..7562a73 100644 --- a/src/Criteria/NewsCriteriaBuilder.php +++ b/src/Criteria/NewsCriteriaBuilder.php @@ -11,6 +11,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; @@ -73,6 +74,8 @@ public function getCriteriaForArchiveModule(array $archives, $begin, $end, Modul $this->setRegularListCriteria($criteria, $module); } catch (NoNewsException $e) { return null; + } catch (CategoryFilteringNotAppliedException $e) { + // noop } return $criteria; @@ -87,7 +90,7 @@ public function getCriteriaForArchiveModule(array $archives, $begin, $end, Modul * * @return NewsCriteria|null */ - public function getCriteriaForListModule(array $archives, $featured, Module $module) + public function getCriteriaForListModule(array $archives, $featured, Module $module, bool $throwOnFilteringNotApplied = false) { $criteria = new NewsCriteria($this->framework); @@ -104,10 +107,14 @@ public function getCriteriaForListModule(array $archives, $featured, Module $mod $this->setRelatedListCriteria($criteria, $module); } else { // Set the regular list criteria - $this->setRegularListCriteria($criteria, $module); + $this->setRegularListCriteria($criteria, $module, $throwOnFilteringNotApplied); } } catch (NoNewsException $e) { return null; + } catch (CategoryFilteringNotAppliedException $e) { + if ($throwOnFilteringNotApplied) { + throw $e; + } } return $criteria; @@ -132,6 +139,8 @@ public function getCriteriaForMenuModule(array $archives, Module $module) $this->setRegularListCriteria($criteria, $module); } catch (NoNewsException $e) { return null; + } catch (CategoryFilteringNotAppliedException $e) { + // noop } return $criteria; @@ -148,9 +157,12 @@ public function getCriteriaForMenuModule(array $archives, Module $module) */ private function setRegularListCriteria(NewsCriteria $criteria, Module $module) { + $filteringApplied = false; + // Filter by default categories if (\count($default = StringUtil::deserialize($module->news_filterDefault, true)) > 0) { $criteria->setDefaultCategories($default); + $filteringApplied = true; } // Filter by multiple active categories @@ -189,13 +201,12 @@ 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(); @@ -210,8 +221,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 5855e5f..aeb32ea 100644 --- a/src/EventListener/NewsListener.php +++ b/src/EventListener/NewsListener.php @@ -12,6 +12,7 @@ use Codefog\NewsCategoriesBundle\Criteria\NewsCriteria; use Codefog\NewsCategoriesBundle\Criteria\NewsCriteriaBuilder; +use Codefog\NewsCategoriesBundle\Exception\CategoryFilteringNotAppliedException; use Codefog\NewsCategoriesBundle\Exception\CategoryNotFoundException; use Contao\CoreBundle\Exception\PageNotFoundException; use Contao\CoreBundle\Framework\FrameworkAwareInterface; @@ -49,8 +50,12 @@ public function __construct(NewsCriteriaBuilder $searchBuilder) */ public function onNewsListCountItems(array $archives, $featured, ModuleNewsList $module) { - 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 $criteria->getNewsModelAdapter()->countBy($criteria->getColumns(), $criteria->getValues()); @@ -69,8 +74,12 @@ public function onNewsListCountItems(array $archives, $featured, ModuleNewsList */ public function onNewsListFetchItems(array $archives, $featured, $limit, $offset, ModuleNewsList $module) { - 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); @@ -97,7 +106,7 @@ public function onNewsListFetchItems(array $archives, $featured, $limit, $offset private function getCriteria(array $archives, $featured, ModuleNewsList $module) { try { - $criteria = $this->searchBuilder->getCriteriaForListModule($archives, $featured, $module); + $criteria = $this->searchBuilder->getCriteriaForListModule($archives, $featured, $module, true); } catch (CategoryNotFoundException $e) { throw new PageNotFoundException($e->getMessage()); } 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 +{ +}