Conditions | 24 |
Paths | 2112 |
Total Lines | 246 |
Code Lines | 174 |
Lines | 39 |
Ratio | 15.85 % |
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 |
||
463 | function editField() |
||
464 | { |
||
465 | $eventsrv =& xhelpNewEventService(); |
||
466 | $session =& Session::singleton(); |
||
467 | $regex_array =& _getRegexArray(); |
||
468 | |||
469 | View Code Duplication | if (!isset( $_REQUEST['id'])) { |
|
470 | redirect_header(xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=> 'manageDepartments'), false), 3, _AM_XHELP_MESSAGE_NO_FIELD); |
||
471 | } |
||
472 | |||
473 | $fld_id = intval($_REQUEST['id']); |
||
474 | $hField =& xhelpGetHandler('ticketField'); |
||
475 | View Code Duplication | if (! $field =& $hField->get($fld_id)) { |
|
476 | redirect_header(xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=> 'manageDepartments'), false), 3, _AM_XHELP_MESSAGE_NO_FIELD); |
||
477 | } |
||
478 | |||
479 | if (!isset($_POST ['editField'])) { |
||
480 | //Get Custom Field From session (if exists) |
||
481 | $field_info = $session->get('xhelp_editField_'.$fld_id); |
||
482 | $field_errors = $session->get('xhelp_editFieldErrors_'.$fld_id); |
||
483 | |||
484 | if ( ! $field_info === false) { |
||
485 | $fld_controltype = $field_info['controltype']; |
||
486 | $fld_datatype = $field_info['datatype']; |
||
487 | $fld_departments = $field_info['departments']; |
||
488 | $fld_name = $field_info['name']; |
||
489 | $fld_fieldname = $field_info['fieldname']; |
||
490 | $fld_description = $field_info['description']; |
||
491 | $fld_required = $field_info['required']; |
||
492 | $fld_length = $field_info['length']; |
||
493 | $fld_weight = $field_info['weight']; |
||
494 | $fld_defaultvalue = $field_info['defaultvalue']; |
||
495 | $fld_values = $field_info['values']; |
||
496 | $fld_validation = $field_info['validation']; |
||
497 | } else { |
||
498 | $hFDept =& xhelpGetHandler('ticketFieldDepartment'); |
||
499 | $depts =& $hFDept->departmentsByField($field->getVar('id'), true); |
||
500 | |||
501 | $fld_controltype = $field->getVar('controltype'); |
||
502 | $fld_datatype = $field->getVar('datatype'); |
||
503 | $fld_departments = array_keys($depts); |
||
504 | $fld_name = $field->getVar('name'); |
||
505 | $fld_fieldname = $field->getVar('fieldname'); |
||
506 | $fld_description = $field->getVar('description'); |
||
507 | $fld_required = $field->getVar('required'); |
||
508 | $fld_length = $field->getVar('fieldlength'); |
||
509 | $fld_weight = $field->getVar('weight'); |
||
510 | $fld_defaultvalue = $field->getVar('defaultvalue'); |
||
511 | $fld_values = _formatValues($field->getVar('fieldvalues')); |
||
512 | $fld_validation = $field->getVar('validation'); |
||
513 | } |
||
514 | |||
515 | //Display Field modification |
||
516 | xoops_cp_header(); |
||
517 | //echo $oAdminButton->renderButtons('manfields'); |
||
518 | $indexAdmin = new ModuleAdmin(); |
||
519 | echo $indexAdmin->addNavigation('fields.php'); |
||
520 | |||
521 | //Edit Field Form |
||
522 | |||
523 | $controls = xhelpGetControlArray(); |
||
524 | $control_select = new XoopsFormSelect(_AM_XHELP_TEXT_CONTROLTYPE, 'fld_controltype', $fld_controltype); |
||
525 | $control_select->setDescription(_AM_XHELP_TEXT_CONTROLTYPE_DESC); |
||
526 | foreach($controls as $key=>$control) { |
||
527 | $control_select->addOption($key, $control['label']); |
||
528 | } |
||
529 | |||
530 | $datatypes = array( |
||
531 | _XHELP_DATATYPE_TEXT => _XHELP_DATATYPE_TEXT, |
||
532 | _XHELP_DATATYPE_NUMBER_INT => _XHELP_DATATYPE_NUMBER_INT, |
||
533 | _XHELP_DATATYPE_NUMBER_DEC => _XHELP_DATATYPE_NUMBER_DEC); |
||
534 | |||
535 | $datatype_select = new XoopsFormSelect(_AM_XHELP_TEXT_DATATYPE, 'fld_datatype', $fld_datatype); |
||
536 | $datatype_select->setDescription(_AM_XHELP_TEXT_DATATYPE_DESC); |
||
537 | $datatype_select->addOptionArray($datatypes); |
||
538 | |||
539 | $hDepts =& xhelpGetHandler('department'); |
||
540 | $depts =& $hDepts->getObjects(); |
||
541 | $dept_select = new XoopsFormSelect(_AM_XHELP_TEXT_DEPARTMENTS, 'fld_departments', $fld_departments, 5, true); |
||
542 | $dept_select->setDescription(_AM_XHELP_TEXT_DEPT_DESC); |
||
543 | foreach($depts as $obj) { |
||
544 | $dept_select->addOption($obj->getVar('id'), $obj->getVar('department')); |
||
545 | } |
||
546 | unset($depts); |
||
547 | |||
548 | View Code Duplication | if (! $field_errors === false) { |
|
549 | xhelpRenderErrors($field_errors, xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=>'clearEditSession', 'id'=>$fld_id))); |
||
550 | } |
||
551 | |||
552 | $form = new xhelpForm(_AM_XHELP_EDIT_FIELD, 'edit_field', xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=>'editfield', 'id'=>$fld_id))); |
||
553 | |||
554 | $nameEle = new XoopsFormText(_AM_XHELP_TEXT_NAME, 'fld_name', 30, 64, $fld_name); |
||
555 | $nameEle->setDescription(_AM_XHELP_TEXT_NAME_DESC); |
||
556 | $form->addElement($nameEle); |
||
557 | |||
558 | $fieldnameEle = new XoopsFormText(_AM_XHELP_TEXT_FIELDNAME, 'fld_fieldname', 30, 64, $fld_fieldname); |
||
559 | $fieldnameEle->setDescription(_AM_XHELP_TEXT_FIELDNAME_DESC); |
||
560 | $form->addElement($fieldnameEle); |
||
561 | |||
562 | $descriptionEle = new XoopsFormTextArea(_AM_XHELP_TEXT_DESCRIPTION, 'fld_description', $fld_description, 5, 60); |
||
563 | $descriptionEle->setDescription(_AM_XHELP_TEXT_DESCRIPTION_DESC); |
||
564 | $form->addElement($descriptionEle); |
||
565 | |||
566 | $form->addElement($dept_select); |
||
567 | $form->addElement($control_select); |
||
568 | $form->addElement($datatype_select); |
||
569 | |||
570 | $required = new XoopsFormRadioYN(_AM_XHELP_TEXT_REQUIRED, 'fld_required', $fld_required); |
||
571 | $required->setDescription(_AM_XHELP_TEXT_REQUIRED_DESC); |
||
572 | $form->addElement($required); |
||
573 | |||
574 | $lengthEle = new XoopsFormText(_AM_XHELP_TEXT_LENGTH, 'fld_length', 5, 5, $fld_length); |
||
575 | $lengthEle->setDescription(_AM_XHELP_TEXT_LENGTH_DESC); |
||
576 | $form->addElement($lengthEle); |
||
577 | |||
578 | $widthEle = new XoopsFormText(_AM_XHELP_TEXT_WEIGHT, 'fld_weight', 5, 5, $fld_weight); |
||
579 | $widthEle->setDescription(_AM_XHELP_TEXT_WEIGHT_DESC); |
||
580 | $form->addElement($widthEle); |
||
581 | |||
582 | $regex_control = new xhelpFormRegex(_AM_XHELP_TEXT_VALIDATION, 'fld_valid', $fld_validation); |
||
583 | $regex_control->setDescription(_AM_XHELP_TEXT_VALIDATION_DESC); |
||
584 | $regex_control->addOptionArray($regex_array); |
||
585 | |||
586 | $form->addElement($regex_control); |
||
587 | |||
588 | $defaultValueEle = new XoopsFormText(_AM_XHELP_TEXT_DEFAULTVALUE, 'fld_defaultvalue', 30, 100, $fld_defaultvalue); |
||
589 | $defaultValueEle->setDescription(_AM_XHELP_TEXT_DEFAULTVALUE_DESC); |
||
590 | $form->addElement($defaultValueEle); |
||
591 | $values = new XoopsFormTextArea(_AM_XHELP_TEXT_FIELDVALUES, 'fld_values', $fld_values, 5, 60); |
||
592 | $values->setDescription(_AM_XHELP_TEXT_FIELDVALUES_DESC); |
||
593 | $form->addElement($values); |
||
594 | |||
595 | $btn_tray = new XoopsFormElementTray(''); |
||
596 | $btn_tray->addElement(new XoopsFormButton('', 'editField', _AM_XHELP_BUTTON_SUBMIT, 'submit')); |
||
597 | $btn_tray->addElement(new XoopsFormButton('','cancel', _AM_XHELP_BUTTON_CANCEL)); |
||
598 | $btn_tray->addElement(new XoopsFormHidden('id', $fld_id)); |
||
599 | |||
600 | $form->addElement($btn_tray); |
||
601 | echo $form->render(); |
||
602 | |||
603 | include_once "admin_footer.php"; |
||
604 | } else { |
||
605 | //Validate Field Information |
||
606 | $has_errors = false; |
||
607 | $errors = array(); |
||
608 | $values =& _parseValues($_POST['fld_values']); |
||
609 | |||
610 | View Code Duplication | if (!$control = xhelpGetControl($_POST['fld_controltype'])) { |
|
611 | $has_errors = true; |
||
612 | $errors['fld_controltype'][] = _AM_XHELP_VALID_ERR_CONTROLTYPE; |
||
613 | } |
||
614 | |||
615 | $fld_needslength = $control['needs_length']; |
||
616 | $fld_needsvalues = $control['needs_values']; |
||
617 | |||
618 | //name field filled? |
||
619 | View Code Duplication | if (trim($_POST['fld_name']) == '') { |
|
620 | $has_errors = true; |
||
621 | $errors['fld_name'][] = _AM_XHELP_VALID_ERR_NAME; |
||
622 | } |
||
623 | |||
624 | //fieldname filled |
||
625 | View Code Duplication | if (trim($_POST['fld_fieldname']) == '') { |
|
626 | $has_errors = true; |
||
627 | $errors['fld_fieldname'][] = _AM_XHELP_VALID_ERR_FIELDNAME; |
||
628 | } |
||
629 | |||
630 | //fieldname unique? |
||
631 | $crit = new CriteriaCompo(new Criteria('id', $fld_id, '!=')); |
||
632 | $crit->add(new Criteria('fieldname', $_POST['fld_fieldname'])); |
||
633 | if ($hField->getCount($crit)) { |
||
634 | $has_errors = true; |
||
635 | $errors['fld_fieldname'][] = _AM_XHELP_VALID_ERR_FIELDNAME_UNIQUE; |
||
636 | } |
||
637 | |||
638 | //Length filled |
||
639 | if (intval($_POST['fld_length']) == 0 && $fld_needslength == true) { |
||
640 | $has_errors = true; |
||
641 | $errors['fld_length'][] = sprintf(_AM_XHELP_VALID_ERR_LENGTH, _XHELP_FIELD_MINLEN, _XHELP_FIELD_MAXLEN); |
||
642 | } |
||
643 | |||
644 | //default value in value set? |
||
645 | View Code Duplication | if (count($values)) { |
|
646 | if (!in_array($_POST['fld_defaultvalue'], $values, true) && !array_key_exists($_POST['fld_defaultvalue'], $values) ) { |
||
647 | $has_errors = true; |
||
648 | $errors['fld_defaultvalue'][] = _AM_XHELP_VALID_ERR_DEFAULTVALUE; |
||
649 | } |
||
650 | |||
651 | //length larger than longest value? |
||
652 | $length = intval($_POST['fld_length']); |
||
653 | foreach($values as $key=>$value) { |
||
654 | if (strlen($key) > $length) { |
||
655 | $has_errors = true; |
||
656 | $errors['fld_values'][] = sprintf(_AM_XHELP_VALID_ERR_VALUE_LENGTH, htmlentities($key), $length); |
||
657 | } |
||
658 | } |
||
659 | } elseif ($fld_needsvalues) { |
||
660 | $has_errors = true; |
||
661 | $errors['fld_values'][] = _AM_XHELP_VALID_ERR_VALUE; |
||
662 | } |
||
663 | |||
664 | if ($has_errors) { |
||
665 | $afield = array(); |
||
666 | $afield['name'] = $_POST['fld_name']; |
||
667 | $afield['description'] = $_POST['fld_description']; |
||
668 | $afield['fieldname'] = $_POST['fld_fieldname']; |
||
669 | $afield['departments'] = $_POST['fld_departments']; |
||
670 | $afield['controltype'] = $_POST['fld_controltype']; |
||
671 | $afield['datatype'] = $_POST['fld_datatype']; |
||
672 | $afield['required'] = $_POST['fld_required']; |
||
673 | $afield['weight'] = $_POST['fld_weight']; |
||
674 | $afield['defaultvalue'] = $_POST['fld_defaultvalue']; |
||
675 | $afield['values'] = $_POST['fld_values']; |
||
676 | $afield['length'] = $_POST['fld_length']; |
||
677 | $afield['validation'] = ($_POST['fld_valid_select'] == $_POST['fld_valid_txtbox'] ? $_POST['fld_valid_select'] : $_POST['fld_valid_txtbox']); |
||
678 | $session->set('xhelp_editField_'.$fld_id, $afield); |
||
679 | $session->set('xhelp_editFieldErrors_'.$fld_id, $errors); |
||
680 | //Redirect to edit page (display errors); |
||
681 | header('Location: '. xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=>'editfield', 'id'=>$fld_id), false)); |
||
682 | exit(); |
||
683 | |||
684 | } |
||
685 | //Store Modified Field info |
||
686 | |||
687 | $field->setVar('name', $_POST['fld_name']); |
||
688 | $field->setVar('description', $_POST['fld_description']); |
||
689 | $field->setVar('fieldname', $_POST['fld_fieldname']); |
||
690 | $field->setVar('controltype', $_POST['fld_controltype']); |
||
691 | $field->setVar('datatype', $_POST['fld_datatype']); |
||
692 | $field->setVar('fieldlength', $_POST['fld_length']); |
||
693 | $field->setVar('required', $_POST['fld_required']); |
||
694 | $field->setVar('weight', $_POST['fld_weight']); |
||
695 | $field->setVar('defaultvalue', $_POST['fld_defaultvalue']); |
||
696 | $field->setVar('validation', ($_POST['fld_valid_select'] == $_POST['fld_valid_txtbox'] ? $_POST['fld_valid_select'] : $_POST['fld_valid_txtbox'])); |
||
697 | $field->setValues($values); |
||
698 | $field->addDepartments($_POST['fld_departments']); |
||
699 | |||
700 | if ($hField->insert($field)) { |
||
701 | _clearEditSessionVars($fld_id); |
||
702 | redirect_header(xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php'), 3, _AM_XHELP_MSG_FIELD_UPD_OK); |
||
703 | } else { |
||
704 | redirect_header(xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=>'editfield', 'id'=>$fld_id), false), 3, _AM_XHELP_MSG_FIELD_UPD_ERR); |
||
705 | } |
||
706 | } |
||
707 | |||
708 | } |
||
709 | |||
772 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.