mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
add more filtering possibilities with order helper
This commit is contained in:
@@ -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] = [];
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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) }}
|
||||
|
@@ -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());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user