mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-03 13:33:48 +00:00
DX: Add features to filterOrder
Allow to add single checkboxes and entitychoices to filter order
This commit is contained in:
@@ -13,6 +13,8 @@ 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;
|
||||
@@ -24,11 +26,16 @@ class FilterOrderHelper
|
||||
{
|
||||
private array $checkboxes = [];
|
||||
|
||||
/**
|
||||
* @var array<string, array{label: string}>
|
||||
*/
|
||||
private array $singleCheckbox = [];
|
||||
|
||||
private array $dateRanges = [];
|
||||
|
||||
private FormFactoryInterface $formFactory;
|
||||
|
||||
private ?string $formName = 'f';
|
||||
public const FORM_NAME = 'f';
|
||||
|
||||
private array $formOptions = [];
|
||||
|
||||
@@ -40,6 +47,11 @@ class FilterOrderHelper
|
||||
|
||||
private ?array $submitted = null;
|
||||
|
||||
/**
|
||||
* @var array<string, array{label: string, choices: array, options: array}>
|
||||
*/
|
||||
private array $entityChoices = [];
|
||||
|
||||
public function __construct(
|
||||
FormFactoryInterface $formFactory,
|
||||
RequestStack $requestStack
|
||||
@@ -48,7 +60,29 @@ class FilterOrderHelper
|
||||
$this->requestStack = $requestStack;
|
||||
}
|
||||
|
||||
public function addCheckbox(string $name, array $choices, ?array $default = [], ?array $trans = []): self
|
||||
public function addSingleCheckbox(string $name, string $label): self
|
||||
{
|
||||
$this->singleCheckbox[$name] = ['label' => $label];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string $class
|
||||
*/
|
||||
public function addEntityChoice(string $name, string $class, string $label, array $choices, array $options = []): self
|
||||
{
|
||||
$this->entityChoices[$name] = ['label' => $label, 'class' => $class, 'choices' => $choices, 'options' => $options];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getEntityChoices(): array
|
||||
{
|
||||
return $this->entityChoices;
|
||||
}
|
||||
|
||||
public function addCheckbox(string $name, array $choices, ?array $default = [], ?array $trans = [], array $options = []): self
|
||||
{
|
||||
$missing = count($choices) - count($trans) - 1;
|
||||
$this->checkboxes[$name] = [
|
||||
@@ -58,6 +92,7 @@ class FilterOrderHelper
|
||||
0 < $missing ?
|
||||
array_fill(0, $missing, null) : []
|
||||
),
|
||||
...$options,
|
||||
];
|
||||
|
||||
return $this;
|
||||
@@ -73,7 +108,7 @@ class FilterOrderHelper
|
||||
public function buildForm(): FormInterface
|
||||
{
|
||||
return $this->formFactory
|
||||
->createNamed($this->formName, $this->formType, $this->getDefaultData(), array_merge([
|
||||
->createNamed(self::FORM_NAME, $this->formType, $this->getDefaultData(), array_merge([
|
||||
'helper' => $this,
|
||||
'method' => 'GET',
|
||||
'csrf_protection' => false,
|
||||
@@ -86,13 +121,31 @@ class FilterOrderHelper
|
||||
return $this->getFormData()['checkboxes'][$name];
|
||||
}
|
||||
|
||||
public function getSingleCheckboxData(string $name): ?bool
|
||||
{
|
||||
return $this->getFormData()['single_checkboxes'][$name];
|
||||
}
|
||||
|
||||
public function getEntityChoiceData($name): mixed
|
||||
{
|
||||
return $this->getFormData()['entity_choices'][$name];
|
||||
}
|
||||
|
||||
public function getCheckboxes(): array
|
||||
{
|
||||
return $this->checkboxes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<'to': DateTimeImmutable, 'from': DateTimeImmutable>
|
||||
* @return array<string, array{label: string}>
|
||||
*/
|
||||
public function getSingleCheckbox(): array
|
||||
{
|
||||
return $this->singleCheckbox;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{to: ?DateTimeImmutable, from: ?DateTimeImmutable}
|
||||
*/
|
||||
public function getDateRangeData(string $name): array
|
||||
{
|
||||
@@ -123,7 +176,12 @@ class FilterOrderHelper
|
||||
|
||||
private function getDefaultData(): array
|
||||
{
|
||||
$r = [];
|
||||
$r = [
|
||||
'checkboxes' => [],
|
||||
'dateRanges' => [],
|
||||
'single_checkboxes' => [],
|
||||
'entity_choices' => []
|
||||
];
|
||||
|
||||
if ($this->hasSearchBox()) {
|
||||
$r['q'] = '';
|
||||
@@ -138,6 +196,14 @@ class FilterOrderHelper
|
||||
$r['dateRanges'][$name]['to'] = $defaults['to'];
|
||||
}
|
||||
|
||||
foreach ($this->singleCheckbox as $name => $c) {
|
||||
$r['single_checkboxes'][$name] = false;
|
||||
}
|
||||
|
||||
foreach ($this->entityChoices as $name => $c) {
|
||||
$r['entity_choices'][$name] = ($c['options']['multiple'] ?? true) ? [] : null;
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
|
@@ -27,6 +27,16 @@ class FilterOrderHelperBuilder
|
||||
|
||||
private ?array $searchBoxFields = null;
|
||||
|
||||
/**
|
||||
* @var array<string, array{label: string}>
|
||||
*/
|
||||
private array $singleCheckboxes = [];
|
||||
|
||||
/**
|
||||
* @var array<string, array{label: string, class: class-string, choices: array, options: array}>
|
||||
*/
|
||||
private array $entityChoices = [];
|
||||
|
||||
public function __construct(
|
||||
FormFactoryInterface $formFactory,
|
||||
RequestStack $requestStack
|
||||
@@ -35,6 +45,13 @@ class FilterOrderHelperBuilder
|
||||
$this->requestStack = $requestStack;
|
||||
}
|
||||
|
||||
public function addSingleCheckbox(string $name, string $label): self
|
||||
{
|
||||
$this->singleCheckboxes[$name] = ['label' => $label];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addCheckbox(string $name, array $choices, ?array $default = [], ?array $trans = []): self
|
||||
{
|
||||
$this->checkboxes[$name] = ['choices' => $choices, 'default' => $default, 'trans' => $trans];
|
||||
@@ -42,6 +59,16 @@ class FilterOrderHelperBuilder
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string $class
|
||||
*/
|
||||
public function addEntityChoice(string $name, string $label, string $class, array $choices, ?array $options = []): self
|
||||
{
|
||||
$this->entityChoices[$name] = ['label' => $label, 'class' => $class, 'choices' => $choices, 'options' => $options];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addDateRange(string $name, ?string $label = null, ?DateTimeImmutable $from = null, ?DateTimeImmutable $to = null): self
|
||||
{
|
||||
$this->dateRanges[$name] = ['from' => $from, 'to' => $to, 'label' => $label];
|
||||
@@ -75,6 +102,18 @@ class FilterOrderHelperBuilder
|
||||
$helper->addCheckbox($name, $choices, $default, $trans);
|
||||
}
|
||||
|
||||
foreach (
|
||||
$this->singleCheckboxes as $name => ['label' => $label]
|
||||
) {
|
||||
$helper->addSingleCheckbox($name, $label);
|
||||
}
|
||||
|
||||
foreach (
|
||||
$this->entityChoices as $name => ['label' => $label, 'class' => $class, 'choices' => $choices, 'options' => $options]
|
||||
) {
|
||||
$helper->addEntityChoice($name, $class, $label, $choices, $options);
|
||||
}
|
||||
|
||||
foreach (
|
||||
$this->dateRanges as $name => [
|
||||
'from' => $from,
|
||||
|
@@ -11,13 +11,23 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Templating\Listing;
|
||||
|
||||
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Twig\Environment;
|
||||
use Twig\Error\LoaderError;
|
||||
use Twig\Error\RuntimeError;
|
||||
use Twig\Error\SyntaxError;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
class Templating extends AbstractExtension
|
||||
{
|
||||
public function getFilters()
|
||||
public function __construct(
|
||||
private readonly RequestStack $requestStack,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getFilters(): array
|
||||
{
|
||||
return [
|
||||
new TwigFilter('chill_render_filter_order_helper', [$this, 'renderFilterOrderHelper'], [
|
||||
@@ -26,16 +36,41 @@ class Templating extends AbstractExtension
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SyntaxError
|
||||
* @throws RuntimeError
|
||||
* @throws LoaderError
|
||||
*/
|
||||
public function renderFilterOrderHelper(
|
||||
Environment $environment,
|
||||
FilterOrderHelper $helper,
|
||||
?string $template = '@ChillMain/FilterOrder/base.html.twig',
|
||||
?array $options = []
|
||||
) {
|
||||
): string {
|
||||
$otherParameters = [];
|
||||
|
||||
foreach ($this->requestStack->getCurrentRequest()->query->getIterator() as $key => $value) {
|
||||
switch ($key) {
|
||||
case FilterOrderHelper::FORM_NAME:
|
||||
break;
|
||||
|
||||
case PaginatorFactory::DEFAULT_CURRENT_PAGE_KEY:
|
||||
// when filtering, go back to page 1
|
||||
$otherParameters[PaginatorFactory::DEFAULT_CURRENT_PAGE_KEY] = 1;
|
||||
|
||||
break;
|
||||
default:
|
||||
$otherParameters[$key] = $value;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $environment->render($template, [
|
||||
'helper' => $helper,
|
||||
'form' => $helper->buildForm()->createView(),
|
||||
'options' => $options,
|
||||
'otherParameters' => $otherParameters,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user