| Conditions | 61 |
| Paths | > 20000 |
| Total Lines | 348 |
| Code Lines | 190 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 372 | public function action_profileedit(): void |
||
| 373 | { |
||
| 374 | global $txt, $context; |
||
| 375 | |||
| 376 | theme()->getTemplates()->load('ManageFeatures'); |
||
| 377 | |||
| 378 | // Sort out the context! |
||
| 379 | $context['fid'] = $this->_req->getQuery('fid', 'intval', 0); |
||
| 380 | $context[$context['admin_menu_name']]['current_subsection'] = 'profile'; |
||
| 381 | $context['page_title'] = $context['fid'] ? $txt['custom_edit_title'] : $txt['custom_add_title']; |
||
| 382 | $context['sub_template'] = 'edit_profile_field'; |
||
| 383 | |||
| 384 | // Any error messages to show? |
||
| 385 | if ($this->_req->hasQuery('msg')) |
||
| 386 | { |
||
| 387 | Txt::load('Errors'); |
||
| 388 | $msg_key = $this->_req->getQuery('msg', 'trim|strval', ''); |
||
| 389 | if (isset($txt['custom_option_' . $msg_key])) |
||
| 390 | { |
||
| 391 | $context['custom_option__error'] = $txt['custom_option_' . $msg_key]; |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | // Load the profile language for section names. |
||
| 396 | Txt::load('Profile'); |
||
| 397 | |||
| 398 | // Load up the profile field if one was supplied |
||
| 399 | if ($context['fid']) |
||
| 400 | { |
||
| 401 | $context['field'] = getProfileField($context['fid']); |
||
| 402 | } |
||
| 403 | |||
| 404 | // Set up the default values as needed. |
||
| 405 | if (empty($context['field'])) |
||
| 406 | { |
||
| 407 | $context['field'] = [ |
||
| 408 | 'name' => '', |
||
| 409 | 'colname' => '???', |
||
| 410 | 'desc' => '', |
||
| 411 | 'profile_area' => 'forumprofile', |
||
| 412 | 'reg' => false, |
||
| 413 | 'display' => false, |
||
| 414 | 'memberlist' => false, |
||
| 415 | 'type' => 'text', |
||
| 416 | 'max_length' => 255, |
||
| 417 | 'rows' => 4, |
||
| 418 | 'cols' => 30, |
||
| 419 | 'bbc' => false, |
||
| 420 | 'default_check' => false, |
||
| 421 | 'default_select' => '', |
||
| 422 | 'default_value' => '', |
||
| 423 | 'options' => ['', '', ''], |
||
| 424 | 'active' => true, |
||
| 425 | 'private' => false, |
||
| 426 | 'can_search' => false, |
||
| 427 | 'mask' => 'nohtml', |
||
| 428 | 'regex' => '', |
||
| 429 | 'enclose' => '', |
||
| 430 | 'placement' => 0, |
||
| 431 | ]; |
||
| 432 | } |
||
| 433 | |||
| 434 | // All the JavaScript for this page... everything else is in admin.js |
||
| 435 | theme()->addJavascriptVar(['startOptID' => count($context['field']['options'])]); |
||
| 436 | theme()->addInlineJavascript('updateInputBoxes();', true); |
||
| 437 | |||
| 438 | // Are we toggling which ones are active? |
||
| 439 | if (isset($this->_req->post->onoff)) |
||
| 440 | { |
||
| 441 | checkSession(); |
||
| 442 | validateToken('admin-scp'); |
||
| 443 | |||
| 444 | // Enable and disable custom fields as required. |
||
| 445 | $enabled = [0]; |
||
| 446 | if (isset($this->_req->post->cust) && is_array($this->_req->post->cust)) |
||
| 447 | { |
||
| 448 | foreach ($this->_req->post->cust as $id) |
||
| 449 | { |
||
| 450 | $enabled[] = (int) $id; |
||
| 451 | } |
||
| 452 | } |
||
| 453 | |||
| 454 | updateRenamedProfileStatus($enabled); |
||
| 455 | } |
||
| 456 | // Are we saving? |
||
| 457 | elseif ($this->_req->hasPost('save')) |
||
| 458 | { |
||
| 459 | checkSession(); |
||
| 460 | validateToken('admin-ecp'); |
||
| 461 | |||
| 462 | // Everyone needs a name - even the (bracket) unknown... |
||
| 463 | if (trim($this->_req->post->field_name) === '') |
||
| 464 | { |
||
| 465 | redirectexit('action=admin;area=featuresettings;sa=profileedit;fid=' . (int) $context['fid'] . ';msg=need_name'); |
||
| 466 | } |
||
| 467 | |||
| 468 | // Regex, you say? Do a very basic test to see if the pattern is valid |
||
| 469 | if (!empty($this->_req->post->regex) && @preg_match($this->_req->post->regex, 'dummy') === false) |
||
| 470 | { |
||
| 471 | redirectexit('action=admin;area=featuresettings;sa=profileedit;fid=' . (int) $context['fid'] . ';msg=regex_error'); |
||
| 472 | } |
||
| 473 | |||
| 474 | $this->_req->post->field_name = $this->_req->getPost('field_name', 'Util::htmlspecialchars'); |
||
| 475 | $this->_req->post->field_desc = $this->_req->getPost('field_desc', 'Util::htmlspecialchars'); |
||
| 476 | |||
| 477 | $rows = isset($this->_req->post->rows) ? (int) $this->_req->post->rows : 4; |
||
| 478 | $cols = isset($this->_req->post->cols) ? (int) $this->_req->post->cols : 30; |
||
| 479 | |||
| 480 | // Checkboxes... |
||
| 481 | $show_reg = $this->_req->getPost('reg', 'intval', 0); |
||
| 482 | $show_display = isset($this->_req->post->display) ? 1 : 0; |
||
| 483 | $show_memberlist = isset($this->_req->post->memberlist) ? 1 : 0; |
||
| 484 | $bbc = isset($this->_req->post->bbc) ? 1 : 0; |
||
| 485 | $show_profile = $this->_req->post->profile_area; |
||
| 486 | $active = isset($this->_req->post->active) ? 1 : 0; |
||
| 487 | $private = $this->_req->getPost('private', 'intval', 0); |
||
| 488 | $can_search = isset($this->_req->post->can_search) ? 1 : 0; |
||
| 489 | |||
| 490 | // Some masking stuff... |
||
| 491 | $mask = $this->_req->getPost('mask', 'strval', ''); |
||
| 492 | if ($mask === 'regex' && isset($this->_req->post->regex)) |
||
| 493 | { |
||
| 494 | $mask .= $this->_req->post->regex; |
||
| 495 | } |
||
| 496 | |||
| 497 | $field_length = $this->_req->getPost('max_length', 'intval', 255); |
||
| 498 | $enclose = $this->_req->getPost('enclose', 'strval', ''); |
||
| 499 | $placement = $this->_req->getPost('placement', 'intval', 0); |
||
| 500 | |||
| 501 | // Select options? |
||
| 502 | $field_options = ''; |
||
| 503 | $newOptions = []; |
||
| 504 | |||
| 505 | // Set default |
||
| 506 | $default = ''; |
||
| 507 | |||
| 508 | switch ($this->_req->post->field_type) |
||
| 509 | { |
||
| 510 | case 'check': |
||
| 511 | $default = isset($this->_req->post->default_check) ? 1 : ''; |
||
| 512 | break; |
||
| 513 | case 'select': |
||
| 514 | case 'radio': |
||
| 515 | if (!empty($this->_req->post->select_option)) |
||
| 516 | { |
||
| 517 | foreach ($this->_req->post->select_option as $k => $v) |
||
| 518 | { |
||
| 519 | // Clean, clean, clean... |
||
| 520 | $v = Util::htmlspecialchars($v); |
||
| 521 | $v = strtr($v, [',' => '']); |
||
| 522 | |||
| 523 | // Nada, zip, etc... |
||
| 524 | if (trim($v) === '') |
||
| 525 | { |
||
| 526 | continue; |
||
| 527 | } |
||
| 528 | |||
| 529 | // Otherwise, save it boy. |
||
| 530 | $field_options .= $v . ','; |
||
| 531 | |||
| 532 | // This is just for working out what happened with old options... |
||
| 533 | $newOptions[$k] = $v; |
||
| 534 | |||
| 535 | // Is it default? |
||
| 536 | if (!isset($this->_req->post->default_select)) |
||
| 537 | { |
||
| 538 | continue; |
||
| 539 | } |
||
| 540 | |||
| 541 | if ($this->_req->post->default_select != $k) |
||
| 542 | { |
||
| 543 | continue; |
||
| 544 | } |
||
| 545 | |||
| 546 | $default = $v; |
||
| 547 | } |
||
| 548 | |||
| 549 | if (isset($_POST['default_select']) && $_POST['default_select'] === 'no_default') |
||
| 550 | { |
||
| 551 | $default = 'no_default'; |
||
| 552 | } |
||
| 553 | |||
| 554 | $field_options = substr($field_options, 0, -1); |
||
| 555 | } |
||
| 556 | |||
| 557 | break; |
||
| 558 | default: |
||
| 559 | $default = $this->_req->post->default_value ?? ''; |
||
| 560 | } |
||
| 561 | |||
| 562 | // Come up with the unique name? |
||
| 563 | if (empty($context['fid'])) |
||
| 564 | { |
||
| 565 | $colname = Util::substr(strtr($this->_req->post->field_name, [' ' => '']), 0, 6); |
||
| 566 | preg_match('~([\w_-]+)~', $colname, $matches); |
||
| 567 | |||
| 568 | // If there is nothing to the name, then let's start our own - for foreign languages etc. |
||
| 569 | if (isset($matches[1])) |
||
| 570 | { |
||
| 571 | $colname = 'cust_' . strtolower($matches[1]); |
||
| 572 | $initial_colname = 'cust_' . strtolower($matches[1]); |
||
| 573 | } |
||
| 574 | else |
||
| 575 | { |
||
| 576 | $colname = 'cust_' . mt_rand(1, 999999); |
||
| 577 | $initial_colname = 'cust_' . mt_rand(1, 999999); |
||
| 578 | } |
||
| 579 | |||
| 580 | $unique = ensureUniqueProfileField($colname, $initial_colname); |
||
| 581 | |||
| 582 | // Still not a unique column name? Leave it up to the user, then. |
||
| 583 | if (!$unique) |
||
| 584 | { |
||
| 585 | throw new Exception('custom_option_not_unique'); |
||
| 586 | } |
||
| 587 | |||
| 588 | // And create a new field |
||
| 589 | $new_field = [ |
||
| 590 | 'col_name' => $colname, |
||
| 591 | 'field_name' => $this->_req->post->field_name, |
||
| 592 | 'field_desc' => $this->_req->post->field_desc, |
||
| 593 | 'field_type' => $this->_req->post->field_type, |
||
| 594 | 'field_length' => $field_length, |
||
| 595 | 'field_options' => $field_options, |
||
| 596 | 'show_reg' => $show_reg, |
||
| 597 | 'show_display' => $show_display, |
||
| 598 | 'show_memberlist' => $show_memberlist, |
||
| 599 | 'show_profile' => $show_profile, |
||
| 600 | 'private' => $private, |
||
| 601 | 'active' => $active, |
||
| 602 | 'default_value' => $default, |
||
| 603 | 'rows' => $rows, |
||
| 604 | 'cols' => $cols, |
||
| 605 | 'can_search' => $can_search, |
||
| 606 | 'bbc' => $bbc, |
||
| 607 | 'mask' => $mask, |
||
| 608 | 'enclose' => $enclose, |
||
| 609 | 'placement' => $placement, |
||
| 610 | 'vieworder' => list_getProfileFieldSize() + 1, |
||
| 611 | ]; |
||
| 612 | addProfileField($new_field); |
||
| 613 | } |
||
| 614 | // Work out what to do with the user data otherwise... |
||
| 615 | else |
||
| 616 | { |
||
| 617 | // Anything going to check or select is pointless keeping - as is anything coming from check! |
||
| 618 | if (($this->_req->post->field_type === 'check' && $context['field']['type'] !== 'check') |
||
| 619 | || (($this->_req->post->field_type === 'select' || $this->_req->post->field_type === 'radio') && $context['field']['type'] !== 'select' && $context['field']['type'] !== 'radio') |
||
| 620 | || ($context['field']['type'] === 'check' && $this->_req->post->field_type !== 'check')) |
||
| 621 | { |
||
| 622 | deleteProfileFieldUserData($context['field']['colname']); |
||
| 623 | } |
||
| 624 | // Otherwise - if the select is edited may need to adjust! |
||
| 625 | elseif ($this->_req->post->field_type === 'select' || $this->_req->post->field_type === 'radio') |
||
| 626 | { |
||
| 627 | $optionChanges = $context['field']['options']; |
||
| 628 | $takenKeys = []; |
||
| 629 | |||
| 630 | // Work out what's changed! |
||
| 631 | foreach ($optionChanges as $k => $option) |
||
| 632 | { |
||
| 633 | if (trim($option) === '') |
||
| 634 | { |
||
| 635 | continue; |
||
| 636 | } |
||
| 637 | |||
| 638 | // Still exists? |
||
| 639 | if (in_array($option, $newOptions)) |
||
| 640 | { |
||
| 641 | $takenKeys[] = $k; |
||
| 642 | } |
||
| 643 | } |
||
| 644 | |||
| 645 | // Finally - have we renamed it - or is it really gone? |
||
| 646 | foreach ($optionChanges as $k => $option) |
||
| 647 | { |
||
| 648 | // Just been renamed? |
||
| 649 | if (in_array($k, $takenKeys)) |
||
| 650 | { |
||
| 651 | continue; |
||
| 652 | } |
||
| 653 | |||
| 654 | if (empty($newOptions[$k])) |
||
| 655 | { |
||
| 656 | continue; |
||
| 657 | } |
||
| 658 | |||
| 659 | updateRenamedProfileField($k, $newOptions, $context['field']['colname'], $option); |
||
| 660 | } |
||
| 661 | } |
||
| 662 | |||
| 663 | // @todo Maybe we should adjust based on new text length limits? |
||
| 664 | |||
| 665 | // And finally update an existing field |
||
| 666 | $field_data = [ |
||
| 667 | 'field_length' => $field_length, |
||
| 668 | 'show_reg' => $show_reg, |
||
| 669 | 'show_display' => $show_display, |
||
| 670 | 'show_memberlist' => $show_memberlist, |
||
| 671 | 'private' => $private, |
||
| 672 | 'active' => $active, |
||
| 673 | 'can_search' => $can_search, |
||
| 674 | 'bbc' => $bbc, |
||
| 675 | 'current_field' => $context['fid'], |
||
| 676 | 'field_name' => $this->_req->post->field_name, |
||
| 677 | 'field_desc' => $this->_req->post->field_desc, |
||
| 678 | 'field_type' => $this->_req->post->field_type, |
||
| 679 | 'field_options' => $field_options, |
||
| 680 | 'show_profile' => $show_profile, |
||
| 681 | 'default_value' => $default, |
||
| 682 | 'mask' => $mask, |
||
| 683 | 'enclose' => $enclose, |
||
| 684 | 'placement' => $placement, |
||
| 685 | 'rows' => $rows, |
||
| 686 | 'cols' => $cols, |
||
| 687 | ]; |
||
| 688 | |||
| 689 | updateProfileField($field_data); |
||
| 690 | |||
| 691 | // Just clean up any old selects - these are a pain! |
||
| 692 | if (($this->_req->post->field_type === 'select' || $this->_req->post->field_type === 'radio') && !empty($newOptions)) |
||
| 693 | { |
||
| 694 | deleteOldProfileFieldSelects($newOptions, $context['field']['colname']); |
||
| 695 | } |
||
| 696 | } |
||
| 697 | } |
||
| 698 | // Deleting? |
||
| 699 | elseif (isset($this->_req->post->delete) && $context['field']['colname']) |
||
| 700 | { |
||
| 701 | checkSession(); |
||
| 702 | validateToken('admin-ecp'); |
||
| 703 | |||
| 704 | // Delete the old data first, then the field. |
||
| 705 | deleteProfileFieldUserData($context['field']['colname']); |
||
| 706 | deleteProfileField($context['fid']); |
||
| 707 | } |
||
| 708 | |||
| 709 | // Rebuild display cache etc. |
||
| 710 | if (isset($this->_req->post->delete) || isset($this->_req->post->save) || isset($this->_req->post->onoff)) |
||
| 711 | { |
||
| 712 | checkSession(); |
||
| 713 | |||
| 714 | // Update the display cache |
||
| 715 | updateDisplayCache(); |
||
| 716 | redirectexit('action=admin;area=featuresettings;sa=profile'); |
||
| 717 | } |
||
| 718 | |||
| 719 | createToken('admin-ecp'); |
||
| 720 | } |
||
| 722 |