Skip to content

Commit

Permalink
Improve filters handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Apr 24, 2024
1 parent 2ff485c commit deace31
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 11 deletions.
10 changes: 10 additions & 0 deletions config/services/twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Webgriffe\SyliusElasticsearchPlugin\Twig\FilterExtension;
use Webgriffe\SyliusElasticsearchPlugin\Twig\FilterRuntime;
use Webgriffe\SyliusElasticsearchPlugin\Twig\SearchExtension;
use Webgriffe\SyliusElasticsearchPlugin\Twig\SearchRuntime;

Expand All @@ -20,4 +22,12 @@
])
->tag('twig.runtime')
;

$services->set('webgriffe.sylius_elasticsearch_plugin.twig.extension.filter', FilterExtension::class)
->tag('twig.extension')
;

$services->set('webgriffe.sylius_elasticsearch_plugin.twig.runtime.filter', FilterRuntime::class)
->tag('twig.runtime')
;
};
2 changes: 1 addition & 1 deletion src/Controller/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __invoke(Request $request, string $slug): Response
$size = $request->query->getInt('limit', $this->taxonDefaultPageLimit);
$page = $request->query->getInt('page', 1);

/** @var array<string, array<string, string>> $requestFilters */
/** @var array<string, array<array-key, array{code: string, value: string}>> $requestFilters */
$requestFilters = $request->query->all('filters');
$filters = FilterHelper::retrieveFilters($requestFilters);

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function __invoke(Request $request, ?string $query = null): Response
$size = $request->query->getInt('limit', $this->searchDefaultPageLimit);
$page = $request->query->getInt('page', 1);

/** @var array<string, array<string, string>> $requestFilters */
/** @var array<string, array<array-key, array{code: string, value: string}>> $requestFilters */
$requestFilters = $request->query->all('filters');
$filters = FilterHelper::retrieveFilters($requestFilters);

Expand Down
8 changes: 4 additions & 4 deletions src/FilterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
final class FilterHelper
{
/**
* @param array<string, array<string, string>> $requestFilters
* @param array<string, array<array-key, array{code: string, value: string}>> $requestFilters
*
* @return QueryFilters
*/
Expand All @@ -31,10 +31,10 @@ public static function retrieveFilters(array $requestFilters = []): array
if (!array_key_exists($type, $filters)) {
continue;
}
foreach ($filter as $code => $value) {
foreach ($filter as $value) {
$filters[$type][] = [
'code' => $code,
'value' => $value,
'code' => $value['code'],
'value' => $value['value'],
];
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/Twig/FilterExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Webgriffe\SyliusElasticsearchPlugin\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;

final class FilterExtension extends AbstractExtension
{
public function getFilters(): array
{
return [
new TwigFilter('is_filter_active', [FilterRuntime::class, 'isFilterActive']),
];
}

public function getFunctions(): array
{
return [
new TwigFunction('merge_filter_value_with_current_active_filters', [FilterRuntime::class, 'mergeFilterValueWithCurrentActiveFilters']),
];
}
}
58 changes: 58 additions & 0 deletions src/Twig/FilterRuntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Webgriffe\SyliusElasticsearchPlugin\Twig;

use Twig\Extension\RuntimeExtensionInterface;
use Webgriffe\SyliusElasticsearchPlugin\Model\FilterInterface;
use Webgriffe\SyliusElasticsearchPlugin\Model\FilterValueInterface;

final class FilterRuntime implements RuntimeExtensionInterface
{
/**
* @param array<string, array<array-key, array{code: string, value: string}>> $activeFilters
*/
public function isFilterActive(FilterValueInterface $filterValue, FilterInterface $filter, array $activeFilters): bool
{
if (!array_key_exists($filter->getType(), $activeFilters)) {
return false;
}
$appliedFilterValues = $activeFilters[$filter->getType()];
foreach ($appliedFilterValues as $appliedFilterValue) {
if ($appliedFilterValue['code'] === $filter->getKeyCode() &&
$appliedFilterValue['value'] === $filterValue->getKey()
) {
return true;
}
}

return false;
}

/**
* @param array<string, array<array-key, array{code: string, value: string}>> $activeFilters
*/
public function mergeFilterValueWithCurrentActiveFilters(
FilterValueInterface $filterValue,
FilterInterface $filter,
array $activeFilters,
): array {
$filterType = $filter->getType();
if (!array_key_exists($filterType, $activeFilters)) {
$activeFilters[$filterType] = [];
}
foreach ($activeFilters[$filterType] as $appliedFilterValue) {
if ($appliedFilterValue['code'] === $filter->getKeyCode() &&
$appliedFilterValue['value'] === $filterValue->getKey()) {
return $activeFilters;
}
}
$activeFilters[$filterType][] = [
'code' => $filter->getKeyCode(),
'value' => $filterValue->getKey(),
];

return $activeFilters;
}
}
9 changes: 4 additions & 5 deletions templates/Common/Components/_attributes.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
<h3>{{ filter.name }}</h3>
<ul>
{% for filterValue in filter.values %}
{% set isCurrentOptionValueUsedAsFilter = currentAppliedFilters[filter.type] is defined and
currentAppliedFilters[filter.type][filter.keyCode] is defined and
currentAppliedFilters[filter.type][filter.keyCode] == filterValue.key
%}
{% set isCurrentOptionValueUsedAsFilter = filterValue|is_filter_active(filter, currentAppliedFilters) %}

<li>
{% set path = path(
app.request.attributes.get('_route'),
app.request.attributes.all('_route_params')|merge(app.request.query.all)|merge({(filter.queryStringKey): filterValue.key}),
app.request.attributes.all('_route_params')|merge({
'filters': merge_filter_value_with_current_active_filters(filterValue, filter, currentAppliedFilters),
}),
) %}
<a class="item {% if isCurrentOptionValueUsedAsFilter %}disabled{% endif %}"
href="{{ path }}"
Expand Down

0 comments on commit deace31

Please sign in to comment.