Fixed: [filterOrder] refactor active filter helper to a dedicated class and fix loading of multiple entity choices

This commit is contained in:
Julien Fastré 2023-07-11 14:17:02 +02:00
parent bf93c1ddb2
commit 88114e3ba6
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
6 changed files with 91 additions and 74 deletions

View File

@ -93,7 +93,6 @@
{% endif %}
</div>
</div>
{% set active = helper.getActiveFilters() %}
{% if active|length > 0 %}
<div class="activeFilters mt-3">
{% for f in active %}

View File

@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Templating\Listing;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyAccess\PropertyPathInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
final readonly class FilterOrderGetActiveFilterHelper
{
public function __construct(
private TranslatorInterface $translator,
private PropertyAccessorInterface $propertyAccessor,
) {
}
/**
* Return all the data required to display the active filters
*
* @param FilterOrderHelper $filterOrderHelper
* @return array<array{label: string, value: string, position: string, name: string}>
*/
public function getActiveFilters(FilterOrderHelper $filterOrderHelper): array
{
$result = [];
if ($filterOrderHelper->hasSearchBox() && '' !== $filterOrderHelper->getQueryString()) {
$result[] = ['label' => '', 'value' => $filterOrderHelper->getQueryString(), 'position' => FilterOrderPositionEnum::SearchBox->value, 'name' => 'q'];
}
foreach ($filterOrderHelper->getDateRanges() as $name => ['label' => $label]) {
$base = ['position' => FilterOrderPositionEnum::DateRange->value, 'name' => $name, 'label' => (string)$label];
if (null !== ($from = $filterOrderHelper->getDateRangeData($name)['from'] ?? null)) {
$result[] = ['value' => $this->translator->trans('filter_order.by_date.From', ['from_date' => $from]), ...$base];
}
if (null !== ($to = $filterOrderHelper->getDateRangeData($name)['to'] ?? null)) {
$result[] = ['value' => $this->translator->trans('filter_order.by_date.To', ['to_date' => $to]), ...$base];
}
}
foreach ($filterOrderHelper->getCheckboxes() as $name => ['choices' => $choices, 'trans' => $trans]) {
$translatedChoice = array_combine($choices, [...$trans]);
foreach ($filterOrderHelper->getCheckboxData($name) as $keyChoice) {
$result[] = ['value' => $this->translator->trans($translatedChoice[$keyChoice]), 'label' => '', 'position' => FilterOrderPositionEnum::Checkboxes->value, 'name' => $name];
}
}
foreach ($filterOrderHelper->getEntityChoices() as $name => ['label' => $label, 'class' => $class, 'choices' => $choices, 'options' => $options]) {
foreach ($filterOrderHelper->getEntityChoiceData($name) as $selected) {
if (is_callable($options['choice_label'])) {
$value = call_user_func($options['choice_label'], $selected);
} elseif ($options['choice_label'] instanceof PropertyPathInterface || is_string($options['choice_label'])) {
$value = $this->propertyAccessor->getValue($selected, $options['choice_label']);
} else {
if (!$selected instanceof \Stringable) {
throw new \UnexpectedValueException(sprintf("we are not able to transform the value of %s to a string. Implements \\Stringable or add a 'choice_label' option to the filterFormBuilder", get_class($selected)));
}
$value = (string)$selected;
}
$result[] = ['value' => $this->translator->trans($value), 'label' => $label, 'position' => FilterOrderPositionEnum::EntityChoice->value, 'name' => $name];
}
}
foreach ($filterOrderHelper->getSingleCheckbox() as $name => ['label' => $label]) {
if (true === $filterOrderHelper->getSingleCheckboxData($name)) {
$result[] = ['label' => '', 'value' => $this->translator->trans($label), 'position' => FilterOrderPositionEnum::SingleCheckbox->value, 'name' => $name];
}
}
return $result;
}
}

View File

@ -55,8 +55,6 @@ final class FilterOrderHelper
public function __construct(
private readonly FormFactoryInterface $formFactory,
private readonly RequestStack $requestStack,
private readonly TranslatorInterface $translator,
private readonly PropertyAccessorInterface $propertyAccessor,
) {
}
@ -84,16 +82,14 @@ final class FilterOrderHelper
public function addCheckbox(string $name, array $choices, ?array $default = [], ?array $trans = [], array $options = []): self
{
$missing = count($choices) - count($trans);
if ([] === $trans) {
$trans = $choices;
}
$this->checkboxes[$name] = [
'choices' => $choices,
'default' => $default,
'trans' => array_merge(
$trans,
0 < $missing ?
array_fill(0, $missing, null) : []
),
'trans' => $trans,
...$options,
];
@ -201,64 +197,6 @@ final class FilterOrderHelper
return $this;
}
/**
* Return all the data required to display the active filters
*
* @return array<array{label: string, value: string, position: string, name: string}>
*/
public function getActiveFilters(): array
{
$result = [];
if ($this->hasSearchBox() && '' !== $this->getQueryString()) {
$result[] = ['label' => '', 'value' => $this->getQueryString(), 'position' => FilterOrderPositionEnum::SearchBox->value, 'name' => 'q'];
}
foreach ($this->dateRanges as $name => ['label' => $label]) {
$base = ['position' => FilterOrderPositionEnum::DateRange->value, 'name' => $name, 'label' => (string) $label];
if (null !== ($from = $this->getDateRangeData($name)['from'] ?? null)) {
$result[] = ['value' => $this->translator->trans('filter_order.by_date.From', ['from_date' => $from]), ...$base];
}
if (null !== ($to = $this->getDateRangeData($name)['to'] ?? null)) {
$result[] = ['value' => $this->translator->trans('filter_order.by_date.To', ['to_date' => $to]), ...$base];
}
}
foreach ($this->checkboxes as $name => ['choices' => $choices, 'trans' => $trans]) {
$translatedChoice = array_combine($choices, [...$trans]);
foreach ($this->getCheckboxData($name) as $keyChoice) {
$result[] = ['value' => $this->translator->trans($translatedChoice[$keyChoice]), 'label' => '', 'position' => FilterOrderPositionEnum::Checkboxes->value, 'name' => $name];
}
}
foreach ($this->entityChoices as $name => ['label' => $label, 'class' => $class, 'choices' => $choices, 'options' => $options]) {
foreach ($this->getEntityChoiceData($name) as $selected) {
if (is_callable($options['choice_label'])) {
$value = call_user_func($options['choice_label'], $selected);
} elseif ($options['choice_label'] instanceof PropertyPathInterface || is_string($options['choice_label'])) {
$value = $this->propertyAccessor->getValue($selected, $options['choice_label']);
} else {
if (!$selected instanceof \Stringable) {
throw new \UnexpectedValueException(sprintf("we are not able to transform the value of %s to a string. Implements \\Stringable or add a 'choice_label' option to the filterFormBuilder", get_class($selected)));
}
$value = (string) $selected;
}
$result[] = ['value' => $this->translator->trans($value), 'label' => $label, 'position' => FilterOrderPositionEnum::EntityChoice->value, 'name' => $name];
}
}
foreach ($this->singleCheckbox as $name => ['label' => $label]) {
if (true === $this->getSingleCheckboxData($name)) {
$result[] = ['label' => '', 'value' => $this->translator->trans($label), 'position' => FilterOrderPositionEnum::SingleCheckbox->value, 'name' => $name];
}
}
return $result;
}
private function getDefaultData(): array
{
$r = [

View File

@ -42,8 +42,6 @@ class FilterOrderHelperBuilder
public function __construct(
FormFactoryInterface $formFactory,
RequestStack $requestStack,
private readonly TranslatorInterface $translator,
private readonly PropertyAccessorInterface $propertyAccessor,
) {
$this->formFactory = $formFactory;
$this->requestStack = $requestStack;
@ -92,8 +90,6 @@ class FilterOrderHelperBuilder
$helper = new FilterOrderHelper(
$this->formFactory,
$this->requestStack,
$this->translator,
$this->propertyAccessor
);
$helper->setSearchBox($this->searchBoxFields);

View File

@ -25,8 +25,6 @@ class FilterOrderHelperFactory implements FilterOrderHelperFactoryInterface
public function __construct(
FormFactoryInterface $formFactory,
RequestStack $requestStack,
private readonly TranslatorInterface $translator,
private readonly PropertyAccessorInterface $propertyAccessor,
) {
$this->formFactory = $formFactory;
$this->requestStack = $requestStack;
@ -34,6 +32,6 @@ class FilterOrderHelperFactory implements FilterOrderHelperFactoryInterface
public function create(string $context, ?array $options = []): FilterOrderHelperBuilder
{
return new FilterOrderHelperBuilder($this->formFactory, $this->requestStack, $this->translator, $this->propertyAccessor);
return new FilterOrderHelperBuilder($this->formFactory, $this->requestStack);
}
}

View File

@ -24,6 +24,7 @@ class Templating extends AbstractExtension
{
public function __construct(
private readonly RequestStack $requestStack,
private readonly FilterOrderGetActiveFilterHelper $filterOrderGetActiveFilterHelper,
) {
}
@ -68,6 +69,7 @@ class Templating extends AbstractExtension
return $environment->render($template, [
'helper' => $helper,
'active' => $this->filterOrderGetActiveFilterHelper->getActiveFilters($helper),
'form' => $helper->buildForm()->createView(),
'options' => $options,
'otherParameters' => $otherParameters,