| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend\Form\View\Theme\Fields; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Util\FrontendHelpers; |
| 6 |
|
| 7 |
class RatingField |
| 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, $form_atomic_Cls_map, $value); |
| 13 |
return $inputWrapper->wrapper($input); |
| 14 |
} |
| 15 |
|
| 16 |
private static function field($field, $rowID, $form_atomic_Cls_map, $value) |
| 17 |
{ |
| 18 |
$fieldHelpers = new FieldHelpers($field, $rowID, $form_atomic_Cls_map); |
| 19 |
|
| 20 |
$name = $fieldHelpers->name(); |
| 21 |
$req = $fieldHelpers->required(); |
| 22 |
|
| 23 |
$bfFrontendFormIds = FrontendHelpers::$bfFrontendFormIds; |
| 24 |
$contentCount = count($bfFrontendFormIds); |
| 25 |
|
| 26 |
$ratingOption = ''; |
| 27 |
|
| 28 |
$checkedIndex = ''; |
| 29 |
|
| 30 |
if (isset($field->opt)) { |
| 31 |
$defaultValue = isset($field->val) ? $field->val : ''; |
| 32 |
foreach ($field->opt as $key => $opt) { |
| 33 |
if ($defaultValue && $defaultValue === $opt->val) { |
| 34 |
$checkedIndex = $key; |
| 35 |
} elseif (isset($opt->check) && true === $opt->check) { |
| 36 |
$checkedIndex = $key; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
$checkedCls = null; |
| 41 |
|
| 42 |
foreach ($field->opt as $key => $opt) { |
| 43 |
$val = $opt->val; |
| 44 |
$img = $opt->img; |
| 45 |
$lbl = $opt->lbl; |
| 46 |
$checked = ''; |
| 47 |
|
| 48 |
// if ($defaultValue && $opt->val === $defaultValue) { |
| 49 |
// $checked = "checked='checked'"; |
| 50 |
// } elseif (isset($opt->check) && true === $opt->check) { |
| 51 |
// $checked = "checked='{$opt->check}'"; |
| 52 |
// } else { |
| 53 |
// $checked = ''; |
| 54 |
// } |
| 55 |
|
| 56 |
if ($defaultValue ) { |
| 57 |
if ($opt->val === $defaultValue) |
| 58 |
$checked = "checked='1'"; |
| 59 |
} else { |
| 60 |
if (isset($opt->check) && true === $opt->check) { |
| 61 |
$checked = "checked='{$opt->check}'"; |
| 62 |
} else { |
| 63 |
$checked = ''; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
if (!empty($checkedIndex) && $key <= $checkedIndex) { |
| 68 |
$checkedCls = $rowID . '-rating-selected'; |
| 69 |
} else { |
| 70 |
$checkedCls = null; |
| 71 |
} |
| 72 |
$ratingOption .= <<<RATINGOPTION |
| 73 |
<label |
| 74 |
class="{$fieldHelpers->getAtomicCls('rating-lbl')} {$fieldHelpers->getCustomClasses('rating-lbl')}" |
| 75 |
for="{$rowID}-{$contentCount}-rating-{$key}" |
| 76 |
{$fieldHelpers->getCustomAttributes('rating-lbl')} |
| 77 |
data-indx="{$key}" |
| 78 |
> |
| 79 |
<input |
| 80 |
type="radio" |
| 81 |
class="{$fieldHelpers->getAtomicCls('rating-input')} {$fieldHelpers->getCustomClasses('rating-input')}" |
| 82 |
{$name} |
| 83 |
value={$val} |
| 84 |
aria-label="{$lbl}" |
| 85 |
id="{$rowID}-{$contentCount}-rating-{$key}" |
| 86 |
{$checked} |
| 87 |
{$req} |
| 88 |
{$fieldHelpers->getCustomAttributes('rating-input')} |
| 89 |
/> |
| 90 |
<img |
| 91 |
class="{$fieldHelpers->getAtomicCls('rating-img')} {$fieldHelpers->getCustomClasses('rating-img')} {$checkedCls}" |
| 92 |
src="{$img}" |
| 93 |
alt="{$lbl}" |
| 94 |
aria-label="{$lbl}" |
| 95 |
{$fieldHelpers->getCustomAttributes('rating-img')} |
| 96 |
/> |
| 97 |
</label> |
| 98 |
RATINGOPTION; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return <<<RATINGFIELD |
| 103 |
<div |
| 104 |
{$fieldHelpers->getCustomAttributes('inp-fld-wrp')} |
| 105 |
class="{$fieldHelpers->getAtomicCls('inp-fld-wrp')} {$fieldHelpers->getCustomClasses('inp-fld-wrp')}" |
| 106 |
> |
| 107 |
<div |
| 108 |
{$fieldHelpers->getCustomAttributes('rating-wrp')} |
| 109 |
class="{$fieldHelpers->getAtomicCls('rating-wrp')} {$fieldHelpers->getCustomClasses('rating-wrp')}" |
| 110 |
tabindex="0" |
| 111 |
> |
| 112 |
{$ratingOption} |
| 113 |
</div> |
| 114 |
<span |
| 115 |
class="{$fieldHelpers->getAtomicCls('rating-msg')} {$fieldHelpers->getCustomClasses('rating-msg')}" |
| 116 |
{$fieldHelpers->getCustomAttributes('rating-msg')} |
| 117 |
> |
| 118 |
</span> |
| 119 |
</div> |
| 120 |
RATINGFIELD; |
| 121 |
} |
| 122 |
} |