mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-03-03 20:49:41 +00:00
Add support for integer fields in FilterOrderHelper and FilterOrderHelperBuilder
- Introduced methods to define, retrieve, and process integer fields in `FilterOrderHelper` and `FilterOrderHelperBuilder`. - Updated `FilterOrderType` to render integer fields dynamically as form inputs. - Added corresponding support in `FilterOrderGetActiveFilterHelper` to handle integer field data. - Updated `FilterOrderPositionEnum` and template files to include integer field rendering logic. - Enhanced entity labels with translation capabilities where applicable.
This commit is contained in:
@@ -17,6 +17,7 @@ use Chill\MainBundle\Templating\Listing\FilterOrderHelper;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SearchType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
@@ -129,6 +130,23 @@ final class FilterOrderType extends \Symfony\Component\Form\AbstractType
|
||||
|
||||
$builder->add($userPickersBuilder);
|
||||
}
|
||||
|
||||
if ([] !== $helper->getIntegerFields()) {
|
||||
$integerFieldBuilder = $builder->create('integer_fields', null, ['compound' => true]);
|
||||
foreach ($helper->getIntegerFields() as $name => ['label' => $label, 'options' => $opts]) {
|
||||
$integerFieldBuilder->add(
|
||||
$name,
|
||||
IntegerType::class,
|
||||
[
|
||||
'label' => $label,
|
||||
'required' => false,
|
||||
...$opts,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
$builder->add($integerFieldBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
public static function buildCheckboxChoices(array $choices, array $trans = []): array
|
||||
|
||||
@@ -115,6 +115,23 @@
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
{% if form.integer_fields is defined %}
|
||||
{% set btnSubmit = 1 %}
|
||||
{% for name, _o in form.integer_fields %}
|
||||
<div class="row my-2">
|
||||
{% if form.integer_fields[name].vars.label is not same as(false) %}
|
||||
{{ form_label(form.integer_fields[name]) }}
|
||||
{% else %}
|
||||
{{ form_label(form.integer_fields[name].vars.label) }}
|
||||
{% endif %}
|
||||
<div class="col-sm-8 pt-2">
|
||||
{{ form_widget(form.integer_fields[name]) }}
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
{% endif %}
|
||||
|
||||
{% if btnSubmit == 1 %}
|
||||
<div class="row my-2">
|
||||
<button type="submit" class="btn btn-sm btn-misc"><i class="fa fa-fw fa-filter"></i>{{ 'Filter'|trans }}</button>
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace Chill\MainBundle\Templating\Listing;
|
||||
use Chill\MainBundle\Templating\Entity\UserRender;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
||||
use Symfony\Component\PropertyAccess\PropertyPathInterface;
|
||||
use Symfony\Contracts\Translation\TranslatableInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
final readonly class FilterOrderGetActiveFilterHelper
|
||||
@@ -85,6 +86,13 @@ final readonly class FilterOrderGetActiveFilterHelper
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($filterOrderHelper->getIntegerFields() as $name => ['label' => $label]) {
|
||||
if (null !== $integerData = $filterOrderHelper->getIntegerField($name)) {
|
||||
$translated = $label instanceof TranslatableInterface ? $label->trans($this->translator) : $this->translator->trans($label);
|
||||
$result[] = ['label' => $translated, 'position' => FilterOrderPositionEnum::IntegerField->value, 'name' => $name, 'value' => $integerData];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,15 +40,20 @@ final class FilterOrderHelper
|
||||
private ?array $submitted = null;
|
||||
|
||||
/**
|
||||
* @var array<string, array{label: string, choices: array, options: array}>
|
||||
* @var array<string, array{label: string|TranslatableInterface, choices: array, options: array}>
|
||||
*/
|
||||
private array $entityChoices = [];
|
||||
|
||||
/**
|
||||
* @var array<string, array{label: string, options: array}>
|
||||
* @var array<string, array{label: TranslatableInterface|string, options: array}>
|
||||
*/
|
||||
private array $userPickers = [];
|
||||
|
||||
/**
|
||||
* @var array<string, array{label: string|TranslatableInterface, options: array}>
|
||||
*/
|
||||
private array $integerFields = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly FormFactoryInterface $formFactory,
|
||||
private readonly RequestStack $requestStack,
|
||||
@@ -122,6 +127,11 @@ final class FilterOrderHelper
|
||||
return $this->userPickers;
|
||||
}
|
||||
|
||||
public function getIntegerField(string $name): ?int
|
||||
{
|
||||
return $this->getFormData()['integer_fields'][$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<User>
|
||||
*/
|
||||
@@ -213,6 +223,23 @@ final class FilterOrderHelper
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addIntegerField(string $name, string|TranslatableInterface|null $label, array $options): self
|
||||
{
|
||||
$this->integerFields[$name] = ['label' => $label, 'options' => $options];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getIntegerFields(): array
|
||||
{
|
||||
return $this->integerFields;
|
||||
}
|
||||
|
||||
public function getIntegerFieldData(string $name): ?int
|
||||
{
|
||||
return $this->getFormData()['integer_fields'][$name];
|
||||
}
|
||||
|
||||
private function getDefaultData(): array
|
||||
{
|
||||
$r = [
|
||||
@@ -221,6 +248,7 @@ final class FilterOrderHelper
|
||||
'single_checkboxes' => [],
|
||||
'entity_choices' => [],
|
||||
'user_pickers' => [],
|
||||
'integer_fields' => [],
|
||||
];
|
||||
|
||||
if ($this->hasSearchBox()) {
|
||||
@@ -248,6 +276,10 @@ final class FilterOrderHelper
|
||||
$r['user_pickers'][$name] = ($u['options']['multiple'] ?? true) ? [] : null;
|
||||
}
|
||||
|
||||
foreach ($this->integerFields as $name => $c) {
|
||||
$r['integer_fields'][$name] = null;
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,20 +24,25 @@ class FilterOrderHelperBuilder
|
||||
private ?array $searchBoxFields = null;
|
||||
|
||||
/**
|
||||
* @var array<string, array{label: string}>
|
||||
* @var array<string, array{label: string|TranslatableInterface}>
|
||||
*/
|
||||
private array $singleCheckboxes = [];
|
||||
|
||||
/**
|
||||
* @var array<string, array{label: string, class: class-string, choices: array, options: array}>
|
||||
* @var array<string, array{label: string|TranslatableInterface, class: class-string, choices: array, options: array}>
|
||||
*/
|
||||
private array $entityChoices = [];
|
||||
|
||||
/**
|
||||
* @var array<string, array{label: string, options: array}>
|
||||
* @var array<string, array{label: string|TranslatableInterface, options: array}>
|
||||
*/
|
||||
private array $userPickers = [];
|
||||
|
||||
/**
|
||||
* @var array<string, array{label: string|TranslatableInterface, options: array}>
|
||||
*/
|
||||
private array $integerField = [];
|
||||
|
||||
public function __construct(private readonly FormFactoryInterface $formFactory, private readonly RequestStack $requestStack) {}
|
||||
|
||||
public function addSingleCheckbox(string $name, string|TranslatableInterface $label): self
|
||||
@@ -71,6 +76,13 @@ class FilterOrderHelperBuilder
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addIntegerField(string $name, string|TranslatableInterface|null $label = null, array $options = []): self
|
||||
{
|
||||
$this->integerField[$name] = ['label' => $label, 'options' => $options];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addSearchBox(?array $fields = [], ?array $options = []): self
|
||||
{
|
||||
$this->searchBoxFields = $fields;
|
||||
@@ -135,6 +147,10 @@ class FilterOrderHelperBuilder
|
||||
$helper->addUserPicker($name, $label, $options);
|
||||
}
|
||||
|
||||
foreach ($this->integerField as $name => $label) {
|
||||
$helper->addIntegerField($name, $label['label'], $label['options']);
|
||||
}
|
||||
|
||||
return $helper;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,4 +19,6 @@ enum FilterOrderPositionEnum: string
|
||||
case EntityChoice = 'entity_choice';
|
||||
case SingleCheckbox = 'single_checkbox';
|
||||
case UserPicker = 'user_picker';
|
||||
|
||||
case IntegerField = 'integer_field';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user