← All changes
|
includes/Frontend/Form/View/Theme/Fields/RadioBoxField.php
+186
-83
2.0
→
V3.3.2
View file →
| @@ -1,25 +1,32 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Frontend\Form\View\Theme\Fields; |
| 4 | 4 | |
| 5 | -class RadioBoxField { | |
| 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 RadioBoxField | |
| 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, $field_name, $form_atomic_Cls_map, $formID, $error, $value); |
| 9 | 13 | return $inputWrapper->wrapper($input); |
| 10 | 14 | } |
| 11 | 15 | |
| 12 | - private static function field($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error, $value) { | |
| 13 | - $fieldHelpers = new FieldHelpers($field, $rowID, $form_atomic_Cls_map); | |
| 16 | + private static function field($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error, $value) | |
| 17 | + { | |
| 18 | + $fieldHelpers = new ClassicFieldHelpers($field, $rowID, $form_atomic_Cls_map); | |
| 14 | 19 | $req = $fieldHelpers->required(); |
| 15 | 20 | $name = $fieldHelpers->name(); |
| 16 | 21 | $value = ''; |
| 17 | 22 | $radioBoxOptions = ''; |
| 18 | - if (property_exists($field, 'opt') && count($field->opt) > 0) { | |
| 23 | + $bfFrontendFormIds = FrontendHelpers::$bfFrontendFormIds; | |
| 24 | + $contentCount = count($bfFrontendFormIds); | |
| 25 | + if (is_object($field) && property_exists($field, 'opt') && is_countable($field->opt) && count($field->opt) > 0) { | |
| 19 | 26 | $defaultValue = isset($field->val) ? $field->val : ''; |
| 20 | 27 | foreach ($field->opt as $key => $opt) { |
| 21 | - $value = isset($opt->val) ? $opt->val : $opt->lbl; | |
| 28 | + $value = isset($opt->val) ? $opt->val : ($opt->lbl ?? ''); | |
| 22 | 29 | $check = ''; |
| 23 | 30 | $req = ''; |
| 24 | 31 | $lbl = $fieldHelpers->renderHTMR($opt->lbl); |
| 25 | 32 | $disabled = ''; |
| @@ -37,110 +44,206 @@ | ||
| 37 | 44 | || $fieldHelpers->property_exists_nested($opt, 'disabled', true)) { |
| 38 | 45 | $disabled = "disabled='disabled'"; |
| 39 | 46 | } |
| 40 | 47 | |
| 41 | - $radioBoxOptions .= <<<RADIOBOXOPTIONS | |
| 42 | - <div class="{$fieldHelpers->getAtomicCls('cw')} {$fieldHelpers->getCustomClasses('cw')}" | |
| 43 | - {$fieldHelpers->getCustomAttributes('cw')} | |
| 48 | + $radioBoxOptions .= sprintf( | |
| 49 | + '<div | |
| 50 | + %1$s | |
| 51 | + class="%2$s %3$s" | |
| 44 | 52 | > |
| 45 | 53 | <input |
| 46 | - id={$rowID}-chk-{$key} | |
| 54 | + %4$s | |
| 55 | + id="%5$s-%6$s-chk-%7$s" | |
| 47 | 56 | type="radio" |
| 48 | - class="{$fieldHelpers->getAtomicCls('ci')} {$fieldHelpers->getCustomClasses('ci')}" | |
| 49 | - value="{$value}" | |
| 50 | - {$check} | |
| 51 | - {$name} | |
| 52 | - {$req} | |
| 53 | - {$fieldHelpers->getCustomAttributes('ci')} | |
| 54 | - {$disabled} | |
| 57 | + class="%8$s %9$s" | |
| 58 | + value="%10$s" | |
| 59 | + %11$s | |
| 60 | + %12$s | |
| 61 | + %13$s | |
| 62 | + %14$s | |
| 55 | 63 | /> |
| 56 | 64 | <label |
| 65 | + %15$s | |
| 57 | 66 | data-cl |
| 58 | - for="{$rowID}-chk-{$key}" | |
| 59 | - class="{$fieldHelpers->getAtomicCls('cl')} {$fieldHelpers->getCustomClasses('cl')}" | |
| 60 | - {$fieldHelpers->getCustomAttributes('cl')} | |
| 67 | + for="%5$s-%6$s-chk-%7$s" | |
| 68 | + class="%16$s %17$s" | |
| 61 | 69 | > |
| 62 | 70 | <span |
| 71 | + %18$s | |
| 63 | 72 | data-bx |
| 64 | - class="{$fieldHelpers->getAtomicCls('bx')} {$fieldHelpers->getCustomClasses('bx')}" | |
| 65 | - {$fieldHelpers->getCustomAttributes('bx')} | |
| 73 | + class="%19$s %20$s" | |
| 66 | 74 | > |
| 67 | - <svg width="12" height="10" viewBox="0 0 12 10" class="{$fieldHelpers->getAtomicCls('svgwrp')} {$fieldHelpers->getCustomClasses('svgwrp')}"> | |
| 68 | - <use data-ck-icn href=#{$rowID}-ck-svg class="{$fieldHelpers->getAtomicCls('ck-icn')} {$fieldHelpers->getCustomClasses('ck-icn')}" /> | |
| 75 | + <svg | |
| 76 | + width="12" | |
| 77 | + height="10" | |
| 78 | + viewBox="0 0 12 10" | |
| 79 | + class="%21$s %22$s" | |
| 80 | + aria-hidden="true" | |
| 81 | + > | |
| 82 | + <use | |
| 83 | + data-ck-icn | |
| 84 | + href=#%5$s-ck-svg | |
| 85 | + class="%23$s %24$s" | |
| 86 | + /> | |
| 69 | 87 | </svg> |
| 70 | 88 | </span> |
| 71 | 89 | <span |
| 72 | - class="{$fieldHelpers->getAtomicCls('ct')} {$fieldHelpers->getCustomClasses('ct')}" | |
| 73 | - {$fieldHelpers->getCustomAttributes('ct')} | |
| 90 | + %25$s | |
| 91 | + class="%26$s %27$s" | |
| 74 | 92 | > |
| 75 | - {$lbl} | |
| 93 | + %28$s | |
| 76 | 94 | </span> |
| 77 | 95 | </label> |
| 78 | - </div> | |
| 79 | -RADIOBOXOPTIONS; | |
| 96 | + </div>', | |
| 97 | + $fieldHelpers->getCustomAttributes('cw'), | |
| 98 | + $fieldHelpers->getAtomicCls('cw'), | |
| 99 | + $fieldHelpers->getCustomClasses('cw'), | |
| 100 | + $fieldHelpers->getCustomAttributes('ci'), | |
| 101 | + $rowID, | |
| 102 | + $contentCount, | |
| 103 | + $key, | |
| 104 | + $fieldHelpers->getAtomicCls('ci'), | |
| 105 | + $fieldHelpers->getCustomClasses('ci'), | |
| 106 | + $fieldHelpers->esc_attr($value), | |
| 107 | + $check, | |
| 108 | + $name, | |
| 109 | + $req, | |
| 110 | + $disabled, | |
| 111 | + $fieldHelpers->getCustomAttributes('cl'), | |
| 112 | + $fieldHelpers->getAtomicCls('cl'), | |
| 113 | + $fieldHelpers->getCustomClasses('cl'), | |
| 114 | + $fieldHelpers->getCustomAttributes('bx'), | |
| 115 | + $fieldHelpers->getAtomicCls('bx'), | |
| 116 | + $fieldHelpers->getCustomClasses('bx'), | |
| 117 | + $fieldHelpers->getAtomicCls('svgwrp'), | |
| 118 | + $fieldHelpers->getCustomClasses('svgwrp'), | |
| 119 | + $fieldHelpers->getAtomicCls('ck-icn'), | |
| 120 | + $fieldHelpers->getCustomClasses('ck-icn'), | |
| 121 | + $fieldHelpers->getCustomAttributes('ct'), | |
| 122 | + $fieldHelpers->getAtomicCls('ct'), | |
| 123 | + $fieldHelpers->getCustomClasses('ct'), | |
| 124 | + $fieldHelpers->kses_post($lbl) | |
| 125 | + ); | |
| 80 | 126 | } |
| 81 | 127 | } |
| 82 | 128 | |
| 83 | 129 | //Other Option |
| 84 | - $optCount = property_exists($field, 'opt') ? count($field->opt) : 0; | |
| 85 | - $inputPh = isset($field->otherInpPh) ? $field->otherInpPh : 'Other...'; | |
| 130 | + $otherOptLbl = !empty($field->otherOptLbl) ? $field->otherOptLbl : __('Other...', 'bit-form'); | |
| 131 | + $optCount = isset($field->opt) && is_countable($field->opt) ? count($field->opt) : 0; | |
| 132 | + $inputPh = isset($field->otherInpPh) ? $field->otherInpPh : $otherOptLbl; | |
| 86 | 133 | $inpReq = isset($field->valid->otherOptReq) ? ($field->valid->otherOptReq ? 'required' : '') : ''; |
| 87 | 134 | if (property_exists($field, 'addOtherOpt') && $field->addOtherOpt) { |
| 88 | - $radioBoxOptions .= <<<RADIOBOXOPTIONS | |
| 89 | - <div class="{$fieldHelpers->getAtomicCls('cw')} {$fieldHelpers->getCustomClasses('cw')}" | |
| 90 | - {$fieldHelpers->getCustomAttributes('cw')} | |
| 91 | - > | |
| 92 | - <input | |
| 93 | - id="{$rowID}-chk-{$optCount}" | |
| 94 | - data-oopt="{$rowID}" | |
| 95 | - type="radio" | |
| 96 | - class="{$fieldHelpers->getAtomicCls('ci')} {$fieldHelpers->getCustomClasses('ci')}" | |
| 97 | - value="" | |
| 98 | - {$check} | |
| 99 | - {$name} | |
| 100 | - {$fieldHelpers->getCustomAttributes('ci')} | |
| 101 | - /> | |
| 102 | - <label | |
| 103 | - data-cl | |
| 104 | - for="{$rowID}-chk-{$optCount}" | |
| 105 | - class="{$fieldHelpers->getAtomicCls('cl')} {$fieldHelpers->getCustomClasses('cl')}" | |
| 106 | - {$fieldHelpers->getCustomAttributes('cl')} | |
| 135 | + $check = $check ?? ''; | |
| 136 | + $radioBoxOptions .= sprintf( | |
| 137 | + ' | |
| 138 | + <div | |
| 139 | + %1$s | |
| 140 | + class="%2$s %3$s" | |
| 107 | 141 | > |
| 108 | - <span | |
| 109 | - data-bx | |
| 110 | - class="{$fieldHelpers->getAtomicCls('bx')} {$fieldHelpers->getAtomicCls('rdo')} {$fieldHelpers->getCustomClasses('rdo')}" | |
| 111 | - {$fieldHelpers->getCustomAttributes('rdo')} | |
| 112 | - > | |
| 113 | - <svg width="12" height="10" viewBox="0 0 12 10" class="{$fieldHelpers->getAtomicCls('svgwrp')} {$fieldHelpers->getCustomClasses('svgwrp')}"> | |
| 114 | - <use data-ck-icn href=#{$rowID}-ck-svg class="{$fieldHelpers->getAtomicCls('ck-icn')} {$fieldHelpers->getCustomClasses('ck-icn')}" /> | |
| 115 | - </svg> | |
| 116 | - </span> | |
| 117 | - <span | |
| 118 | - class="{$fieldHelpers->getAtomicCls('ct')} {$fieldHelpers->getCustomClasses('ct')}" | |
| 119 | - {$fieldHelpers->getCustomAttributes('ct')} | |
| 120 | - > | |
| 121 | - Other.. | |
| 122 | - </span> | |
| 123 | - </label> | |
| 124 | - <div data-oinp-wrp class="{$fieldHelpers->getAtomicCls('other-inp-wrp')}"> | |
| 125 | 142 | <input |
| 126 | - data-bf-other-inp="{$rowID}-chk-{$optCount}" | |
| 127 | - type="text" | |
| 128 | - class="{$fieldHelpers->getAtomicCls('other-inp')}" | |
| 129 | - {$inpReq} | |
| 130 | - placeholder="{$inputPh}" | |
| 143 | + %4$s | |
| 144 | + id="%5$s-%6$s-chk-%7$s" | |
| 145 | + data-oopt="%5$s" | |
| 146 | + type="radio" | |
| 147 | + class="%8$s %9$s" | |
| 148 | + value="" | |
| 149 | + %10$s | |
| 150 | + %11$s | |
| 151 | + /> | |
| 152 | + <label | |
| 153 | + %12$s | |
| 154 | + data-cl | |
| 155 | + for="%5$s-%6$s-chk-%7$s" | |
| 156 | + class="%13$s %14$s" | |
| 131 | 157 | > |
| 132 | - </div> | |
| 133 | - </div> | |
| 134 | -RADIOBOXOPTIONS; | |
| 158 | + <span | |
| 159 | + %15$s | |
| 160 | + data-bx | |
| 161 | + class="%16$s %17$s %18$s" | |
| 162 | + > | |
| 163 | + <svg | |
| 164 | + width="12" | |
| 165 | + height="10" | |
| 166 | + viewBox="0 0 12 10" | |
| 167 | + class="%19$s %20$s" | |
| 168 | + > | |
| 169 | + <use | |
| 170 | + data-ck-icn | |
| 171 | + href=#%5$s-ck-svg | |
| 172 | + class="%21$s %22$s" | |
| 173 | + /> | |
| 174 | + </svg> | |
| 175 | + </span> | |
| 176 | + <span | |
| 177 | + %23$s | |
| 178 | + class="%24$s %25$s" | |
| 179 | + > | |
| 180 | + %26$s | |
| 181 | + </span> | |
| 182 | + </label> | |
| 183 | + <div data-oinp-wrp class="%27$s"> | |
| 184 | + <input | |
| 185 | + data-bf-other-inp="%5$s-chk-%7$s" | |
| 186 | + type="text" | |
| 187 | + class="%28$s" | |
| 188 | + %29$s | |
| 189 | + placeholder="%30$s" | |
| 190 | + > | |
| 191 | + </div> | |
| 192 | + </div>', | |
| 193 | + $fieldHelpers->getCustomAttributes('cw'), | |
| 194 | + $fieldHelpers->getAtomicCls('cw'), | |
| 195 | + $fieldHelpers->getCustomClasses('cw'), | |
| 196 | + $fieldHelpers->getCustomAttributes('ci'), | |
| 197 | + $rowID, | |
| 198 | + $contentCount, | |
| 199 | + $optCount, | |
| 200 | + $fieldHelpers->getAtomicCls('ci'), | |
| 201 | + $fieldHelpers->getCustomClasses('ci'), | |
| 202 | + $check, | |
| 203 | + $name, | |
| 204 | + $fieldHelpers->getCustomAttributes('cl'), | |
| 205 | + $fieldHelpers->getAtomicCls('cl'), | |
| 206 | + $fieldHelpers->getCustomClasses('cl'), | |
| 207 | + $fieldHelpers->getCustomAttributes('rdo'), | |
| 208 | + $fieldHelpers->getAtomicCls('bx'), | |
| 209 | + $fieldHelpers->getAtomicCls('rdo'), | |
| 210 | + $fieldHelpers->getCustomClasses('rdo'), | |
| 211 | + $fieldHelpers->getAtomicCls('svgwrp'), | |
| 212 | + $fieldHelpers->getCustomClasses('svgwrp'), | |
| 213 | + $fieldHelpers->getAtomicCls('ck-icn'), | |
| 214 | + $fieldHelpers->getCustomClasses('ck-icn'), | |
| 215 | + $fieldHelpers->getCustomAttributes('ct'), | |
| 216 | + $fieldHelpers->getAtomicCls('ct'), | |
| 217 | + $fieldHelpers->getCustomClasses('ct'), | |
| 218 | + $otherOptLbl, | |
| 219 | + $fieldHelpers->getAtomicCls('other-inp-wrp'), | |
| 220 | + $fieldHelpers->getAtomicCls('other-inp'), | |
| 221 | + $inpReq, | |
| 222 | + $fieldHelpers->esc_attr($inputPh) | |
| 223 | + ); | |
| 135 | 224 | } |
| 136 | 225 | |
| 137 | - return <<<RADIOBOX | |
| 138 | - <div | |
| 139 | - class="{$fieldHelpers->getAtomicCls('cc')} {$fieldHelpers->getCustomClasses('cc')}" | |
| 140 | - {$fieldHelpers->getCustomAttributes('cc')} | |
| 226 | + $labelId = $fieldHelpers->getLabelId(); | |
| 227 | + $ariaDescribedBy = $fieldHelpers->ariaDescribedBy(); | |
| 228 | + $ariaRequired = $fieldHelpers->ariaRequired(); | |
| 229 | + return sprintf( | |
| 230 | + '<div | |
| 231 | + %1$s | |
| 232 | + class="%2$s %3$s" | |
| 233 | + role="radiogroup" | |
| 234 | + aria-labelledby="%4$s" | |
| 235 | + %5$s | |
| 236 | + %6$s | |
| 141 | 237 | > |
| 142 | - {$radioBoxOptions} | |
| 143 | - </div> | |
| 144 | -RADIOBOX; | |
| 238 | + %7$s | |
| 239 | + </div>', | |
| 240 | + $fieldHelpers->getCustomAttributes('cc'), | |
| 241 | + $fieldHelpers->getAtomicCls('cc'), | |
| 242 | + $fieldHelpers->getCustomClasses('cc'), | |
| 243 | + $labelId, | |
| 244 | + $ariaDescribedBy, | |
| 245 | + $ariaRequired, | |
| 246 | + $radioBoxOptions | |
| 247 | + ); | |
| 145 | 248 | } |
| 146 | 249 | } |