PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / V3.3.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder vV3.3.2
V3.3.2 3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 All 139 releases
bit-form / includes / Frontend / Form / View / Theme / Fields / SliderField.php

SliderField.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder V3.3.2, at includes/Frontend/Form/View/Theme/Fields/SliderField.php

131 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Frontend\Form\View\Theme\Fields;
4
5 use BitCode\BitForm\Core\Util\FieldValueHandler;
6 use BitCode\BitForm\Core\Util\FrontendHelpers;
7
8 class SliderField
9 {
10 public static function init($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error = null, $value = null)
11 {
12 $inputWrapper = new ClassicInputWrapper($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 field($field, $rowID, $form_atomic_Cls_map, $value)
18 {
19 $fieldHelpers = new ClassicFieldHelpers($field, $rowID, $form_atomic_Cls_map);
20
21 $prefixIcn = $fieldHelpers->icon('prefixIcn', 'pre-i');
22 $suffixIcn = $fieldHelpers->icon('suffixIcn', 'suf-i');
23 $sugg = '';
24 $req = $fieldHelpers->required();
25 $disabled = $fieldHelpers->disabled();
26 $readonly = $fieldHelpers->readonly();
27 $inputMode = '';
28 $name = $fieldHelpers->name();
29 $ac = $fieldHelpers->autocomplete();
30 $mx = '';
31 $mn = '';
32 $step = '';
33 $ph = $fieldHelpers->placeholder();
34 $value = $fieldHelpers->value();
35 $ariaDescribedBy = $fieldHelpers->ariaDescribedBy();
36 $ariaRequired = $fieldHelpers->ariaRequired();
37 $list = '';
38 $bfFrontendFormIds = FrontendHelpers::$bfFrontendFormIds;
39 $contentCount = count($bfFrontendFormIds);
40
41 if (is_object($field) && property_exists($field, 'suggestions') && is_countable($field->suggestions) && count($field->suggestions) > 0) {
42 $list = "list='{$rowID}-{$contentCount}-datalist'";
43 $sugg .= "<datalist id='{$rowID}-{$contentCount}-datalist'>";
44 foreach ($field->suggestions as $suggestion) {
45 $val = (isset($suggestion->val) && !empty($suggestion->val)) ? $suggestion->val : $suggestion->lbl;
46 $sugg .= "<option value='{$fieldHelpers->esc_attr($val)}'>{$fieldHelpers->esc_html($suggestion->lbl)}</option>";
47 }
48 $sugg .= '</datalist>';
49 }
50
51 if (property_exists($field, 'mn') && '' !== $field->mn) {
52 $mn = "min='{$fieldHelpers->esc_attr($field->mn)}'";
53 }
54
55 if (property_exists($field, 'mx') && '' !== $field->mx) {
56 $mx = "max='{$fieldHelpers->esc_attr($field->mx)}'";
57 }
58
59 if (property_exists($field, 'step') && '' !== $field->step) {
60 $step = "step='{$fieldHelpers->esc_attr($field->step)}'";
61 }
62
63 if (property_exists($field, 'inputMode') && '' !== $field->inputMode) {
64 $inputMode = "inputMode='{$fieldHelpers->esc_attr($field->inputMode)}'";
65 }
66
67 $minValue = self::numericOr(property_exists($field, 'mn') ? $field->mn : null, 0);
68 $maxValue = self::numericOr(property_exists($field, 'mx') ? $field->mx : null, 100);
69
70 $defaultVal = (($maxValue - $minValue) / 2) + $minValue;
71 // The builder used to seed Default value with the field label ("Slider"), so a stored value
72 // or default can be non-numeric; arithmetic on it fatals on PHP 8. Fall back to the minimum.
73 if ($fieldHelpers->property_exists_nested($field, 'val', '', 1)) {
74 $defaultVal = self::numericOr($field->val, $minValue);
75 $value = "value='{$fieldHelpers->esc_attr($defaultVal)}'";
76 } elseif ($fieldHelpers->property_exists_nested($field, 'defaultValue', '', 1)) {
77 $defaultVal = self::numericOr(FieldValueHandler::replaceSmartTagWithValue($field->defaultValue), $minValue);
78 $value = "value='{$fieldHelpers->esc_attr($defaultVal)}'";
79 }
80
81 // Min equal to max leaves no track to fill; avoid a division by zero.
82 $range = $maxValue - $minValue;
83 $lowerTrackPercentage = 0.0 !== (float) $range ? ($defaultVal - $minValue) / $range * 100 : 0;
84
85 $wrpperStyle = "style='--bfv-fld-val: \"" . $fieldHelpers->esc_attr($defaultVal) . "\";'";
86 $inputStyle = "style='--bfv-fill-lower-track: " . $fieldHelpers->esc_attr($lowerTrackPercentage) . "% !important;'";
87
88 return ' <div
89 ' . $fieldHelpers->getCustomAttributes('inp-fld-wrp') . '
90 class="' . $fieldHelpers->getAtomicCls('inp-fld-wrp') . ' ' . $fieldHelpers->getCustomClasses('inp-fld-wrp') . '"
91 ' . $wrpperStyle . '
92 >
93 <input
94 ' . $fieldHelpers->getCustomAttributes('fld') . '
95 id="' . $rowID . '-' . $contentCount . '"
96 ' . $list . '
97 class="' . $fieldHelpers->getAtomicCls('fld') . ' ' . $fieldHelpers->getCustomClasses('fld') . '"
98 type="' . $fieldHelpers->esc_attr($field->typ) . '"
99 ' . $req . '
100 ' . $ariaRequired . '
101 ' . $ariaDescribedBy . '
102 ' . $disabled . '
103 ' . $readonly . '
104 ' . $ph . '
105 ' . $mn . '
106 ' . $mx . '
107 ' . $step . '
108 ' . $ac . '
109 ' . $inputMode . '
110 ' . $name . '
111 ' . $value . '
112 ' . $inputStyle . '
113 />
114 ' . $prefixIcn . '
115 ' . $suffixIcn . '
116 <span class="' . $fieldHelpers->getAtomicCls('slider-val') . '">' . esc_html__('Value:', 'bit-form') . ' </span>
117 </div>
118 ' . $sugg . "\n";
119 }
120
121 /**
122 * @param mixed $value
123 * @param int|float $fallback
124 * @return int|float
125 */
126 private static function numericOr($value, $fallback)
127 {
128 return is_numeric($value) ? $value + 0 : $fallback;
129 }
130 }
131