[FEATURE] allow adding of user filters in filter order - template still to be done

This commit is contained in:
Julie Lenaerts 2023-07-05 10:54:37 +02:00
parent 0e5f1b4ab9
commit 25d4b6acbb
3 changed files with 83 additions and 1 deletions

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\MainBundle\Form\Type\Listing; namespace Chill\MainBundle\Form\Type\Listing;
use Chill\MainBundle\Form\Type\ChillDateType; use Chill\MainBundle\Form\Type\ChillDateType;
use Chill\MainBundle\Form\Type\PickUserDynamicType;
use Chill\MainBundle\Templating\Listing\FilterOrderHelper; use Chill\MainBundle\Templating\Listing\FilterOrderHelper;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
@ -120,6 +121,35 @@ final class FilterOrderType extends \Symfony\Component\Form\AbstractType
$builder->add($singleCheckBoxBuilder); $builder->add($singleCheckBoxBuilder);
} }
if ([] !== $helper->getUserPickers()) {
$userPickersBuilder = $builder->create('userPicker', null, ['compound' => true]);
foreach ($helper->getUserPickers() as $name => [
'label' => $label, 'options' => $options
]) {
$userPicker = $userPickersBuilder->create($name, null, [
'compound' => true,
'label' => $label,
]);
$userPicker->add(
$name,
PickUserDynamicType::class,
[
'multiple' => true,
'label' => $label,
...$options,
]
);
$userPickersBuilder->add($userPicker);
}
$builder->add($userPickersBuilder);
}
} }
public function buildView(FormView $view, FormInterface $form, array $options) public function buildView(FormView $view, FormInterface $form, array $options)

View File

@ -52,6 +52,11 @@ class FilterOrderHelper
*/ */
private array $entityChoices = []; private array $entityChoices = [];
/**
* @var array<string, array{label: string, options: array}>
*/
private array $userPickers = [];
public function __construct( public function __construct(
FormFactoryInterface $formFactory, FormFactoryInterface $formFactory,
RequestStack $requestStack RequestStack $requestStack
@ -82,6 +87,14 @@ class FilterOrderHelper
return $this->entityChoices; return $this->entityChoices;
} }
public function addUserPickers(string $name, ?string $label = null, array $options = []): self
{
$this->userPickers[$name] = ['label' => $label, 'options' => $options];
return $this;
}
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) - 1; $missing = count($choices) - count($trans) - 1;
@ -116,6 +129,16 @@ class FilterOrderHelper
->handleRequest($this->requestStack->getCurrentRequest()); ->handleRequest($this->requestStack->getCurrentRequest());
} }
public function getUserPickers(): array
{
return $this->userPickers;
}
public function getUserPickerData(string $name): array
{
return $this->getFormData()['userPickers'][$name];
}
public function getCheckboxData(string $name): array public function getCheckboxData(string $name): array
{ {
return $this->getFormData()['checkboxes'][$name]; return $this->getFormData()['checkboxes'][$name];
@ -180,7 +203,8 @@ class FilterOrderHelper
'checkboxes' => [], 'checkboxes' => [],
'dateRanges' => [], 'dateRanges' => [],
'single_checkboxes' => [], 'single_checkboxes' => [],
'entity_choices' => [] 'entity_choices' => [],
'user_pickers' => []
]; ];
if ($this->hasSearchBox()) { if ($this->hasSearchBox()) {
@ -204,7 +228,12 @@ class FilterOrderHelper
$r['entity_choices'][$name] = ($c['options']['multiple'] ?? true) ? [] : null; $r['entity_choices'][$name] = ($c['options']['multiple'] ?? true) ? [] : null;
} }
foreach ($this->userPickers as $name => $u) {
$r['user_pickers'][$name] = ($u['options']['multiple'] ?? true) ? [] : null;
}
return $r; return $r;
} }
private function getFormData(): array private function getFormData(): array

View File

@ -37,6 +37,11 @@ class FilterOrderHelperBuilder
*/ */
private array $entityChoices = []; private array $entityChoices = [];
/**
* @var array<string, array{label: string, options: array}>
*/
private array $userPickers = [];
public function __construct( public function __construct(
FormFactoryInterface $formFactory, FormFactoryInterface $formFactory,
RequestStack $requestStack RequestStack $requestStack
@ -83,6 +88,13 @@ class FilterOrderHelperBuilder
return $this; return $this;
} }
public function addUserPickers(string $name, ?string $label = null, ?array $options = []): self
{
$this->userPickers[$name] = ['label' => $label, 'options' => $options];
return $this;
}
public function build(): FilterOrderHelper public function build(): FilterOrderHelper
{ {
$helper = new FilterOrderHelper( $helper = new FilterOrderHelper(
@ -124,6 +136,17 @@ class FilterOrderHelperBuilder
$helper->addDateRange($name, $label, $from, $to); $helper->addDateRange($name, $label, $from, $to);
} }
foreach (
$this->userPickers as $name => [
'label' => $label,
'options' => $options
]
) {
$helper->addUserPickers($name, $label, $options);
}
return $helper; return $helper;
} }
} }