mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Fixed: [filterOrder] refactor active filter helper to a dedicated class and fix loading of multiple entity choices
This commit is contained in:
parent
bf93c1ddb2
commit
88114e3ba6
@ -93,7 +93,6 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% set active = helper.getActiveFilters() %}
|
|
||||||
{% if active|length > 0 %}
|
{% if active|length > 0 %}
|
||||||
<div class="activeFilters mt-3">
|
<div class="activeFilters mt-3">
|
||||||
{% for f in active %}
|
{% for f in active %}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -55,8 +55,6 @@ final class FilterOrderHelper
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly FormFactoryInterface $formFactory,
|
private readonly FormFactoryInterface $formFactory,
|
||||||
private readonly RequestStack $requestStack,
|
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
|
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] = [
|
$this->checkboxes[$name] = [
|
||||||
'choices' => $choices,
|
'choices' => $choices,
|
||||||
'default' => $default,
|
'default' => $default,
|
||||||
'trans' => array_merge(
|
'trans' => $trans,
|
||||||
$trans,
|
|
||||||
0 < $missing ?
|
|
||||||
array_fill(0, $missing, null) : []
|
|
||||||
),
|
|
||||||
...$options,
|
...$options,
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -201,64 +197,6 @@ final class FilterOrderHelper
|
|||||||
return $this;
|
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
|
private function getDefaultData(): array
|
||||||
{
|
{
|
||||||
$r = [
|
$r = [
|
||||||
|
@ -42,8 +42,6 @@ class FilterOrderHelperBuilder
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
FormFactoryInterface $formFactory,
|
FormFactoryInterface $formFactory,
|
||||||
RequestStack $requestStack,
|
RequestStack $requestStack,
|
||||||
private readonly TranslatorInterface $translator,
|
|
||||||
private readonly PropertyAccessorInterface $propertyAccessor,
|
|
||||||
) {
|
) {
|
||||||
$this->formFactory = $formFactory;
|
$this->formFactory = $formFactory;
|
||||||
$this->requestStack = $requestStack;
|
$this->requestStack = $requestStack;
|
||||||
@ -92,8 +90,6 @@ class FilterOrderHelperBuilder
|
|||||||
$helper = new FilterOrderHelper(
|
$helper = new FilterOrderHelper(
|
||||||
$this->formFactory,
|
$this->formFactory,
|
||||||
$this->requestStack,
|
$this->requestStack,
|
||||||
$this->translator,
|
|
||||||
$this->propertyAccessor
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$helper->setSearchBox($this->searchBoxFields);
|
$helper->setSearchBox($this->searchBoxFields);
|
||||||
|
@ -25,8 +25,6 @@ class FilterOrderHelperFactory implements FilterOrderHelperFactoryInterface
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
FormFactoryInterface $formFactory,
|
FormFactoryInterface $formFactory,
|
||||||
RequestStack $requestStack,
|
RequestStack $requestStack,
|
||||||
private readonly TranslatorInterface $translator,
|
|
||||||
private readonly PropertyAccessorInterface $propertyAccessor,
|
|
||||||
) {
|
) {
|
||||||
$this->formFactory = $formFactory;
|
$this->formFactory = $formFactory;
|
||||||
$this->requestStack = $requestStack;
|
$this->requestStack = $requestStack;
|
||||||
@ -34,6 +32,6 @@ class FilterOrderHelperFactory implements FilterOrderHelperFactoryInterface
|
|||||||
|
|
||||||
public function create(string $context, ?array $options = []): FilterOrderHelperBuilder
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ class Templating extends AbstractExtension
|
|||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly RequestStack $requestStack,
|
private readonly RequestStack $requestStack,
|
||||||
|
private readonly FilterOrderGetActiveFilterHelper $filterOrderGetActiveFilterHelper,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,6 +69,7 @@ class Templating extends AbstractExtension
|
|||||||
|
|
||||||
return $environment->render($template, [
|
return $environment->render($template, [
|
||||||
'helper' => $helper,
|
'helper' => $helper,
|
||||||
|
'active' => $this->filterOrderGetActiveFilterHelper->getActiveFilters($helper),
|
||||||
'form' => $helper->buildForm()->createView(),
|
'form' => $helper->buildForm()->createView(),
|
||||||
'options' => $options,
|
'options' => $options,
|
||||||
'otherParameters' => $otherParameters,
|
'otherParameters' => $otherParameters,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user