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