| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\FormBuilder\Components; |
| 4 |
|
| 5 |
use FluentForm\App; |
| 6 |
use FluentForm\App\Helpers\Helper; |
| 7 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 8 |
|
| 9 |
class SelectCountry extends BaseComponent |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Compile and echo the html element |
| 13 |
* |
| 14 |
* @param array $data [element data] |
| 15 |
* @param \stdClass $form [Form Object] |
| 16 |
* |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public function compile($data, $form) |
| 20 |
{ |
| 21 |
$elementName = $data['element']; |
| 22 |
$data = apply_filters('fluentform_rendering_field_data_' . $elementName, $data, $form); |
| 23 |
|
| 24 |
$data = $this->loadCountries($data); |
| 25 |
$defaultValues = (array) $this->extractValueFromAttributes($data); |
| 26 |
$data['attributes']['class'] = trim('ff-el-form-control ' . $data['attributes']['class']); |
| 27 |
$data['attributes']['id'] = $this->makeElementId($data, $form); |
| 28 |
$isSearchable = ArrayHelper::get($data, 'settings.enable_select_2'); |
| 29 |
if ('yes' == $isSearchable) { |
| 30 |
wp_enqueue_script('choices'); |
| 31 |
wp_enqueue_style('ff_choices'); |
| 32 |
$data['attributes']['class'] .= ' ff_has_multi_select'; |
| 33 |
} |
| 34 |
|
| 35 |
if ($tabIndex = Helper::getNextTabIndex()) { |
| 36 |
$data['attributes']['tabindex'] = $tabIndex; |
| 37 |
} |
| 38 |
|
| 39 |
$placeholder = ArrayHelper::get($data, 'attributes.placeholder'); |
| 40 |
|
| 41 |
$activeList = ArrayHelper::get($data, 'settings.country_list.active_list'); |
| 42 |
|
| 43 |
$elMarkup = '<select ' . $this->buildAttributes($data['attributes']) . "><option value=''>" . wp_strip_all_tags($placeholder) . '</option>'; |
| 44 |
|
| 45 |
if ('priority_based' == $activeList) { |
| 46 |
$selectCountries = ArrayHelper::get($data, 'settings.country_list.priority_based', []); |
| 47 |
$priorityCountries = $this->getSelectedCountries($selectCountries); |
| 48 |
$primaryListLabel = ArrayHelper::get($data, 'settings.primary_label'); |
| 49 |
$otherListLabel = ArrayHelper::get($data, 'settings.other_label'); |
| 50 |
$elMarkup .= '<optgroup label="' . wp_strip_all_tags($primaryListLabel) . '">'; |
| 51 |
$elMarkup .= $this->buildOptions($priorityCountries, $defaultValues); |
| 52 |
$elMarkup .= '</optgroup><optgroup label="' . wp_strip_all_tags($otherListLabel) . '">'; |
| 53 |
$elMarkup .= $this->buildOptions($data['options'], $defaultValues); |
| 54 |
$elMarkup .= '</optgroup>'; |
| 55 |
} else { |
| 56 |
$elMarkup .= $this->buildOptions($data['options'], $defaultValues); |
| 57 |
} |
| 58 |
|
| 59 |
$elMarkup .= '</select>'; |
| 60 |
|
| 61 |
$html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 62 |
|
| 63 |
$this->printContent('fluentform_rendering_field_html_' . $elementName, $html, $data, $form); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Load countt list from file |
| 68 |
* |
| 69 |
* @param array $data |
| 70 |
* |
| 71 |
* @return array |
| 72 |
*/ |
| 73 |
public function loadCountries($data) |
| 74 |
{ |
| 75 |
$app = App::make(); |
| 76 |
$data['options'] = []; |
| 77 |
$activeList = ArrayHelper::get($data, 'settings.country_list.active_list'); |
| 78 |
$countries = $app->load($app->appPath('Services/FormBuilder/CountryNames.php')); |
| 79 |
|
| 80 |
if ('visible_list' == $activeList) { |
| 81 |
$selectCountries = ArrayHelper::get($data, 'settings.country_list.' . $activeList, []); |
| 82 |
foreach ($selectCountries as $value) { |
| 83 |
$data['options'][$value] = $countries[$value]; |
| 84 |
} |
| 85 |
} elseif ('hidden_list' == $activeList || 'priority_based' == $activeList) { |
| 86 |
$data['options'] = $countries; |
| 87 |
$selectCountries = ArrayHelper::get($data, 'settings.country_list.' . $activeList, []); |
| 88 |
foreach ($selectCountries as $value) { |
| 89 |
unset($data['options'][$value]); |
| 90 |
} |
| 91 |
} else { |
| 92 |
$data['options'] = $countries; |
| 93 |
} |
| 94 |
|
| 95 |
$selectedCountries = $data['options']; |
| 96 |
$selectedCountries = array_flip($selectedCountries); |
| 97 |
ksort($selectedCountries); |
| 98 |
$selectedCountries = array_flip($selectedCountries); |
| 99 |
$data['options'] = $selectedCountries; |
| 100 |
|
| 101 |
return $data; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Build options for country list/select |
| 106 |
* |
| 107 |
* @param array $options |
| 108 |
* |
| 109 |
* @return string/html [compiled options] |
| 110 |
*/ |
| 111 |
protected function buildOptions($options, $defaultValues = []) |
| 112 |
{ |
| 113 |
$opts = ''; |
| 114 |
foreach ($options as $value => $label) { |
| 115 |
if (in_array($value, $defaultValues)) { |
| 116 |
$selected = 'selected'; |
| 117 |
} else { |
| 118 |
$selected = ''; |
| 119 |
} |
| 120 |
$opts .= "<option value='" . esc_attr($value) . "' {$selected}>" . esc_attr($label) . '</option>'; |
| 121 |
} |
| 122 |
return $opts; |
| 123 |
} |
| 124 |
|
| 125 |
public function getSelectedCountries($keys = []) |
| 126 |
{ |
| 127 |
$app = App::make(); |
| 128 |
$options = []; |
| 129 |
$countries = $app->load($app->appPath('Services/FormBuilder/CountryNames.php')); |
| 130 |
foreach ($keys as $value) { |
| 131 |
$options[$value] = $countries[$value]; |
| 132 |
} |
| 133 |
|
| 134 |
$options = array_flip($options); |
| 135 |
ksort($options); |
| 136 |
return array_flip($options); |
| 137 |
} |
| 138 |
} |
| 139 |
|