PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.64
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.64
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Services / FormBuilder / Components / SelectCountry.php

SelectCountry.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 3.6.64, at app/Services/FormBuilder/Components/SelectCountry.php

133 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $isSearchable = ArrayHelper::get ($data,'settings.enable_select_2');
27 if($isSearchable == 'yes'){
28 wp_enqueue_script('choices');
29 wp_enqueue_style('ff_choices');
30 $data['attributes']['class'] .= ' ff_has_multi_select';
31 }
32
33 if ($tabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex()) {
34 $data['attributes']['tabindex'] = $tabIndex;
35 }
36
37 $placeholder = ArrayHelper::get($data, 'attributes.placeholder');
38
39 $activeList = ArrayHelper::get($data, 'settings.country_list.active_list');
40
41 $elMarkup = "<select " . $this->buildAttributes($data['attributes']) . "><option value=''>" . $placeholder . "</option>";
42
43 if ($activeList == 'priority_based') {
44 $selectCountries = ArrayHelper::get($data, 'settings.country_list.priority_based', []);
45 $priorityCountries = $this->getSelectedCountries($selectCountries);
46 $primaryListLabel = ArrayHelper::get($data, 'settings.primary_label');
47 $otherListLabel = ArrayHelper::get($data, 'settings.other_label');
48 $elMarkup .= '<optgroup label="' . $primaryListLabel . '">';
49 $elMarkup .= $this->buildOptions($priorityCountries, $defaultValues);
50 $elMarkup .= '</optgroup><optgroup label="' . $otherListLabel . '">';
51 $elMarkup .= $this->buildOptions($data['options'], $defaultValues);
52 $elMarkup .= '</optgroup>';
53 } else {
54 $elMarkup .= $this->buildOptions($data['options'], $defaultValues);
55 }
56
57 $elMarkup .= "</select>";
58
59 $html = $this->buildElementMarkup($elMarkup, $data, $form);
60 echo apply_filters('fluenform_rendering_field_html_' . $elementName, $html, $data, $form);
61 }
62
63 /**
64 * Load countt list from file
65 * @param array $data
66 * @return array
67 */
68 protected function loadCountries($data)
69 {
70 $app = App::make();
71 $data['options'] = array();
72 $activeList = ArrayHelper::get($data, 'settings.country_list.active_list');
73 $countries = $app->load($app->appPath('Services/FormBuilder/CountryNames.php'));
74
75 if ($activeList == 'visible_list') {
76 $selectCountries = ArrayHelper::get($data, 'settings.country_list.' . $activeList, []);
77 foreach ($selectCountries as $value) {
78 $data['options'][$value] = $countries[$value];
79 }
80 } elseif ($activeList == 'hidden_list' || $activeList == 'priority_based') {
81 $data['options'] = $countries;
82 $selectCountries = ArrayHelper::get($data, 'settings.country_list.' . $activeList, []);
83 foreach ($selectCountries as $value) {
84 unset($data['options'][$value]);
85 }
86 } else {
87 $data['options'] = $countries;
88 }
89
90 $selectedCountries = $data['options'];
91 $selectedCountries = array_flip($selectedCountries);
92 ksort($selectedCountries);
93 $selectedCountries = array_flip($selectedCountries);
94 $data['options'] = $selectedCountries;
95
96 return $data;
97 }
98
99 /**
100 * Build options for country list/select
101 * @param array $options
102 * @return string/html [compiled options]
103 */
104 protected function buildOptions($options, $defaultValues = [])
105 {
106 $opts = '';
107 foreach ($options as $value => $label) {
108 if (in_array($value, $defaultValues)) {
109 $selected = 'selected';
110 } else {
111 $selected = '';
112 }
113 $opts .= "<option value='{$value}' {$selected}>{$label}</option>";
114 }
115 return $opts;
116 }
117
118 protected function getSelectedCountries($keys = [])
119 {
120 $app = App::make();
121 $options = [];
122 $countries = $app->load($app->appPath('Services/FormBuilder/CountryNames.php'));
123 foreach ($keys as $value) {
124 $options[$value] = $countries[$value];
125 }
126
127 $options = array_flip($options);
128 ksort($options);
129 return array_flip($options);
130 }
131
132 }
133