mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-30 19:43:49 +00:00
[FilterOrder] add a method to get all the active filters
This commit is contained in:
@@ -13,16 +13,19 @@ namespace Chill\MainBundle\Templating\Listing;
|
||||
|
||||
use Chill\MainBundle\Form\Type\Listing\FilterOrderType;
|
||||
use DateTimeImmutable;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
use Symfony\Component\PropertyAccess\PropertyAccessor;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
||||
use Symfony\Component\PropertyAccess\PropertyPath;
|
||||
use Symfony\Component\PropertyAccess\PropertyPathInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use function array_merge;
|
||||
use function count;
|
||||
|
||||
class FilterOrderHelper
|
||||
final class FilterOrderHelper
|
||||
{
|
||||
private array $checkboxes = [];
|
||||
|
||||
@@ -33,16 +36,12 @@ class FilterOrderHelper
|
||||
|
||||
private array $dateRanges = [];
|
||||
|
||||
private FormFactoryInterface $formFactory;
|
||||
|
||||
public const FORM_NAME = 'f';
|
||||
|
||||
private array $formOptions = [];
|
||||
|
||||
private string $formType = FilterOrderType::class;
|
||||
|
||||
private RequestStack $requestStack;
|
||||
|
||||
private ?array $searchBoxFields = null;
|
||||
|
||||
private ?array $submitted = null;
|
||||
@@ -52,12 +51,13 @@ class FilterOrderHelper
|
||||
*/
|
||||
private array $entityChoices = [];
|
||||
|
||||
|
||||
public function __construct(
|
||||
FormFactoryInterface $formFactory,
|
||||
RequestStack $requestStack
|
||||
private readonly FormFactoryInterface $formFactory,
|
||||
private readonly RequestStack $requestStack,
|
||||
private readonly TranslatorInterface $translator,
|
||||
private readonly PropertyAccessorInterface $propertyAccessor,
|
||||
) {
|
||||
$this->formFactory = $formFactory;
|
||||
$this->requestStack = $requestStack;
|
||||
}
|
||||
|
||||
public function addSingleCheckbox(string $name, string $label): self
|
||||
@@ -199,6 +199,63 @@ 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, 'options' => $options]) {
|
||||
foreach ($this->getCheckboxData($name) as $keyChoice) {
|
||||
$result[] = ['value' => $choices['keyChoice'], 'label' => $options['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' => $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 = [
|
||||
|
@@ -14,6 +14,8 @@ namespace Chill\MainBundle\Templating\Listing;
|
||||
use DateTimeImmutable;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class FilterOrderHelperBuilder
|
||||
{
|
||||
@@ -39,7 +41,9 @@ class FilterOrderHelperBuilder
|
||||
|
||||
public function __construct(
|
||||
FormFactoryInterface $formFactory,
|
||||
RequestStack $requestStack
|
||||
RequestStack $requestStack,
|
||||
private readonly TranslatorInterface $translator,
|
||||
private readonly PropertyAccessorInterface $propertyAccessor,
|
||||
) {
|
||||
$this->formFactory = $formFactory;
|
||||
$this->requestStack = $requestStack;
|
||||
@@ -87,7 +91,9 @@ class FilterOrderHelperBuilder
|
||||
{
|
||||
$helper = new FilterOrderHelper(
|
||||
$this->formFactory,
|
||||
$this->requestStack
|
||||
$this->requestStack,
|
||||
$this->translator,
|
||||
$this->propertyAccessor
|
||||
);
|
||||
|
||||
$helper->setSearchBox($this->searchBoxFields);
|
||||
|
@@ -13,6 +13,8 @@ namespace Chill\MainBundle\Templating\Listing;
|
||||
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class FilterOrderHelperFactory implements FilterOrderHelperFactoryInterface
|
||||
{
|
||||
@@ -22,7 +24,9 @@ class FilterOrderHelperFactory implements FilterOrderHelperFactoryInterface
|
||||
|
||||
public function __construct(
|
||||
FormFactoryInterface $formFactory,
|
||||
RequestStack $requestStack
|
||||
RequestStack $requestStack,
|
||||
private readonly TranslatorInterface $translator,
|
||||
private readonly PropertyAccessorInterface $propertyAccessor,
|
||||
) {
|
||||
$this->formFactory = $formFactory;
|
||||
$this->requestStack = $requestStack;
|
||||
@@ -30,6 +34,6 @@ class FilterOrderHelperFactory implements FilterOrderHelperFactoryInterface
|
||||
|
||||
public function create(string $context, ?array $options = []): FilterOrderHelperBuilder
|
||||
{
|
||||
return new FilterOrderHelperBuilder($this->formFactory, $this->requestStack);
|
||||
return new FilterOrderHelperBuilder($this->formFactory, $this->requestStack, $this->translator, $this->propertyAccessor);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\MainBundle\Templating\Listing;
|
||||
|
||||
enum FilterOrderPositionEnum: string
|
||||
{
|
||||
case SearchBox = 'search_box';
|
||||
case Checkboxes = 'checkboxes';
|
||||
case DateRange = 'date_range';
|
||||
case EntityChoice = 'entity_choice';
|
||||
case SingleCheckbox = 'single_checkbox';
|
||||
}
|
Reference in New Issue
Block a user