| Total Complexity | 84 |
| Total Lines | 691 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ManageCustomProfile often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ManageCustomProfile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class ManageCustomProfile extends AbstractController |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Pre-dispatch, called before other methods. |
||
| 33 | */ |
||
| 34 | public function pre_dispatch() |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Default action for this controller. |
||
| 44 | */ |
||
| 45 | public function action_index() |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Show all the custom profile fields available to the user. |
||
| 62 | * |
||
| 63 | * - Allows for drag/drop sorting of custom profile fields |
||
| 64 | * - Accessed with ?action=admin;area=featuresettings;sa=profile |
||
| 65 | * |
||
| 66 | * @uses sub template show_custom_profile |
||
| 67 | */ |
||
| 68 | public function action_profile(): void |
||
| 69 | { |
||
| 70 | global $txt, $context; |
||
| 71 | |||
| 72 | theme()->getTemplates()->load('ManageFeatures'); |
||
| 73 | $context['page_title'] = $txt['custom_profile_title']; |
||
| 74 | $context['sub_template'] = 'show_custom_profile'; |
||
| 75 | |||
| 76 | // What about standard fields they can tweak? |
||
| 77 | $standard_fields = ['website', 'posts', 'warning_status', 'date_registered', 'action']; |
||
| 78 | |||
| 79 | // What fields can't you put on the registration page? |
||
| 80 | $context['fields_no_registration'] = ['posts', 'warning_status', 'date_registered', 'action']; |
||
| 81 | |||
| 82 | // Are we saving any standard field changes? |
||
| 83 | if ($this->_req->hasPost('save')) |
||
| 84 | { |
||
| 85 | checkSession(); |
||
| 86 | validateToken('admin-scp'); |
||
| 87 | |||
| 88 | $changes = []; |
||
| 89 | |||
| 90 | // Do the active ones first. |
||
| 91 | $disable_fields = array_flip($standard_fields); |
||
| 92 | if (!empty($this->_req->post->active)) |
||
| 93 | { |
||
| 94 | foreach ($this->_req->post->active as $value) |
||
| 95 | { |
||
| 96 | if (isset($disable_fields[$value])) |
||
| 97 | { |
||
| 98 | unset($disable_fields[$value]); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | // What we have left! |
||
| 104 | $changes['disabled_profile_fields'] = empty($disable_fields) ? '' : implode(',', array_keys($disable_fields)); |
||
| 105 | |||
| 106 | // Things we want to show on registration? |
||
| 107 | $reg_fields = []; |
||
| 108 | if (!empty($this->_req->post->reg)) |
||
| 109 | { |
||
| 110 | foreach ($this->_req->post->reg as $value) |
||
| 111 | { |
||
| 112 | if (!in_array($value, $standard_fields)) |
||
| 113 | { |
||
| 114 | continue; |
||
| 115 | } |
||
| 116 | |||
| 117 | if (isset($disable_fields[$value])) |
||
| 118 | { |
||
| 119 | continue; |
||
| 120 | } |
||
| 121 | |||
| 122 | $reg_fields[] = $value; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | // What we have left! |
||
| 127 | $changes['registration_fields'] = empty($reg_fields) ? '' : implode(',', $reg_fields); |
||
| 128 | |||
| 129 | updateSettings($changes); |
||
| 130 | } |
||
| 131 | |||
| 132 | createToken('admin-scp'); |
||
| 133 | |||
| 134 | // Create a listing for all our standard fields |
||
| 135 | $listOptions = [ |
||
| 136 | 'id' => 'standard_profile_fields', |
||
| 137 | 'title' => $txt['standard_profile_title'], |
||
| 138 | 'base_href' => getUrl('admin', ['action' => 'admin', 'area' => 'featuresettings', 'sa' => 'profile']), |
||
| 139 | 'get_items' => [ |
||
| 140 | 'function' => 'list_getProfileFields', |
||
| 141 | 'params' => [ |
||
| 142 | true, |
||
| 143 | ], |
||
| 144 | ], |
||
| 145 | 'columns' => [ |
||
| 146 | 'field' => [ |
||
| 147 | 'header' => [ |
||
| 148 | 'value' => $txt['standard_profile_field'], |
||
| 149 | ], |
||
| 150 | 'data' => [ |
||
| 151 | 'db' => 'label', |
||
| 152 | 'style' => 'width: 60%;', |
||
| 153 | ], |
||
| 154 | ], |
||
| 155 | 'active' => [ |
||
| 156 | 'header' => [ |
||
| 157 | 'value' => $txt['custom_edit_active'], |
||
| 158 | 'class' => 'centertext', |
||
| 159 | ], |
||
| 160 | 'data' => [ |
||
| 161 | 'function' => static function ($rowData) { |
||
| 162 | $isChecked = $rowData['disabled'] ? '' : ' checked="checked"'; |
||
| 163 | $onClickHandler = $rowData['can_show_register'] ? sprintf('onclick="document.getElementById(\'reg_%1$s\').disabled = !this.checked;"', $rowData['id']) : ''; |
||
| 164 | |||
| 165 | return sprintf('<input type="checkbox" name="active[]" id="active_%1$s" value="%1$s" class="input_check" %2$s %3$s />', $rowData['id'], $isChecked, $onClickHandler); |
||
| 166 | }, |
||
| 167 | 'style' => 'width: 20%;', |
||
| 168 | 'class' => 'centertext', |
||
| 169 | ], |
||
| 170 | ], |
||
| 171 | 'show_on_registration' => [ |
||
| 172 | 'header' => [ |
||
| 173 | 'value' => $txt['custom_edit_registration'], |
||
| 174 | 'class' => 'centertext', |
||
| 175 | ], |
||
| 176 | 'data' => [ |
||
| 177 | 'function' => static function ($rowData) { |
||
| 178 | $isChecked = $rowData['on_register'] && !$rowData['disabled'] ? ' checked="checked"' : ''; |
||
| 179 | $isDisabled = $rowData['can_show_register'] ? '' : ' disabled="disabled"'; |
||
| 180 | |||
| 181 | return sprintf('<input type="checkbox" name="reg[]" id="reg_%1$s" value="%1$s" class="input_check" %2$s %3$s />', $rowData['id'], $isChecked, $isDisabled); |
||
| 182 | }, |
||
| 183 | 'style' => 'width: 20%;', |
||
| 184 | 'class' => 'centertext', |
||
| 185 | ], |
||
| 186 | ], |
||
| 187 | ], |
||
| 188 | 'form' => [ |
||
| 189 | 'href' => getUrl('admin', ['action' => 'admin', 'area' => 'featuresettings', 'sa' => 'profile']), |
||
| 190 | 'name' => 'standardProfileFields', |
||
| 191 | 'token' => 'admin-scp', |
||
| 192 | ], |
||
| 193 | 'additional_rows' => [ |
||
| 194 | [ |
||
| 195 | 'position' => 'below_table_data', |
||
| 196 | 'value' => '<input type="submit" name="save" value="' . $txt['save'] . '" class="right_submit" />', |
||
| 197 | ], |
||
| 198 | ], |
||
| 199 | ]; |
||
| 200 | createList($listOptions); |
||
| 201 | |||
| 202 | // And now we do the same for all of our custom ones |
||
| 203 | $token = createToken('admin-sort'); |
||
| 204 | $listOptions = [ |
||
| 205 | 'id' => 'custom_profile_fields', |
||
| 206 | 'title' => $txt['custom_profile_title'], |
||
| 207 | 'base_href' => getUrl('admin', ['action' => 'admin', 'area' => 'featuresettings', 'sa' => 'profile']), |
||
| 208 | 'default_sort_col' => 'vieworder', |
||
| 209 | 'no_items_label' => $txt['custom_profile_none'], |
||
| 210 | 'items_per_page' => 25, |
||
| 211 | 'sortable' => true, |
||
| 212 | 'get_items' => [ |
||
| 213 | 'function' => 'list_getProfileFields', |
||
| 214 | 'params' => [ |
||
| 215 | false, |
||
| 216 | ], |
||
| 217 | ], |
||
| 218 | 'get_count' => [ |
||
| 219 | 'function' => 'list_getProfileFieldSize', |
||
| 220 | ], |
||
| 221 | 'columns' => [ |
||
| 222 | 'vieworder' => [ |
||
| 223 | 'header' => [ |
||
| 224 | 'value' => '', |
||
| 225 | 'class' => 'hide', |
||
| 226 | ], |
||
| 227 | 'data' => [ |
||
| 228 | 'db' => 'vieworder', |
||
| 229 | 'class' => 'hide', |
||
| 230 | ], |
||
| 231 | 'sort' => [ |
||
| 232 | 'default' => 'vieworder', |
||
| 233 | ], |
||
| 234 | ], |
||
| 235 | 'field_name' => [ |
||
| 236 | 'header' => [ |
||
| 237 | 'value' => $txt['custom_profile_fieldname'], |
||
| 238 | ], |
||
| 239 | 'data' => [ |
||
| 240 | 'function' => static fn($rowData) => sprintf('<a href="%1$s">%2$s</a><div class="smalltext">%3$s</div>', getUrl('admin', ['action' => 'admin', 'area' => 'featuresettings', 'sa' => 'profileedit', 'fid' => (int) $rowData['id_field']]), $rowData['field_name'], $rowData['field_desc']), |
||
| 241 | 'style' => 'width: 65%;', |
||
| 242 | ], |
||
| 243 | 'sort' => [ |
||
| 244 | 'default' => 'field_name', |
||
| 245 | 'reverse' => 'field_name DESC', |
||
| 246 | ], |
||
| 247 | ], |
||
| 248 | 'field_type' => [ |
||
| 249 | 'header' => [ |
||
| 250 | 'value' => $txt['custom_profile_fieldtype'], |
||
| 251 | ], |
||
| 252 | 'data' => [ |
||
| 253 | 'function' => static function ($rowData) { |
||
| 254 | global $txt; |
||
| 255 | |||
| 256 | $textKey = sprintf('custom_profile_type_%1$s', $rowData['field_type']); |
||
| 257 | |||
| 258 | return $txt[$textKey] ?? $textKey; |
||
| 259 | }, |
||
| 260 | 'style' => 'width: 10%;', |
||
| 261 | ], |
||
| 262 | 'sort' => [ |
||
| 263 | 'default' => 'field_type', |
||
| 264 | 'reverse' => 'field_type DESC', |
||
| 265 | ], |
||
| 266 | ], |
||
| 267 | 'cust' => [ |
||
| 268 | 'header' => [ |
||
| 269 | 'value' => $txt['custom_profile_active'], |
||
| 270 | 'class' => 'centertext', |
||
| 271 | ], |
||
| 272 | 'data' => [ |
||
| 273 | 'function' => static function ($rowData) { |
||
| 274 | $isChecked = $rowData['active'] === '1' ? ' checked="checked"' : ''; |
||
| 275 | |||
| 276 | return sprintf('<input type="checkbox" name="cust[]" id="cust_%1$s" value="%1$s" class="input_check"%2$s />', $rowData['id_field'], $isChecked); |
||
| 277 | }, |
||
| 278 | 'style' => 'width: 8%;', |
||
| 279 | 'class' => 'centertext', |
||
| 280 | ], |
||
| 281 | 'sort' => [ |
||
| 282 | 'default' => 'active DESC', |
||
| 283 | 'reverse' => 'active', |
||
| 284 | ], |
||
| 285 | ], |
||
| 286 | 'placement' => [ |
||
| 287 | 'header' => [ |
||
| 288 | 'value' => $txt['custom_profile_placement'], |
||
| 289 | ], |
||
| 290 | 'data' => [ |
||
| 291 | 'function' => static function ($rowData) { |
||
| 292 | global $txt; |
||
| 293 | |||
| 294 | $placement = 'custom_profile_placement_'; |
||
| 295 | switch ((int) $rowData['placement']) |
||
| 296 | { |
||
| 297 | case 0: |
||
| 298 | $placement .= 'standard'; |
||
| 299 | break; |
||
| 300 | case 1: |
||
| 301 | $placement .= 'withicons'; |
||
| 302 | break; |
||
| 303 | case 2: |
||
| 304 | $placement .= 'abovesignature'; |
||
| 305 | break; |
||
| 306 | case 3: |
||
| 307 | $placement .= 'aboveicons'; |
||
| 308 | break; |
||
| 309 | } |
||
| 310 | |||
| 311 | return $txt[$placement]; |
||
| 312 | }, |
||
| 313 | 'style' => 'width: 5%;', |
||
| 314 | ], |
||
| 315 | 'sort' => [ |
||
| 316 | 'default' => 'placement DESC', |
||
| 317 | 'reverse' => 'placement', |
||
| 318 | ], |
||
| 319 | ], |
||
| 320 | 'modify' => [ |
||
| 321 | 'data' => [ |
||
| 322 | 'sprintf' => [ |
||
| 323 | 'format' => '<a href="' . getUrl('admin', ['action' => 'admin', 'area' => 'featuresettings', 'sa' => 'profileedit']) . ';fid=%1$s">' . $txt['modify'] . '</a>', |
||
| 324 | 'params' => [ |
||
| 325 | 'id_field' => false, |
||
| 326 | ], |
||
| 327 | ], |
||
| 328 | 'style' => 'width: 5%;', |
||
| 329 | ], |
||
| 330 | ], |
||
| 331 | ], |
||
| 332 | 'form' => [ |
||
| 333 | 'href' => getUrl('admin', ['action' => 'admin', 'area' => 'featuresettings', 'sa' => 'profileedit']), |
||
| 334 | 'name' => 'customProfileFields', |
||
| 335 | 'token' => 'admin-scp', |
||
| 336 | ], |
||
| 337 | 'additional_rows' => [ |
||
| 338 | [ |
||
| 339 | 'class' => 'submitbutton flow_flex_additional_row', |
||
| 340 | 'position' => 'below_table_data', |
||
| 341 | 'value' => ' |
||
| 342 | <input type="submit" name="onoff" value="' . $txt['save'] . '" /> |
||
| 343 | <input type="submit" name="new" value="' . $txt['custom_profile_make_new'] . '" />', |
||
| 344 | ], |
||
| 345 | [ |
||
| 346 | 'position' => 'top_of_list', |
||
| 347 | 'value' => '<p class="infobox">' . $txt['custom_profile_sort'] . '</p>', |
||
| 348 | ], |
||
| 349 | ], |
||
| 350 | 'javascript' => ' |
||
| 351 | $().elkSortable({ |
||
| 352 | sa: "profileorder", |
||
| 353 | error: "' . $txt['admin_order_error'] . '", |
||
| 354 | title: "' . $txt['admin_order_title'] . '", |
||
| 355 | placeholder: "ui-state-highlight", |
||
| 356 | href: "?action=admin;area=featuresettings;sa=profile", |
||
| 357 | token: {token_var: "' . $token['admin-sort_token_var'] . '", token_id: "' . $token['admin-sort_token'] . '"} |
||
| 358 | }); |
||
| 359 | ', |
||
| 360 | ]; |
||
| 361 | |||
| 362 | createList($listOptions); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Edit some profile fields? |
||
| 367 | * |
||
| 368 | * - Accessed with ?action=admin;area=featuresettings;sa=profileedit |
||
| 369 | * |
||
| 370 | * @uses sub template edit_profile_field |
||
| 371 | */ |
||
| 372 | public function action_profileedit(): void |
||
| 720 | } |
||
| 721 | } |
||
| 722 |