1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\Form; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
6
|
|
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType; |
7
|
|
|
use Symfony\Component\Form\AbstractType; |
8
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
9
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
10
|
|
|
use Symfony\Component\Form\Extension\Core\Type\EmailType; |
11
|
|
|
use Symfony\Component\Form\Extension\Core\Type\PasswordType; |
12
|
|
|
use Symfony\Component\Form\Extension\Core\Type\RepeatedType; |
13
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
14
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
15
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* UserType defines the form used for {@link User} |
19
|
|
|
*/ |
20
|
|
|
class UserType extends AbstractType implements RoleDependentUserFormInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
private $canEditAllFields = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Setter to check if we can display all form fields |
29
|
|
|
* |
30
|
|
|
* @param $canEditAllFields |
31
|
|
|
* |
32
|
|
|
* @return bool |
|
|
|
|
33
|
|
|
*/ |
34
|
1 |
|
public function setCanEditAllFields($canEditAllFields) |
35
|
|
|
{ |
36
|
1 |
|
$this->canEditAllFields = (bool) $canEditAllFields; |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
1 |
|
public function buildForm(FormBuilderInterface $builder, array $options) |
43
|
|
|
{ |
44
|
1 |
|
$languages = array(); |
45
|
1 |
|
foreach ($options['langs'] as $lang) { |
46
|
1 |
|
$languages[$lang] = $lang; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
$this->canEditAllFields = $options['can_edit_all_fields']; |
50
|
|
|
|
51
|
1 |
|
$builder->add('username', TextType::class, array('required' => true, 'label' => 'settings.user.username')) |
52
|
1 |
|
->add('plainPassword', RepeatedType::class, array( |
53
|
1 |
|
'type' => PasswordType::class, |
54
|
1 |
|
'required' => $options['password_required'], |
55
|
1 |
|
'invalid_message' => 'errors.password.dontmatch', |
56
|
|
|
'first_options' => array( |
57
|
|
|
'label' => 'settings.user.password', |
58
|
|
|
), |
59
|
|
|
'second_options' => array( |
60
|
|
|
'label' => 'settings.user.repeatedpassword', |
61
|
|
|
), |
62
|
|
|
)) |
63
|
1 |
|
->add('email', EmailType::class, array('required' => true, 'label' => 'settings.user.email')) |
64
|
1 |
|
->add('adminLocale', ChoiceType::class, array( |
65
|
1 |
|
'choices' => $languages, |
66
|
1 |
|
'label' => 'settings.user.adminlang', |
67
|
|
|
'required' => true, |
68
|
|
|
'placeholder' => false, |
69
|
|
|
)); |
70
|
|
|
|
71
|
1 |
|
if ($this->canEditAllFields) { |
72
|
1 |
|
$builder->add('enabled', CheckboxType::class, array('required' => false, 'label' => 'settings.user.enabled')) |
73
|
1 |
|
->add('groups', EntityType::class, array( |
74
|
1 |
|
'label' => 'settings.user.roles', |
75
|
1 |
|
'class' => 'KunstmaanAdminBundle:Group', |
76
|
|
|
'query_builder' => function (EntityRepository $er) { |
77
|
|
|
return $er->createQueryBuilder('g') |
78
|
|
|
->orderBy('g.name', 'ASC'); |
79
|
1 |
|
}, |
80
|
|
|
'multiple' => true, |
81
|
|
|
'expanded' => false, |
82
|
|
|
'required' => false, |
83
|
|
|
'attr' => array( |
84
|
|
|
'placeholder' => 'settings.user.roles_placeholder', |
85
|
|
|
'class' => 'js-advanced-select form-control advanced-select', |
86
|
|
|
), |
87
|
|
|
) |
88
|
|
|
); |
89
|
|
|
} |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
1 |
|
public function getBlockPrefix() |
96
|
|
|
{ |
97
|
1 |
|
return 'user'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
1 |
|
public function configureOptions(OptionsResolver $resolver) |
104
|
|
|
{ |
105
|
1 |
|
$resolver->setDefaults( |
106
|
|
|
array( |
107
|
1 |
|
'password_required' => false, |
108
|
|
|
'data_class' => 'Kunstmaan\AdminBundle\Entity\User', |
109
|
|
|
'langs' => null, |
110
|
|
|
'can_edit_all_fields' => null, |
111
|
|
|
) |
112
|
|
|
); |
113
|
1 |
|
$resolver->addAllowedValues('password_required', array(true, false)); |
114
|
1 |
|
} |
115
|
|
|
} |
116
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.