| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\FormBuilder\Components; |
| 4 |
|
| 5 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 6 |
|
| 7 |
class Address extends BaseComponent |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Wrapper class for address element |
| 11 |
* |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
protected $wrapperClass = 'fluent-address'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Compile and echo the html element |
| 18 |
* |
| 19 |
* @param array $data [element data] |
| 20 |
* @param \stdClass $form [Form Object] |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function compile($data, $form) |
| 25 |
{ |
| 26 |
$elementName = $data['element']; |
| 27 |
|
| 28 |
$data = apply_filters('fluentform_rendering_field_data_' . $elementName, $data, $form); |
| 29 |
|
| 30 |
$rootName = $data['attributes']['name']; |
| 31 |
$hasConditions = $this->hasConditions($data) ? 'has-conditions ' : ''; |
| 32 |
$data['attributes']['class'] .= ' ff-name-address-wrapper ' . $this->wrapperClass . ' ' . $hasConditions; |
| 33 |
$data['attributes']['class'] = trim($data['attributes']['class']); |
| 34 |
|
| 35 |
if ('yes' == ArrayHelper::get($data, 'settings.enable_g_autocomplete')) { |
| 36 |
$data['attributes']['class'] .= ' ff_map_autocomplete'; |
| 37 |
if ('yes' == ArrayHelper::get($data, 'settings.enable_g_map')) { |
| 38 |
$data['attributes']['data-ff_with_g_map'] = '1'; |
| 39 |
} |
| 40 |
$data['attributes']['data-ff_with_auto_locate'] = ArrayHelper::get($data, 'settings.enable_auto_locate', false); |
| 41 |
do_action('fluentform_address_map_autocomplete', $data, $form); |
| 42 |
} |
| 43 |
|
| 44 |
$atts = $this->buildAttributes( |
| 45 |
ArrayHelper::except($data['attributes'], 'name') |
| 46 |
); |
| 47 |
|
| 48 |
//re order fields from version 4.3.2 |
| 49 |
if ($order = ArrayHelper::get($data, 'settings.field_order')) { |
| 50 |
$order = array_values(array_column($order, 'value')); |
| 51 |
$fields = ArrayHelper::get($data, 'fields'); |
| 52 |
$data['fields'] = array_merge(array_flip($order), $fields); |
| 53 |
} |
| 54 |
ob_start(); |
| 55 |
echo '<div ' . $atts . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $atts is escaped before being passed in. |
| 56 |
do_action('fluentform_rendering_address_field', $data, $form); |
| 57 |
if ($data['settings']['label']): |
| 58 |
echo "<div class='ff-el-input--label'>"; |
| 59 |
echo '<label>' . fluentform_sanitize_html($data['settings']['label'], false) . '</label>'; |
| 60 |
echo '</div>'; |
| 61 |
endif; |
| 62 |
echo "<div class='ff-el-input--content'>"; |
| 63 |
|
| 64 |
$visibleFields = array_chunk(array_filter($data['fields'], function ($field) { |
| 65 |
return $field['settings']['visible']; |
| 66 |
}), 2); |
| 67 |
|
| 68 |
$googleAutoComplete = 'yes' === ArrayHelper::get($data, 'settings.enable_g_autocomplete'); |
| 69 |
foreach ($visibleFields as $chunked) { |
| 70 |
echo "<div class='ff-t-container'>"; |
| 71 |
foreach ($chunked as $item) { |
| 72 |
if ($item['settings']['visible']) { |
| 73 |
$itemName = $item['attributes']['name']; |
| 74 |
$item['attributes']['data-key_name'] = $itemName; |
| 75 |
$item['attributes']['name'] = $rootName . '[' . $itemName . ']'; |
| 76 |
|
| 77 |
if ('select_country' === $item['element'] && $googleAutoComplete) { |
| 78 |
$selectedCountries = (array) ArrayHelper::get($item, 'attributes.value', []); |
| 79 |
if ('visible_list' === ArrayHelper::get($item, 'settings.country_list.active_list')) { |
| 80 |
$selectedCountries = array_unique( |
| 81 |
array_merge( |
| 82 |
$selectedCountries, |
| 83 |
ArrayHelper::get($item, 'settings.country_list.visible_list', []) |
| 84 |
) |
| 85 |
); |
| 86 |
} |
| 87 |
$item['attributes']['data-autocomplete_restrictions'] = json_encode(array_filter($selectedCountries)); |
| 88 |
} |
| 89 |
|
| 90 |
$item = apply_filters('fluentform_before_render_item', $item, $form); |
| 91 |
echo "<div class='ff-t-cell'>"; |
| 92 |
do_action('fluentform_render_item_' . $item['element'], $item, $form); |
| 93 |
echo '</div>'; |
| 94 |
} |
| 95 |
} |
| 96 |
echo '</div>'; |
| 97 |
} |
| 98 |
|
| 99 |
echo '</div>'; |
| 100 |
echo '</div>'; |
| 101 |
|
| 102 |
$html = ob_get_clean(); |
| 103 |
|
| 104 |
$this->printContent('fluentform_rendering_field_html_' . $elementName, $html, $data, $form); |
| 105 |
} |
| 106 |
} |
| 107 |
|