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