| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend\Form\View\Theme\Fields; |
| 4 |
|
| 5 |
use BitCode\BitForm\Frontend\Form\View\Theme\Fields\FieldHelpers; |
| 6 |
|
| 7 |
class CountryField |
| 8 |
{ |
| 9 |
public static function init($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error = null, $value = null) |
| 10 |
{ |
| 11 |
$inputWrapper = new InputWrapper($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error, $value); |
| 12 |
$input = self::field($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error, $value); |
| 13 |
return $inputWrapper->wrapper($input); |
| 14 |
} |
| 15 |
|
| 16 |
private static function field($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error = null, $value = null) |
| 17 |
{ |
| 18 |
$fieldHelpers = new FieldHelpers($field, $rowID, $form_atomic_Cls_map); |
| 19 |
$asset_url = BITFORMS_ASSET_URI; |
| 20 |
$img_url = BITFORMS_ASSET_URI . '/../static/countries/'; |
| 21 |
$req = $fieldHelpers->required(); |
| 22 |
$disabled = $fieldHelpers->disabled(); |
| 23 |
$readonly = $fieldHelpers->readonly(); |
| 24 |
$name = $fieldHelpers->name(); |
| 25 |
$selectedFlagImage = ''; |
| 26 |
$tabIndx = isset($field->disabled) ? -1 : 0; |
| 27 |
$selectedCountryClearable = ''; |
| 28 |
$searchPlaceholder = ''; |
| 29 |
$searchClearable = ''; |
| 30 |
$options = ''; |
| 31 |
$readonlyCls = isset($field->readonly) ? 'readonly' : ''; |
| 32 |
$disabledCls = isset($field->disabled) ? 'disabled' : ''; |
| 33 |
$val = $fieldHelpers->value(); |
| 34 |
|
| 35 |
$selectedItm = null; |
| 36 |
foreach ($field->options as $opt) { |
| 37 |
if (isset($opt->check) && true === $opt->check) { |
| 38 |
$selectedItm = $opt; |
| 39 |
break; |
| 40 |
} |
| 41 |
} |
| 42 |
if ($selectedItm) { |
| 43 |
$img = $img_url . $selectedItm->img; |
| 44 |
$ph = $selectedItm->lbl; |
| 45 |
} else { |
| 46 |
$img = htmlentities("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>"); |
| 47 |
$ph = isset($field->ph) ? $field->ph : ''; |
| 48 |
} |
| 49 |
|
| 50 |
if ($fieldHelpers->property_exists_nested($field, 'config->selectedFlagImage', true)) { |
| 51 |
$img = <<<FLAGIMAGE |
| 52 |
<img |
| 53 |
{$fieldHelpers->getCustomAttributes('selected-country-img')} |
| 54 |
class="{$fieldHelpers->getAtomicCls('selected-country-img')} {$fieldHelpers->getCustomClasses('selected-country-img')}" |
| 55 |
aria-hidden="true" |
| 56 |
alt="selected country flag" |
| 57 |
src="{$img}" |
| 58 |
> |
| 59 |
FLAGIMAGE; |
| 60 |
} else { |
| 61 |
$img = ''; |
| 62 |
} |
| 63 |
|
| 64 |
$selectedFlagImage = <<<SELECTEDOPTLABEL |
| 65 |
<div class="{$fieldHelpers->getAtomicCls('selected-country-wrp')}"> |
| 66 |
{$img} |
| 67 |
<span |
| 68 |
{$fieldHelpers->getCustomAttributes('selected-country-lbl')} |
| 69 |
class="{$fieldHelpers->getAtomicCls('selected-country-lbl')} {$fieldHelpers->getCustomClasses('selected-country-lbl')}" |
| 70 |
> |
| 71 |
{$fieldHelpers->esc_html($ph)} |
| 72 |
</span> |
| 73 |
</div> |
| 74 |
SELECTEDOPTLABEL; |
| 75 |
|
| 76 |
if ($fieldHelpers->property_exists_nested($field, 'config->selectedCountryClearable', true)) { |
| 77 |
$selectedCountryClearable = <<<CLEARABLE |
| 78 |
<button |
| 79 |
{$fieldHelpers->getCustomAttributes('inp-clr-btn')} |
| 80 |
type="button" |
| 81 |
title="Clear selected country value" |
| 82 |
class="{$fieldHelpers->getAtomicCls('inp-clr-btn')} {$fieldHelpers->getCustomClasses('inp-clr-btn')}" |
| 83 |
> |
| 84 |
<svg |
| 85 |
width="15" |
| 86 |
height="15" |
| 87 |
role="img" |
| 88 |
title="Cross icon" |
| 89 |
viewBox="0 0 24 24" |
| 90 |
fill="none" |
| 91 |
stroke="currentColor" |
| 92 |
stroke-width="2" |
| 93 |
stroke-linecap="round" |
| 94 |
stroke-linejoin="round" |
| 95 |
> |
| 96 |
<line x1="18" y1="6" x2="6" y2="18" /> |
| 97 |
<line x1="6" y1="6" x2="18" y2="18" /> |
| 98 |
</svg> |
| 99 |
</button> |
| 100 |
CLEARABLE; |
| 101 |
} |
| 102 |
|
| 103 |
if ($fieldHelpers->property_exists_nested($field, 'config->searchPlaceholder', '', 1)) { |
| 104 |
$searchPlaceholder = "placeholder='{$fieldHelpers->esc_attr($field->config->searchPlaceholder)}'"; |
| 105 |
} |
| 106 |
|
| 107 |
if ($fieldHelpers->property_exists_nested($field, 'config->searchClearable', true)) { |
| 108 |
$searchClearable = <<<CLEARABLE |
| 109 |
<button |
| 110 |
{$fieldHelpers->getCustomAttributes('search-clear-btn')} |
| 111 |
type="button" |
| 112 |
title="Clear search" |
| 113 |
class="{$fieldHelpers->getAtomicCls('icn')} {$fieldHelpers->getAtomicCls('search-clear-btn')} {$fieldHelpers->getCustomClasses('search-clear-btn')}" |
| 114 |
tabIndex="-1" |
| 115 |
> |
| 116 |
<svg |
| 117 |
width="13" |
| 118 |
height="13" |
| 119 |
role="img" |
| 120 |
title="Cross icon" |
| 121 |
viewBox="0 0 24 24" |
| 122 |
fill="none" |
| 123 |
stroke="currentColor" |
| 124 |
stroke-width="2" |
| 125 |
stroke-linecap="round" |
| 126 |
stroke-linejoin="round" |
| 127 |
> |
| 128 |
<line x1="18" y1="6" x2="6" y2="18" /> |
| 129 |
<line x1="6" y1="6" x2="18" y2="18" /> |
| 130 |
</svg> |
| 131 |
</button> |
| 132 |
CLEARABLE; |
| 133 |
} |
| 134 |
|
| 135 |
return <<<COUNTRYFIELD |
| 136 |
<div class="{$fieldHelpers->getAtomicCls('country-fld-container')}"> |
| 137 |
<div |
| 138 |
{$fieldHelpers->getCustomAttributes('country-fld-wrp')} |
| 139 |
class="{$fieldHelpers->getAtomicCls('country-fld-wrp')} {$fieldHelpers->getCustomClasses('country-fld-wrp')} {$disabled} {$readonly}" |
| 140 |
> |
| 141 |
<input |
| 142 |
{$name} |
| 143 |
{$req} |
| 144 |
type="text" |
| 145 |
title="Country Hidden Input" |
| 146 |
class="{$fieldHelpers->getAtomicCls('country-hidden-input')} d-none" |
| 147 |
{$disabled} |
| 148 |
{$readonly} |
| 149 |
{$val} |
| 150 |
/> |
| 151 |
<div |
| 152 |
class="{$fieldHelpers->getAtomicCls('dpd-wrp')}" |
| 153 |
aria-live="assertive" |
| 154 |
aria-label="Select a Country" |
| 155 |
role="combobox" |
| 156 |
aria-expanded="false" |
| 157 |
tabIndex={$tabIndx} |
| 158 |
> |
| 159 |
{$selectedFlagImage} |
| 160 |
|
| 161 |
<div class="{$fieldHelpers->getAtomicCls('dpd-btn-wrp')}"> |
| 162 |
{$selectedCountryClearable} |
| 163 |
<div class="{$fieldHelpers->getAtomicCls('dpd-down-btn')}"> |
| 164 |
<svg |
| 165 |
width="15" |
| 166 |
height="15" |
| 167 |
viewBox="0 0 24 24" |
| 168 |
title="Cross icon" |
| 169 |
role="img" |
| 170 |
fill="none" |
| 171 |
stroke="currentColor" |
| 172 |
stroke-width="2" |
| 173 |
stroke-linecap="round" |
| 174 |
stroke-linejoin="round" |
| 175 |
> |
| 176 |
<polyline points="6 9 12 15 18 9" /> |
| 177 |
</svg> |
| 178 |
</div> |
| 179 |
</div> |
| 180 |
</div> |
| 181 |
<div |
| 182 |
{$fieldHelpers->getCustomAttributes('option-wrp')} |
| 183 |
class="{$fieldHelpers->getAtomicCls('option-wrp')} {$fieldHelpers->getCustomClasses('option-wrp')}" |
| 184 |
> |
| 185 |
<div class="{$fieldHelpers->getAtomicCls('option-inner-wrp')}"> |
| 186 |
<div class="{$fieldHelpers->getAtomicCls('option-search-wrp')}"> |
| 187 |
<input |
| 188 |
{$fieldHelpers->getCustomAttributes('opt-search-input')} |
| 189 |
type="search" |
| 190 |
class="{$fieldHelpers->getAtomicCls('opt-search-input')} {$fieldHelpers->getCustomClasses('opt-search-input')}" |
| 191 |
{$searchPlaceholder} |
| 192 |
autoComplete="country-name" |
| 193 |
tabIndex="-1" |
| 194 |
/> |
| 195 |
<svg |
| 196 |
{$fieldHelpers->getCustomAttributes('opt-search-icn')} |
| 197 |
class="{$fieldHelpers->getAtomicCls('opt-search-icn')} {$fieldHelpers->getCustomClasses('opt-search-icn')}" |
| 198 |
aria-hidden="true" |
| 199 |
width="22" |
| 200 |
height="22" |
| 201 |
role="img" |
| 202 |
title="Search icon" |
| 203 |
viewBox="0 0 24 24" |
| 204 |
fill="none" |
| 205 |
stroke="currentColor" |
| 206 |
stroke-width="2" |
| 207 |
stroke-linecap="round" |
| 208 |
stroke-linejoin="round" |
| 209 |
> |
| 210 |
<circle cx="11" cy="11" r="8" /> |
| 211 |
<line x1="21" y1="21" x2="16.65" y2="16.65" /> |
| 212 |
</svg> |
| 213 |
{$searchClearable} |
| 214 |
</div> |
| 215 |
<ul |
| 216 |
{$fieldHelpers->getCustomAttributes('option-list')} |
| 217 |
class="{$fieldHelpers->getAtomicCls('option-list')} {$fieldHelpers->getCustomClasses('option-list')}" |
| 218 |
tabIndex="-1" |
| 219 |
role="listbox" |
| 220 |
aria-label="country list" |
| 221 |
> |
| 222 |
{$options} |
| 223 |
</ul> |
| 224 |
</div> |
| 225 |
</div> |
| 226 |
</div> |
| 227 |
</div> |
| 228 |
COUNTRYFIELD; |
| 229 |
} |
| 230 |
} |
| 231 |
|