From 25d4b6acbb2b5d547abb38072f69da7f764e4141 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 5 Jul 2023 10:54:37 +0200 Subject: [PATCH] [FEATURE] allow adding of user filters in filter order - template still to be done --- .../Form/Type/Listing/FilterOrderType.php | 30 ++++++++++++++++++ .../Templating/Listing/FilterOrderHelper.php | 31 ++++++++++++++++++- .../Listing/FilterOrderHelperBuilder.php | 23 ++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/src/Bundle/ChillMainBundle/Form/Type/Listing/FilterOrderType.php b/src/Bundle/ChillMainBundle/Form/Type/Listing/FilterOrderType.php index 16038515d..9aebab144 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Listing/FilterOrderType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Listing/FilterOrderType.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\MainBundle\Form\Type\Listing; use Chill\MainBundle\Form\Type\ChillDateType; +use Chill\MainBundle\Form\Type\PickUserDynamicType; use Chill\MainBundle\Templating\Listing\FilterOrderHelper; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; @@ -120,6 +121,35 @@ final class FilterOrderType extends \Symfony\Component\Form\AbstractType $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) diff --git a/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelper.php b/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelper.php index 28cc8e331..f12c23299 100644 --- a/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelper.php +++ b/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelper.php @@ -52,6 +52,11 @@ class FilterOrderHelper */ private array $entityChoices = []; + /** + * @var array + */ + private array $userPickers = []; + public function __construct( FormFactoryInterface $formFactory, RequestStack $requestStack @@ -82,6 +87,14 @@ class FilterOrderHelper 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 { $missing = count($choices) - count($trans) - 1; @@ -116,6 +129,16 @@ class FilterOrderHelper ->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 { return $this->getFormData()['checkboxes'][$name]; @@ -180,7 +203,8 @@ class FilterOrderHelper 'checkboxes' => [], 'dateRanges' => [], 'single_checkboxes' => [], - 'entity_choices' => [] + 'entity_choices' => [], + 'user_pickers' => [] ]; if ($this->hasSearchBox()) { @@ -204,7 +228,12 @@ class FilterOrderHelper $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; + } private function getFormData(): array diff --git a/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelperBuilder.php b/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelperBuilder.php index e176e27c6..6fd0eeb42 100644 --- a/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelperBuilder.php +++ b/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelperBuilder.php @@ -37,6 +37,11 @@ class FilterOrderHelperBuilder */ private array $entityChoices = []; + /** + * @var array + */ + private array $userPickers = []; + public function __construct( FormFactoryInterface $formFactory, RequestStack $requestStack @@ -83,6 +88,13 @@ class FilterOrderHelperBuilder 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 { $helper = new FilterOrderHelper( @@ -124,6 +136,17 @@ class FilterOrderHelperBuilder $helper->addDateRange($name, $label, $from, $to); } + + foreach ( + $this->userPickers as $name => [ + 'label' => $label, + 'options' => $options + ] + ) { + $helper->addUserPickers($name, $label, $options); + } + + return $helper; } }