| Conditions | 31 |
| Paths | 4209 |
| Total Lines | 355 |
| Code Lines | 247 |
| 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 |
||
| 547 | public function toHtml() |
||
| 548 | { |
||
| 549 | if ($this->_flagFrozen) { |
||
| 550 | return $this->getFrozenHtml(); |
||
| 551 | } |
||
| 552 | |||
| 553 | $tabs = $this->_getTabs(); |
||
| 554 | $tab = $this->_getTab(); |
||
| 555 | |||
| 556 | $selectId = $this->getName(); |
||
| 557 | $selectName = $this->getName().'[]'; |
||
| 558 | $selectNameFrom = $this->getName().'-f[]'; |
||
| 559 | $selectNameTo = $this->getName().'[]'; |
||
| 560 | $selected_count = 0; |
||
| 561 | $rightAll = ''; |
||
| 562 | $leftAll = ''; |
||
| 563 | |||
| 564 | // placeholder {unselected} existence determines if we will render |
||
| 565 | if (strpos($this->_elementTemplate, '{unselected}') === false) { |
||
| 566 | // ... a single multi-select with checkboxes |
||
| 567 | $id = $this->getAttribute('name'); |
||
| 568 | |||
| 569 | $strHtmlSelected = $tab . '<div id="qfams_'.$id.'">' . PHP_EOL; |
||
| 570 | $unselected_count = count($this->_options); |
||
| 571 | $checkbox_id_suffix = 0; |
||
| 572 | |||
| 573 | foreach ($this->_options as $option) { |
||
| 574 | $_labelAttributes |
||
| 575 | = array('style', 'class', 'onmouseover', 'onmouseout'); |
||
| 576 | $labelAttributes = array(); |
||
| 577 | foreach ($_labelAttributes as $attr) { |
||
| 578 | if (isset($option['attr'][$attr])) { |
||
| 579 | $labelAttributes[$attr] = $option['attr'][$attr]; |
||
| 580 | unset($option['attr'][$attr]); |
||
| 581 | } |
||
| 582 | } |
||
| 583 | |||
| 584 | if (is_array($this->_values) && in_array((string)$option['attr']['value'], $this->_values)) { |
||
| 585 | // The items is *selected* |
||
| 586 | $checked = ' checked="checked"'; |
||
| 587 | $selected_count++; |
||
| 588 | } else { |
||
| 589 | // The item is *unselected* so we want to put it |
||
| 590 | $checked = ''; |
||
| 591 | } |
||
| 592 | $checkbox_id_suffix++; |
||
| 593 | $strHtmlSelected .= $tab |
||
| 594 | .'<label' |
||
| 595 | .$this->_getAttrString($labelAttributes).'>' |
||
| 596 | .'<input type="checkbox"' |
||
| 597 | .' id="'.$selectId.$checkbox_id_suffix.'"' |
||
| 598 | .' name="'.$selectName.'"' |
||
| 599 | .$checked.$this->_getAttrString($option['attr']) |
||
| 600 | .' />'.$option['text'].'</label>' |
||
| 601 | .PHP_EOL; |
||
| 602 | } |
||
| 603 | $strHtmlSelected .= $tab . '</div>'. PHP_EOL; |
||
| 604 | |||
| 605 | $strHtmlHidden = ''; |
||
| 606 | $strHtmlUnselected = ''; |
||
| 607 | $strHtmlAdd = ''; |
||
| 608 | $strHtmlRemove = ''; |
||
| 609 | |||
| 610 | // build the select all button with all its attributes |
||
| 611 | $attributes = []; |
||
| 612 | $this->_allButtonAttributes = array_merge($this->_allButtonAttributes, $attributes); |
||
| 613 | $attrStrAll = $this->_getAttrString($this->_allButtonAttributes); |
||
| 614 | $strHtmlAll = "<input$attrStrAll />". PHP_EOL; |
||
| 615 | |||
| 616 | // build the select none button with all its attributes |
||
| 617 | $attributes = []; |
||
| 618 | $this->_noneButtonAttributes |
||
| 619 | = array_merge($this->_noneButtonAttributes, $attributes); |
||
| 620 | $attrStrNone = $this->_getAttrString($this->_noneButtonAttributes); |
||
| 621 | $strHtmlNone = "<input$attrStrNone />". PHP_EOL; |
||
| 622 | |||
| 623 | // build the toggle selection button with all its attributes |
||
| 624 | $attributes = []; |
||
| 625 | $this->_toggleButtonAttributes = array_merge($this->_toggleButtonAttributes, $attributes); |
||
| 626 | $attrStrToggle = $this->_getAttrString($this->_toggleButtonAttributes); |
||
| 627 | $strHtmlToggle = "<input$attrStrToggle />". PHP_EOL; |
||
| 628 | |||
| 629 | $strHtmlMoveUp = ''; |
||
| 630 | $strHtmlMoveDown = ''; |
||
| 631 | $strHtmlMoveTop = ''; |
||
| 632 | $strHtmlMoveBottom = ''; |
||
| 633 | |||
| 634 | // default selection counters |
||
| 635 | $strHtmlSelectedCount = $selected_count . '/' . $unselected_count; |
||
| 636 | } else { |
||
| 637 | // set name of Select From Box |
||
| 638 | $this->_attributesUnselected |
||
| 639 | = array( |
||
| 640 | 'id' => $selectId.'', |
||
| 641 | 'name' => $selectNameFrom, |
||
| 642 | ); |
||
| 643 | $this->_attributesUnselected = array_merge($this->_attributes, $this->_attributesUnselected); |
||
| 644 | $attrUnselected = $this->_getAttrString($this->_attributesUnselected); |
||
| 645 | |||
| 646 | // set name of Select To Box |
||
| 647 | $this->_attributesSelected |
||
| 648 | = array( |
||
| 649 | 'id' => $selectId.'_to', |
||
| 650 | 'name' => $selectNameTo, |
||
| 651 | ); |
||
| 652 | $this->_attributesSelected = array_merge($this->_attributes, $this->_attributesSelected); |
||
| 653 | $attrSelected = $this->_getAttrString($this->_attributesSelected); |
||
| 654 | |||
| 655 | // set name of Select hidden Box |
||
| 656 | $this->_attributesHidden |
||
| 657 | = array( |
||
| 658 | 'name' => $selectName, |
||
| 659 | 'style' => 'overflow: hidden; visibility: hidden; width: 1px; height: 0;', |
||
| 660 | ); |
||
| 661 | $this->_attributesHidden |
||
| 662 | = array_merge($this->_attributes, $this->_attributesHidden); |
||
| 663 | $attrHidden = $this->_getAttrString($this->_attributesHidden); |
||
| 664 | |||
| 665 | // prepare option tables to be displayed as in POST order |
||
| 666 | $append = empty($this->_values) ? 0 : count($this->_values); |
||
| 667 | if ($append > 0) { |
||
| 668 | $arrHtmlSelected = array_fill(0, $append, ' '); |
||
| 669 | } else { |
||
| 670 | $arrHtmlSelected = array(); |
||
| 671 | } |
||
| 672 | |||
| 673 | $options = count($this->_options); |
||
| 674 | $arrHtmlUnselected = array(); |
||
| 675 | if ($options > 0) { |
||
| 676 | $arrHtmlHidden = array_fill(0, $options, ' '); |
||
| 677 | foreach ($this->_options as $option) { |
||
| 678 | if (is_array($this->_values) && in_array((string) $option['attr']['value'], $this->_values)) { |
||
| 679 | // Get the post order |
||
| 680 | $key = array_search( |
||
| 681 | $option['attr']['value'], |
||
| 682 | $this->_values |
||
| 683 | ); |
||
| 684 | |||
| 685 | /** The items is *selected* so we want to put it |
||
| 686 | in the 'selected' multi-select */ |
||
| 687 | $arrHtmlSelected[$key] = $option; |
||
| 688 | /** Add it to the 'hidden' multi-select |
||
| 689 | and set it as 'selected' */ |
||
| 690 | if (isset($option['attr']['disabled'])) { |
||
| 691 | unset($option['attr']['disabled']); |
||
| 692 | } |
||
| 693 | $option['attr']['selected'] = 'selected'; |
||
| 694 | $arrHtmlHidden[$key] = $option; |
||
| 695 | } else { |
||
| 696 | /** The item is *unselected* so we want to put it |
||
| 697 | in the 'unselected' multi-select */ |
||
| 698 | $arrHtmlUnselected[] = $option; |
||
| 699 | // Add it to the hidden multi-select as 'unselected' |
||
| 700 | $arrHtmlHidden[$append] = $option; |
||
| 701 | $append++; |
||
| 702 | } |
||
| 703 | } |
||
| 704 | } |
||
| 705 | |||
| 706 | // The 'unselected' multi-select which appears on the left |
||
| 707 | $unselected_count = count($arrHtmlUnselected); |
||
| 708 | if ($unselected_count == 0) { |
||
| 709 | $this->_attributesUnselected = array_merge($this->_attributes, $this->_attributesUnselected); |
||
| 710 | $attrUnselected = $this->_getAttrString($this->_attributesUnselected); |
||
| 711 | } |
||
| 712 | $strHtmlUnselected = "<select$attrUnselected>". PHP_EOL; |
||
| 713 | if ($unselected_count > 0) { |
||
| 714 | foreach ($arrHtmlUnselected as $data) { |
||
| 715 | $strHtmlUnselected |
||
| 716 | .= $tabs.$tab |
||
| 717 | .'<option'.$this->_getAttrString($data['attr']).'>' |
||
| 718 | .$data['text'].'</option>'.PHP_EOL; |
||
| 719 | } |
||
| 720 | } |
||
| 721 | $strHtmlUnselected .= '</select>'; |
||
| 722 | |||
| 723 | // The 'selected' multi-select which appears on the right |
||
| 724 | $selected_count = count($arrHtmlSelected); |
||
| 725 | if ($selected_count == 0) { |
||
| 726 | $this->_attributesSelected = array_merge($this->_attributes, $this->_attributesSelected); |
||
| 727 | $attrSelected = $this->_getAttrString($this->_attributesSelected); |
||
| 728 | } |
||
| 729 | $strHtmlSelected = "<select$attrSelected>"; |
||
| 730 | if ($selected_count > 0) { |
||
| 731 | foreach ($arrHtmlSelected as $data) { |
||
| 732 | if (!is_array($data)) { |
||
| 733 | continue; |
||
| 734 | } |
||
| 735 | $attribute = null; |
||
| 736 | if (isset($data['attr'])) { |
||
| 737 | $attribute = $this->_getAttrString($data['attr']); |
||
| 738 | } |
||
| 739 | |||
| 740 | $text = null; |
||
| 741 | if (isset($data['text'])) { |
||
| 742 | $text = $data['text']; |
||
| 743 | } |
||
| 744 | $strHtmlSelected |
||
| 745 | .= $tabs.$tab |
||
| 746 | .'<option'.$attribute.'>' |
||
| 747 | .$text.'</option>'; |
||
| 748 | } |
||
| 749 | } |
||
| 750 | $strHtmlSelected .= '</select>'; |
||
| 751 | $strHtmlHidden = ''; |
||
| 752 | $attributes = array('id' => $selectId.'_leftSelected'); |
||
| 753 | $this->_removeButtonAttributes |
||
| 754 | = array_merge($this->_removeButtonAttributes, $attributes); |
||
| 755 | $attrStrRemove = $this->_getAttrString($this->_removeButtonAttributes); |
||
| 756 | $strHtmlRemove = "<button $attrStrRemove /> <em class='fa fa-arrow-left'></em></button>"; |
||
| 757 | |||
| 758 | // build the add button with all its attributes |
||
| 759 | $attributes = array('id' => $selectId.'_rightSelected'); |
||
| 760 | $this->_addButtonAttributes = array_merge($this->_addButtonAttributes, $attributes); |
||
| 761 | $attrStrAdd = $this->_getAttrString($this->_addButtonAttributes); |
||
| 762 | $strHtmlAdd = "<button $attrStrAdd /> <em class='fa fa-arrow-right'></em></button><br /><br />"; |
||
| 763 | |||
| 764 | if ($this->selectAllCheckBox) { |
||
| 765 | $attributes = array('id' => $selectId.'_rightAll'); |
||
| 766 | $this->_addButtonAttributes = array_merge($this->_addButtonAttributes, $attributes); |
||
| 767 | $attrStrAdd = $this->_getAttrString($this->_addButtonAttributes); |
||
| 768 | $rightAll = "<button $attrStrAdd /> <em class='fa fa-forward'></em></button><br /><br />"; |
||
| 769 | |||
| 770 | $attributes = array('id' => $selectId.'_leftAll'); |
||
| 771 | $this->_addButtonAttributes = array_merge($this->_addButtonAttributes, $attributes); |
||
| 772 | $attrStrAdd = $this->_getAttrString($this->_addButtonAttributes); |
||
| 773 | $leftAll = "<br /><br /><button $attrStrAdd /> <em class='fa fa-backward'></em></button>"; |
||
| 774 | } |
||
| 775 | |||
| 776 | // build the select all button with all its attributes |
||
| 777 | $strHtmlAll = ''; |
||
| 778 | |||
| 779 | // build the select none button with all its attributes |
||
| 780 | $attributes = []; |
||
| 781 | $this->_noneButtonAttributes |
||
| 782 | = array_merge($this->_noneButtonAttributes, $attributes); |
||
| 783 | $attrStrNone = $this->_getAttrString($this->_noneButtonAttributes); |
||
| 784 | $strHtmlNone = "<input$attrStrNone />". PHP_EOL; |
||
| 785 | |||
| 786 | // build the toggle button with all its attributes |
||
| 787 | $attributes = []; |
||
| 788 | $this->_toggleButtonAttributes |
||
| 789 | = array_merge($this->_toggleButtonAttributes, $attributes); |
||
| 790 | $attrStrToggle = $this->_getAttrString($this->_toggleButtonAttributes); |
||
| 791 | $strHtmlToggle = "<input$attrStrToggle />". PHP_EOL; |
||
| 792 | |||
| 793 | // build the move up button with all its attributes |
||
| 794 | $attributes = []; |
||
| 795 | $this->_upButtonAttributes |
||
| 796 | = array_merge($this->_upButtonAttributes, $attributes); |
||
| 797 | $attrStrUp = $this->_getAttrString($this->_upButtonAttributes); |
||
| 798 | $strHtmlMoveUp = "<input$attrStrUp />". PHP_EOL; |
||
| 799 | |||
| 800 | // build the move down button with all its attributes |
||
| 801 | $attributes = []; |
||
| 802 | $this->_downButtonAttributes |
||
| 803 | = array_merge($this->_downButtonAttributes, $attributes); |
||
| 804 | $attrStrDown = $this->_getAttrString($this->_downButtonAttributes); |
||
| 805 | $strHtmlMoveDown = "<input$attrStrDown />". PHP_EOL; |
||
| 806 | |||
| 807 | // build the move top button with all its attributes |
||
| 808 | $attributes = []; |
||
| 809 | $this->_topButtonAttributes |
||
| 810 | = array_merge($this->_topButtonAttributes, $attributes); |
||
| 811 | $attrStrTop = $this->_getAttrString($this->_topButtonAttributes); |
||
| 812 | $strHtmlMoveTop = "<input$attrStrTop />". PHP_EOL; |
||
| 813 | |||
| 814 | // build the move bottom button with all its attributes |
||
| 815 | $attributes = []; |
||
| 816 | $this->_bottomButtonAttributes |
||
| 817 | = array_merge($this->_bottomButtonAttributes, $attributes); |
||
| 818 | $attrStrBottom = $this->_getAttrString($this->_bottomButtonAttributes); |
||
| 819 | $strHtmlMoveBottom = "<input$attrStrBottom />". PHP_EOL; |
||
| 820 | |||
| 821 | // default selection counters |
||
| 822 | $strHtmlSelectedCount = $selected_count; |
||
| 823 | } |
||
| 824 | $strHtmlUnselectedCount = $unselected_count; |
||
| 825 | $strHtmlSelectedCountId = $selectId .'_selected'; |
||
| 826 | $strHtmlUnselectedCountId = $selectId .'_unselected'; |
||
| 827 | |||
| 828 | // render all part of the multi select component with the template |
||
| 829 | $strHtml = $this->_elementTemplate; |
||
| 830 | |||
| 831 | // Prepare multiple labels |
||
| 832 | $labels = $this->getLabel(); |
||
| 833 | if (is_array($labels)) { |
||
| 834 | array_shift($labels); |
||
| 835 | } |
||
| 836 | // render extra labels, if any |
||
| 837 | if (is_array($labels)) { |
||
| 838 | foreach ($labels as $key => $text) { |
||
| 839 | $key = is_int($key) ? $key + 2 : $key; |
||
| 840 | $strHtml = str_replace("{label_{$key}}", $text, $strHtml); |
||
| 841 | $strHtml = str_replace("<!-- BEGIN label_{$key} -->", '', $strHtml); |
||
| 842 | $strHtml = str_replace("<!-- END label_{$key} -->", '', $strHtml); |
||
| 843 | } |
||
| 844 | } |
||
| 845 | |||
| 846 | // clean up useless label tags |
||
| 847 | if (strpos($strHtml, '{label_')) { |
||
| 848 | $strHtml = preg_replace('/\s*<!-- BEGIN label_(\S+) -->'. |
||
| 849 | '.*<!-- END label_\1 -->\s*/i', '', $strHtml); |
||
| 850 | } |
||
| 851 | |||
| 852 | $placeHolders = array( |
||
| 853 | '{stylesheet}', |
||
| 854 | '{javascript}', |
||
| 855 | '{class}', |
||
| 856 | '{unselected_count_id}', |
||
| 857 | '{selected_count_id}', |
||
| 858 | '{unselected_count}', |
||
| 859 | '{selected_count}', |
||
| 860 | '{unselected}', |
||
| 861 | '{selected}', |
||
| 862 | '{add}', |
||
| 863 | '{remove}', |
||
| 864 | '{all}', |
||
| 865 | '{none}', |
||
| 866 | '{toggle}', |
||
| 867 | '{moveup}', |
||
| 868 | '{movedown}', |
||
| 869 | '{movetop}', |
||
| 870 | '{movebottom}', |
||
| 871 | ); |
||
| 872 | |||
| 873 | $htmlElements = array( |
||
| 874 | $this->getElementCss(false), |
||
| 875 | $this->getElementJs(false), |
||
| 876 | $this->_tableAttributes, |
||
| 877 | $strHtmlUnselectedCountId, |
||
| 878 | $strHtmlSelectedCountId, |
||
| 879 | $strHtmlUnselectedCount, |
||
| 880 | $strHtmlSelectedCount, |
||
| 881 | $strHtmlUnselected, |
||
| 882 | $strHtmlSelected.$strHtmlHidden, |
||
| 883 | $rightAll.$strHtmlAdd, |
||
| 884 | $strHtmlRemove.$leftAll, |
||
| 885 | $strHtmlAll, |
||
| 886 | $strHtmlNone, |
||
| 887 | $strHtmlToggle, |
||
| 888 | $strHtmlMoveUp, |
||
| 889 | $strHtmlMoveDown, |
||
| 890 | $strHtmlMoveTop, |
||
| 891 | $strHtmlMoveBottom, |
||
| 892 | ); |
||
| 893 | |||
| 894 | $strHtml = str_replace($placeHolders, $htmlElements, $strHtml); |
||
| 895 | $comment = $this->getComment(); |
||
| 896 | |||
| 897 | if (!empty($comment)) { |
||
| 898 | $strHtml = $tabs . '<!-- ' . $comment . " //-->" . PHP_EOL . $strHtml; |
||
| 899 | } |
||
| 900 | |||
| 901 | return $strHtml; |
||
| 902 | } |
||
| 1005 |