Completed
Push — master ( 2c86a7...690ed0 )
by
unknown
36:26 queued 28:50
created

WidgetType::addCriteriasFields()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.439
c 0
b 0
f 0
cc 5
eloc 24
nc 1
nop 2
1
<?php
2
3
namespace Victoire\Bundle\CoreBundle\Form;
4
5
use Symfony\Component\Form\AbstractType;
6
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
7
use Symfony\Component\Form\FormBuilderInterface;
8
use Symfony\Component\Form\FormEvent;
9
use Symfony\Component\Form\FormEvents;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
use Victoire\Bundle\CriteriaBundle\Entity\Criteria;
13
use Victoire\Bundle\CriteriaBundle\Form\Type\CriteriaType;
14
use Victoire\Bundle\WidgetBundle\Model\Widget;
15
16
/**
17
 * Base Widget form type.
18
 */
19
class WidgetType extends AbstractType
20
{
21
    /**
22
     * Define form fields.
23
     *
24
     * @param FormBuilderInterface $builder The builder
25
     * @param array                $options The options
26
     *
27
     * @throws \Exception
28
     */
29
    public function buildForm(FormBuilderInterface $builder, array $options)
30
    {
31
        if ($options['businessEntityId'] !== null) {
32
            if ($options['namespace'] === null) {
33
                throw new \Exception('The namespace is mandatory if the business_entity_id is given.');
34
            }
35
            if ($options['mode'] === null) {
36
                throw new \Exception('The mode is mandatory if the business_entity_id is given.');
37
            }
38
        }
39
40
        if ($options['mode'] === Widget::MODE_ENTITY) {
41
            $this->addEntityFields($builder, $options);
42
        }
43
44
        if ($options['mode'] === Widget::MODE_QUERY) {
45
            $this->addQueryFields($builder, $options);
46
        }
47
48
        if ($options['mode'] === Widget::MODE_BUSINESS_ENTITY) {
49
            $this->addBusinessEntityFields($builder, $options);
50
        }
51
52
        //add the mode to the form
53
        $builder->add('mode', HiddenType::class, [
54
            'data' => $options['mode'],
55
        ]);
56
        $builder->add('asynchronous', null, [
57
                'label'    => 'victoire.widget.type.asynchronous.label',
58
                'required' => false,
59
            ]);
60
        $builder->add('theme', HiddenType::class);
61
        $builder->add('quantum', HiddenType::class);
62
63
        //add the slot to the form
64
        $builder->add('slot', HiddenType::class, []);
65
66
        $this->addCriteriasFields($builder, $options);
67
        //we use the PRE_SUBMIT event to set the mode option
68
        $builder->addEventListener(
69
            FormEvents::PRE_SUBMIT,
70
            function (FormEvent $event) use ($options) {
71
                //we get the raw data for the widget form
72
                $rawData = $event->getData();
73
74
                //get the posted mode
75
                $mode = $rawData['mode'];
76
77
                //get the form to add more fields
78
                $form = $event->getForm();
79
80
                //the controller does not use the mode to construct the form, so we update it automatically
81
                if ($mode === Widget::MODE_ENTITY) {
82
                    $this->addEntityFields($form, $options);
83
                }
84
85
                if ($mode === Widget::MODE_QUERY) {
86
                    $this->addQueryFields($form, $options);
87
                }
88
                if ($mode === Widget::MODE_BUSINESS_ENTITY) {
89
                    $this->addBusinessEntityFields($form, $options);
90
                }
91
            }
92
        );
93
    }
94
95
    /**
96
     * Add the criterias fields.
97
     *
98
     * @param FormBuilderInterface $builder
99
     * @param array                $options
100
     */
101
    protected function addCriteriasFields($builder, $options)
102
    {
103
        $builder->add('criterias', CriteriaCollectionType::class, [
104
            'label'         => 'victoire.widget.type.criterias.label',
105
            'entry_type'    => CriteriaType::class,
106
            'required'      => false,
107
            'entry_options' => [
108
                'dataSources' => $options['dataSources'],
109
            ],
110
        ]);
111
        $builder->addEventListener(
112
            FormEvents::PRE_SET_DATA,
113
            function (FormEvent $event) use ($options) {
114
                $dataSources = $options['dataSources']->getDataSources();
115
                $widget = $event->getData();
116
                foreach ($dataSources as $alias => $dataSource) {
117
                    if (!$widget->hasCriteriaNamed($alias)) {
118
                        $criteria = new Criteria();
119
                        $criteria->setName($alias);
120
                        $widget->addCriteria($criteria);
121
                    }
122
                }
123
            }
124
        );
125
        $builder->addEventListener(
126
            FormEvents::POST_SUBMIT,
127
            function (FormEvent $event) use ($options) {
128
                $widget = $event->getData();
129
                /** @var Criteria $criteria */
130
                foreach ($widget->getCriterias() as $criteria) {
131
                    if ($criteria->getValue() === null) {
132
                        $widget->removeCriteria($criteria);
133
                    }
134
                }
135
            }
136
        );
137
    }
138
139
    /**
140
     * Add the fields for the business entity mode.
141
     *
142
     * @param FormBuilderInterface|FormInterface $form
143
     * @param array                              $options
144
     */
145 View Code Duplication
    protected function addBusinessEntityFields($form, $options)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
    {
147
        $form->add('fields', WidgetFieldsFormType::class, [
148
            'label'     => 'widget.form.entity.fields.label',
149
            'namespace' => $options['namespace'],
150
            'widget'    => $options['widget'],
151
        ]);
152
    }
153
154
    /**
155
     * Add the fields for the form and the entity mode.
156
     *
157
     * @param FormBuilderInterface|FormInterface $form
158
     * @param array                              $options
159
     */
160
    protected function addEntityFields($form, $options)
161
    {
162
        $form
163
        ->add('fields', WidgetFieldsFormType::class, [
164
            'label'     => 'widget.form.entity.fields.label',
165
            'namespace' => $options['namespace'],
166
            'widget'    => $options['widget'],
167
        ])
168
        ->add('entity_proxy', EntityProxyFormType::class, [
169
            'business_entity_id' => $options['businessEntityId'],
170
            'namespace'          => $options['namespace'],
171
            'widget'             => $options['widget'],
172
        ]);
173
    }
174
175
    /**
176
     * Add the fields to the form for the query mode.
177
     *
178
     * @param FormBuilderInterface|FormInterface $form
179
     * @param array                              $options
180
     */
181 View Code Duplication
    protected function addQueryFields($form, $options)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183
        $form->add('query');
184
        $form->add('fields', WidgetFieldsFormType::class, [
185
            'label'     => 'widget.form.entity.fields.label',
186
            'namespace' => $options['namespace'],
187
            'widget'    => $options['widget'],
188
        ]);
189
    }
190
191
    /**
192
     * bind form to WidgetRedactor entity.
193
     *
194
     * @param OptionsResolver $resolver
195
     */
196
    public function configureOptions(OptionsResolver $resolver)
197
    {
198
        $resolver->setDefaults([
199
            'data_class'         => 'Victoire\Bundle\WidgetBundle\Entity\Widget',
200
            'translation_domain' => 'victoire',
201
            'mode'               => Widget::MODE_STATIC,
202
        ]);
203
204
        $resolver->setDefined([
205
            'widget',
206
            'slot',
207
            'namespace',
208
            'businessEntityId',
209
            'dataSources',
210
        ]);
211
    }
212
}
213