Issues (3627)

app/bundles/UserBundle/Form/Type/UserType.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\UserBundle\Form\Type;
13
14
use Doctrine\ORM\EntityRepository;
15
use Mautic\CoreBundle\Form\EventListener\CleanFormSubscriber;
16
use Mautic\CoreBundle\Form\EventListener\FormExitSubscriber;
17
use Mautic\CoreBundle\Form\Type\FormButtonsType;
18
use Mautic\CoreBundle\Form\Type\YesNoButtonGroupType;
19
use Mautic\CoreBundle\Helper\LanguageHelper;
20
use Mautic\UserBundle\Entity\Role;
21
use Mautic\UserBundle\Entity\User;
22
use Mautic\UserBundle\Model\UserModel;
23
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
24
use Symfony\Component\Form\AbstractType;
25
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
26
use Symfony\Component\Form\Extension\Core\Type\EmailType;
27
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
28
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
29
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
30
use Symfony\Component\Form\Extension\Core\Type\TextType;
31
use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
32
use Symfony\Component\Form\FormBuilderInterface;
33
use Symfony\Component\OptionsResolver\OptionsResolver;
34
use Symfony\Component\Translation\TranslatorInterface;
35
36
class UserType extends AbstractType
37
{
38
    /**
39
     * @var TranslatorInterface
40
     */
41
    private $translator;
42
43
    /**
44
     * @var UserModel
45
     */
46
    private $model;
47
48
    /**
49
     * @var LanguageHelper
50
     */
51
    private $languageHelper;
52
53
    public function __construct(
54
        TranslatorInterface $translator,
55
        UserModel $model,
56
        LanguageHelper $languageHelper
57
    ) {
58
        $this->translator       = $translator;
59
        $this->model            = $model;
60
        $this->languageHelper   = $languageHelper;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function buildForm(FormBuilderInterface $builder, array $options)
67
    {
68
        $builder->addEventSubscriber(new CleanFormSubscriber());
69
        $builder->addEventSubscriber(new FormExitSubscriber('user.user', $options));
70
71
        $builder->add(
72
            'username',
73
            TextType::class,
74
            [
75
                'label'      => 'mautic.core.username',
76
                'label_attr' => ['class' => 'control-label'],
77
                'attr'       => [
78
                    'class'        => 'form-control',
79
                    'preaddon'     => 'fa fa-user',
80
                    'autocomplete' => 'off',
81
                ],
82
            ]
83
        );
84
85
        $builder->add(
86
            'firstName',
87
            TextType::class,
88
            [
89
                'label'      => 'mautic.core.firstname',
90
                'label_attr' => ['class' => 'control-label'],
91
                'attr'       => ['class' => 'form-control'],
92
            ]
93
        );
94
95
        $builder->add(
96
            'lastName',
97
            TextType::class,
98
            [
99
                'label'      => 'mautic.core.lastname',
100
                'label_attr' => ['class' => 'control-label'],
101
                'attr'       => ['class' => 'form-control'],
102
            ]
103
        );
104
105
        $positions = $this->model->getLookupResults('position', null, 0, true);
0 ignored issues
show
The call to Mautic\UserBundle\Model\...del::getLookupResults() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
        /** @scrutinizer ignore-call */ 
106
        $positions = $this->model->getLookupResults('position', null, 0, true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
106
        $builder->add(
107
            'position',
108
            TextType::class,
109
            [
110
                'label'      => 'mautic.core.position',
111
                'label_attr' => ['class' => 'control-label'],
112
                'attr'       => [
113
                    'class'        => 'form-control',
114
                    'data-options' => json_encode($positions),
115
                ],
116
                'required' => false,
117
            ]
118
        );
119
120
        $builder->add(
121
            'email',
122
            EmailType::class,
123
            [
124
                'label'      => 'mautic.core.type.email',
125
                'label_attr' => ['class' => 'control-label'],
126
                'attr'       => [
127
                    'class'    => 'form-control',
128
                    'preaddon' => 'fa fa-envelope',
129
                ],
130
            ]
131
        );
132
133
        $existing    = (!empty($options['data']) && $options['data']->getId());
134
        $placeholder = ($existing) ?
135
            $this->translator->trans('mautic.user.user.form.passwordplaceholder') : '';
136
        $required = ($existing) ? false : true;
137
        $builder->add(
138
            'plainPassword',
139
            RepeatedType::class,
140
            [
141
                'first_name'    => 'password',
142
                'first_options' => [
143
                    'label'      => 'mautic.core.password',
144
                    'label_attr' => ['class' => 'control-label'],
145
                    'attr'       => [
146
                        'class'        => 'form-control',
147
                        'placeholder'  => $placeholder,
148
                        'tooltip'      => 'mautic.user.user.form.help.passwordrequirements',
149
                        'preaddon'     => 'fa fa-lock',
150
                        'autocomplete' => 'off',
151
                    ],
152
                    'required'       => $required,
153
                    'error_bubbling' => false,
154
                ],
155
                'second_name'    => 'confirm',
156
                'second_options' => [
157
                    'label'      => 'mautic.user.user.form.passwordconfirm',
158
                    'label_attr' => ['class' => 'control-label'],
159
                    'attr'       => [
160
                        'class'        => 'form-control',
161
                        'placeholder'  => $placeholder,
162
                        'tooltip'      => 'mautic.user.user.form.help.passwordrequirements',
163
                        'preaddon'     => 'fa fa-lock',
164
                        'autocomplete' => 'off',
165
                    ],
166
                    'required'       => $required,
167
                    'error_bubbling' => false,
168
                ],
169
                'type'            => PasswordType::class,
170
                'invalid_message' => 'mautic.user.user.password.mismatch',
171
                'required'        => $required,
172
                'error_bubbling'  => false,
173
            ]
174
        );
175
176
        $builder->add(
177
            'timezone',
178
            TimezoneType::class,
179
            [
180
                'label'      => 'mautic.core.timezone',
181
                'label_attr' => ['class' => 'control-label'],
182
                'attr'       => [
183
                    'class' => 'form-control',
184
                ],
185
                'multiple'    => false,
186
                'placeholder' => 'mautic.user.user.form.defaulttimezone',
187
            ]
188
        );
189
190
        $builder->add(
191
            'locale',
192
            ChoiceType::class,
193
            [
194
                'choices'           => $this->getSupportedLanguageChoices(),
195
                'label'             => 'mautic.core.language',
196
                'label_attr'        => ['class' => 'control-label'],
197
                'attr'              => [
198
                    'class' => 'form-control',
199
                ],
200
                'multiple'    => false,
201
                'placeholder' => 'mautic.user.user.form.defaultlocale',
202
            ]
203
        );
204
205
        $defaultSignature = '';
206
        if (isset($options['data']) && null === $options['data']->getSignature()) {
207
            $defaultSignature = $this->translator->trans('mautic.email.default.signature', ['%from_name%' => '|FROM_NAME|']);
208
        } elseif (isset($options['data'])) {
209
            $defaultSignature = $options['data']->getSignature();
210
        }
211
212
        $builder->add(
213
            'signature',
214
            TextareaType::class,
215
            [
216
                'label'      => 'mautic.email.token.signature',
217
                'label_attr' => ['class' => 'control-label'],
218
                'required'   => false,
219
                'attr'       => [
220
                    'class' => 'form-control',
221
                ],
222
                'data' => $defaultSignature,
223
            ]
224
        );
225
226
        if (empty($options['in_profile'])) {
227
            $builder->add(
228
                $builder->create(
229
                    'role',
230
                    EntityType::class,
231
                    [
232
                        'label'      => 'mautic.user.role',
233
                        'label_attr' => ['class' => 'control-label'],
234
                        'attr'       => [
235
                            'class' => 'form-control',
236
                        ],
237
                        'class'         => Role::class,
238
                        'choice_label'  => 'name',
239
                        'query_builder' => function (EntityRepository $er) {
240
                            return $er->createQueryBuilder('r')
241
                                ->where('r.isPublished = true')
242
                                ->orderBy('r.name', 'ASC');
243
                        },
244
                    ]
245
                )
246
            );
247
248
            $builder->add('isPublished', YesNoButtonGroupType::class);
249
250
            $builder->add('buttons', FormButtonsType::class);
251
        } else {
252
            $builder->add(
253
                'buttons',
254
                FormButtonsType::class,
255
                [
256
                    'save_text'  => 'mautic.core.form.apply',
257
                    'apply_text' => false,
258
                ]
259
            );
260
        }
261
262
        if (!empty($options['action'])) {
263
            $builder->setAction($options['action']);
264
        }
265
    }
266
267
    /**
268
     * {@inheritdoc}
269
     */
270
    public function configureOptions(OptionsResolver $resolver)
271
    {
272
        $resolver->setDefaults(
273
            [
274
                'data_class'        => User::class,
275
                'validation_groups' => [
276
                    User::class,
277
                    'determineValidationGroups',
278
                ],
279
                'ignore_formexit' => false,
280
                'in_profile'      => false,
281
            ]
282
        );
283
    }
284
285
    /**
286
     * {@inheritdoc}
287
     */
288
    public function getBlockPrefix()
289
    {
290
        return 'user';
291
    }
292
293
    /**
294
     * @return array
295
     */
296
    private function getSupportedLanguageChoices()
297
    {
298
        // Get the list of available languages
299
        $languages = $this->languageHelper->fetchLanguages(false, false);
300
        $choices   = [];
301
302
        foreach ($languages as $code => $langData) {
303
            $choices[$langData['name']] = $code;
304
        }
305
        $choices = array_merge($choices, array_flip($this->languageHelper->getSupportedLanguages()));
306
307
        // Alpha sort the languages by name
308
        ksort($choices);
309
310
        return $choices;
311
    }
312
}
313