Merge branch '118-design-filterOrder' into testing

This commit is contained in:
Julien Fastré 2023-07-11 17:03:23 +02:00
commit 98b8f3dcff
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
10 changed files with 263 additions and 115 deletions

View File

@ -96,9 +96,6 @@ activity_filter:
My activities: Mes échanges (où j'interviens) My activities: Mes échanges (où j'interviens)
Types: Par type d'échange Types: Par type d'échange
Jobs: Par métier impliqué Jobs: Par métier impliqué
By: Filtrer par
Search: Chercher dans la liste
By date: Filtrer par date
#timeline #timeline
'%user% has done an %activity_type%': '%user% a effectué un échange de type "%activity_type%"' '%user% has done an %activity_type%': '%user% a effectué un échange de type "%activity_type%"'

View File

@ -39,7 +39,7 @@ final class FilterOrderType extends \Symfony\Component\Form\AbstractType
'label' => false, 'label' => false,
'required' => false, 'required' => false,
'attr' => [ 'attr' => [
'placeholder' => 'activity_filter.Search', 'placeholder' => 'filter_order.Search',
] ]
]); ]);
} }
@ -47,16 +47,7 @@ final class FilterOrderType extends \Symfony\Component\Form\AbstractType
$checkboxesBuilder = $builder->create('checkboxes', null, ['compound' => true]); $checkboxesBuilder = $builder->create('checkboxes', null, ['compound' => true]);
foreach ($helper->getCheckboxes() as $name => $c) { foreach ($helper->getCheckboxes() as $name => $c) {
$choices = array_combine( $choices = self::buildCheckboxChoices($c['choices'], $c['trans']);
array_map(static function ($c, $t) {
if (null !== $t) {
return $t;
}
return $c;
}, $c['choices'], $c['trans']),
$c['choices']
);
$checkboxesBuilder->add($name, ChoiceType::class, [ $checkboxesBuilder->add($name, ChoiceType::class, [
'choices' => $choices, 'choices' => $choices,
@ -125,6 +116,20 @@ final class FilterOrderType extends \Symfony\Component\Form\AbstractType
} }
} }
public static function buildCheckboxChoices(array $choices, array $trans = []): array
{
return array_combine(
array_map(static function ($c, $t) {
if (null !== $t) {
return $t;
}
return $c;
}, $choices, $trans),
$choices
);
}
public function buildView(FormView $view, FormInterface $form, array $options) public function buildView(FormView $view, FormInterface $form, array $options)
{ {
/** @var FilterOrderHelper $helper */ /** @var FilterOrderHelper $helper */

View File

@ -1,92 +1,120 @@
{{ form_start(form) }} {{ form_start(form) }}
{% set btnSubmit = 0 %} <div class="accordion my-3" id="filterOrderAccordion">
<div class="chill_filter_order container-xxl p-5 py-2 my-3"> <h2 class="accordion-header" id="filterOrderHeading">
<div class="row my-2"> <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#filterOrderCollapse" aria-expanded="true" aria-controls="filterOrderCollapse">
{% if form.vars.has_search_box %} <strong><i class="fa fa-fw fa-filter"></i>Filtrer la liste</strong>
<div class="col-sm-12"> </button>
<div class="input-group"> </h2>
{{ form_widget(form.q) }} <div class="accordion-collapse collapse" id="filterOrderCollapse" aria-labelledby="filterOrderHeading" data-bs-parent="#filterOrderAccordion">
<button type="submit" class="btn btn-misc"><i class="fa fa-search"></i></button> {% set btnSubmit = 0 %}
</div> <div class="accordion-body chill_filter_order container-xxl p-5 py-2">
</div> <div class="row my-2">
{% endif %} {% if form.vars.has_search_box %}
</div> <div class="col-sm-12">
{% if form.dateRanges is defined %} <div class="input-group">
{% set btnSubmit = 1 %} {{ form_widget(form.q) }}
{% if form.dateRanges|length > 0 %} <button type="submit" class="btn btn-misc"><i class="fa fa-search"></i></button>
{% for dateRangeName, _o in form.dateRanges %} </div>
<div class="row my-2"> </div>
{% if form.dateRanges[dateRangeName].vars.label is not same as(false) %} {% endif %}
{{ form_label(form.dateRanges[dateRangeName])}} </div>
{% else %} {% if form.dateRanges is defined %}
<div class="col-sm-4 col-form-label">{{ 'activity_filter.By date'|trans }}</div> {% set btnSubmit = 1 %}
{% endif %} {% if form.dateRanges|length > 0 %}
<div class="col-sm-8 pt-1"> {% for dateRangeName, _o in form.dateRanges %}
<div class="input-group"> <div class="row my-2">
<span class="input-group-text">{{ 'chill_calendar.From'|trans }}</span> {% if form.dateRanges[dateRangeName].vars.label is not same as(false) %}
{{ form_widget(form.dateRanges[dateRangeName]['from']) }} {{ form_label(form.dateRanges[dateRangeName])}}
<span class="input-group-text">{{ 'chill_calendar.To'|trans }}</span> {% else %}
{{ form_widget(form.dateRanges[dateRangeName]['to']) }} <div class="col-sm-4 col-form-label">{{ 'filter_order.By date'|trans }}</div>
{% endif %}
<div class="col-sm-8 pt-1">
<div class="input-group">
<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>
</div> </div>
</div> {% endfor %}
{% endfor %} {% endif %}
{% endif %} {% endif %}
{% endif %} {% if form.checkboxes is defined %}
{% if form.checkboxes is defined %} {% set btnSubmit = 1 %}
{% set btnSubmit = 1 %} {% if form.checkboxes|length > 0 %}
{% if form.checkboxes|length > 0 %} {% for checkbox_name, options in form.checkboxes %}
{% for checkbox_name, options in form.checkboxes %} <div class="row my-2">
<div class="col-sm-4 col-form-label">{{ 'filter_order.By'|trans }}</div>
<div class="col-sm-8 pt-2">
{% for c in form['checkboxes'][checkbox_name].children %}
{{ form_widget(c) }}
{{ form_label(c) }}
{% endfor %}
</div>
</div>
{% endfor %}
{% endif %}
{% endif %}
{% if form.entity_choices is defined %}
{% set btnSubmit = 1 %}
{% if form.entity_choices |length > 0 %}
{% for checkbox_name, options in form.entity_choices %}
<div class="row my-2">
{% if form.entity_choices[checkbox_name].vars.label is not same as(false) %}
{{ form_label(form.entity_choices[checkbox_name])}}
{% endif %}
<div class="col-sm-8 pt-2">
{% for c in form['entity_choices'][checkbox_name].children %}
{{ form_widget(c) }}
{{ form_label(c) }}
{% endfor %}
</div>
</div>
{% endfor %}
{% endif %}
{% endif %}
{% if form.single_checkboxes is defined %}
{% set btnSubmit = 1 %}
{% for name, _o in form.single_checkboxes %}
<div class="row my-2"> <div class="row my-2">
<div class="col-sm-4 col-form-label">{{ 'activity_filter.By'|trans }}</div> <div class="col-sm-4 col-form-label">{{ 'filter_order.By'|trans }}</div>
<div class="col-sm-8 pt-2"> <div class="col-sm-8 pt-2">
{% for c in form['checkboxes'][checkbox_name].children %} {{ form_widget(form.single_checkboxes[name]) }}
{{ form_widget(c) }}
{{ form_label(c) }}
{% endfor %}
</div> </div>
</div> </div>
{% endfor %} {% endfor %}
{% endif %} {% endif %}
{% endif %}
{% if form.entity_choices is defined %}
{% set btnSubmit = 1 %}
{% if form.entity_choices |length > 0 %}
{% for checkbox_name, options in form.entity_choices %}
<div class="row my-2">
{% if form.entity_choices[checkbox_name].vars.label is not same as(false) %}
{{ form_label(form.entity_choices[checkbox_name])}}
{% endif %}
<div class="col-sm-8 pt-2">
{% for c in form['entity_choices'][checkbox_name].children %}
{{ form_widget(c) }}
{{ form_label(c) }}
{% endfor %}
</div>
</div>
{% endfor %}
{% endif %}
{% endif %}
{% if form.single_checkboxes is defined %}
{% set btnSubmit = 1 %}
{% for name, _o in form.single_checkboxes %}
<div class="row my-2">
<div class="col-sm-4 col-form-label">{{ 'activity_filter.By'|trans }}</div>
<div class="col-sm-8 pt-2">
{{ form_widget(form.single_checkboxes[name]) }}
</div>
</div>
{% endfor %}
{% endif %}
{% if btnSubmit == 1 %}
<div class="row my-2">
<button type="submit" class="btn btn-sm btn-misc"><i class="fa fa-fw fa-filter"></i>{{ 'Filter'|trans }}</button>
</div>
{% endif %}
</div>
{% for k,v in otherParameters %} {% if btnSubmit == 1 %}
<input type="hidden" name="{{ k }}" value="{{ v }}" /> <div class="row my-2">
{% endfor %} <button type="submit" class="btn btn-sm btn-misc"><i class="fa fa-fw fa-filter"></i>{{ 'Filter'|trans }}</button>
</div>
{% endif %}
</div>
</div>
{% if active|length > 0 %}
<div class="activeFilters mt-3">
{% for f in active %}
<span class="badge rounded-pill bg-secondary ms-1 {{ f.position }} {{ f.name }}">
{%- if f.label != '' %}
<span class="text-dark">{{ f.label|trans }}&nbsp;: </span>
{% endif -%}
{%- if f.position == 'search_box' and f.value is not null %}
<span class="text-dark">{{ 'filter_order.search_box'|trans ~ ' :' }}</span>
{% endif -%}
{{ f.value}}{#
#}</span>
{% endfor %}
</div>
{% endif %}
<div>
</div>
</div>
{% for k,v in otherParameters %}
<input type="hidden" name="{{ k }}" value="{{ v }}" />
{% endfor %}
{{ form_end(form) }} {{ form_end(form) }}

View File

@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Templating\Listing;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyAccess\PropertyPathInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
final readonly class FilterOrderGetActiveFilterHelper
{
public function __construct(
private TranslatorInterface $translator,
private PropertyAccessorInterface $propertyAccessor,
) {
}
/**
* Return all the data required to display the active filters
*
* @param FilterOrderHelper $filterOrderHelper
* @return array<array{label: string, value: string, position: string, name: string}>
*/
public function getActiveFilters(FilterOrderHelper $filterOrderHelper): array
{
$result = [];
if ($filterOrderHelper->hasSearchBox() && '' !== $filterOrderHelper->getQueryString()) {
$result[] = ['label' => '', 'value' => $filterOrderHelper->getQueryString(), 'position' => FilterOrderPositionEnum::SearchBox->value, 'name' => 'q'];
}
foreach ($filterOrderHelper->getDateRanges() as $name => ['label' => $label]) {
$base = ['position' => FilterOrderPositionEnum::DateRange->value, 'name' => $name, 'label' => (string)$label];
if (null !== ($from = $filterOrderHelper->getDateRangeData($name)['from'] ?? null)) {
$result[] = ['value' => $this->translator->trans('filter_order.by_date.From', ['from_date' => $from]), ...$base];
}
if (null !== ($to = $filterOrderHelper->getDateRangeData($name)['to'] ?? null)) {
$result[] = ['value' => $this->translator->trans('filter_order.by_date.To', ['to_date' => $to]), ...$base];
}
}
foreach ($filterOrderHelper->getCheckboxes() as $name => ['choices' => $choices, 'trans' => $trans]) {
$translatedChoice = array_combine($choices, [...$trans]);
foreach ($filterOrderHelper->getCheckboxData($name) as $keyChoice) {
$result[] = ['value' => $this->translator->trans($translatedChoice[$keyChoice]), 'label' => '', 'position' => FilterOrderPositionEnum::Checkboxes->value, 'name' => $name];
}
}
foreach ($filterOrderHelper->getEntityChoices() as $name => ['label' => $label, 'class' => $class, 'choices' => $choices, 'options' => $options]) {
foreach ($filterOrderHelper->getEntityChoiceData($name) as $selected) {
if (is_callable($options['choice_label'])) {
$value = call_user_func($options['choice_label'], $selected);
} elseif ($options['choice_label'] instanceof PropertyPathInterface || is_string($options['choice_label'])) {
$value = $this->propertyAccessor->getValue($selected, $options['choice_label']);
} else {
if (!$selected instanceof \Stringable) {
throw new \UnexpectedValueException(sprintf("we are not able to transform the value of %s to a string. Implements \\Stringable or add a 'choice_label' option to the filterFormBuilder", get_class($selected)));
}
$value = (string)$selected;
}
$result[] = ['value' => $this->translator->trans($value), 'label' => $label, 'position' => FilterOrderPositionEnum::EntityChoice->value, 'name' => $name];
}
}
foreach ($filterOrderHelper->getSingleCheckbox() as $name => ['label' => $label]) {
if (true === $filterOrderHelper->getSingleCheckboxData($name)) {
$result[] = ['label' => '', 'value' => $this->translator->trans($label), 'position' => FilterOrderPositionEnum::SingleCheckbox->value, 'name' => $name];
}
}
return $result;
}
}

View File

@ -13,16 +13,19 @@ namespace Chill\MainBundle\Templating\Listing;
use Chill\MainBundle\Form\Type\Listing\FilterOrderType; use Chill\MainBundle\Form\Type\Listing\FilterOrderType;
use DateTimeImmutable; use DateTimeImmutable;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\PropertyAccess\PropertyPathInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use function array_merge; use function array_merge;
use function count; use function count;
class FilterOrderHelper final class FilterOrderHelper
{ {
private array $checkboxes = []; private array $checkboxes = [];
@ -33,16 +36,12 @@ class FilterOrderHelper
private array $dateRanges = []; private array $dateRanges = [];
private FormFactoryInterface $formFactory;
public const FORM_NAME = 'f'; public const FORM_NAME = 'f';
private array $formOptions = []; private array $formOptions = [];
private string $formType = FilterOrderType::class; private string $formType = FilterOrderType::class;
private RequestStack $requestStack;
private ?array $searchBoxFields = null; private ?array $searchBoxFields = null;
private ?array $submitted = null; private ?array $submitted = null;
@ -52,12 +51,11 @@ class FilterOrderHelper
*/ */
private array $entityChoices = []; private array $entityChoices = [];
public function __construct( public function __construct(
FormFactoryInterface $formFactory, private readonly FormFactoryInterface $formFactory,
RequestStack $requestStack private readonly RequestStack $requestStack,
) { ) {
$this->formFactory = $formFactory;
$this->requestStack = $requestStack;
} }
public function addSingleCheckbox(string $name, string $label): self public function addSingleCheckbox(string $name, string $label): self
@ -84,14 +82,14 @@ class FilterOrderHelper
public function addCheckbox(string $name, array $choices, ?array $default = [], ?array $trans = [], array $options = []): self public function addCheckbox(string $name, array $choices, ?array $default = [], ?array $trans = [], array $options = []): self
{ {
$missing = count($choices) - count($trans) - 1; if ([] === $trans) {
$trans = $choices;
}
$this->checkboxes[$name] = [ $this->checkboxes[$name] = [
'choices' => $choices, 'default' => $default, 'choices' => $choices,
'trans' => array_merge( 'default' => $default,
$trans, 'trans' => $trans,
0 < $missing ?
array_fill(0, $missing, null) : []
),
...$options, ...$options,
]; ];

View File

@ -14,6 +14,8 @@ namespace Chill\MainBundle\Templating\Listing;
use DateTimeImmutable; use DateTimeImmutable;
use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class FilterOrderHelperBuilder class FilterOrderHelperBuilder
{ {
@ -39,7 +41,7 @@ class FilterOrderHelperBuilder
public function __construct( public function __construct(
FormFactoryInterface $formFactory, FormFactoryInterface $formFactory,
RequestStack $requestStack RequestStack $requestStack,
) { ) {
$this->formFactory = $formFactory; $this->formFactory = $formFactory;
$this->requestStack = $requestStack; $this->requestStack = $requestStack;
@ -87,7 +89,7 @@ class FilterOrderHelperBuilder
{ {
$helper = new FilterOrderHelper( $helper = new FilterOrderHelper(
$this->formFactory, $this->formFactory,
$this->requestStack $this->requestStack,
); );
$helper->setSearchBox($this->searchBoxFields); $helper->setSearchBox($this->searchBoxFields);

View File

@ -13,6 +13,8 @@ namespace Chill\MainBundle\Templating\Listing;
use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class FilterOrderHelperFactory implements FilterOrderHelperFactoryInterface class FilterOrderHelperFactory implements FilterOrderHelperFactoryInterface
{ {
@ -22,7 +24,7 @@ class FilterOrderHelperFactory implements FilterOrderHelperFactoryInterface
public function __construct( public function __construct(
FormFactoryInterface $formFactory, FormFactoryInterface $formFactory,
RequestStack $requestStack RequestStack $requestStack,
) { ) {
$this->formFactory = $formFactory; $this->formFactory = $formFactory;
$this->requestStack = $requestStack; $this->requestStack = $requestStack;

View File

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Templating\Listing;
enum FilterOrderPositionEnum: string
{
case SearchBox = 'search_box';
case Checkboxes = 'checkboxes';
case DateRange = 'date_range';
case EntityChoice = 'entity_choice';
case SingleCheckbox = 'single_checkbox';
}

View File

@ -24,6 +24,7 @@ class Templating extends AbstractExtension
{ {
public function __construct( public function __construct(
private readonly RequestStack $requestStack, private readonly RequestStack $requestStack,
private readonly FilterOrderGetActiveFilterHelper $filterOrderGetActiveFilterHelper,
) { ) {
} }
@ -68,6 +69,7 @@ class Templating extends AbstractExtension
return $environment->render($template, [ return $environment->render($template, [
'helper' => $helper, 'helper' => $helper,
'active' => $this->filterOrderGetActiveFilterHelper->getActiveFilters($helper),
'form' => $helper->buildForm()->createView(), 'form' => $helper->buildForm()->createView(),
'options' => $options, 'options' => $options,
'otherParameters' => $otherParameters, 'otherParameters' => $otherParameters,

View File

@ -54,3 +54,12 @@ duration:
few {# minutes} few {# minutes}
other {# minutes} other {# minutes}
} }
filter_order:
by_date:
From: Depuis le {from_date, date, long}
To: Jusqu'au {to_date, date, long}
By: Filtrer par
Search: Chercher dans la liste
By date: Filtrer par date
search_box: Filtrer par contenu