PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.62
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.62
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.62, at app/Services/FormBuilder/Components/SelectCountry.php

127 lines 4.6 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
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 $selectedCountries = $data['options'];
85 $selectedCountries = array_flip($selectedCountries);
86 ksort($selectedCountries);
87 $selectedCountries = array_flip($selectedCountries);
88 $data['options'] = $selectedCountries;
89
90 return $data;
91 }
92
93 /**
94 * Build options for country list/select
95 * @param array $options
96 * @return string/html [compiled options]
97 */
98 protected function buildOptions($options, $defaultValues = [])
99 {
100 $opts = '';
101 foreach ($options as $value => $label) {
102 if (in_array($value, $defaultValues)) {
103 $selected = 'selected';
104 } else {
105 $selected = '';
106 }
107 $opts .= "<option value='{$value}' {$selected}>{$label}</option>";
108 }
109 return $opts;
110 }
111
112 protected function getSelectedCountries($keys = [])
113 {
114 $app = App::make();
115 $options = [];
116 $countries = $app->load($app->appPath('Services/FormBuilder/CountryNames.php'));
117 foreach ($keys as $value) {
118 $options[$value] = $countries[$value];
119 }
120
121 $options = array_flip($options);
122 ksort($options);
123 return array_flip($options);
124 }
125
126 }
127