Skip to content

Commit

Permalink
Revert "Track whether filtering was applied (codefog#263)"
Browse files Browse the repository at this point in the history
This reverts commit b0c24ca.
  • Loading branch information
qzminski committed Oct 4, 2024
1 parent b0c24ca commit daa53b8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 53 deletions.
30 changes: 6 additions & 24 deletions src/Criteria/NewsCriteriaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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 @@ -67,8 +66,6 @@ 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 @@ -77,7 +74,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, bool $throwOnFilteringNotApplied = false): NewsCriteria|null
public function getCriteriaForListModule(array $archives, bool|null $featured, Module $module): NewsCriteria|null
{
$criteria = new NewsCriteria($this->framework, $this->tokenChecker);

Expand All @@ -94,14 +91,10 @@ public function getCriteriaForListModule(array $archives, bool|null $featured, M
$this->setRelatedListCriteria($criteria, $module);
} else {
// Set the regular list criteria
$this->setRegularListCriteria($criteria, $module, $throwOnFilteringNotApplied);
$this->setRegularListCriteria($criteria, $module);
}
} catch (NoNewsException) {
return null;
} catch (CategoryFilteringNotAppliedException $e) {
if ($throwOnFilteringNotApplied) {
throw $e;
}
}

return $criteria;
Expand All @@ -123,8 +116,6 @@ public function getCriteriaForMenuModule(array $archives, Module $module): NewsC
$this->setRegularListCriteria($criteria, $module);
} catch (NoNewsException) {
return null;
} catch (CategoryFilteringNotAppliedException $e) {
// noop
}

return $criteria;
Expand All @@ -138,12 +129,9 @@ 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 @@ -177,13 +165,13 @@ private function setRegularListCriteria(NewsCriteria $criteria, Module $module):
}
}
}

$filteringApplied = true;
}

return;
}

// Filter by active category
elseif ($module->news_filterCategories) {
/** @var Input $input */
if ($module->news_filterCategories) {
$input = $this->framework->getAdapter(Input::class);
$param = $this->manager->getParameterName();

Expand All @@ -196,14 +184,8 @@ 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: 5 additions & 14 deletions src/EventListener/NewsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

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 @@ -31,12 +30,8 @@ public function __construct(private readonly NewsCriteriaBuilder $searchBuilder)
#[AsHook('newsListCountItems')]
public function onNewsListCountItems(array $archives, bool|null $featured, ModuleNewsList $module): int
{
try {
if (null === ($criteria = $this->getCriteria($archives, $featured, $module))) {
return 0;
}
} catch (CategoryFilteringNotAppliedException $e) {
return false;
if (null === ($criteria = $this->getCriteria($archives, $featured, $module))) {
return 0;
}

return NewsModel::countBy($criteria->getColumns(), $criteria->getValues());
Expand All @@ -48,12 +43,8 @@ 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
{
try {
if (null === ($criteria = $this->getCriteria($archives, $featured, $module))) {
return null;
}
} catch (CategoryFilteringNotAppliedException $e) {
return false;
if (null === ($criteria = $this->getCriteria($archives, $featured, $module))) {
return null;
}

$criteria->setLimit($limit);
Expand All @@ -69,7 +60,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, true);
$criteria = $this->searchBuilder->getCriteriaForListModule($archives, $featured, $module);
} catch (CategoryNotFoundException $e) {
throw new PageNotFoundException($e->getMessage(), 0, $e);
}
Expand Down
15 changes: 0 additions & 15 deletions src/Exception/CategoryFilteringNotAppliedException.php

This file was deleted.

0 comments on commit daa53b8

Please sign in to comment.