| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend\Form\View\Theme\Fields; |
| 4 |
|
| 5 |
class CurrencyField |
| 6 |
{ |
| 7 |
public static function init($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error = null, $value = '') |
| 8 |
{ |
| 9 |
$inputWrapper = new ClassicInputWrapper($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error, $value); |
| 10 |
$input = self::field($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error, $value); |
| 11 |
return $inputWrapper->wrapper($input); |
| 12 |
} |
| 13 |
|
| 14 |
private static function field($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error = null, $value = '') |
| 15 |
{ |
| 16 |
$fh = new ClassicFieldHelpers($field, $rowID, $form_atomic_Cls_map); |
| 17 |
$img_url = BITFORMS_ASSET_URI . '/../static/currencies/'; |
| 18 |
|
| 19 |
$req = $fh->required(); |
| 20 |
$disabled = $fh->disabled(); |
| 21 |
$readonly = $fh->readonly(); |
| 22 |
$name = $fh->name(); |
| 23 |
$ph = $fh->placeholder(); |
| 24 |
$ariaDescribedBy = $fh->ariaDescribedBy(); |
| 25 |
$ariaRequired = $fh->ariaRequired(); |
| 26 |
// Get dynamic label ID to properly link the combobox to its label for screen readers |
| 27 |
$labelId = $fh->getLabelId(); |
| 28 |
$selectedFlagImage = ''; |
| 29 |
$tabIndx = isset($field->disabled) ? -1 : 0; |
| 30 |
$selectedCurrencyClearable = ''; |
| 31 |
$searchPlaceholder = ''; |
| 32 |
$searchClearable = ''; |
| 33 |
$options = ''; |
| 34 |
$readonlyCls = isset($field->readonly) ? 'readonly' : ''; |
| 35 |
$disabledCls = isset($field->disabled) ? 'disabled' : ''; |
| 36 |
$value = is_string($value) ? $value : ''; |
| 37 |
$numValue = preg_replace('/[^0-9.-]/', '', $value); |
| 38 |
|
| 39 |
$img = esc_url(includes_url('images/blank.gif')); |
| 40 |
|
| 41 |
if ($fh->property_exists_nested($field, 'config->selectedFlagImage', true)) { |
| 42 |
$selectedFlagImage = sprintf( |
| 43 |
'<div class="%1$s"> |
| 44 |
<img |
| 45 |
%2$s |
| 46 |
class="%3$s %4$s" |
| 47 |
aria-hidden="true" |
| 48 |
alt="selected country flag" |
| 49 |
src="%5$s" |
| 50 |
/> |
| 51 |
</div>', |
| 52 |
$fh->getAtomicCls('selected-currency-wrp'), |
| 53 |
$fh->getCustomAttributes('selected-currency-img'), |
| 54 |
$fh->getAtomicCls('selected-currency-img'), |
| 55 |
$fh->getCustomClasses('selected-currency-img'), |
| 56 |
$img |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
if ($fh->property_exists_nested($field, 'config->selectedCurrencyClearable', true)) { |
| 61 |
$selectedCurrencyClearable = sprintf( |
| 62 |
'<button |
| 63 |
%1$s |
| 64 |
type="button" |
| 65 |
title="Clear selected currency value" |
| 66 |
class="%2$s %3$s" |
| 67 |
> |
| 68 |
<svg |
| 69 |
width="12" |
| 70 |
height="12" |
| 71 |
role="img" |
| 72 |
title="Cross icon" |
| 73 |
viewBox="0 0 24 24" |
| 74 |
fill="none" |
| 75 |
stroke="currentColor" |
| 76 |
stroke-width="2" |
| 77 |
stroke-linecap="round" |
| 78 |
stroke-linejoin="round" |
| 79 |
> |
| 80 |
<line x1="18" y1="6" x2="6" y2="18" /> |
| 81 |
<line x1="6" y1="6" x2="18" y2="18" /> |
| 82 |
</svg> |
| 83 |
</button>', |
| 84 |
$fh->getCustomAttributes('input-clear-btn'), |
| 85 |
$fh->getAtomicCls('input-clear-btn'), |
| 86 |
$fh->getCustomClasses('input-clear-btn') |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
if ($fh->property_exists_nested($field, 'config->searchPlaceholder', '', 1)) { |
| 91 |
$searchPlaceholder = "placeholder='{$fh->esc_attr($field->config->searchPlaceholder)}'"; |
| 92 |
} |
| 93 |
|
| 94 |
if ($fh->property_exists_nested($field, 'config->searchClearable', true)) { |
| 95 |
$searchClearable = sprintf( |
| 96 |
'<button |
| 97 |
%1$s |
| 98 |
type="button" |
| 99 |
title="Clear search" |
| 100 |
class="%2$s %3$s" |
| 101 |
tabIndex="-1" |
| 102 |
> |
| 103 |
<svg |
| 104 |
width="12" |
| 105 |
height="12" |
| 106 |
role="img" |
| 107 |
title="Cross icon" |
| 108 |
viewBox="0 0 24 24" |
| 109 |
fill="none" |
| 110 |
stroke="currentColor" |
| 111 |
stroke-width="2" |
| 112 |
stroke-linecap="round" |
| 113 |
stroke-linejoin="round" |
| 114 |
> |
| 115 |
<line x1="18" y1="6" x2="6" y2="18" /> |
| 116 |
<line x1="6" y1="6" x2="18" y2="18" /> |
| 117 |
</svg> |
| 118 |
</button>', |
| 119 |
$fh->getCustomAttributes('search-clear-btn'), |
| 120 |
$fh->getAtomicCls('search-clear-btn'), |
| 121 |
$fh->getCustomClasses('search-clear-btn') |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
// Container wrapper |
| 126 |
$html = sprintf( |
| 127 |
'<div class="%1$s"> |
| 128 |
<div |
| 129 |
%2$s |
| 130 |
class="%3$s %4$s %5$s %6$s" |
| 131 |
>', |
| 132 |
$fh->getAtomicCls('currency-fld-container'), |
| 133 |
$fh->getCustomAttributes('currency-fld-wrp'), |
| 134 |
$fh->getAtomicCls('currency-fld-wrp'), |
| 135 |
$fh->getCustomClasses('currency-fld-wrp'), |
| 136 |
$disabled, |
| 137 |
$readonly |
| 138 |
); |
| 139 |
|
| 140 |
// Hidden input |
| 141 |
$html .= sprintf( |
| 142 |
'<input |
| 143 |
%1$s |
| 144 |
%2$s |
| 145 |
%3$s |
| 146 |
%4$s |
| 147 |
%5$s |
| 148 |
type="text" |
| 149 |
title="Currency Hidden Input" |
| 150 |
class="%6$s d-none" |
| 151 |
%7$s |
| 152 |
%8$s |
| 153 |
value="%9$s" |
| 154 |
/>', |
| 155 |
$fh->getCustomAttributes('currency-hidden-input'), |
| 156 |
$name, |
| 157 |
$req, |
| 158 |
$ariaRequired, |
| 159 |
$ariaDescribedBy, |
| 160 |
$fh->getAtomicCls('currency-hidden-input'), |
| 161 |
$disabled, |
| 162 |
$readonly, |
| 163 |
$fh->esc_attr($value) |
| 164 |
); |
| 165 |
|
| 166 |
// Currency inner wrapper with dropdown |
| 167 |
$html .= sprintf( |
| 168 |
'<div class="%1$s"> |
| 169 |
<div |
| 170 |
%2$s |
| 171 |
class="%3$s %4$s" |
| 172 |
role="combobox" |
| 173 |
aria-controls="currency-dropdown" |
| 174 |
aria-haspopup="listbox" |
| 175 |
aria-live="assertive" |
| 176 |
aria-labelledby="{$labelId}" |
| 177 |
aria-expanded="false" |
| 178 |
tabIndex="%5$s" |
| 179 |
> |
| 180 |
%6$s |
| 181 |
<div class="%7$s"> |
| 182 |
<svg |
| 183 |
width="15" |
| 184 |
height="15" |
| 185 |
role="img" |
| 186 |
title="Downarrow icon" |
| 187 |
viewBox="0 0 24 24" |
| 188 |
fill="none" |
| 189 |
stroke="currentColor" |
| 190 |
stroke-width="2" |
| 191 |
stroke-linecap="round" |
| 192 |
stroke-linejoin="round" |
| 193 |
> |
| 194 |
<polyline points="6 9 12 15 18 9" /> |
| 195 |
</svg> |
| 196 |
</div> |
| 197 |
</div>', |
| 198 |
$fh->getAtomicCls('currency-inner-wrp'), |
| 199 |
$fh->getCustomAttributes('dpd-wrp'), |
| 200 |
$fh->getAtomicCls('dpd-wrp'), |
| 201 |
$fh->getCustomClasses('dpd-wrp'), |
| 202 |
$tabIndx, |
| 203 |
$selectedFlagImage, |
| 204 |
$fh->getAtomicCls('dpd-down-btn') |
| 205 |
); |
| 206 |
|
| 207 |
// Currency amount input |
| 208 |
$html .= sprintf( |
| 209 |
'<input |
| 210 |
%1$s |
| 211 |
aria-label="Currency Input" |
| 212 |
type="text" |
| 213 |
class="%2$s %3$s" |
| 214 |
%4$s |
| 215 |
tabIndex="%5$s" |
| 216 |
data-num-value="%6$s" |
| 217 |
/> |
| 218 |
%7$s |
| 219 |
</div>', |
| 220 |
$fh->getCustomAttributes('currency-amount-input'), |
| 221 |
$fh->getAtomicCls('currency-amount-input'), |
| 222 |
$fh->getCustomClasses('currency-amount-input'), |
| 223 |
$ph, |
| 224 |
$tabIndx, |
| 225 |
$numValue, |
| 226 |
$selectedCurrencyClearable |
| 227 |
); |
| 228 |
|
| 229 |
// Option wrapper with search |
| 230 |
$html .= sprintf( |
| 231 |
'<div |
| 232 |
%1$s |
| 233 |
class="%2$s %3$s" |
| 234 |
> |
| 235 |
<div class="%4$s"> |
| 236 |
<div |
| 237 |
%5$s |
| 238 |
class="%6$s %7$s" |
| 239 |
> |
| 240 |
<input |
| 241 |
%8$s |
| 242 |
type="search" |
| 243 |
class="%9$s %10$s" |
| 244 |
%11$s |
| 245 |
autoComplete="off" |
| 246 |
tabIndex="-1" |
| 247 |
/> |
| 248 |
<svg |
| 249 |
%12$s |
| 250 |
class="%13$s %14$s" |
| 251 |
aria-hidden="true" |
| 252 |
width="22" |
| 253 |
height="22" |
| 254 |
viewBox="0 0 24 24" |
| 255 |
fill="none" |
| 256 |
stroke="currentColor" |
| 257 |
stroke-width="2" |
| 258 |
stroke-linecap="round" |
| 259 |
stroke-linejoin="round" |
| 260 |
> |
| 261 |
<circle cx="11" cy="11" r="8" /> |
| 262 |
<line x1="21" y1="21" x2="16.65" y2="16.65" /> |
| 263 |
</svg> |
| 264 |
%15$s |
| 265 |
</div>', |
| 266 |
$fh->getCustomAttributes('option-wrp'), // 1 |
| 267 |
$fh->getAtomicCls('option-wrp'), // 2 |
| 268 |
$fh->getCustomClasses('option-wrp'), // 3 |
| 269 |
$fh->getAtomicCls('option-inner-wrp'), // 4 |
| 270 |
$fh->getCustomAttributes('option-search-wrp'), // 5 |
| 271 |
$fh->getAtomicCls('option-search-wrp'), // 6 |
| 272 |
$fh->getCustomClasses('option-search-wrp'), // 7 |
| 273 |
$fh->getCustomAttributes('opt-search-input'), // 8 |
| 274 |
$fh->getAtomicCls('opt-search-input'), // 9 |
| 275 |
$fh->getCustomClasses('opt-search-input'), // 10 |
| 276 |
$searchPlaceholder, // 11 |
| 277 |
$fh->getCustomAttributes('opt-search-icn'), // 12 |
| 278 |
$fh->getAtomicCls('opt-search-icn'), // 13 |
| 279 |
$fh->getCustomClasses('opt-search-icn'), // 14 |
| 280 |
$searchClearable // 15 |
| 281 |
); |
| 282 |
|
| 283 |
// Option list and closing tags |
| 284 |
$html .= sprintf( |
| 285 |
'<ul |
| 286 |
%1$s |
| 287 |
class="%2$s %3$s" |
| 288 |
tabIndex="-1" |
| 289 |
role="listbox" |
| 290 |
aria-label="currency list" |
| 291 |
> |
| 292 |
%4$s |
| 293 |
</ul> |
| 294 |
</div> |
| 295 |
</div> |
| 296 |
</div> |
| 297 |
</div>', |
| 298 |
$fh->getCustomAttributes('option-list'), |
| 299 |
$fh->getAtomicCls('option-list'), |
| 300 |
$fh->getCustomClasses('option-list'), |
| 301 |
$options |
| 302 |
); |
| 303 |
|
| 304 |
return $html; |
| 305 |
} |
| 306 |
} |
| 307 |
|