| 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\Form\FormBuilderInterface; |
| 9: | use Symfony\Component\Form\FormInterface; |
| 10: | use Symfony\Component\Form\FormView; |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | class PlainType extends BaseType |
| 17: | { |
| 18: | |
| 19: | protected $default_prefix = '<info-circle>'; |
| 20: | protected $default_suffix = ''; |
| 21: | protected $class_prefix = ''; |
| 22: | protected $class_suffix = ''; |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | public function configureOptions(OptionsResolver $resolver): void |
| 29: | { |
| 30: | parent::configureOptions($resolver); |
| 31: | |
| 32: | |
| 33: | $mapped = function (Options $options) { |
| 34: | |
| 35: | if ($options['content'] === NULL) return true; |
| 36: | |
| 37: | if (preg_match('/^[^%]*(%[0-9\.]*[sdf])[^%]*$/', $options['content'])) return true; |
| 38: | return false; |
| 39: | }; |
| 40: | |
| 41: | $resolver->setDefaults(array( |
| 42: | |
| 43: | 'mapped' => false, |
| 44: | 'compound' => false, |
| 45: | 'disabled' => true, |
| 46: | 'required' => false, |
| 47: | 'attr' => array('class' => $this->form_alias), |
| 48: | 'content' => null, |
| 49: | 'dtFormat' => 'Y-m-d H:i:s', |
| 50: | 'html' => false, |
| 51: | 'trans' => false, |
| 52: | 'property' => null, |
| 53: | |
| 54: | |
| 55: | )); |
| 56: | |
| 57: | $resolver->setAllowedTypes('html', array('bool')); |
| 58: | $resolver->setAllowedTypes('trans', array('bool')); |
| 59: | $resolver->setAllowedTypes('property', array('null','string')); |
| 60: | } |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | public function buildView(FormView $view, FormInterface $form, array $options): void |
| 67: | { |
| 68: | parent::buildView($view, $form, $options); |
| 69: | |
| 70: | |
| 71: | $view->vars['input_group'] = false; |
| 72: | $this->setViewGroupClass($view, $options); |
| 73: | |
| 74: | |
| 75: | $value = null; |
| 76: | if ($options['mapped']) { |
| 77: | $value = $form->getViewData(); |
| 78: | |
| 79: | switch (true) { |
| 80: | case is_null($value) : |
| 81: | $value = '-'; |
| 82: | break; |
| 83: | case is_bool($value) : |
| 84: | $value = ($value) ? 'widget.boolean.true' : 'widget.boolean.false'; |
| 85: | break; |
| 86: | case is_array($value) : |
| 87: | $value = implode(', ', $value); |
| 88: | break; |
| 89: | case ($value instanceof \DateTime) : |
| 90: | $value = $value->format($options['dtFormat']); |
| 91: | break; |
| 92: | case is_object($value) : |
| 93: | if (isset($options['property'])) $value = $value->{$options['property']}; |
| 94: | elseif (method_exists($value, '__toString')) $value = $value->__toString(); |
| 95: | else $value = get_class($value); |
| 96: | break; |
| 97: | default: |
| 98: | } |
| 99: | if ($options['content'] !== null) $value = sprintf($options['content'], $value); |
| 100: | $view->vars['value'] = $value; |
| 101: | } else { |
| 102: | $view->vars['value'] = $options['content']; |
| 103: | } |
| 104: | |
| 105: | $view->vars['html'] = ($options['html']) ? true : false; |
| 106: | $view->vars['trans'] = ($options['trans']) ? true : false; |
| 107: | } |
| 108: | |
| 109: | |
| 110: | } |
| 111: | |