| 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 |
* @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 |
$data = apply_filters('fluenform_rendering_field_data_' . $elementName, $data, $form); |
| 21 |
|
| 22 |
$data = $this->loadCountries($data); |
| 23 |
$defaultValues = (array)$this->extractValueFromAttributes($data); |
| 24 |
$data['attributes']['class'] = trim('ff-el-form-control ' . $data['attributes']['class']); |
| 25 |
$data['attributes']['id'] = $this->makeElementId($data, $form); |
| 26 |
|
| 27 |
if ($tabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex()) { |
| 28 |
$data['attributes']['tabindex'] = $tabIndex; |
| 29 |
} |
| 30 |
|
| 31 |
$placeholder = ArrayHelper::get($data, 'attributes.placeholder'); |
| 32 |
|
| 33 |
$activeList = ArrayHelper::get($data, 'settings.country_list.active_list'); |
| 34 |
|
| 35 |
$elMarkup = "<select " . $this->buildAttributes($data['attributes']) . "><option value=''>".$placeholder."</option>"; |
| 36 |
|
| 37 |
if ($activeList == 'priority_based') { |
| 38 |
$selectCountries = ArrayHelper::get($data, 'settings.country_list.priority_based', []); |
| 39 |
$priorityCountries = $this->getSelectedCountries($selectCountries); |
| 40 |
$primaryListLabel = ArrayHelper::get($data, 'settings.primary_label'); |
| 41 |
$otherListLabel = ArrayHelper::get($data, 'settings.other_label'); |
| 42 |
$elMarkup .= '<optgroup label="'.$primaryListLabel.'">'; |
| 43 |
$elMarkup .= $this->buildOptions($priorityCountries, $defaultValues); |
| 44 |
$elMarkup .= '</optgroup><optgroup label="'.$otherListLabel.'">'; |
| 45 |
$elMarkup .= $this->buildOptions($data['options'], $defaultValues); |
| 46 |
$elMarkup .= '</optgroup>'; |
| 47 |
} else { |
| 48 |
$elMarkup .= $this->buildOptions($data['options'], $defaultValues); |
| 49 |
} |
| 50 |
|
| 51 |
$elMarkup .= "</select>"; |
| 52 |
|
| 53 |
$html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 54 |
echo apply_filters('fluenform_rendering_field_html_' . $elementName, $html, $data, $form); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Load countt list from file |
| 59 |
* @param array $data |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
protected function loadCountries($data) |
| 63 |
{ |
| 64 |
$app = App::make(); |
| 65 |
$data['options'] = array(); |
| 66 |
$activeList = ArrayHelper::get($data, 'settings.country_list.active_list'); |
| 67 |
$countries = $app->load($app->appPath('Services/FormBuilder/CountryNames.php')); |
| 68 |
|
| 69 |
if ($activeList == 'visible_list') { |
| 70 |
$selectCountries = ArrayHelper::get($data, 'settings.country_list.' . $activeList, []); |
| 71 |
foreach ($selectCountries as $value) { |
| 72 |
$data['options'][$value] = $countries[$value]; |
| 73 |
} |
| 74 |
} elseif ($activeList == 'hidden_list' || $activeList == 'priority_based') { |
| 75 |
$data['options'] = $countries; |
| 76 |
$selectCountries = ArrayHelper::get($data, 'settings.country_list.' . $activeList, []); |
| 77 |
foreach ($selectCountries as $value) { |
| 78 |
unset($data['options'][$value]); |
| 79 |
} |
| 80 |
} else { |
| 81 |
$data['options'] = $countries; |
| 82 |
} |
| 83 |
|
| 84 |
return $data; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Build options for country list/select |
| 89 |
* @param array $options |
| 90 |
* @return string/html [compiled options] |
| 91 |
*/ |
| 92 |
protected function buildOptions($options, $defaultValues = []) |
| 93 |
{ |
| 94 |
$opts = ''; |
| 95 |
foreach ($options as $value => $label) { |
| 96 |
if (in_array($value, $defaultValues)) { |
| 97 |
$selected = 'selected'; |
| 98 |
} else { |
| 99 |
$selected = ''; |
| 100 |
} |
| 101 |
$opts .= "<option value='{$value}' {$selected}>{$label}</option>"; |
| 102 |
} |
| 103 |
return $opts; |
| 104 |
} |
| 105 |
|
| 106 |
protected function getSelectedCountries($keys = []) |
| 107 |
{ |
| 108 |
$app = App::make(); |
| 109 |
$options = []; |
| 110 |
$countries = $app->load($app->appPath('Services/FormBuilder/CountryNames.php')); |
| 111 |
foreach ($keys as $value) { |
| 112 |
$options[$value] = $countries[$value]; |
| 113 |
} |
| 114 |
|
| 115 |
return $options; |
| 116 |
} |
| 117 |
|
| 118 |
} |
| 119 |
|