1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\Form\Type; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Form\AbstractType; |
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TimeType; |
16
|
|
|
use Symfony\Component\Form\FormInterface; |
17
|
|
|
use Symfony\Component\Form\FormView; |
18
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
19
|
|
|
|
20
|
|
|
final class TimePickerType extends AbstractType |
21
|
|
|
{ |
22
|
|
|
public function finishView(FormView $view, FormInterface $form, array $options): void |
23
|
|
|
{ |
24
|
|
|
$format = 'HH'; |
25
|
|
|
|
26
|
|
|
if (true === $options['with_minutes']) { |
27
|
|
|
$format .= ':mm'; |
28
|
|
|
$options['dp_use_minutes'] = true; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (true === $options['with_seconds']) { |
32
|
|
|
$format .= ':ss'; |
33
|
|
|
$options['dp_use_seconds'] = true; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$view->vars['moment_format'] = $format; |
37
|
|
|
$view->vars['datepicker_use_button'] = (bool) $options['datepicker_use_button']; |
38
|
|
|
$view->vars['dp_options'] = self::createDpOptions($options); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function configureOptions(OptionsResolver $resolver): void |
42
|
|
|
{ |
43
|
|
|
$resolver |
44
|
|
|
->setDefaults([ |
45
|
|
|
'input' => 'datetime', |
46
|
|
|
'widget' => 'single_text', |
47
|
|
|
'datepicker_use_button' => true, |
48
|
|
|
'dp_pick_date' => false, |
49
|
|
|
'dp_minute_stepping' => 1, |
50
|
|
|
]) |
51
|
|
|
->setAllowedTypes('input', 'string') |
52
|
|
|
->setAllowedTypes('widget', 'string') |
53
|
|
|
; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getParent(): string |
57
|
|
|
{ |
58
|
|
|
return TimeType::class; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getBlockPrefix(): string |
62
|
|
|
{ |
63
|
|
|
return 'core23_type_time_picker'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private static function createDpOptions(array $options): array |
67
|
|
|
{ |
68
|
|
|
$dpOptions = []; |
69
|
|
|
|
70
|
|
|
foreach ($options as $key => $value) { |
71
|
|
|
if (false !== strpos($key, 'dp_')) { |
72
|
|
|
// We remove 'dp_' and camelize the options names |
73
|
|
|
$dpKey = substr($key, 3); |
74
|
|
|
$dpKey = preg_replace_callback( |
75
|
|
|
'/_([a-z])/', |
76
|
|
|
static function ($c) { |
77
|
|
|
return strtoupper($c[1]); |
78
|
|
|
}, |
79
|
|
|
$dpKey |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$dpOptions[$dpKey] = $value; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $dpOptions; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|