mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
list calendar by period: date range and query acl aware
This commit is contained in:
@@ -11,6 +11,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Form\Type\Listing;
|
||||
|
||||
use Chill\MainBundle\Form\Type\ChillDateType;
|
||||
use Chill\MainBundle\Templating\Listing\FilterOrderHelper;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
@@ -70,10 +71,38 @@ final class FilterOrderType extends \Symfony\Component\Form\AbstractType
|
||||
$builder->add($checkboxesBuilder);
|
||||
}
|
||||
|
||||
if (0 < count($helper->getDateRanges())) {
|
||||
$dateRangesBuilder = $builder->create('dateRanges', null, ['compound' => true]);
|
||||
|
||||
foreach ($helper->getDateRanges() as $name => $opts) {
|
||||
$rangeBuilder = $dateRangesBuilder->create($name, null, [
|
||||
'compound' => true,
|
||||
'label' => $opts['label'] ?? $name,
|
||||
]);
|
||||
|
||||
$rangeBuilder->add(
|
||||
'from',
|
||||
ChillDateType::class,
|
||||
['input' => 'datetime_immutable', 'required' => false]
|
||||
);
|
||||
$rangeBuilder->add(
|
||||
'to',
|
||||
ChillDateType::class,
|
||||
['input' => 'datetime_immutable', 'required' => false]
|
||||
);
|
||||
|
||||
$dateRangesBuilder->add($rangeBuilder);
|
||||
}
|
||||
|
||||
$builder->add($dateRangesBuilder);
|
||||
}
|
||||
|
||||
foreach ($this->requestStack->getCurrentRequest()->query->getIterator() as $key => $value) {
|
||||
switch ($key) {
|
||||
case 'q':
|
||||
case 'checkboxes' . $key:
|
||||
case $key . '_from':
|
||||
case $key . '_to':
|
||||
break;
|
||||
|
||||
case 'page':
|
||||
|
@@ -10,6 +10,28 @@
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if form.dateRanges is defined %}
|
||||
{% if form.dateRanges|length > 0 %}
|
||||
{% for dateRangeName, _o in form.dateRanges %}
|
||||
<div class="row gx-2">
|
||||
<div class="col-md-5">
|
||||
{{ form_label(form.dateRanges[dateRangeName])}}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div class="input-group mb-3">
|
||||
<span class="input-group-text">{{ 'chill_calendar.From'|trans }}</span>
|
||||
{{ form_widget(form.dateRanges[dateRangeName]['from']) }}
|
||||
<span class="input-group-text">{{ 'chill_calendar.To'|trans }}</span>
|
||||
{{ form_widget(form.dateRanges[dateRangeName]['to']) }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-1">
|
||||
<button type="submit" class="btn btn-misc"><i class="fa fa-filter"></i></button>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% if form.checkboxes is defined %}
|
||||
{% if form.checkboxes|length > 0 %}
|
||||
{% for checkbox_name, options in form.checkboxes %}
|
||||
|
@@ -12,6 +12,7 @@ declare(strict_types=1);
|
||||
namespace Chill\MainBundle\Templating\Listing;
|
||||
|
||||
use Chill\MainBundle\Form\Type\Listing\FilterOrderType;
|
||||
use \DateTimeImmutable;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
@@ -23,6 +24,8 @@ class FilterOrderHelper
|
||||
{
|
||||
private array $checkboxes = [];
|
||||
|
||||
private array $dateRanges = [];
|
||||
|
||||
private FormFactoryInterface $formFactory;
|
||||
|
||||
private ?string $formName = 'f';
|
||||
@@ -60,6 +63,13 @@ class FilterOrderHelper
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addDateRange(string $name, string $label, ?DateTimeImmutable $from = null, ?DateTimeImmutable $to = null): self
|
||||
{
|
||||
$this->dateRanges[$name] = ['from' => $from, 'to' => $to, 'label' => $label];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function buildForm(): FormInterface
|
||||
{
|
||||
return $this->formFactory
|
||||
@@ -76,6 +86,19 @@ class FilterOrderHelper
|
||||
return $this->getFormData()['checkboxes'][$name];
|
||||
}
|
||||
|
||||
public function getDateRanges(): array
|
||||
{
|
||||
return $this->dateRanges;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<'to': DateTimeImmutable, 'from': DateTimeImmutable>
|
||||
*/
|
||||
public function getDateRangeData(string $name): array
|
||||
{
|
||||
return $this->getFormData()['dateRanges'][$name];
|
||||
}
|
||||
|
||||
public function getCheckboxes(): array
|
||||
{
|
||||
return $this->checkboxes;
|
||||
@@ -110,6 +133,11 @@ class FilterOrderHelper
|
||||
$r['checkboxes'][$name] = $c['default'];
|
||||
}
|
||||
|
||||
foreach ($this->dateRanges as $name => $defaults) {
|
||||
$r['dateRanges'][$name]['from'] = $defaults['from'];
|
||||
$r['dateRanges'][$name]['to'] = $defaults['to'];
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
|
@@ -11,6 +11,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Templating\Listing;
|
||||
|
||||
use \DateTimeImmutable;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
@@ -18,6 +19,8 @@ class FilterOrderHelperBuilder
|
||||
{
|
||||
private array $checkboxes = [];
|
||||
|
||||
private array $dateRanges = [];
|
||||
|
||||
private FormFactoryInterface $formFactory;
|
||||
|
||||
private RequestStack $requestStack;
|
||||
@@ -39,6 +42,13 @@ class FilterOrderHelperBuilder
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addDateRange(string $name, string $label, ?DateTimeImmutable $from = null, ?DateTimeImmutable $to = null): self
|
||||
{
|
||||
$this->dateRanges[$name] = ['from' => $from, 'to' => $to, 'label' => $label];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addSearchBox(?array $fields = [], ?array $options = []): self
|
||||
{
|
||||
$this->searchBoxFields = $fields;
|
||||
@@ -65,6 +75,16 @@ class FilterOrderHelperBuilder
|
||||
$helper->addCheckbox($name, $choices, $default, $trans);
|
||||
}
|
||||
|
||||
foreach (
|
||||
$this->dateRanges as $name => [
|
||||
'from' => $from,
|
||||
'to' => $to,
|
||||
'label' => $label,
|
||||
]
|
||||
) {
|
||||
$helper->addDateRange($name, $label, $from, $to);
|
||||
}
|
||||
|
||||
return $helper;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user