diff --git a/src/Bundle/ChillMainBundle/Form/Type/Listing/FilterOrderType.php b/src/Bundle/ChillMainBundle/Form/Type/Listing/FilterOrderType.php index 3976625a1..3bbd6b9ae 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Listing/FilterOrderType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Listing/FilterOrderType.php @@ -3,9 +3,12 @@ namespace Chill\MainBundle\Form\Type\Listing; use Chill\MainBundle\Templating\Listing\FilterOrderHelper; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\SearchType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; use Symfony\Component\HttpFoundation\RequestStack; final class FilterOrderType extends \Symfony\Component\Form\AbstractType @@ -29,9 +32,27 @@ final class FilterOrderType extends \Symfony\Component\Form\AbstractType ]); } + foreach ($helper->getCheckboxes() as $name => $c) { + + $choices = \array_combine( + \array_map(function($c, $t) { + if ($t !== NULL) { return $t; } + else { return $c; } + }, $c['choices'], $c['trans']), + $c['choices'] + ); + + $builder->add('c_'.$name, ChoiceType::class, [ + 'choices' => $choices, + 'expanded' => true, + 'multiple' => true, + ]); + } + foreach ($this->requestStack->getCurrentRequest()->query->getIterator() as $key => $value) { switch($key) { case 'q': + case 'c_'.$key: break; case 'page': $builder->add($key, HiddenType::class, [ @@ -47,6 +68,17 @@ final class FilterOrderType extends \Symfony\Component\Form\AbstractType } } + public function buildView(FormView $view, FormInterface $form, array $options) + { + /** @var FilterOrderHelper $helper */ + $helper = $options['helper']; + $view->vars['has_search_box'] = $helper->hasSearchBox(); + $view->vars['checkboxes'] = []; + foreach ($helper->getCheckboxes() as $name => $c) { + $view->vars['checkboxes']['c_'.$name] = []; + } + } + public function configureOptions(\Symfony\Component\OptionsResolver\OptionsResolver $resolver) { $resolver->setRequired('helper') diff --git a/src/Bundle/ChillMainBundle/Resources/views/FilterOrder/base.html.twig b/src/Bundle/ChillMainBundle/Resources/views/FilterOrder/base.html.twig index 074f3bb94..8358d0e9b 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/FilterOrder/base.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/FilterOrder/base.html.twig @@ -1,12 +1,26 @@ {{ form_start(form) }}
-
-
- {{ form_widget(form.q)}} - + {% if form.vars.has_search_box %} +
+
+ {{ form_widget(form.q)}} + +
-
+ {% endif %}
+ {% for checkbox_name, options in form.vars.checkboxes %} +
+
+ {{ form_widget(form[checkbox_name]) }} +
+ {% if loop.last %} +
+ +
+ {% endif %} +
+ {% endfor %}
{{ form_end(form) }} diff --git a/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelper.php b/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelper.php index 0b9eb60bb..b91dddb9e 100644 --- a/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelper.php +++ b/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelper.php @@ -12,6 +12,7 @@ class FilterOrderHelper private FormFactoryInterface $formFactory; private RequestStack $requestStack; private ?array $searchBoxFields = null; + private array $checkboxes = []; public function __construct( FormFactoryInterface $formFactory, @@ -28,6 +29,33 @@ class FilterOrderHelper return $this; } + public function addCheckbox(string $name, array $choices, ?array $default = [], ?array $trans = []): self + { + $missing = count($choices) - count($trans) - 1; + $this->checkboxes[$name] = [ + 'choices' => $choices, 'default' => $default, + 'trans' => + \array_merge( + $trans, + 0 < $missing ? + array_fill(0, $missing, null) : [] + ) + ]; + + return $this; + } + + public function getCheckbox(string $name): array + { + return $this->requestStack->getCurrentRequest() + ->query->get('c_'.$name, $this->checkboxes[$name]['default']); + } + + public function getCheckboxes(): array + { + return $this->checkboxes; + } + public function hasSearchBox(): bool { return $this->searchBoxFields !== null; @@ -35,9 +63,17 @@ class FilterOrderHelper private function getFormData(): array { - return [ - 'q' => $this->getQueryString() + $r = [ + 'q' => $this->getQueryString(), ]; + + foreach ($this->checkboxes as $name => $c) { + $r['c_'.$name] = $this->getCheckbox($name); + } + + dump($r); + + return $r; } public function getQueryString(): ?string @@ -50,11 +86,13 @@ class FilterOrderHelper public function buildForm($name = null, string $type = FilterOrderType::class, array $options = []): FormInterface { - return $this->formFactory + $form = $this->formFactory ->createNamed($name, $type, $this->getFormData(), \array_merge([ 'helper' => $this, 'method' => 'GET', 'csrf_protection' => false, ], $options)); + + return $form; } } diff --git a/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelperBuilder.php b/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelperBuilder.php index ec0f6b60b..a1791f89f 100644 --- a/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelperBuilder.php +++ b/src/Bundle/ChillMainBundle/Templating/Listing/FilterOrderHelperBuilder.php @@ -8,6 +8,7 @@ use Symfony\Component\HttpFoundation\RequestStack; class FilterOrderHelperBuilder { private ?array $searchBoxFields = null; + private array $checkboxes = []; private FormFactoryInterface $formFactory; private RequestStack $requestStack; @@ -26,6 +27,13 @@ class FilterOrderHelperBuilder return $this; } + public function addCheckbox(string $name, array $choices, ?array $default = [], ?array $trans = []): self + { + $this->checkboxes[$name] = [ 'choices' => $choices, 'default' => $default, 'trans' => $trans]; + + return $this; + } + public function build(): FilterOrderHelper { $helper = new FilterOrderHelper( @@ -34,6 +42,13 @@ class FilterOrderHelperBuilder ); $helper->setSearchBox($this->searchBoxFields); + foreach ($this->checkboxes as $name => [ + 'choices' => $choices, + 'default' => $default, + 'trans' => $trans + ]) { + $helper->addCheckbox($name, $choices, $default, $trans); + } return $helper; }