add more filtering possibilities with order helper

This commit is contained in:
2021-10-28 00:11:05 +02:00
parent aea5e9b1d7
commit 97dbc4bc16
6 changed files with 127 additions and 79 deletions

View File

@@ -32,6 +32,7 @@ final class FilterOrderType extends \Symfony\Component\Form\AbstractType
]);
}
$checkboxesBuilder = $builder->create('checkboxes', null, [ 'compound' => true ]);
foreach ($helper->getCheckboxes() as $name => $c) {
$choices = \array_combine(
@@ -42,17 +43,21 @@ final class FilterOrderType extends \Symfony\Component\Form\AbstractType
$c['choices']
);
$builder->add('c_'.$name, ChoiceType::class, [
$checkboxesBuilder->add($name, ChoiceType::class, [
'choices' => $choices,
'expanded' => true,
'multiple' => true,
]);
}
if (0 < count($helper->getCheckboxes())) {
$builder->add($checkboxesBuilder);
}
foreach ($this->requestStack->getCurrentRequest()->query->getIterator() as $key => $value) {
switch($key) {
case 'q':
case 'c_'.$key:
case 'checkboxes'.$key:
break;
case 'page':
$builder->add($key, HiddenType::class, [
@@ -75,7 +80,7 @@ final class FilterOrderType extends \Symfony\Component\Form\AbstractType
$view->vars['has_search_box'] = $helper->hasSearchBox();
$view->vars['checkboxes'] = [];
foreach ($helper->getCheckboxes() as $name => $c) {
$view->vars['checkboxes']['c_'.$name] = [];
$view->vars['checkboxes'][$name] = [];
}
}

View File

@@ -10,17 +10,30 @@
</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]) }}
{% if form.checkboxes|length > 0 %}
{% for checkbox_name, options in form.checkboxes %}
<div class="row gx-0">
<div class="col-md-12">
{% for c in form['checkboxes'][checkbox_name].children %}
<div class="form-check form-check-inline">
{{ form_widget(c) }}
{{ form_label(c) }}
</div>
{% endfor %}
</div>
</div>
{% if loop.last %}
<div class="col-md-1">
<button type="submit" class="btn btn-misc"><i class="fa fa-filter"></i></button>
</div>
<div class="row gx-0">
<div class="col-md-12">
<ul class="record_actions">
<li>
<button type="submit" class="btn btn-misc"><i class="fa fa-filter"></i></button>
</li>
</ul>
</div>
</div>
{% endif %}
</div>
{% endfor %}
{% endfor %}
{% endif %}
</div>
{{ form_end(form) }}

View File

@@ -13,6 +13,11 @@ class FilterOrderHelper
private RequestStack $requestStack;
private ?array $searchBoxFields = null;
private array $checkboxes = [];
private ?array $submitted = null;
private ?string $formName = 'filter';
private string $formType = FilterOrderType::class;
private array $formOptions = [];
public function __construct(
FormFactoryInterface $formFactory,
@@ -45,10 +50,9 @@ class FilterOrderHelper
return $this;
}
public function getCheckbox(string $name): array
public function getCheckboxData(string $name): array
{
return $this->requestStack->getCurrentRequest()
->query->get('c_'.$name, $this->checkboxes[$name]['default']);
return $this->getFormData()['checkboxes'][$name];
}
public function getCheckboxes(): array
@@ -63,12 +67,23 @@ class FilterOrderHelper
private function getFormData(): array
{
$r = [
'q' => $this->getQueryString(),
];
if (NULL === $this->submitted) {
$this->submitted = $this->buildForm()
->getData();
}
return $this->submitted;
}
private function getDefaultData(): array
{
$r = [];
if ($this->hasSearchBox()) {
$r['q'] = '';
}
foreach ($this->checkboxes as $name => $c) {
$r[$name] = $this->getCheckbox($name);
$r['checkboxes'][$name] = $c['default'];
}
return $r;
@@ -76,21 +91,17 @@ class FilterOrderHelper
public function getQueryString(): ?string
{
$q = $this->requestStack->getCurrentRequest()
->query->get('q', null);
return empty($q) ? NULL : $q;
return $this->getFormData()['q'];
}
public function buildForm($name = null, string $type = FilterOrderType::class, array $options = []): FormInterface
public function buildForm(): FormInterface
{
$form = $this->formFactory
->createNamed($name, $type, $this->getFormData(), \array_merge([
return $this->formFactory
->createNamed($this->formName, $this->formType, $this->getDefaultData(), \array_merge([
'helper' => $this,
'method' => 'GET',
'csrf_protection' => false,
], $options));
return $form;
], $this->formOptions))
->handleRequest($this->requestStack->getCurrentRequest());
}
}