| 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 HtmlSelectField |
| 9 |
{ |
| 10 |
public static function init($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error = null, $value = null) |
| 11 |
{ |
| 12 |
$inputWrapper = new InputWrapper($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error, $value); |
| 13 |
$input = self::field($field, $rowID, $form_atomic_Cls_map, $value); |
| 14 |
return $inputWrapper->wrapper($input); |
| 15 |
} |
| 16 |
|
| 17 |
private static function checkSelected($opt, $val) |
| 18 |
{ |
| 19 |
$selected = ''; |
| 20 |
if (isset($opt->val) && $opt->val === $val) { |
| 21 |
$selected = 'selected'; |
| 22 |
} elseif (isset($opt->lbl) && $opt->lbl === $val) { |
| 23 |
$selected = 'selected'; |
| 24 |
} elseif (property_exists($opt, 'check')) { |
| 25 |
$selected = 'selected'; |
| 26 |
} |
| 27 |
return $selected; |
| 28 |
} |
| 29 |
|
| 30 |
private static function field($field, $rowID, $form_atomic_Cls_map, $v) |
| 31 |
{ |
| 32 |
$fieldHelpers = new FieldHelpers($field, $rowID, $form_atomic_Cls_map); |
| 33 |
$disabled = $fieldHelpers->disabled(); |
| 34 |
$readonly = $fieldHelpers->readonly(); |
| 35 |
$name = $fieldHelpers->name(); |
| 36 |
$value = ''; |
| 37 |
if ($v) { |
| 38 |
$value = $v; |
| 39 |
} elseif (isset($field->val)) { |
| 40 |
$value = $field->val; |
| 41 |
} |
| 42 |
|
| 43 |
$readonlyCls = $readonly ? 'readonly' : ''; |
| 44 |
$phHide = ''; |
| 45 |
$optionsHTML = ''; |
| 46 |
$bfFrontendFormIds = FrontendHelpers::$bfFrontendFormIds; |
| 47 |
$contentCount = count($bfFrontendFormIds); |
| 48 |
if ($fieldHelpers->property_exists_nested($field, 'phHide', true)) { |
| 49 |
$phHide = <<<PHHIDE |
| 50 |
<option |
| 51 |
{$fieldHelpers->getCustomAttributes('slct-optn')} |
| 52 |
class="{$fieldHelpers->getAtomicCls('slct-optn')} {$fieldHelpers->getCustomClasses('slct-optn')}" |
| 53 |
value |
| 54 |
> |
| 55 |
{$fieldHelpers->kses_post($field->ph)} |
| 56 |
</option> |
| 57 |
PHHIDE; |
| 58 |
} |
| 59 |
|
| 60 |
if (property_exists($field, 'opt')) { |
| 61 |
foreach ($field->opt as $opt) { |
| 62 |
$disabled = property_exists($opt, 'disabled') ? 'disabled' : ''; |
| 63 |
if (property_exists($opt, 'type')) { |
| 64 |
$optionsHTML .= <<<OPTGRP |
| 65 |
<optgroup |
| 66 |
{$fieldHelpers->getCustomAttributes('slct-opt-grp')} |
| 67 |
class="{$fieldHelpers->getAtomicCls('slct-opt-grp')} {$fieldHelpers->getCustomClasses('slct-opt-grp')}" |
| 68 |
label="{$fieldHelpers->esc_attr($opt->title)}" |
| 69 |
{$disabled} |
| 70 |
> |
| 71 |
OPTGRP; |
| 72 |
foreach ($opt->childs as $child) { |
| 73 |
$val = isset($child->val) ? $child->val : $child->lbl; |
| 74 |
$selected = self::checkSelected($child, $value); |
| 75 |
$disabled = property_exists($child, 'disabled') ? 'disabled' : ''; |
| 76 |
$optionsHTML .= <<<OPT |
| 77 |
<option |
| 78 |
{$fieldHelpers->getCustomAttributes('slct-optn')} |
| 79 |
class="{$fieldHelpers->getAtomicCls('slct-optn')} {$fieldHelpers->getCustomClasses('slct-optn')}" |
| 80 |
value="{$fieldHelpers->esc_attr($val)}" |
| 81 |
{$selected} |
| 82 |
{$disabled} |
| 83 |
> |
| 84 |
{$fieldHelpers->kses_post($child->lbl)} |
| 85 |
</option> |
| 86 |
OPT; |
| 87 |
} |
| 88 |
$optionsHTML .= '</optgroup>'; |
| 89 |
} else { |
| 90 |
$selected = self::checkSelected($opt, $value); |
| 91 |
$val = isset($opt->val) ? $opt->val : $opt->lbl; |
| 92 |
$optionsHTML .= <<<OPT |
| 93 |
<option |
| 94 |
{$fieldHelpers->getCustomAttributes('slct-optn')} |
| 95 |
class="{$fieldHelpers->getAtomicCls('slct-optn')} {$fieldHelpers->getCustomClasses('slct-optn')}" |
| 96 |
value="{$fieldHelpers->esc_attr($val)}" |
| 97 |
{$selected} |
| 98 |
{$disabled} |
| 99 |
> |
| 100 |
{$fieldHelpers->kses_post($opt->lbl)} |
| 101 |
</option> |
| 102 |
OPT; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return <<<HTMLSELECTFIELD |
| 108 |
<div |
| 109 |
{$fieldHelpers->getCustomAttributes('inp-fld-wrp')} |
| 110 |
class="{$fieldHelpers->getAtomicCls('inp-fld-wrp')} {$fieldHelpers->getCustomClasses('inp-fld-wrp')}" |
| 111 |
> |
| 112 |
<select |
| 113 |
{$fieldHelpers->getCustomAttributes('fld')} |
| 114 |
id="{$rowID}-{$contentCount}" |
| 115 |
class="{$fieldHelpers->getAtomicCls('fld')} {$fieldHelpers->getCustomClasses('fld')} {$readonlyCls}" |
| 116 |
{$readonly} |
| 117 |
{$disabled} |
| 118 |
{$name} |
| 119 |
value="{$fieldHelpers->esc_attr($value)}" |
| 120 |
> |
| 121 |
{$phHide} |
| 122 |
{$optionsHTML} |
| 123 |
</select> |
| 124 |
</div> |
| 125 |
HTMLSELECTFIELD; |
| 126 |
} |
| 127 |
} |
| 128 |
|