← All changes
|
includes/Frontend/Form/View/Theme/Fields/HtmlSelectField.php
+124
-65
2.0
→
V3.3.2
View file →
| @@ -1,32 +1,40 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Frontend\Form\View\Theme\Fields; |
| 4 | 4 | |
| 5 | -class HtmlSelectField { | |
| 6 | - public static function init($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error = null, $value = null) { | |
| 7 | - $inputWrapper = new InputWrapper($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error, $value); | |
| 5 | +use BitCode\BitForm\Core\Util\FrontendHelpers; | |
| 6 | + | |
| 7 | +class HtmlSelectField | |
| 8 | +{ | |
| 9 | + public static function init($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error = null, $value = null) | |
| 10 | + { | |
| 11 | + $inputWrapper = new ClassicInputWrapper($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error, $value); | |
| 8 | 12 | $input = self::field($field, $rowID, $form_atomic_Cls_map, $value); |
| 9 | 13 | return $inputWrapper->wrapper($input); |
| 10 | 14 | } |
| 11 | 15 | |
| 12 | - private static function checkSelected($opt, $val) { | |
| 16 | + private static function checkSelected($opt, $val) | |
| 17 | + { | |
| 13 | 18 | $selected = ''; |
| 14 | 19 | if (isset($opt->val) && $opt->val === $val) { |
| 15 | 20 | $selected = 'selected'; |
| 16 | 21 | } elseif (isset($opt->lbl) && $opt->lbl === $val) { |
| 17 | 22 | $selected = 'selected'; |
| 18 | - } elseif (property_exists($opt, 'check')) { | |
| 23 | + } elseif (is_object($opt) && property_exists($opt, 'check')) { | |
| 19 | 24 | $selected = 'selected'; |
| 20 | 25 | } |
| 21 | 26 | return $selected; |
| 22 | 27 | } |
| 23 | 28 | |
| 24 | - private static function field($field, $rowID, $form_atomic_Cls_map, $v) { | |
| 25 | - $fieldHelpers = new FieldHelpers($field, $rowID, $form_atomic_Cls_map); | |
| 29 | + private static function field($field, $rowID, $form_atomic_Cls_map, $v) | |
| 30 | + { | |
| 31 | + $fieldHelpers = new ClassicFieldHelpers($field, $rowID, $form_atomic_Cls_map); | |
| 26 | 32 | $disabled = $fieldHelpers->disabled(); |
| 27 | 33 | $readonly = $fieldHelpers->readonly(); |
| 28 | 34 | $name = $fieldHelpers->name(); |
| 35 | + $ariaDescribedBy = $fieldHelpers->ariaDescribedBy(); | |
| 36 | + $ariaRequired = $fieldHelpers->ariaRequired(); | |
| 29 | 37 | $value = ''; |
| 30 | 38 | if ($v) { |
| 31 | 39 | $value = $v; |
| 32 | 40 | } elseif (isset($field->val)) { |
| @@ -35,84 +43,135 @@ | ||
| 35 | 43 | |
| 36 | 44 | $readonlyCls = $readonly ? 'readonly' : ''; |
| 37 | 45 | $phHide = ''; |
| 38 | 46 | $optionsHTML = ''; |
| 39 | - | |
| 47 | + $bfFrontendFormIds = FrontendHelpers::$bfFrontendFormIds; | |
| 48 | + $contentCount = count($bfFrontendFormIds); | |
| 40 | 49 | if ($fieldHelpers->property_exists_nested($field, 'phHide', true)) { |
| 41 | - $phHide = <<<PHHIDE | |
| 42 | - <option | |
| 43 | - class="{$fieldHelpers->getAtomicCls('slct-optn')} {$fieldHelpers->getCustomClasses('slct-optn')}" | |
| 44 | - value="{$field->ph}" | |
| 45 | - {$fieldHelpers->getCustomAttributes('slct-optn')} | |
| 50 | + $phHide = sprintf( | |
| 51 | + '<option | |
| 52 | + %1$s | |
| 53 | + class="%2$s %3$s" | |
| 54 | + value | |
| 46 | 55 | > |
| 47 | - {$field->ph} | |
| 48 | - </option> | |
| 49 | -PHHIDE; | |
| 56 | + %4$s | |
| 57 | + </option>', | |
| 58 | + $fieldHelpers->getCustomAttributes('slct-optn'), | |
| 59 | + $fieldHelpers->getAtomicCls('slct-optn'), | |
| 60 | + $fieldHelpers->getCustomClasses('slct-optn'), | |
| 61 | + $fieldHelpers->kses_post($field->ph) | |
| 62 | + ); | |
| 50 | 63 | } |
| 51 | 64 | |
| 52 | - if (property_exists($field, 'opt')) { | |
| 65 | + if (property_exists($field, 'opt') && is_iterable($field->opt)) { | |
| 53 | 66 | foreach ($field->opt as $opt) { |
| 67 | + if (!is_object($opt)) { | |
| 68 | + continue; | |
| 69 | + } | |
| 54 | 70 | $disabled = property_exists($opt, 'disabled') ? 'disabled' : ''; |
| 55 | 71 | if (property_exists($opt, 'type')) { |
| 56 | - $optionsHTML .= <<<OPTGRP | |
| 57 | - <optgroup | |
| 58 | - class="{$fieldHelpers->getAtomicCls('slct-opt-grp')} {$fieldHelpers->getCustomClasses('slct-opt-grp')}" | |
| 59 | - label="{$opt->title}" | |
| 60 | - {$fieldHelpers->getCustomAttributes('slct-opt-grp')} | |
| 61 | - {$disabled} | |
| 62 | - > | |
| 63 | -OPTGRP; | |
| 64 | - foreach ($opt->childs as $child) { | |
| 65 | - $val = isset($child->val) ? $child->val : $child->lbl; | |
| 72 | + $optionsHTML .= sprintf( | |
| 73 | + '<optgroup | |
| 74 | + %1$s | |
| 75 | + class="%2$s %3$s" | |
| 76 | + label="%4$s" | |
| 77 | + %5$s | |
| 78 | + >', | |
| 79 | + $fieldHelpers->getCustomAttributes('slct-opt-grp'), | |
| 80 | + $fieldHelpers->getAtomicCls('slct-opt-grp'), | |
| 81 | + $fieldHelpers->getCustomClasses('slct-opt-grp'), | |
| 82 | + $fieldHelpers->esc_attr($opt->title), | |
| 83 | + $disabled | |
| 84 | + ); | |
| 85 | + $optChilds = isset($opt->childs) && is_iterable($opt->childs) ? $opt->childs : []; | |
| 86 | + foreach ($optChilds as $child) { | |
| 87 | + if (!is_object($child)) { | |
| 88 | + continue; | |
| 89 | + } | |
| 90 | + $val = isset($child->val) ? $child->val : ($child->lbl ?? ''); | |
| 66 | 91 | $selected = self::checkSelected($child, $value); |
| 67 | 92 | $disabled = property_exists($child, 'disabled') ? 'disabled' : ''; |
| 68 | - $optionsHTML .= <<<OPT | |
| 69 | - <option | |
| 70 | - class="{$fieldHelpers->getAtomicCls('slct-optn')} {$fieldHelpers->getCustomClasses('slct-optn')}" | |
| 71 | - value="{$val}" | |
| 72 | - {$fieldHelpers->getCustomAttributes('slct-optn')} | |
| 73 | - {$selected} | |
| 74 | - {$disabled} | |
| 93 | + $optionsHTML .= sprintf( | |
| 94 | + '<option | |
| 95 | + %1$s | |
| 96 | + class="%2$s %3$s" | |
| 97 | + value="%4$s" | |
| 98 | + %5$s | |
| 99 | + %6$s | |
| 75 | 100 | > |
| 76 | - {$child->lbl} | |
| 77 | - </option> | |
| 78 | -OPT; | |
| 101 | + %7$s | |
| 102 | + </option>', | |
| 103 | + $fieldHelpers->getCustomAttributes('slct-optn'), | |
| 104 | + $fieldHelpers->getAtomicCls('slct-optn'), | |
| 105 | + $fieldHelpers->getCustomClasses('slct-optn'), | |
| 106 | + $fieldHelpers->esc_attr($val), | |
| 107 | + $selected, | |
| 108 | + $disabled, | |
| 109 | + $fieldHelpers->kses_post($child->lbl) | |
| 110 | + ); | |
| 79 | 111 | } |
| 80 | 112 | $optionsHTML .= '</optgroup>'; |
| 81 | 113 | } else { |
| 82 | 114 | $selected = self::checkSelected($opt, $value); |
| 83 | - $val = isset($opt->val) ? $opt->val : $opt->lbl; | |
| 84 | - $optionsHTML .= <<<OPT | |
| 85 | - <option | |
| 86 | - class="{$fieldHelpers->getAtomicCls('slct-optn')} {$fieldHelpers->getCustomClasses('slct-optn')}" | |
| 87 | - value="{$val}" | |
| 88 | - {$selected} | |
| 89 | - {$fieldHelpers->getCustomAttributes('slct-optn')} | |
| 90 | - {$disabled} | |
| 115 | + $val = isset($opt->val) ? $opt->val : ($opt->lbl ?? ''); | |
| 116 | + $optionsHTML .= sprintf( | |
| 117 | + '<option | |
| 118 | + %1$s | |
| 119 | + class="%2$s %3$s" | |
| 120 | + value="%4$s" | |
| 121 | + %5$s | |
| 122 | + %6$s | |
| 91 | 123 | > |
| 92 | - {$opt->lbl} | |
| 93 | - </option> | |
| 94 | -OPT; | |
| 124 | + %7$s | |
| 125 | + </option>', | |
| 126 | + $fieldHelpers->getCustomAttributes('slct-optn'), | |
| 127 | + $fieldHelpers->getAtomicCls('slct-optn'), | |
| 128 | + $fieldHelpers->getCustomClasses('slct-optn'), | |
| 129 | + $fieldHelpers->esc_attr($val), | |
| 130 | + $selected, | |
| 131 | + $disabled, | |
| 132 | + $fieldHelpers->kses_post($opt->lbl) | |
| 133 | + ); | |
| 95 | 134 | } |
| 96 | 135 | } |
| 97 | 136 | } |
| 98 | 137 | |
| 99 | - return <<<HTMLSELECTFIELD | |
| 100 | - <div class="{$fieldHelpers->getAtomicCls('inp-fld-wrp')} {$fieldHelpers->getCustomClasses('inp-fld-wrp')}" | |
| 101 | - {$fieldHelpers->getCustomAttributes('inp-fld-wrp')} | |
| 138 | + return sprintf( | |
| 139 | + '<div | |
| 140 | + %1$s | |
| 141 | + class="%2$s %3$s" | |
| 102 | 142 | > |
| 103 | - <select | |
| 104 | - id={$rowID} | |
| 105 | - class="{$fieldHelpers->getAtomicCls('fld')} {$fieldHelpers->getCustomClasses('fld')} {$readonlyCls}" | |
| 106 | - {$readonly} | |
| 107 | - {$disabled} | |
| 108 | - {$name} | |
| 109 | - value="{$value}" | |
| 110 | - {$fieldHelpers->getCustomAttributes('fld')} | |
| 111 | - > | |
| 112 | - {$phHide} | |
| 113 | - {$optionsHTML} | |
| 114 | - </select> | |
| 115 | - </div> | |
| 116 | -HTMLSELECTFIELD; | |
| 143 | + <select | |
| 144 | + %4$s | |
| 145 | + id="%5$s-%6$s" | |
| 146 | + class="%7$s %8$s %9$s" | |
| 147 | + %10$s | |
| 148 | + %11$s | |
| 149 | + %12$s | |
| 150 | + %13$s | |
| 151 | + %14$s | |
| 152 | + value="%15$s" | |
| 153 | + > | |
| 154 | + %16$s | |
| 155 | + %17$s | |
| 156 | + </select> | |
| 157 | + </div>', | |
| 158 | + $fieldHelpers->getCustomAttributes('inp-fld-wrp'), | |
| 159 | + $fieldHelpers->getAtomicCls('inp-fld-wrp'), | |
| 160 | + $fieldHelpers->getCustomClasses('inp-fld-wrp'), | |
| 161 | + $fieldHelpers->getCustomAttributes('fld'), | |
| 162 | + $rowID, | |
| 163 | + $contentCount, | |
| 164 | + $fieldHelpers->getAtomicCls('fld'), | |
| 165 | + $fieldHelpers->getCustomClasses('fld'), | |
| 166 | + $readonlyCls, | |
| 167 | + $ariaRequired, | |
| 168 | + $ariaDescribedBy, | |
| 169 | + $readonly, | |
| 170 | + $disabled, | |
| 171 | + $name, | |
| 172 | + $fieldHelpers->esc_attr($value), | |
| 173 | + $phHide, | |
| 174 | + $optionsHTML | |
| 175 | + ); | |
| 117 | 176 | } |
| 118 | 177 | } |