| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\FormBuilder\Components; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Helper; |
| 6 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 7 |
|
| 8 |
class Select extends BaseComponent |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Compile and echo the html element |
| 12 |
* @param array $data [element data] |
| 13 |
* @param stdClass $form [Form Object] |
| 14 |
* @return viod |
| 15 |
*/ |
| 16 |
public function compile($data, $form) |
| 17 |
{ |
| 18 |
$elementName = $data['element']; |
| 19 |
$data = apply_filters('fluenform_rendering_field_data_' . $elementName, $data, $form); |
| 20 |
|
| 21 |
$data['attributes']['id'] = $this->makeElementId($data, $form); |
| 22 |
|
| 23 |
$isMulti = ArrayHelper::get($data, 'settings.enable_select_2') == 'yes'; |
| 24 |
|
| 25 |
if (ArrayHelper::get($data['attributes'], 'multiple')) { |
| 26 |
$data['attributes']['name'] = $data['attributes']['name'] . '[]'; |
| 27 |
wp_enqueue_script('choices'); |
| 28 |
wp_enqueue_style('ff_choices'); |
| 29 |
$data['attributes']['class'] .= ' ff_has_multi_select'; |
| 30 |
} else if ($isMulti) { |
| 31 |
wp_enqueue_script('choices'); |
| 32 |
wp_enqueue_style('ff_choices'); |
| 33 |
$data['attributes']['class'] .= ' ff_has_multi_select'; |
| 34 |
} |
| 35 |
|
| 36 |
if ($maxSelection = ArrayHelper::get($data, 'settings.max_selection')) { |
| 37 |
$data['attributes']['data-max_selected_options'] = $maxSelection; |
| 38 |
} |
| 39 |
|
| 40 |
$data['attributes']['data-calc_value'] = 0; |
| 41 |
|
| 42 |
if (!isset($data['attributes']['class'])) { |
| 43 |
$data['attributes']['class'] = ''; |
| 44 |
} |
| 45 |
|
| 46 |
$data['attributes']['class'] = trim('ff-el-form-control ' . $data['attributes']['class']); |
| 47 |
|
| 48 |
if ($tabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex()) { |
| 49 |
$data['attributes']['tabindex'] = $tabIndex; |
| 50 |
} |
| 51 |
|
| 52 |
$defaultValues = (array)$this->extractValueFromAttributes($data); |
| 53 |
|
| 54 |
if ($dynamicValues = $this->extractDynamicValues($data, $form)) { |
| 55 |
$defaultValues = $dynamicValues; |
| 56 |
} |
| 57 |
|
| 58 |
$elMarkup = "<select " . $this->buildAttributes($data['attributes']) . ">" . $this->buildOptions($data, $defaultValues) . "</select>"; |
| 59 |
|
| 60 |
$html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 61 |
echo apply_filters('fluenform_rendering_field_html_' . $elementName, $html, $data, $form); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Build options for select |
| 66 |
* @param array $options |
| 67 |
* @return string/html [compiled options] |
| 68 |
*/ |
| 69 |
protected function buildOptions($data, $defaultValues) |
| 70 |
{ |
| 71 |
if (!$formattedOptions = ArrayHelper::get($data, 'settings.advanced_options')) { |
| 72 |
$options = ArrayHelper::get($data, 'options', []); |
| 73 |
$formattedOptions = []; |
| 74 |
foreach ($options as $value => $label) { |
| 75 |
$formattedOptions[] = [ |
| 76 |
'label' => $label, |
| 77 |
'value' => $value, |
| 78 |
'calc_value' => '' |
| 79 |
]; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if(ArrayHelper::get($data, 'settings.randomize_options') == 'yes') { |
| 84 |
shuffle($formattedOptions); |
| 85 |
} |
| 86 |
|
| 87 |
$opts = ''; |
| 88 |
if (!empty($data['settings']['placeholder'])) { |
| 89 |
$opts .= '<option value="">' . $data['settings']['placeholder'] . '</option>'; |
| 90 |
} else if (!empty($data['attributes']['placeholder'])) { |
| 91 |
$opts .= '<option value="">' . $data['attributes']['placeholder'] . '</option>'; |
| 92 |
} |
| 93 |
|
| 94 |
foreach ($formattedOptions as $option) { |
| 95 |
if (in_array($option['value'], $defaultValues)) { |
| 96 |
$selected = 'selected'; |
| 97 |
} else { |
| 98 |
$selected = ''; |
| 99 |
} |
| 100 |
|
| 101 |
$atts = [ |
| 102 |
'data-calc_value' => ArrayHelper::get($option, 'calc_value'), |
| 103 |
'data-custom-properties' => ArrayHelper::get($option, 'calc_value'), |
| 104 |
'value' => ArrayHelper::get($option, 'value'), |
| 105 |
]; |
| 106 |
|
| 107 |
$opts .= "<option " . $this->buildAttributes($atts) . " {$selected}>{$option['label']}</option>"; |
| 108 |
} |
| 109 |
|
| 110 |
return $opts; |
| 111 |
} |
| 112 |
} |
| 113 |
|