| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage com_vikbooking |
| 5 |
* @author Alessio Gaggii - E4J srl |
| 6 |
* @copyright Copyright (C) 2024 E4J srl. All rights reserved. |
| 7 |
* @license GNU General Public License version 2 or later; see LICENSE |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 12 |
|
| 13 |
/** |
| 14 |
* Display data attributes. |
| 15 |
* |
| 16 |
* @var VBOHelpWizardInstruction $instruction |
| 17 |
*/ |
| 18 |
extract($displayData); |
| 19 |
|
| 20 |
$countries = []; |
| 21 |
|
| 22 |
foreach (VikBooking::getCountriesArray($tn = false, $no_id = false) as $country) { |
| 23 |
$country = (object) $country; |
| 24 |
$countries[$country->country_2_code] = $country; |
| 25 |
} |
| 26 |
|
| 27 |
$defaultCountry = $instruction->guessCountry(); |
| 28 |
|
| 29 |
$states = []; |
| 30 |
|
| 31 |
if ($defaultCountry) { |
| 32 |
// load all the states of the pre-selected country |
| 33 |
$states = array_map(function($state) { |
| 34 |
return (object) $state; |
| 35 |
}, VBOStateHelper::getCountryStates($countries[$defaultCountry]->id ?? null)); |
| 36 |
} |
| 37 |
|
| 38 |
?> |
| 39 |
|
| 40 |
<div class="vbo-help-wizard-instruction-description" style="margin-bottom: 20px;"> |
| 41 |
<?php echo JText::translate('VBO_HELP_WIZARD_GENERAL_BASE_COUNTRY_SUMMARY'); ?> |
| 42 |
</div> |
| 43 |
|
| 44 |
<form id="help-wizard-instruction-form" class="vbo-admin-container vbo-admin-container-full vbo-admin-container-compact"> |
| 45 |
|
| 46 |
<div class="vbo-params-wrap"> |
| 47 |
|
| 48 |
<div class="vbo-params-container"> |
| 49 |
|
| 50 |
<div class="vbo-param-container country-field"> |
| 51 |
<div class="vbo-param-label"><?php echo JText::translate('VBCUSTOMERCOUNTRY'); ?></div> |
| 52 |
<div class="vbo-param-setting"> |
| 53 |
<select name="country"> |
| 54 |
<option value=""><?php echo JText::translate('VBO_SELECT_COUNTRY'); ?></option> |
| 55 |
<?php echo JHtml::fetch('select.options', $countries, 'country_2_code', 'country_name', $defaultCountry); ?> |
| 56 |
</select> |
| 57 |
<span class="vbo-param-setting-comment"><?php echo JText::translate('VBO_HELP_WIZARD_GENERAL_BASE_COUNTRY_VALUE_DESC'); ?></span> |
| 58 |
</div> |
| 59 |
</div> |
| 60 |
|
| 61 |
<div class="vbo-param-container state-field" style="<?php echo $states ? '' : 'display: none;'; ?>"> |
| 62 |
<div class="vbo-param-label"><?php echo JText::translate('VBO_STATE_PROVINCE'); ?></div> |
| 63 |
<div class="vbo-param-setting"> |
| 64 |
<select name="state"> |
| 65 |
<option value=""><?php echo JText::translate('JGLOBAL_SELECT_AN_OPTION'); ?></option> |
| 66 |
<?php echo JHtml::fetch('select.options', $states, 'state_2_code', 'state_name'); ?> |
| 67 |
</select> |
| 68 |
<span class="vbo-param-setting-comment"><?php echo JText::translate('VBO_HELP_WIZARD_GENERAL_BASE_COUNTRY_STATE_DESC'); ?></span> |
| 69 |
</div> |
| 70 |
</div> |
| 71 |
|
| 72 |
</div> |
| 73 |
|
| 74 |
</div> |
| 75 |
|
| 76 |
</form> |
| 77 |
|
| 78 |
<script> |
| 79 |
(function($) { |
| 80 |
'use strict'; |
| 81 |
|
| 82 |
$(function() { |
| 83 |
const form = $('#help-wizard-instruction-form'); |
| 84 |
|
| 85 |
const countrySelect = form.find('select[name="country"]'); |
| 86 |
const stateSelect = form.find('select[name="state"]'); |
| 87 |
|
| 88 |
countrySelect.on('change', async function() { |
| 89 |
const countryCode = $(this).val(); |
| 90 |
|
| 91 |
// remove all the options (except for the first one) |
| 92 |
stateSelect.find('option:not(:first)').remove(); |
| 93 |
|
| 94 |
if (!countryCode) { |
| 95 |
stateSelect.closest('.state-field').hide(); |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
try { |
| 100 |
// get states for the selected country |
| 101 |
let response = await VBOHelpWizard.processInstruction('<?php echo $instruction->getID(); ?>', { |
| 102 |
scope: 'states', |
| 103 |
country: countryCode, |
| 104 |
}); |
| 105 |
|
| 106 |
if (!response?.states?.length) { |
| 107 |
throw 'no_states'; |
| 108 |
} |
| 109 |
|
| 110 |
response.states.forEach((state) => { |
| 111 |
stateSelect.append($('<option></option>').val(state.state_2_code).text(state.state_name)); |
| 112 |
}); |
| 113 |
|
| 114 |
stateSelect.closest('.state-field').show(); |
| 115 |
} catch (err) { |
| 116 |
stateSelect.closest('.state-field').hide(); |
| 117 |
} |
| 118 |
}); |
| 119 |
}); |
| 120 |
})(jQuery); |
| 121 |
</script> |