| 1: | <?php |
| 2: | namespace TIC\FormBundle\Form\Type; |
| 3: | |
| 4: | |
| 5: | use TIC\FormBundle\Base\TICWidgetType as BaseType; |
| 6: | |
| 7: | use Symfony\Component\OptionsResolver\OptionsResolver; |
| 8: | use Symfony\Component\OptionsResolver\Options; |
| 9: | use Symfony\Component\Form\FormBuilderInterface; |
| 10: | use Symfony\Component\Form\FormInterface; |
| 11: | use Symfony\Component\Form\FormView; |
| 12: | |
| 13: | use TIC\FormBundle\Form\DataTransformer\StringToTimeTransformer; |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | class TimeType extends BaseType |
| 31: | { |
| 32: | protected $form_parent = \Symfony\Component\Form\Extension\Core\Type\TimeType::class; |
| 33: | protected $default_prefix = '<clock>'; |
| 34: | protected $default_suffix = '<clock>'; |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | public function configureOptions(OptionsResolver $resolver): void |
| 41: | { |
| 42: | parent::configureOptions($resolver); |
| 43: | |
| 44: | $resolver->setDefaults(array( |
| 45: | |
| 46: | 'placeholder' => false, |
| 47: | 'suffix' => true, |
| 48: | |
| 49: | |
| 50: | 'input' => 'string', |
| 51: | 'with_minutes'=> true, |
| 52: | 'with_seconds'=> false, |
| 53: | |
| 54: | |
| 55: | |
| 56: | 'widget' => 'single_text', |
| 57: | 'html5' => true, |
| 58: | |
| 59: | |
| 60: | 'with_js' => true, |
| 61: | 'culture' => \Locale::getPrimaryLanguage(\Locale::getDefault()), |
| 62: | 'buttons' => true, |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | )); |
| 69: | |
| 70: | $resolver->setAllowedTypes('with_js', array('bool')); |
| 71: | $resolver->setAllowedTypes('culture', array('string')); |
| 72: | $resolver->setAllowedTypes('buttons', array('bool', 'array')); |
| 73: | |
| 74: | $resolver->setNormalizer('with_js', function (Options $options, $with_js) { |
| 75: | if ($options['widget'] != 'single_text') $with_js = false; |
| 76: | return $with_js; |
| 77: | }); |
| 78: | $resolver->setNormalizer('buttons', function (Options $options, $buttons) { |
| 79: | if (is_bool($buttons)) $buttons = ($buttons) ? array('today', 'clear', 'close') : array(); |
| 80: | return $buttons; |
| 81: | }); |
| 82: | |
| 83: | |
| 84: | $resolver->setNormalizer('placeholder', function (Options $options, $value) { return $value; }); |
| 85: | } |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | public function buildForm(FormBuilderInterface $builder, array $options): void |
| 92: | { |
| 93: | if ('string' === $options['input'] && ! $options['strict']) { |
| 94: | $builder->resetModelTransformers(); |
| 95: | $builder->addModelTransformer(new StringToTimeTransformer($options['model_timezone'], $options['model_timezone'])); |
| 96: | } |
| 97: | } |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | public function finishView(FormView $view, FormInterface $form, array $options): void |
| 104: | { |
| 105: | parent::finishView($view, $form, $options); |
| 106: | |
| 107: | |
| 108: | if ($options['widget'] == 'text') { |
| 109: | $child_options = array('attr' => array('maxlength' => 2, 'size' => 2)); |
| 110: | if ($options['html5']) { |
| 111: | $child_options['type'] = 'number'; |
| 112: | $child_options['attr']['size'] = 4; |
| 113: | $child_options['attr']['min'] = 0; |
| 114: | $child_options['attr']['max'] = 60; |
| 115: | } |
| 116: | $view->vars['text_options'] = $child_options; |
| 117: | } |
| 118: | |
| 119: | |
| 120: | if ($options['with_js']) { |
| 121: | $view->vars['with_js'] = true; |
| 122: | $view->vars['format'] = $options['with_seconds'] ? 'LTS' : 'LT'; |
| 123: | $view->vars['culture'] = $options['culture']; |
| 124: | $view->vars['buttons'] = $options['buttons']; |
| 125: | $view->vars['group_class'].= ' date'; |
| 126: | } |
| 127: | |
| 128: | |
| 129: | $this->setViewInputSizing($view); |
| 130: | } |
| 131: | |
| 132: | |
| 133: | } |
| 134: | |