Conditions | 18 |
Paths | 141 |
Total Lines | 164 |
Code Lines | 87 |
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 |
||
52 | public function buildForm(FormBuilderInterface $builder, array $options): void |
||
53 | { |
||
54 | $languages = array_flip($this->languageRepository->getAllAvailableToArray()); |
||
55 | |||
56 | $builder |
||
57 | ->add('firstname', TextType::class, ['label' => 'Firstname', 'required' => true]) |
||
58 | ->add('lastname', TextType::class, ['label' => 'Lastname', 'required' => true]) |
||
59 | ->add('email', EmailType::class, ['label' => 'Email', 'required' => true]) |
||
60 | //->add('official_code', TextType::class) |
||
61 | //->add('groups') |
||
62 | ->add('locale', LocaleType::class, [ |
||
63 | //'preferred_choices' => ['en', 'fr_FR', 'es_ES', 'pt', 'nl'], |
||
64 | 'choices' => $languages, |
||
65 | 'choice_loader' => null, |
||
66 | ]) |
||
67 | /*->add( 'dateOfBirth', |
||
68 | BirthdayType::class, |
||
69 | [ |
||
70 | 'label' => 'form.label_date_of_birth', |
||
71 | 'required' => false, |
||
72 | 'widget' => 'single_text', |
||
73 | ] |
||
74 | ) |
||
75 | ->add( |
||
76 | 'biography', |
||
77 | TextareaType::class, |
||
78 | [ |
||
79 | 'label' => 'form.label_biography', |
||
80 | 'required' => false, |
||
81 | ] |
||
82 | )*/ |
||
83 | /*->add('locale', 'locale', array( |
||
84 | 'label' => 'form.label_locale', |
||
85 | 'required' => false, |
||
86 | ))*/ |
||
87 | ->add('timezone', TimezoneType::class, ['label' => 'Timezone', 'required' => true]) |
||
88 | ->add('phone', TextType::class, ['label' => 'Phone number', 'required' => false]) |
||
89 | ->add( |
||
90 | 'illustration', |
||
91 | IllustrationType::class, |
||
92 | ['label' => 'Picture', 'required' => false, 'mapped' => false] |
||
93 | ) |
||
94 | //->add('website', UrlType::class, ['label' => 'Website', 'required' => false]) |
||
95 | /*->add( |
||
96 | 'extraFieldValues', |
||
97 | CollectionType::class, |
||
98 | array( |
||
99 | 'required' => false, |
||
100 | 'allow_add' => true, |
||
101 | 'allow_delete' => true, |
||
102 | 'type' => 'chamilo_user_extra_field_value', |
||
103 | 'by_reference' => false, |
||
104 | 'prototype' => true, |
||
105 | 'widget_add_btn' => ['label' => 'Add'], |
||
106 | 'options' => array( // options for collection fields |
||
107 | 'widget_remove_btn' => array('label' => 'Remove'), |
||
108 | 'label_render' => false, |
||
109 | ) |
||
110 | ) |
||
111 | )*/ |
||
112 | //->add('save', 'submit', array('label' => 'Update') ) |
||
113 | ; |
||
114 | |||
115 | $user = $this->security->getUser(); |
||
116 | |||
117 | if (null === $user) { |
||
118 | return; |
||
119 | } |
||
120 | |||
121 | $extraFields = $this->extraFieldRepository->getExtraFields(ExtraField::USER_FIELD_TYPE); |
||
122 | $values = $this->extraFieldValuesRepository->getExtraFieldValuesFromItem($user, ExtraField::USER_FIELD_TYPE); |
||
123 | |||
124 | $data = []; |
||
125 | foreach ($values as $value) { |
||
126 | $data[$value->getField()->getVariable()] = $value->getValue(); |
||
127 | } |
||
128 | |||
129 | foreach ($extraFields as $extraField) { |
||
130 | $text = $extraField->getDisplayText(); |
||
131 | $variable = $extraField->getVariable(); |
||
132 | $value = $data[$extraField->getVariable()] ?? ''; |
||
133 | |||
134 | // @todo |
||
135 | switch ($extraField->getFieldType()) { |
||
136 | case \ExtraField::FIELD_TYPE_DATETIME: |
||
137 | $builder->add($variable, DateTimeType::class, [ |
||
138 | 'label' => $text, |
||
139 | 'required' => false, |
||
140 | 'by_reference' => false, |
||
141 | 'mapped' => false, |
||
142 | 'data' => $value, |
||
143 | ]); |
||
144 | |||
145 | break; |
||
146 | case \ExtraField::FIELD_TYPE_DATE: |
||
147 | $builder->add($variable, DateType::class, [ |
||
148 | 'label' => $text, |
||
149 | 'required' => false, |
||
150 | 'by_reference' => false, |
||
151 | 'mapped' => false, |
||
152 | 'data' => $value, |
||
153 | ]); |
||
154 | |||
155 | break; |
||
156 | case \ExtraField::FIELD_TYPE_TEXTAREA: |
||
157 | case \ExtraField::FIELD_TYPE_TEXT: |
||
158 | $builder->add($variable, TextType::class, [ |
||
159 | 'label' => $text, |
||
160 | 'required' => false, |
||
161 | 'by_reference' => false, |
||
162 | 'mapped' => false, |
||
163 | 'data' => $value, |
||
164 | ]); |
||
165 | |||
166 | break; |
||
167 | case \ExtraField::FIELD_TYPE_CHECKBOX: |
||
168 | case \ExtraField::FIELD_TYPE_RADIO: |
||
169 | case \ExtraField::FIELD_TYPE_SELECT: |
||
170 | case \ExtraField::FIELD_TYPE_SELECT_MULTIPLE: |
||
171 | $options = $extraField->getOptions(); |
||
172 | $choices = []; |
||
173 | foreach ($options as $option) { |
||
174 | $choices[$option->getDisplayText()] = $option->getValue(); |
||
175 | } |
||
176 | |||
177 | $params = [ |
||
178 | 'label' => $text, |
||
179 | 'required' => false, |
||
180 | 'by_reference' => false, |
||
181 | 'mapped' => false, |
||
182 | 'data' => $value, |
||
183 | 'choices' => $choices, |
||
184 | ]; |
||
185 | |||
186 | if (\ExtraField::FIELD_TYPE_CHECKBOX === $extraField->getFieldType()) { |
||
187 | $params['expanded'] = true; |
||
188 | $params['multiple'] = true; |
||
189 | } |
||
190 | if (\ExtraField::FIELD_TYPE_SELECT === $extraField->getFieldType()) { |
||
191 | $params['expanded'] = false; |
||
192 | $params['multiple'] = false; |
||
193 | } |
||
194 | if (\ExtraField::FIELD_TYPE_SELECT_MULTIPLE === $extraField->getFieldType()) { |
||
195 | $params['expanded'] = false; |
||
196 | $params['multiple'] = true; |
||
197 | } |
||
198 | $builder->add($variable, ChoiceType::class, $params); |
||
199 | |||
200 | break; |
||
201 | } |
||
202 | } |
||
203 | |||
204 | $builder->addEventListener( |
||
205 | FormEvents::PRE_SUBMIT, |
||
206 | function (FormEvent $event) use ($user, $extraFields): void { |
||
207 | $data = $event->getData(); |
||
208 | |||
209 | /** @var ExtraField $extraField */ |
||
210 | foreach ($extraFields as $extraField) { |
||
211 | $newValue = $data[$extraField->getVariable()] ?? ''; |
||
212 | if (empty($newValue)) { |
||
213 | continue; |
||
214 | } |
||
215 | $this->extraFieldValuesRepository->updateItemData($extraField, $user, $newValue); |
||
216 | } |
||
235 |