← All changes
|
includes/Frontend/Form/View/Theme/Fields/CheckBoxField.php
+106
-74
2.0
→
3.3.1
View file →
| @@ -1,24 +1,35 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Frontend\Form\View\Theme\Fields; |
| 4 | 4 | |
| 5 | -class CheckBoxField { | |
| 6 | - public static function init($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error, $value) { | |
| 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 CheckBoxField | |
| 8 | +{ | |
| 9 | + public static function init($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error, $value) | |
| 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 | |
| 15 | 20 | $req = $fieldHelpers->required(); |
| 16 | 21 | $name = $fieldHelpers->name(); |
| 17 | 22 | $checkBoxOptions = ''; |
| 23 | + $bfFrontendFormIds = FrontendHelpers::$bfFrontendFormIds; | |
| 24 | + $contentCount = count($bfFrontendFormIds); | |
| 18 | 25 | |
| 19 | - if (property_exists($field, 'opt') && count($field->opt) > 0) { | |
| 20 | - $defaultValues = isset($field->val) ? explode(',', $field->val) : []; | |
| 26 | + if (is_object($field) && property_exists($field, 'opt') && is_countable($field->opt) && count($field->opt) > 0) { | |
| 27 | + if (isset($field->val) && false !== strpos($field->val, BITFORMS_BF_SEPARATOR)) { | |
| 28 | + $defaultValues = explode(BITFORMS_BF_SEPARATOR, $field->val); | |
| 29 | + } else { | |
| 30 | + $defaultValues = isset($field->val) ? explode(',', $field->val) : []; | |
| 31 | + } | |
| 21 | 32 | foreach ($field->opt as $key => $opt) { |
| 22 | 33 | $value = isset($opt->val) ? $opt->val : $opt->lbl; |
| 23 | 34 | $check = ''; |
| 24 | 35 | $req = ''; |
| @@ -35,121 +46,142 @@ | ||
| 35 | 46 | $req = 'required'; |
| 36 | 47 | } |
| 37 | 48 | if ($fieldHelpers->property_exists_nested($field, 'valid->disabled', true) |
| 38 | 49 | || $fieldHelpers->property_exists_nested($opt, 'disabled', true)) { |
| 39 | - $disabled = "disabled='disabled'"; | |
| 50 | + $disabled = 'disabled'; | |
| 40 | 51 | } |
| 41 | 52 | |
| 42 | - $checkBoxOptions .= <<<CHECKBOXOPTIONS | |
| 53 | + if (isset($field->mx) && !empty($field->valid->disableOnMax) && !$check && ((int) $field->mx <= count($defaultValues))) { | |
| 54 | + $disabled = 'disabled'; | |
| 55 | + } | |
| 56 | + | |
| 57 | + $checkBoxOptions .= ' | |
| 43 | 58 | <div |
| 44 | - class="{$fieldHelpers->getAtomicCls('cw')} {$fieldHelpers->getCustomClasses('cw')}" | |
| 45 | - {$fieldHelpers->getCustomAttributes('cw')} | |
| 59 | + ' . $fieldHelpers->getCustomAttributes('cw') . ' | |
| 60 | + class="' . $fieldHelpers->getAtomicCls('cw') . ' ' . $fieldHelpers->getCustomClasses('cw') . '" | |
| 46 | 61 | > |
| 47 | 62 | <input |
| 48 | - id="{$rowID}-chk-{$key}" | |
| 63 | + id="' . $rowID . '-' . $contentCount . '-chk-' . $key . '" | |
| 49 | 64 | type="checkbox" |
| 50 | - class="{$fieldHelpers->getAtomicCls('ci')} {$fieldHelpers->getCustomClasses('ci')}" | |
| 51 | - {$disabled} | |
| 52 | - value="{$value}" | |
| 53 | - {$check} | |
| 54 | - {$req} | |
| 55 | - {$name} | |
| 65 | + class="' . $fieldHelpers->getAtomicCls('ci') . ' ' . $fieldHelpers->getCustomClasses('ci') . '" | |
| 66 | + ' . $disabled . ' | |
| 67 | + value="' . $value . '" | |
| 68 | + ' . $check . ' | |
| 69 | + ' . $req . ' | |
| 70 | + ' . $name . ' | |
| 56 | 71 | /> |
| 57 | 72 | <label |
| 73 | + ' . $fieldHelpers->getCustomAttributes('cl') . ' | |
| 58 | 74 | data-cl |
| 59 | - for="{$rowID}-chk-{$key}" | |
| 60 | - class="{$fieldHelpers->getAtomicCls('cl')} {$fieldHelpers->getCustomClasses('cl')}" | |
| 61 | - {$fieldHelpers->getCustomAttributes('cl')} | |
| 75 | + for="' . $rowID . '-' . $contentCount . '-chk-' . $key . '" | |
| 76 | + class="' . $fieldHelpers->getAtomicCls('cl') . ' ' . $fieldHelpers->getCustomClasses('cl') . '" | |
| 62 | 77 | > |
| 63 | 78 | <span |
| 79 | + ' . $fieldHelpers->getCustomAttributes('bx') . ' | |
| 64 | 80 | data-bx |
| 65 | - class="{$fieldHelpers->getAtomicCls('bx')} {$fieldHelpers->getCustomClasses('bx')}" | |
| 66 | - {$fieldHelpers->getCustomAttributes('bx')} | |
| 81 | + class="' . $fieldHelpers->getAtomicCls('bx') . ' ' . $fieldHelpers->getCustomClasses('bx') . '" | |
| 67 | 82 | > |
| 68 | - <svg width="12" height="10" viewBox="0 0 12 10" class="{$fieldHelpers->getAtomicCls('svgwrp')} {$fieldHelpers->getCustomClasses('svgwrp')}"> | |
| 69 | - <use data-ck-icn href="$href" class="{$fieldHelpers->getAtomicCls('ck-icn')} {$fieldHelpers->getCustomClasses('ck-icn')}" /> | |
| 83 | + <svg width="12" height="10" viewBox="0 0 12 10" class="' . $fieldHelpers->getAtomicCls('svgwrp') . ' ' . $fieldHelpers->getCustomClasses('svgwrp') . '" aria-hidden="true"> | |
| 84 | + <use data-ck-icn href="' . $href . '" class="' . $fieldHelpers->getAtomicCls('ck-icn') . ' ' . $fieldHelpers->getCustomClasses('ck-icn') . '" /> | |
| 70 | 85 | </svg> |
| 71 | 86 | </span> |
| 72 | 87 | <span |
| 73 | - class="{$fieldHelpers->getAtomicCls('ct')} {$fieldHelpers->getCustomClasses('ct')}" | |
| 74 | - {$fieldHelpers->getCustomAttributes('ct')} | |
| 88 | + ' . $fieldHelpers->getCustomAttributes('ct') . ' | |
| 89 | + class="' . $fieldHelpers->getAtomicCls('ct') . ' ' . $fieldHelpers->getCustomClasses('ct') . '" | |
| 75 | 90 | > |
| 76 | - {$fieldHelpers->renderHTMR($opt->lbl)} | |
| 91 | + ' . $fieldHelpers->kses_post($fieldHelpers->renderHTMR($opt->lbl)) . ' | |
| 77 | 92 | </span> |
| 78 | 93 | </label> |
| 79 | - </div> | |
| 80 | -CHECKBOXOPTIONS; | |
| 94 | + </div>'; | |
| 81 | 95 | } |
| 82 | 96 | } |
| 83 | 97 | |
| 84 | 98 | //Other Option |
| 85 | - $optCount = property_exists($field, 'opt') ? count($field->opt) : 0; | |
| 86 | - $inputPh = isset($field->otherInpPh) ? "placeholder='$field->otherInpPh'" : "placeholder='Other...'"; | |
| 99 | + $optCount = isset($field->opt) && is_countable($field->opt) ? count($field->opt) : 0; | |
| 87 | 100 | $inpReq = isset($field->valid->otherOptReq) ? ($field->valid->otherOptReq ? 'required' : '') : ''; |
| 101 | + $otherOptLbl = !empty($field->otherOptLbl) ? $field->otherOptLbl : __('Other...', 'bit-form'); | |
| 102 | + $inputPh = isset($field->otherInpPh) ? "placeholder='{$fieldHelpers->esc_attr($field->otherInpPh)}'" : "placeholder='{$otherOptLbl}'"; | |
| 88 | 103 | if (property_exists($field, 'addOtherOpt') && $field->addOtherOpt) { |
| 89 | - $checkBoxOptions .= <<<CHECKBOXOPTIONS | |
| 90 | - <div | |
| 91 | - class="{$fieldHelpers->getAtomicCls('cw')} {$fieldHelpers->getCustomClasses('cw')}" | |
| 92 | - {$fieldHelpers->getCustomAttributes('cw')} | |
| 104 | + $checkBoxOptions .= | |
| 105 | + '<div | |
| 106 | + ' . $fieldHelpers->getCustomAttributes('cw') . ' | |
| 107 | + class="' . $fieldHelpers->getAtomicCls('cw') . ' ' . $fieldHelpers->getCustomClasses('cw') . '" | |
| 93 | 108 | > |
| 94 | 109 | <input |
| 95 | - id="{$rowID}-chk-{$optCount}" | |
| 96 | - data-oopt="{$rowID}" | |
| 110 | + id="' . $rowID . '-' . $contentCount . '-chk-' . $optCount . '" | |
| 111 | + data-oopt="' . $rowID . '" | |
| 97 | 112 | type="checkbox" |
| 98 | - class="{$fieldHelpers->getAtomicCls('ci')} {$fieldHelpers->getCustomClasses('ci')}" | |
| 99 | - {$disabled} | |
| 113 | + class="' . $fieldHelpers->getAtomicCls('ci') . ' ' . $fieldHelpers->getCustomClasses('ci') . '" | |
| 114 | + ' . $disabled . ' | |
| 100 | 115 | value="" |
| 101 | - {$name} | |
| 116 | + ' . $name . ' | |
| 102 | 117 | /> |
| 103 | 118 | <label |
| 119 | + ' . $fieldHelpers->getCustomAttributes('cl') . ' | |
| 104 | 120 | data-cl |
| 105 | - for="{$rowID}-chk-{$optCount}" | |
| 106 | - class="{$fieldHelpers->getAtomicCls('cl')} {$fieldHelpers->getCustomClasses('cl')}" | |
| 107 | - {$fieldHelpers->getCustomAttributes('cl')} | |
| 121 | + for="' . $rowID . '-' . $contentCount . '-chk-' . $optCount . '" | |
| 122 | + class="' . $fieldHelpers->getAtomicCls('cl') . ' ' . $fieldHelpers->getCustomClasses('cl') . '" | |
| 108 | 123 | > |
| 109 | 124 | <span |
| 125 | + ' . $fieldHelpers->getCustomAttributes('ck') . ' | |
| 110 | 126 | data-bx |
| 111 | - class="{$fieldHelpers->getAtomicCls('bx')} {$fieldHelpers->getAtomicCls('ck')} {$fieldHelpers->getCustomClasses('ck')}" | |
| 112 | - {$fieldHelpers->getCustomAttributes('ck')} | |
| 127 | + class="' . $fieldHelpers->getAtomicCls('bx') . ' ' . $fieldHelpers->getAtomicCls('ck') . ' ' . $fieldHelpers->getCustomClasses('ck') . '" | |
| 113 | 128 | > |
| 114 | - <svg width="12" height="10" viewBox="0 0 12 10" class="{$fieldHelpers->getAtomicCls('svgwrp')} {$fieldHelpers->getCustomClasses('svgwrp')}"> | |
| 115 | - <use data-ck-icn href="#$rowID-ck-svg" class="{$fieldHelpers->getAtomicCls('ck-icn')} {$fieldHelpers->getCustomClasses('ck-icn')}" /> | |
| 129 | + <svg width="12" height="10" viewBox="0 0 12 10" class="' . $fieldHelpers->getAtomicCls('svgwrp') . ' ' . $fieldHelpers->getCustomClasses('svgwrp') . '" aria-hidden="true"> | |
| 130 | + <use data-ck-icn href="#' . $rowID . '-ck-svg" class="' . $fieldHelpers->getAtomicCls('ck-icn') . ' ' . $fieldHelpers->getCustomClasses('ck-icn') . '" /> | |
| 116 | 131 | </svg> |
| 117 | 132 | </span> |
| 118 | 133 | <span |
| 119 | - class="{$fieldHelpers->getAtomicCls('ct')} {$fieldHelpers->getCustomClasses('ct')}" | |
| 120 | - {$fieldHelpers->getCustomAttributes('ct')} | |
| 134 | + ' . $fieldHelpers->getCustomAttributes('ct') . ' | |
| 135 | + class="' . $fieldHelpers->getAtomicCls('ct') . ' ' . $fieldHelpers->getCustomClasses('ct') . '" | |
| 121 | 136 | > |
| 122 | - Other.. | |
| 137 | + ' . $otherOptLbl . ' | |
| 123 | 138 | </span> |
| 124 | 139 | </label> |
| 125 | - <div data-oinp-wrp class="{$fieldHelpers->getAtomicCls('other-inp-wrp')}"> | |
| 140 | + <div data-oinp-wrp class="' . $fieldHelpers->getAtomicCls('other-inp-wrp') . '"> | |
| 126 | 141 | <input |
| 127 | - data-bf-other-inp='{$rowID}-chk-{$optCount}' | |
| 142 | + data-bf-other-inp="' . $rowID . '-chk-' . $optCount . '" | |
| 128 | 143 | type="text" |
| 129 | - class="{$fieldHelpers->getAtomicCls('other-inp')} {$fieldHelpers->getCustomClasses('other-inp')}" | |
| 130 | - {$inpReq} | |
| 131 | - {$inputPh} | |
| 144 | + class="' . $fieldHelpers->getAtomicCls('other-inp') . ' ' . $fieldHelpers->getCustomClasses('other-inp') . '" | |
| 145 | + ' . $inpReq . ' | |
| 146 | + ' . $inputPh . ' | |
| 132 | 147 | /> |
| 133 | 148 | </div> |
| 134 | - </div> | |
| 135 | -CHECKBOXOPTIONS; | |
| 149 | + </div>'; | |
| 136 | 150 | } |
| 137 | 151 | |
| 138 | - return <<<CHECKBOXFIELD | |
| 139 | -<div | |
| 140 | - class="{$fieldHelpers->getAtomicCls('cc')} {$fieldHelpers->getCustomClasses('cc')}" | |
| 141 | - {$fieldHelpers->getCustomAttributes('cc')} | |
| 142 | -> | |
| 143 | - <svg class="{$fieldHelpers->getAtomicCls('cks')}"> | |
| 144 | - <symbol id="{$rowID}-ck-svg" viewBox="0 0 12 10"> | |
| 145 | - <polyline | |
| 146 | - class="{$fieldHelpers->getAtomicCls('ck-svgline')}" | |
| 147 | - points="1.5 6 4.5 9 10.5 1" | |
| 148 | - ></polygon> | |
| 149 | - </symbol> | |
| 150 | - </svg> | |
| 151 | - {$checkBoxOptions} | |
| 152 | -</div> | |
| 153 | -CHECKBOXFIELD; | |
| 152 | + $labelId = $fieldHelpers->getLabelId(); | |
| 153 | + $ariaDescribedBy = $fieldHelpers->ariaDescribedBy(); | |
| 154 | + $ariaRequired = $fieldHelpers->ariaRequired(); | |
| 155 | + | |
| 156 | + return sprintf( | |
| 157 | + '<div | |
| 158 | + %1$s | |
| 159 | + class="%2$s %3$s" | |
| 160 | + role="group" | |
| 161 | + aria-labelledby="%4$s" | |
| 162 | + %5$s | |
| 163 | + %6$s | |
| 164 | + > | |
| 165 | + <svg class="%7$s" aria-hidden="true"> | |
| 166 | + <symbol id="%8$s-ck-svg" viewBox="0 0 12 10"> | |
| 167 | + <polyline | |
| 168 | + class="%9$s" | |
| 169 | + points="1.5 6 4.5 9 10.5 1" | |
| 170 | + ></polyline> | |
| 171 | + </symbol> | |
| 172 | + </svg> | |
| 173 | + %10$s | |
| 174 | + </div>', | |
| 175 | + $fieldHelpers->getCustomAttributes('cc'), | |
| 176 | + $fieldHelpers->getAtomicCls('cc'), | |
| 177 | + $fieldHelpers->getCustomClasses('cc'), | |
| 178 | + $labelId, | |
| 179 | + $ariaDescribedBy, | |
| 180 | + $ariaRequired, | |
| 181 | + $fieldHelpers->getAtomicCls('cks'), | |
| 182 | + $rowID, | |
| 183 | + $fieldHelpers->getAtomicCls('ck-svgline'), | |
| 184 | + $checkBoxOptions | |
| 185 | + ); | |
| 154 | 186 | } |
| 155 | 187 | } |