Conditions | 6 |
Paths | 5 |
Total Lines | 112 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
44 | public function buildForm(FormBuilderInterface $builder, array $options): void |
||
45 | { |
||
46 | $languages = array_flip($this->languageRepository->getAllAvailableToArray()); |
||
47 | |||
48 | $builder |
||
49 | ->add('firstname', TextType::class, ['label' => 'Firstname', 'required' => true]) |
||
50 | ->add('lastname', TextType::class, ['label' => 'Lastname', 'required' => true]) |
||
51 | ->add('email', EmailType::class, ['label' => 'Email', 'required' => true]) |
||
52 | //->add('official_code', TextType::class) |
||
53 | //->add('groups') |
||
54 | ->add('locale', LocaleType::class, [ |
||
55 | //'preferred_choices' => ['en', 'fr_FR', 'es_ES', 'pt', 'nl'], |
||
56 | 'choices' => $languages, |
||
57 | 'choice_loader' => null, |
||
58 | ]) |
||
59 | /*->add( 'dateOfBirth', |
||
60 | BirthdayType::class, |
||
61 | [ |
||
62 | 'label' => 'form.label_date_of_birth', |
||
63 | 'required' => false, |
||
64 | 'widget' => 'single_text', |
||
65 | ] |
||
66 | ) |
||
67 | ->add( |
||
68 | 'biography', |
||
69 | TextareaType::class, |
||
70 | [ |
||
71 | 'label' => 'form.label_biography', |
||
72 | 'required' => false, |
||
73 | ] |
||
74 | )*/ |
||
75 | /*->add('locale', 'locale', array( |
||
76 | 'label' => 'form.label_locale', |
||
77 | 'required' => false, |
||
78 | ))*/ |
||
79 | ->add('timezone', TimezoneType::class, ['label' => 'Timezone', 'required' => true]) |
||
80 | ->add('phone', TextType::class, ['label' => 'Phone number', 'required' => false]) |
||
81 | ->add( |
||
82 | 'illustration', |
||
83 | IllustrationType::class, |
||
84 | ['label' => 'Picture', 'required' => false, 'mapped' => false] |
||
85 | ) |
||
86 | //->add('website', UrlType::class, ['label' => 'Website', 'required' => false]) |
||
87 | /*->add( |
||
88 | 'extraFieldValues', |
||
89 | CollectionType::class, |
||
90 | array( |
||
91 | 'required' => false, |
||
92 | 'allow_add' => true, |
||
93 | 'allow_delete' => true, |
||
94 | 'type' => 'chamilo_user_extra_field_value', |
||
95 | 'by_reference' => false, |
||
96 | 'prototype' => true, |
||
97 | 'widget_add_btn' => ['label' => 'Add'], |
||
98 | 'options' => array( // options for collection fields |
||
99 | 'widget_remove_btn' => array('label' => 'Remove'), |
||
100 | 'label_render' => false, |
||
101 | ) |
||
102 | ) |
||
103 | )*/ |
||
104 | //->add('save', 'submit', array('label' => 'Update') ) |
||
105 | ; |
||
106 | |||
107 | $user = $this->security->getUser(); |
||
108 | |||
109 | if (null === $user) { |
||
110 | return; |
||
111 | } |
||
112 | |||
113 | $extraFields = $this->extraFieldRepository->getExtraFields(); |
||
114 | $values = $this->extraFieldValuesRepository->getExtraFieldValuesFromItem($user); |
||
115 | |||
116 | $data = []; |
||
117 | foreach ($values as $value) { |
||
118 | $data[$value->getField()->getVariable()] = $value->getValue(); |
||
119 | } |
||
120 | |||
121 | foreach ($extraFields as $extraField) { |
||
122 | $text = $extraField->getDisplayText(); |
||
123 | $variable = $extraField->getVariable(); |
||
124 | |||
125 | $value = $data[$extraField->getVariable()] ?? ''; |
||
126 | |||
127 | // @todo |
||
128 | /*switch ($extraField->getFieldType()) { |
||
129 | case \ExtraField::FIELD_TYPE_TEXTAREA: |
||
130 | case \ExtraField::FIELD_TYPE_TEXT: |
||
131 | |||
132 | break; |
||
133 | }*/ |
||
134 | |||
135 | $builder->add($variable, TextType::class, [ |
||
136 | 'label' => $text, |
||
137 | 'required' => false, |
||
138 | 'by_reference' => false, |
||
139 | 'mapped' => false, |
||
140 | 'data' => $value, |
||
141 | ]); |
||
142 | } |
||
143 | |||
144 | $builder->addEventListener( |
||
145 | FormEvents::PRE_SUBMIT, |
||
146 | function (FormEvent $event) use ($user, $extraFields): void { |
||
147 | $data = $event->getData(); |
||
148 | |||
149 | /** @var ExtraField $extraField */ |
||
150 | foreach ($extraFields as $extraField) { |
||
151 | $newValue = $data[$extraField->getVariable()] ?? ''; |
||
152 | if (empty($newValue)) { |
||
153 | continue; |
||
154 | } |
||
155 | $this->extraFieldValuesRepository->updateItemData($extraField, $user, $newValue); |
||
156 | } |
||
175 |