improve filter order by adding checkboxes

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

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