Skip to content

Commit cd73541

Browse files
committed
Created date-range-picker form type
1 parent 6c635fa commit cd73541

File tree

4 files changed

+194
-1
lines changed

4 files changed

+194
-1
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace EWZ\SymfonyAdminBundle\Form\DataTransformer;
4+
5+
use Symfony\Component\Form\DataTransformerInterface;
6+
use Symfony\Component\Form\Exception\UnexpectedTypeException;
7+
8+
class StringToDateRangeTransformer implements DataTransformerInterface
9+
{
10+
/** @var string */
11+
private $localeSeparator;
12+
13+
/**
14+
* @param string $localeSeparator
15+
*/
16+
public function __construct(string $localeSeparator)
17+
{
18+
$this->localeSeparator = $localeSeparator;
19+
}
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function transform($value)
25+
{
26+
if (null === $value) {
27+
return null;
28+
}
29+
30+
if (!\is_array($value)) {
31+
throw new UnexpectedTypeException($value, 'array');
32+
}
33+
34+
return sprintf(
35+
'%s%s%s',
36+
$value[0]->format('Y-m-d H:i:s'),
37+
$this->localeSeparator,
38+
isset($value[1])
39+
? $value[1]->format('Y-m-d H:i:s')
40+
: null
41+
);
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function reverseTransform($value)
48+
{
49+
if (null === $value || '' === $value) {
50+
return;
51+
}
52+
53+
if (!\is_string($value)) {
54+
throw new UnexpectedTypeException($value, 'string');
55+
}
56+
57+
list($from, $to) = explode($this->localeSeparator, $value);
58+
59+
return [new \DateTime($from), $to ? new \DateTime($to) : null];
60+
}
61+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace EWZ\SymfonyAdminBundle\Form\Type;
4+
5+
use EWZ\SymfonyAdminBundle\Form\DataTransformer\StringToDateRangeTransformer;
6+
use Symfony\Component\Form\AbstractType;
7+
use Symfony\Component\Form\Extension\Core\Type\TextType;
8+
use Symfony\Component\Form\FormBuilderInterface;
9+
use Symfony\Component\Form\FormInterface;
10+
use Symfony\Component\Form\FormView;
11+
use Symfony\Component\OptionsResolver\OptionsResolver;
12+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
13+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
14+
15+
class DateRangePickerType extends AbstractType
16+
{
17+
public const DEFAULT_SEPARATOR = ' - ';
18+
19+
/** @var TokenStorageInterface */
20+
private $tokenStorage;
21+
22+
/**
23+
* @param TokenStorageInterface $tokenStorage
24+
*/
25+
public function __construct(TokenStorageInterface $tokenStorage)
26+
{
27+
$this->tokenStorage = $tokenStorage;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function buildForm(FormBuilderInterface $builder, array $options)
34+
{
35+
$builder->addModelTransformer(new StringToDateRangeTransformer($options['locale_separator']));
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function buildView(FormView $view, FormInterface $form, array $options)
42+
{
43+
if ($options['locale_separator']) {
44+
$view->vars['locale_separator'] = $options['locale_separator'];
45+
}
46+
47+
if ($options['single_date']) {
48+
$view->vars['single_date'] = true;
49+
}
50+
51+
if ($options['time_picker']) {
52+
$view->vars['time_picker'] = true;
53+
$view->vars['time_interval'] = (int) ($options['time_interval']);
54+
$view->vars['time_24hour'] = (bool) ($options['time_24hour']);
55+
$view->vars['time_hours'] = (array) $options['time_hours'];
56+
}
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function configureOptions(OptionsResolver $resolver)
63+
{
64+
$defaults = [
65+
'locale_separator' => self::DEFAULT_SEPARATOR,
66+
'single_date' => false,
67+
'time_picker' => false,
68+
'time_interval' => 30,
69+
'time_24hour' => false,
70+
'time_hours' => [],
71+
];
72+
73+
/** @var TokenInterface $token */
74+
if ($token = $this->tokenStorage->getToken()) {
75+
$user = $token->getUser();
76+
77+
if ($user instanceof User) {
78+
$defaults['view_timezone'] = $user->getTimezone();
79+
}
80+
}
81+
82+
$resolver->setDefaults($defaults);
83+
}
84+
85+
/**
86+
* {@inheritdoc}
87+
*/
88+
public function getParent()
89+
{
90+
return TextType::class;
91+
}
92+
}

src/Resources/config/services.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ services:
108108
# Form
109109
#
110110

111+
EWZ\SymfonyAdminBundle\Form\Type\DateRangePickerType:
112+
tags:
113+
- { name: form.type }
114+
arguments:
115+
- '@security.token_storage'
116+
111117
EWZ\SymfonyAdminBundle\Form\Type\DateTimePickerType:
112118
tags:
113119
- { name: form.type }

src/Resources/views/form/bootstrap_4_layout.html.twig

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,53 @@
3131
</div>
3232
{%- endblock address_widget %}
3333

34+
{% block date_range_picker_widget -%}
35+
{%- if attr.original|default(false) -%}
36+
{{ form_widget(form) }}
37+
{%- else -%}
38+
{{ form_widget(form, {type: 'hidden'}) }}
39+
40+
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-control js-daterangepicker')|trim}) -%}
41+
<input type="text"
42+
{{ block('attributes') }}
43+
{%- if disabled %} disabled="disabled"{% endif -%}
44+
{%- if required %} required="required"{% endif -%}
45+
46+
{%- if locale_separator|default(false) -%}
47+
data-locale-separator="{{ locale_separator }}"
48+
{%- endif -%}
49+
50+
{%- if single_date|default(false) -%}
51+
data-single-date="true"
52+
{%- endif -%}
53+
54+
{%- if time_picker|default(false) -%}
55+
data-time-picker="true"
56+
data-time-increment="{{ time_interval }}"
57+
data-time-24hour="{{ time_24hour }}"
58+
data-time-hours="{{ time_hours|json_encode }}"
59+
data-format="{% if is_granted('ROLE_USER') and app.user.dateFormat == constant('EWZ\\SymfonyAdminBundle\\Model\\User::PHP_DATE_FORMAT_US') %}{{ constant('EWZ\\SymfonyAdminBundle\\Model\\User::JS_DATE_FORMAT_US') }}{% else %}{{ constant('EWZ\\SymfonyAdminBundle\\Model\\User::JS_DATE_FORMAT_OTHER') }}{% endif %} {% if is_granted('ROLE_USER') and app.user.dateFormat == constant('EWZ\\SymfonyAdminBundle\\Model\\User::PHP_TIME_FORMAT_24HOURS') %}{{ constant('EWZ\\SymfonyAdminBundle\\Model\\User::JS_TIME_FORMAT_24HOURS') }}{% else %}{{ constant('EWZ\\SymfonyAdminBundle\\Model\\User::JS_TIME_FORMAT_12HOURS') }}{% endif %}"
60+
{%- else -%}
61+
data-format="{% if is_granted('ROLE_USER') and app.user.dateFormat == constant('EWZ\\SymfonyAdminBundle\\Model\\User::PHP_DATE_FORMAT_US') %}{{ constant('EWZ\\SymfonyAdminBundle\\Model\\User::JS_DATE_FORMAT_US') }}{% else %}{{ constant('EWZ\\SymfonyAdminBundle\\Model\\User::JS_DATE_FORMAT_OTHER') }}{% endif %}"
62+
{%- endif -%}
63+
/>
64+
{%- endif -%}
65+
{%- endblock date_range_picker_widget %}
66+
3467
{% block date_widget -%}
3568
{%- if attr.original|default(false) -%}
3669
{{ form_widget(form) }}
3770
{%- else -%}
3871
{{ form_widget(form, {type: 'hidden'}) }}
3972

40-
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-control js-datepicker')|trim}) -%}
73+
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-control js-daterangepicker')|trim}) -%}
4174
<input type="text"
4275
{{ block('attributes') }}
4376
{%- if disabled %} disabled="disabled"{% endif -%}
4477
{%- if required %} required="required"{% endif -%}
4578
{%- if value is not empty %}value="{{ value|date(is_granted('ROLE_USER') ? app.user.dateFormat : constant('EWZ\\SymfonyAdminBundle\\Model\\User::PHP_DATE_FORMAT_US')) }}"{% endif -%}
4679
data-format="{% if is_granted('ROLE_USER') and app.user.dateFormat == constant('EWZ\\SymfonyAdminBundle\\Model\\User::PHP_DATE_FORMAT_US') %}{{ constant('EWZ\\SymfonyAdminBundle\\Model\\User::JS_DATE_FORMAT_US') }}{% else %}{{ constant('EWZ\\SymfonyAdminBundle\\Model\\User::JS_DATE_FORMAT_OTHER') }}{% endif %}"
80+
data-single-date="true"
4781
/>
4882
{%- endif -%}
4983
{%- endblock date_widget %}

0 commit comments

Comments
 (0)