| 1: | <?php |
| 2: | namespace TIC\ListBundle\Form; |
| 3: | |
| 4: | use Symfony\Component\Form\AbstractType; |
| 5: | use Symfony\Component\OptionsResolver\OptionsResolver; |
| 6: | use Symfony\Component\Form\FormBuilderInterface; |
| 7: | use Symfony\Component\Form\FormEvents; |
| 8: | use Symfony\Component\Form\FormEvent; |
| 9: | |
| 10: | use TIC\FormBundle\Form\Type\BooleanType; |
| 11: | use TIC\FormBundle\Form\Type\NumberType; |
| 12: | use TIC\FormBundle\Form\Type\StringType; |
| 13: | use TIC\FormBundle\Form\Type\MultiValueType; |
| 14: | |
| 15: | use TIC\ListBundle\Entity\ListItem; |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | class ListItemType extends AbstractType |
| 21: | { |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | public function configureOptions(OptionsResolver $resolver) { |
| 27: | $resolver->setDefaults(array( |
| 28: | 'data_class' => ListItem::class, |
| 29: | 'capacities' => array(), |
| 30: | 'allow_cfg' => false, |
| 31: | 'allow_mod' => true, |
| 32: | 'locales' => array('fr', 'en'), |
| 33: | )); |
| 34: | } |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | public function buildForm(FormBuilderInterface $builder, array $options) { |
| 41: | $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $e) use ($options) { |
| 42: | $form = $e->getForm(); |
| 43: | $data = $e->getData(); |
| 44: | $edit = (is_object($data) && $data->getId()); |
| 45: | $can_mod = (! $edit || $options['allow_mod']); |
| 46: | |
| 47: | $is_valuable = in_array('valuable', $options['capacities']); |
| 48: | $is_referable = in_array('referable', $options['capacities']); |
| 49: | $is_deactivatable = in_array('deactivatable', $options['capacities']); |
| 50: | $is_sortable = in_array('sortable', $options['capacities']); |
| 51: | $is_iconable = in_array('iconable', $options['capacities']); |
| 52: | |
| 53: | $form |
| 54: | ->add('label', StringType::class, array( |
| 55: | 'prefix' => '<fa-chevron-right>', |
| 56: | 'placeholder' => true, |
| 57: | 'disabled' => ! $can_mod, |
| 58: | )) |
| 59: | ; |
| 60: | |
| 61: | if ($is_valuable || $options['allow_cfg']) $form |
| 62: | ->add('value', StringType::class, array( |
| 63: | 'prefix' => '<fa-tag>', |
| 64: | 'placeholder' => true, |
| 65: | 'attr' => array('data-capacity' => 'valuable'), |
| 66: | 'disabled' => ! ($can_mod && $is_valuable), |
| 67: | 'required' => false, |
| 68: | )) |
| 69: | ; |
| 70: | if ($is_referable || $options['allow_cfg']) $form |
| 71: | ->add('ref', StringType::class, array( |
| 72: | 'prefix' => '<fa-barcode>', |
| 73: | 'placeholder' => true, |
| 74: | 'attr' => array('data-capacity' => 'referable'), |
| 75: | 'disabled' => ! ($can_mod && $is_referable), |
| 76: | 'required' => false, |
| 77: | )) |
| 78: | ; |
| 79: | if ($is_iconable || $options['allow_cfg']) $form |
| 80: | ->add('icon', StringType::class, array( |
| 81: | 'prefix' => '<fa-image>', |
| 82: | 'placeholder' => true, |
| 83: | 'attr' => array('data-capacity' => 'iconable'), |
| 84: | 'disabled' => ! ($can_mod && $is_iconable), |
| 85: | 'required' => false, |
| 86: | )) |
| 87: | ; |
| 88: | if ($is_deactivatable || $options['allow_cfg']) $form |
| 89: | ->add('enabled', BooleanType::class, array( |
| 90: | 'labels' => '', |
| 91: | 'expanded' => false, |
| 92: | 'choice_attr' => array('data-capacity' => 'deactivatable'), |
| 93: | 'disabled' => ! ($can_mod && $is_deactivatable), |
| 94: | 'required' => false, |
| 95: | )) |
| 96: | ; |
| 97: | if ($is_sortable || $options['allow_cfg']) $form |
| 98: | ->add('ranking', NumberType::class, array( |
| 99: | 'prefix' => '<fa-sort>', |
| 100: | 'placeholder' => true, |
| 101: | 'mode' => 'html5', |
| 102: | 'attr' => array('data-capacity' => 'sortable'), |
| 103: | 'disabled' => ! ($can_mod && $is_sortable), |
| 104: | 'required' => false, |
| 105: | )) |
| 106: | ; |
| 107: | }); |
| 108: | } |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | public function getBlockPrefix() { |
| 115: | return 'ticlist_listitem_form'; |
| 116: | } |
| 117: | |
| 118: | } |
| 119: | |