improve filter order by adding checkboxes

This commit is contained in:
Julien Fastré 2021-10-27 13:32:33 +02:00
parent 333a4d94b2
commit 691c5ffd21
4 changed files with 107 additions and 8 deletions

View File

@ -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')

View File

@ -1,12 +1,26 @@
{{ form_start(form) }}
<div class="chill_filter_order container">
<div class="row">
<div class="col-md-12">
<div class="input-group mb-3">
{{ form_widget(form.q)}}
<button type="submit" class="btn btn-chill-l-gray"><i class="fa fa-search"></i></button>
{% if form.vars.has_search_box %}
<div class="col-md-12">
<div class="input-group mb-3">
{{ form_widget(form.q)}}
<button type="submit" class="btn btn-chill-l-gray"><i class="fa fa-search"></i></button>
</div>
</div>
</div>
{% endif %}
</div>
{% for checkbox_name, options in form.vars.checkboxes %}
<div class="col-md-12">
<div class="col-md-11">
{{ form_widget(form[checkbox_name]) }}
</div>
{% if loop.last %}
<div class="col-md-1">
<button type="submit" class="btn btn-misc"><i class="fa fa-filter"></i></button>
</div>
{% endif %}
</div>
{% endfor %}
</div>
{{ form_end(form) }}

View File

@ -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;
}
}

View File

@ -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;
}