| 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 SelectCountry 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 = $this->loadCountries($data); |
| 34 |
$defaultValues = (array) $this->extractValueFromAttributes($data); |
| 35 |
$data['attributes']['class'] = trim('ff-el-form-control ' . $data['attributes']['class']); |
| 36 |
$data['attributes']['id'] = $this->makeElementId($data, $form); |
| 37 |
$isSearchable = ArrayHelper::get($data, 'settings.enable_select_2'); |
| 38 |
if ('yes' == $isSearchable) { |
| 39 |
wp_enqueue_script('choices'); |
| 40 |
wp_enqueue_style('ff_choices'); |
| 41 |
$data['attributes']['class'] .= ' ff_has_multi_select'; |
| 42 |
} |
| 43 |
|
| 44 |
if ($tabIndex = Helper::getNextTabIndex()) { |
| 45 |
$data['attributes']['tabindex'] = $tabIndex; |
| 46 |
} |
| 47 |
|
| 48 |
$placeholder = ArrayHelper::get($data, 'attributes.placeholder'); |
| 49 |
|
| 50 |
$activeList = ArrayHelper::get($data, 'settings.country_list.active_list'); |
| 51 |
|
| 52 |
$ariaRequired = 'false'; |
| 53 |
if (ArrayHelper::get($data, 'settings.validation_rules.required.value')) { |
| 54 |
$ariaRequired = 'true'; |
| 55 |
} |
| 56 |
|
| 57 |
$elMarkup = '<select ' . $this->buildAttributes($data['attributes']) . "aria-invalid='false' aria-required=$ariaRequired><option value=''>" . wp_strip_all_tags($placeholder) . '</option>'; |
| 58 |
|
| 59 |
if ('priority_based' == $activeList) { |
| 60 |
$selectCountries = ArrayHelper::get($data, 'settings.country_list.priority_based', []); |
| 61 |
$priorityCountries = $this->getSelectedCountries($selectCountries); |
| 62 |
$primaryListLabel = ArrayHelper::get($data, 'settings.primary_label'); |
| 63 |
$otherListLabel = ArrayHelper::get($data, 'settings.other_label'); |
| 64 |
$elMarkup .= '<optgroup label="' . wp_strip_all_tags($primaryListLabel) . '">'; |
| 65 |
$elMarkup .= $this->buildOptions($priorityCountries, $defaultValues); |
| 66 |
$elMarkup .= '</optgroup><optgroup label="' . wp_strip_all_tags($otherListLabel) . '">'; |
| 67 |
$elMarkup .= $this->buildOptions($data['options'], $defaultValues); |
| 68 |
$elMarkup .= '</optgroup>'; |
| 69 |
} else { |
| 70 |
$elMarkup .= $this->buildOptions($data['options'], $defaultValues); |
| 71 |
} |
| 72 |
|
| 73 |
$elMarkup .= '</select>'; |
| 74 |
|
| 75 |
$html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 76 |
|
| 77 |
$html = apply_filters_deprecated( |
| 78 |
'fluentform_rendering_field_html_' . $elementName, |
| 79 |
[ |
| 80 |
$html, |
| 81 |
$data, |
| 82 |
$form |
| 83 |
], |
| 84 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 85 |
'fluentform/rendering_field_html_' . $elementName, |
| 86 |
'Use fluentform/rendering_field_html_' . $elementName . ' instead of fluentform_rendering_field_html_' . $elementName |
| 87 |
); |
| 88 |
|
| 89 |
$this->printContent('fluentform/rendering_field_html_' . $elementName, $html, $data, $form); |
| 90 |
} |
| 91 |
/** |
| 92 |
* Load countt list from file |
| 93 |
* |
| 94 |
* @param array $data |
| 95 |
* |
| 96 |
* @return array |
| 97 |
*/ |
| 98 |
public function loadCountries($data) |
| 99 |
{ |
| 100 |
$app = wpFluentForm(); |
| 101 |
$data['options'] = []; |
| 102 |
$activeList = ArrayHelper::get($data, 'settings.country_list.active_list'); |
| 103 |
$countries = getFluentFormCountryList(); |
| 104 |
|
| 105 |
if ('visible_list' == $activeList) { |
| 106 |
$selectCountries = ArrayHelper::get($data, 'settings.country_list.' . $activeList, []); |
| 107 |
foreach ($selectCountries as $value) { |
| 108 |
$data['options'][$value] = $countries[$value]; |
| 109 |
} |
| 110 |
} elseif ('hidden_list' == $activeList || 'priority_based' == $activeList) { |
| 111 |
$data['options'] = $countries; |
| 112 |
$selectCountries = ArrayHelper::get($data, 'settings.country_list.' . $activeList, []); |
| 113 |
foreach ($selectCountries as $value) { |
| 114 |
unset($data['options'][$value]); |
| 115 |
} |
| 116 |
} else { |
| 117 |
$data['options'] = $countries; |
| 118 |
} |
| 119 |
|
| 120 |
$selectedCountries = $data['options']; |
| 121 |
$selectedCountries = array_flip($selectedCountries); |
| 122 |
ksort($selectedCountries); |
| 123 |
$selectedCountries = array_flip($selectedCountries); |
| 124 |
$data['options'] = $selectedCountries; |
| 125 |
|
| 126 |
return $data; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Build options for country list/select |
| 131 |
* |
| 132 |
* @param array $options |
| 133 |
* |
| 134 |
* @return string/html [compiled options] |
| 135 |
*/ |
| 136 |
protected function buildOptions($options, $defaultValues = []) |
| 137 |
{ |
| 138 |
$opts = ''; |
| 139 |
foreach ($options as $value => $label) { |
| 140 |
if (in_array($value, $defaultValues)) { |
| 141 |
$selected = 'selected'; |
| 142 |
} else { |
| 143 |
$selected = ''; |
| 144 |
} |
| 145 |
$opts .= "<option value='" . esc_attr($value) . "' {$selected}>" . esc_attr($label) . '</option>'; |
| 146 |
} |
| 147 |
return $opts; |
| 148 |
} |
| 149 |
|
| 150 |
public function getSelectedCountries($keys = []) |
| 151 |
{ |
| 152 |
$options = []; |
| 153 |
$countries = getFluentFormCountryList(); |
| 154 |
foreach ($keys as $value) { |
| 155 |
$options[$value] = $countries[$value]; |
| 156 |
} |
| 157 |
|
| 158 |
$options = array_flip($options); |
| 159 |
ksort($options); |
| 160 |
return array_flip($options); |
| 161 |
} |
| 162 |
} |
| 163 |
|