Skip to content

Commit

Permalink
Track whether filtering was applied (codefog#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg committed Oct 4, 2024
1 parent daa53b8 commit 617df59
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
30 changes: 24 additions & 6 deletions src/Criteria/NewsCriteriaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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();

Expand All @@ -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();
}
}

/**
Expand Down
19 changes: 14 additions & 5 deletions src/EventListener/NewsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
15 changes: 15 additions & 0 deletions src/Exception/CategoryFilteringNotAppliedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/*
* News Categories bundle for Contao Open Source CMS.
*
* @copyright Copyright (c) 2017, Codefog
* @author Codefog <https://codefog.pl>
* @license MIT
*/

namespace Codefog\NewsCategoriesBundle\Exception;

class CategoryFilteringNotAppliedException extends \RuntimeException
{
}

0 comments on commit 617df59

Please sign in to comment.