| 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 |
* |
| 13 |
* @param array $data [element data] |
| 14 |
* @param \stdClass $form [Form Object] |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public function compile($data, $form) |
| 19 |
{ |
| 20 |
$elementName = $data['element']; |
| 21 |
$data = apply_filters_deprecated( |
| 22 |
'fluentform_rendering_field_data_' . $elementName, |
| 23 |
[ |
| 24 |
$data, |
| 25 |
$form |
| 26 |
], |
| 27 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 28 |
'fluentform/rendering_field_data_' . $elementName, |
| 29 |
'Use fluentform/rendering_field_data_' . $elementName . ' instead of fluentform_rendering_field_data_' . $elementName |
| 30 |
); |
| 31 |
$data = apply_filters('fluentform/rendering_field_data_' . $elementName, $data, $form); |
| 32 |
|
| 33 |
$data['attributes']['id'] = $this->makeElementId($data, $form); |
| 34 |
|
| 35 |
$isMulti = 'yes' == ArrayHelper::get($data, 'settings.enable_select_2'); |
| 36 |
|
| 37 |
if (ArrayHelper::get($data['attributes'], 'multiple')) { |
| 38 |
$data['attributes']['name'] = $data['attributes']['name'] . '[]'; |
| 39 |
wp_enqueue_script('choices'); |
| 40 |
wp_enqueue_style('ff_choices'); |
| 41 |
$data['attributes']['class'] .= ' ff_has_multi_select'; |
| 42 |
} elseif ($isMulti) { |
| 43 |
wp_enqueue_script('choices'); |
| 44 |
wp_enqueue_style('ff_choices'); |
| 45 |
$data['attributes']['class'] .= ' ff_has_multi_select'; |
| 46 |
} |
| 47 |
|
| 48 |
if ($maxSelection = ArrayHelper::get($data, 'settings.max_selection')) { |
| 49 |
$data['attributes']['data-max_selected_options'] = $maxSelection; |
| 50 |
} |
| 51 |
|
| 52 |
$data['attributes']['data-calc_value'] = 0; |
| 53 |
|
| 54 |
if (! isset($data['attributes']['class'])) { |
| 55 |
$data['attributes']['class'] = ''; |
| 56 |
} |
| 57 |
|
| 58 |
$data['attributes']['class'] = trim('ff-el-form-control ' . $data['attributes']['class']); |
| 59 |
|
| 60 |
if ($tabIndex = Helper::getNextTabIndex()) { |
| 61 |
$data['attributes']['tabindex'] = $tabIndex; |
| 62 |
} |
| 63 |
|
| 64 |
$defaultValues = (array) $this->extractValueFromAttributes($data); |
| 65 |
|
| 66 |
if ($dynamicValues = $this->extractDynamicValues($data, $form)) { |
| 67 |
$defaultValues = $dynamicValues; |
| 68 |
} |
| 69 |
|
| 70 |
$atts = $this->buildAttributes($data['attributes']); |
| 71 |
$options = $this->buildOptions($data, $defaultValues); |
| 72 |
|
| 73 |
$ariaRequired = 'false'; |
| 74 |
if (ArrayHelper::get($data, 'settings.validation_rules.required.value')) { |
| 75 |
$ariaRequired = 'true'; |
| 76 |
} |
| 77 |
|
| 78 |
$ariaLabelledBy = 'label_' . ArrayHelper::get($data, 'attributes.id'); |
| 79 |
|
| 80 |
$elMarkup = '<select ' . $atts . ' aria-invalid="false" aria-required="' . $ariaRequired . '" aria-labelledby="' . $ariaLabelledBy .'"'.'>' . $options . '</select>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $atts, $options are escaped before being passed in. |
| 81 |
|
| 82 |
$html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 83 |
|
| 84 |
$html = apply_filters_deprecated( |
| 85 |
'fluentform_rendering_field_html_' . $elementName, |
| 86 |
[ |
| 87 |
$html, |
| 88 |
$data, |
| 89 |
$form |
| 90 |
], |
| 91 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 92 |
'fluentform/rendering_field_html_' . $elementName, |
| 93 |
'Use fluentform/rendering_field_html_' . $elementName . ' instead of fluentform_rendering_field_html_' . $elementName |
| 94 |
); |
| 95 |
|
| 96 |
$this->printContent('fluentform/rendering_field_html_' . $elementName, $html, $data, $form); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Build options for select |
| 101 |
* |
| 102 |
* @param array $options |
| 103 |
* |
| 104 |
* @return string/html [compiled options] |
| 105 |
*/ |
| 106 |
protected function buildOptions($data, $defaultValues) |
| 107 |
{ |
| 108 |
if (! $formattedOptions = ArrayHelper::get($data, 'settings.advanced_options')) { |
| 109 |
$options = ArrayHelper::get($data, 'options', []); |
| 110 |
$formattedOptions = []; |
| 111 |
foreach ($options as $value => $label) { |
| 112 |
$formattedOptions[] = [ |
| 113 |
'label' => $label, |
| 114 |
'value' => $value, |
| 115 |
'calc_value' => '', |
| 116 |
]; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if ('yes' == ArrayHelper::get($data, 'settings.randomize_options')) { |
| 121 |
shuffle($formattedOptions); |
| 122 |
} |
| 123 |
|
| 124 |
$opts = ''; |
| 125 |
if (! empty($data['settings']['placeholder'])) { |
| 126 |
$opts .= '<option value="">' . wp_strip_all_tags($data['settings']['placeholder']) . '</option>'; |
| 127 |
} elseif (! empty($data['attributes']['placeholder'])) { |
| 128 |
$opts .= '<option value="">' . wp_strip_all_tags($data['attributes']['placeholder']) . '</option>'; |
| 129 |
} |
| 130 |
foreach ($formattedOptions as $option) { |
| 131 |
if (Helper::isOptionGroup($option)) { |
| 132 |
$groupLabel = wp_strip_all_tags(ArrayHelper::get($option, 'label', '')); |
| 133 |
$groupOptions = $this->buildOptionMarkup( |
| 134 |
ArrayHelper::get($option, 'options', []), |
| 135 |
$defaultValues |
| 136 |
); |
| 137 |
|
| 138 |
if ($groupOptions) { |
| 139 |
$opts .= '<optgroup label="' . esc_attr($groupLabel) . '">' . $groupOptions . '</optgroup>'; |
| 140 |
} |
| 141 |
continue; |
| 142 |
} |
| 143 |
|
| 144 |
$opts .= $this->buildOptionMarkup([$option], $defaultValues); |
| 145 |
} |
| 146 |
|
| 147 |
return $opts; |
| 148 |
} |
| 149 |
|
| 150 |
protected function buildOptionMarkup($formattedOptions, $defaultValues) |
| 151 |
{ |
| 152 |
$opts = ''; |
| 153 |
|
| 154 |
foreach ($formattedOptions as $option) { |
| 155 |
if (in_array($option['value'], $defaultValues)) { |
| 156 |
$selected = 'selected'; |
| 157 |
} else { |
| 158 |
$selected = ''; |
| 159 |
} |
| 160 |
|
| 161 |
$atts = [ |
| 162 |
'data-calc_value' => ArrayHelper::get($option, 'calc_value'), |
| 163 |
'data-custom-properties' => ArrayHelper::get($option, 'calc_value'), |
| 164 |
'value' => ArrayHelper::get($option, 'value'), |
| 165 |
'disabled' => ArrayHelper::get($option, 'disabled') ? 'disabled' : '' |
| 166 |
]; |
| 167 |
|
| 168 |
$opts .= '<option '. $this->buildAttributes($atts) . " {$selected}>" . wp_strip_all_tags($option['label']) . '</option>'; |
| 169 |
} |
| 170 |
|
| 171 |
return $opts; |
| 172 |
} |
| 173 |
} |
| 174 |
|