| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\FormBuilder\Components; |
| 4 |
|
| 5 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 6 |
|
| 7 |
class Checkable extends BaseComponent |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Compile and echo the html element |
| 11 |
* |
| 12 |
* @param array $data [element data] |
| 13 |
* @param \stdClass $form [Form Object] |
| 14 |
* |
| 15 |
* @return void |
| 16 |
*/ |
| 17 |
public function compile($data, $form) |
| 18 |
{ |
| 19 |
$elementName = $data['element']; |
| 20 |
|
| 21 |
$data = apply_filters('fluentform_rendering_field_data_' . $elementName, $data, $form); |
| 22 |
|
| 23 |
$data['attributes']['class'] = trim( |
| 24 |
'ff-el-form-check-input ' . |
| 25 |
'ff-el-form-check-' . $data['attributes']['type'] . ' ' . |
| 26 |
ArrayHelper::get($data, 'attributes.class') |
| 27 |
); |
| 28 |
|
| 29 |
if ('checkbox' == $data['attributes']['type']) { |
| 30 |
$data['attributes']['name'] = $data['attributes']['name'] . '[]'; |
| 31 |
} |
| 32 |
|
| 33 |
$defaultValues = (array) $this->extractValueFromAttributes($data); |
| 34 |
|
| 35 |
if ($dynamicValues = $this->extractDynamicValues($data, $form)) { |
| 36 |
$defaultValues = $dynamicValues; |
| 37 |
} |
| 38 |
|
| 39 |
$elMarkup = ''; |
| 40 |
|
| 41 |
$firstTabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex(); |
| 42 |
|
| 43 |
if (! $formattedOptions = ArrayHelper::get($data, 'settings.advanced_options')) { |
| 44 |
$options = ArrayHelper::get($data, 'options', []); |
| 45 |
$formattedOptions = []; |
| 46 |
foreach ($options as $value => $label) { |
| 47 |
$formattedOptions[] = [ |
| 48 |
'label' => $label, |
| 49 |
'value' => $value, |
| 50 |
'calc_value' => '', |
| 51 |
'image' => '', |
| 52 |
]; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
$hasImageOption = ArrayHelper::get($data, 'settings.enable_image_input'); |
| 57 |
|
| 58 |
if ($hasImageOption) { |
| 59 |
if (empty($data['settings']['layout_class'])) { |
| 60 |
$data['settings']['layout_class'] = 'ff_list_buttons'; |
| 61 |
} |
| 62 |
$elMarkup .= '<div class="ff_el_checkable_photo_holders">'; |
| 63 |
} |
| 64 |
|
| 65 |
$data['settings']['container_class'] .= ' ' . ArrayHelper::get($data, 'settings.layout_class'); |
| 66 |
|
| 67 |
if ('yes' == ArrayHelper::get($data, 'settings.randomize_options')) { |
| 68 |
shuffle($formattedOptions); |
| 69 |
} |
| 70 |
|
| 71 |
foreach ($formattedOptions as $option) { |
| 72 |
$displayType = isset($data['settings']['display_type']) ? ' ff-el-form-check-' . $data['settings']['display_type'] : ''; |
| 73 |
$parentClass = 'ff-el-form-check' . esc_attr($displayType) . ''; |
| 74 |
|
| 75 |
if (in_array($option['value'], $defaultValues)) { |
| 76 |
$data['attributes']['checked'] = true; |
| 77 |
$parentClass .= ' ff_item_selected'; |
| 78 |
} else { |
| 79 |
$data['attributes']['checked'] = false; |
| 80 |
} |
| 81 |
|
| 82 |
if ($firstTabIndex) { |
| 83 |
$data['attributes']['tabindex'] = $firstTabIndex; |
| 84 |
$firstTabIndex = '-1'; |
| 85 |
} |
| 86 |
|
| 87 |
$data['attributes']['value'] = $option['value']; |
| 88 |
$data['attributes']['data-calc_value'] = ArrayHelper::get($option, 'calc_value'); |
| 89 |
|
| 90 |
$atts = $this->buildAttributes($data['attributes']); |
| 91 |
|
| 92 |
$id = $this->getUniqueid(str_replace(['[', ']'], ['', ''], $data['attributes']['name'])); |
| 93 |
|
| 94 |
if ($hasImageOption) { |
| 95 |
$parentClass .= ' ff-el-image-holder'; |
| 96 |
} |
| 97 |
|
| 98 |
$elMarkup .= "<div class='" . esc_attr($parentClass) . "'>"; |
| 99 |
|
| 100 |
$id = esc_attr($id); |
| 101 |
|
| 102 |
// Here we can push the visual items |
| 103 |
if ($hasImageOption) { |
| 104 |
$elMarkup .= "<label style='background-image: url(" . esc_url($option['image']) . ")' class='ff-el-image-input-src' for={$id}></label>"; |
| 105 |
} |
| 106 |
|
| 107 |
$elMarkup .= "<label class='ff-el-form-check-label' for={$id}><input {$atts} id='{$id}'> <span>" . fluentform_sanitize_html($option['label'], false) . '</span></label>'; |
| 108 |
$elMarkup .= '</div>'; |
| 109 |
} |
| 110 |
|
| 111 |
if ($hasImageOption) { |
| 112 |
$elMarkup .= '</div>'; |
| 113 |
} |
| 114 |
|
| 115 |
$html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 116 |
|
| 117 |
$this->printContent('fluentform_rendering_field_html_' . $elementName, $html, $data, $form); |
| 118 |
} |
| 119 |
} |
| 120 |
|