1: <?php
2: namespace TIC\ListBundle\Form;
3:
4: use Symfony\Component\Form\AbstractType;
5: use Symfony\Component\OptionsResolver\OptionsResolver;
6: use Symfony\Component\OptionsResolver\Options;
7: use Symfony\Component\Form\FormBuilderInterface;
8: use Symfony\Component\Form\FormEvents;
9: use Symfony\Component\Form\FormEvent;
10:
11: use TIC\FormBundle\Form\Type\StringType;
12: use TIC\FormBundle\Form\Type\EditorType;
13: use TIC\FormBundle\Form\Type\ChoiceType;
14: use TIC\FormBundle\Form\Type\BooleanType;
15: use TIC\FormBundle\Form\Type\CollectionType;
16:
17: use Symfony\Component\Form\Extension\Core\Type\MoneyType;
18: use Symfony\Component\Form\Extension\Core\Type\TextareaType;
19: use Symfony\Component\Form\Extension\Core\Type\TextType;
20: use Symfony\Bridge\Doctrine\Form\Type\EntityType;
21:
22: use TIC\ListBundle\Entity\ListItem;
23: use TIC\ListBundle\Entity\ListInfo;
24:
25: /**
26: *
27: */
28: class ListInfoType extends AbstractType
29: {
30:
31: /**
32: * {@inheritdoc}
33: */
34: public function configureOptions(OptionsResolver $resolver) {
35: $resolver->setDefaults(array(
36: 'data_class' => ListInfo::class,
37: 'allow_cfg' => true, // désactivable selon rôle par ex.
38: 'allow_mod' => true, // désactivable selon rôle par ex.
39: 'allow_add' => true, // désactivable selon rôle par ex.
40: 'allow_del' => true, // désactivable selon rôle par ex.
41: ));
42:
43: // ATTENTION: allow_del=true requiert allow_mod=true
44: // (impossibilité de faire du 'del' sans 'mod')
45: // (mais possibilité de faire du 'mod' sans 'del')
46: // Alors allow_mod=false impose allow_del=false (dans ce sens, car options à true par défaut)
47: $resolver->setNormalizer('allow_del', function (Options $options, $allow_del) {
48: return ($options['allow_mod']) ? $allow_del : false;
49: });
50: }
51:
52:
53: /**
54: * {@inheritdoc}
55: */
56: public function buildForm(FormBuilderInterface $builder, array $options) {
57: $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $e) use ($options) {
58: $form = $e->getForm();
59: $data = $e->getData();
60: $edit = (is_object($data) && $data->getId());
61:
62: $form
63: ->add('ref', StringType::class, array(
64: # 'prefix' => '<barcode>',
65: 'attr' => ['icon' => 'barcode'],
66: # 'placeholder' => true,
67: 'disabled' => ($edit || empty($options['allow_cfg'])),
68: ))
69: ->add('title', TextType::class, array(
70: # 'prefix' => '<info-sign>',
71: 'attr' => ['icon' => 'info-circle'],
72: # 'placeholder' => true,
73: 'required' => false,
74: 'disabled' => empty($options['allow_cfg']),
75: ))
76: ->add('description', EditorType::class, array(
77: 'wysiwyg' => false,
78: 'placeholder' => true,
79: 'rows' => 2,
80: 'required' => false,
81: 'disabled' => empty($options['allow_cfg']),
82: ))
83: ;
84: if ($options['allow_cfg']) $form
85: ->add('capacities', ChoiceType::class, array(
86: 'choices' => $data->getCapacityChoices(),
87: 'multiple' => true,
88: 'expanded' => true,
89: 'inline' => true,
90: 'required' => false,
91: # 'disabled' => empty($options['allow_cfg']),
92: ))
93: ;
94: if (($options['allow_mod'] || $options['allow_del'] || $options['allow_add'])
95: && ($data->getEditable())
96: ) $form
97: ->add('items', CollectionType::class, array(
98: 'label' => false,
99: 'entry_type' => ListItemType::class,
100: 'entry_options' => array(
101: 'capacities' => $data->getCapacities(),
102: 'allow_cfg' => $options['allow_cfg'],
103: 'allow_mod' => $options['allow_mod'],
104: ),
105: 'prototype' => true,
106: 'prototype_data' => new ListItem(),
107: 'allow_add' => $options['allow_add'],
108: 'allow_delete' => $options['allow_del'],
109: 'by_reference' => false,
110: 'display' => 'table',
111: # 'disabled' => ! $data->getEditable(),
112: 'sortable' => false
113: ))
114: ;
115: });
116:
117: // important de mettre à jour les capacités du ListInfo pour le submit des ListItems
118: $builder->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $e) use ($options) {
119: $form = $e->getForm();
120: $data = $e->getData();
121: if (($options['allow_mod'] || $options['allow_del'] || $options['allow_add'])
122: // IMPORTANT: ne pas ajouter si aucune données provenant du formulaire (sinon perte des éléments)
123: // Lorsque editable devient activé : le formulaire ne présentait par les éléments => aucune édition des éléments
124: // Lorsque editable devient désactivé : le formulaire présentait les éléments => dernière édition possible
125: // (mais dans ce cas en prenant tout de même bien en compte les nouvelles valeurs des autres capacités)
126: && isset($data['items']) && isset($data['capacities'])
127: ) $form
128: ->add('items', CollectionType::class, array(
129: 'label' => false,
130: 'entry_type' => ListItemType::class,
131: 'entry_options' => array(
132: 'capacities' => $data['capacities'],
133: 'allow_cfg' => $options['allow_cfg'],
134: 'allow_mod' => $options['allow_mod'],
135: ),
136: 'prototype' => true,
137: 'prototype_data' => new ListItem(),
138: 'allow_add' => $options['allow_add'],
139: 'allow_delete' => $options['allow_del'],
140: 'by_reference' => false,
141: 'display' => 'table',
142: # 'disabled' => in_array('editable', $data['capacities'])
143: ))
144: ;
145: });
146: }
147:
148:
149: /**
150: * {@inheritdoc}
151: */
152: public function getBlockPrefix() {
153: return 'ticlist_list_form';
154: }
155:
156: }
157: