Issues (3627)

bundles/DashboardBundle/Form/Type/WidgetType.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\DashboardBundle\Form\Type;
13
14
use Mautic\CoreBundle\Form\Type\FormButtonsType;
15
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
16
use Mautic\DashboardBundle\DashboardEvents;
17
use Mautic\DashboardBundle\Event\WidgetFormEvent;
18
use Mautic\DashboardBundle\Event\WidgetTypeListEvent;
19
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
use Symfony\Component\Form\AbstractType;
22
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
23
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
24
use Symfony\Component\Form\Extension\Core\Type\TextType;
25
use Symfony\Component\Form\FormBuilderInterface;
26
use Symfony\Component\Form\FormEvent;
27
use Symfony\Component\Form\FormEvents;
28
29
/**
30
 * Class WidgetType.
31
 */
32
class WidgetType extends AbstractType
33
{
34
    /**
35
     * @var ContainerAwareEventDispatcher
36
     */
37
    protected $dispatcher;
38
39
    /**
40
     * @var CorePermissions
41
     */
42
    protected $security;
43
44
    public function __construct(EventDispatcherInterface $dispatcher, CorePermissions $security)
45
    {
46
        $this->dispatcher = $dispatcher;
0 ignored issues
show
Documentation Bug introduced by
$dispatcher is of type Symfony\Component\EventD...ventDispatcherInterface, but the property $dispatcher was declared to be of type Symfony\Component\EventD...nerAwareEventDispatcher. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
47
        $this->security   = $security;
48
    }
49
50
    public function buildForm(FormBuilderInterface $builder, array $options)
51
    {
52
        $builder->add(
53
            'name',
54
            TextType::class,
55
            [
56
                'label'      => 'mautic.dashboard.widget.form.name',
57
                'label_attr' => ['class' => 'control-label'],
58
                'attr'       => ['class' => 'form-control'],
59
                'required'   => false,
60
            ]
61
        );
62
63
        $event = new WidgetTypeListEvent();
64
        $event->setSecurity($this->security);
65
        $this->dispatcher->dispatch(DashboardEvents::DASHBOARD_ON_MODULE_LIST_GENERATE, $event);
66
67
        $types = array_map(function ($category) {
68
            return array_flip($category);
69
        }, $event->getTypes());
70
71
        $builder->add(
72
            'type',
73
            ChoiceType::class,
74
            [
75
                'label'             => 'mautic.dashboard.widget.form.type',
76
                'choices'           => $types,
77
                'label_attr'        => ['class' => 'control-label'],
78
                'placeholder'       => 'mautic.core.select',
79
                'attr'              => [
80
                    'class'    => 'form-control',
81
                    'onchange' => 'Mautic.updateWidgetForm(this)',
82
                ],
83
            ]
84
        );
85
86
        $builder->add(
87
            'width',
88
            ChoiceType::class,
89
            [
90
                'label'   => 'mautic.dashboard.widget.form.width',
91
                'choices' => [
92
                    '25%'  => '25',
93
                    '50%'  => '50',
94
                    '75%'  => '75',
95
                    '100%' => '100',
96
                ],
97
                'empty_data'        => '100',
98
                'label_attr'        => ['class' => 'control-label'],
99
                'attr'              => ['class' => 'form-control'],
100
                'required'          => false,
101
            ]
102
        );
103
104
        $builder->add(
105
            'height',
106
            ChoiceType::class,
107
            [
108
                'label'   => 'mautic.dashboard.widget.form.height',
109
                'choices' => [
110
                    '215px' => '215',
111
                    '330px' => '330',
112
                    '445px' => '445',
113
                    '560px' => '560',
114
                    '675px' => '675',
115
                ],
116
                'empty_data'        => '330',
117
                'label_attr'        => ['class' => 'control-label'],
118
                'attr'              => ['class' => 'form-control'],
119
                'required'          => false,
120
            ]
121
        );
122
123
        // function to add a form for specific widget type dynamically
124
        $func = function (FormEvent $e) {
125
            $data   = $e->getData();
126
            $form   = $e->getForm();
127
            $event  = new WidgetFormEvent();
128
            $type   = null;
129
            $params = [];
130
131
            // $data is object on load, array on save (??)
132
            if (is_array($data)) {
133
                if (isset($data['type'])) {
134
                    $type = $data['type'];
135
                }
136
                if (isset($data['params'])) {
137
                    $params = $data['params'];
138
                }
139
            } else {
140
                $type   = $data->getType();
141
                $params = $data->getParams();
142
            }
143
144
            $event->setType($type);
145
            $this->dispatcher->dispatch(DashboardEvents::DASHBOARD_ON_MODULE_FORM_GENERATE, $event);
146
            $widgetForm = $event->getForm();
147
            $form->setData($params);
148
149
            if (isset($widgetForm['formAlias'])) {
150
                $form->add('params', $widgetForm['formAlias'], [
151
                    'label' => false,
152
                ]);
153
            }
154
        };
155
156
        $builder->add(
157
            'id',
158
            HiddenType::class,
159
            [
160
                'mapped' => false,
161
            ]
162
        );
163
164
        $builder->add(
165
            'buttons',
166
            FormButtonsType::class,
167
            [
168
                'apply_text' => false,
169
                'save_text'  => 'mautic.core.form.save',
170
            ]
171
        );
172
173
        if (!empty($options['action'])) {
174
            $builder->setAction($options['action']);
175
        }
176
177
        // Register the function above as EventListener on PreSet and PreBind
178
        $builder->addEventListener(FormEvents::PRE_SET_DATA, $func);
179
        $builder->addEventListener(FormEvents::PRE_SUBMIT, $func);
180
    }
181
182
    /**
183
     * @return string
184
     */
185
    public function getBlockPrefix()
186
    {
187
        return 'widget';
188
    }
189
}
190