Files
kupshop/web/common/templates/components/block.smartForm.tpl
2025-08-02 16:30:27 +02:00

219 lines
10 KiB
Smarty

{get_contexts country=1 assign='contexts'}
<script type="text/javascript">
if (!$form) {
var $form = $('[data-form="user"]');
}
var invoice_country = $('[name=\'invoice[country]\'], [name=\'country\']').val();
var delivery_country = $('[name=\'delivery[country]\'], [name=\'dcountry\']').val();
$('body').on('change', '[name*=\'country\']', function () {
var inputName = $(this).attr('name'),
instance = inputName.indexOf('[') > 0 ? inputName.substring(0, inputName.indexOf('[')) : (inputName === 'country' ? 'invoice' : 'delivery'),
country = $(this).val();
// UserAddressForms support
if (instance === "user_address_form") {
instance = 'delivery';
}
var data = {
inputName: inputName,
instance: instance,
country: country
};
$('body').trigger('countryChanged', [data]);
});
</script>
{if $dbcfg.smartform == "Y"}
<script type="text/javascript" src="https://client.smartform.cz/v2/smartform.js" async></script>
<script type="text/javascript">
var smartform = smartform || {};
smartform.beforeInit = function () {
smartform.setClientId('v9QX920R74');
wpj.smartform = smartform;
}
smartform.afterInit = function () {
smartform.getInstance('smartform-instance-invoice').addressControl.setAllSuggestionsEnabled((invoice_country === 'CZ' || invoice_country === 'SK'));
smartform.getInstance('smartform-instance-invoice').companyControl.setAllSuggestionsEnabled((invoice_country === 'CZ'));
smartform.getInstance('smartform-instance-invoice').addressControl.setCountry(invoice_country);
smartform.getInstance('smartform-instance-invoice').phoneControl.setDefaultPrefix(invoice_country);
smartform.getInstance('smartform-instance-invoice').phoneControl.addValidationCallback(function (result) {
phoneValidationCallback(result, $('.smartform-instance-invoice.smartform-phone-number'));
});
smartform.getInstance('smartform-instance-invoice').emailControl.addValidationCallback(emailValidationCallback);
smartform.getInstance('smartform-instance-delivery').addressControl.setAllSuggestionsEnabled((delivery_country === 'CZ' || delivery_country === 'SK'));
smartform.getInstance('smartform-instance-delivery').companyControl.setAllSuggestionsEnabled((delivery_country === 'CZ'));
smartform.getInstance('smartform-instance-delivery').addressControl.setCountry(delivery_country);
smartform.getInstance('smartform-instance-delivery').phoneControl.setDefaultPrefix(delivery_country);
smartform.getInstance('smartform-instance-delivery').phoneControl.addValidationCallback(function (result) {
phoneValidationCallback(result, $('.smartform-instance-delivery.smartform-phone-number'));
});
}
function handlePhoneFocusOut(instanceName, $input) {
var phone_number = $input.data('last-value') || "";
if ($input.val() !== phone_number) {
$input.data('last-value', $input.val());
} else {
const result = smartform.getInstance(instanceName).phoneControl.getLastValidationResult();
phoneValidationCallback(result, $input);
}
}
$('.smartform-phone-number').on('focusout', function () {
const instanceName = $(this).hasClass('smartform-instance-invoice')
? 'smartform-instance-invoice'
: 'smartform-instance-delivery';
handlePhoneFocusOut(instanceName, $(this));
});
{block "smartform-phone"}
function phoneValidationCallback(response, $input) {
if (response && $input.length) {
const field_name = $($input).attr('name')
console.log(response);
if (response.result.resultType === "VALID") {
$($input[0]).val(response.result.formattedNumber.INTERNATIONAL);
$form.data('bootstrapValidator').enableFieldValidators(field_name, true);
$form.data('bootstrapValidator').updateStatus(field_name, 'VALID', 'phone');
wpj.domUtils.resetTimer('smartFormPhoneRevalidate', 300, function () {
$form.data('bootstrapValidator').updateStatus(field_name, 'VALID', 'phone');
$form.find('[name="' + field_name + '"]').removeClass('validation-warning');
});
} else {
$form.data('bootstrapValidator').updateStatus(field_name, 'INVALID', 'phone');
wpj.domUtils.resetTimer('smartFormPhoneRevalidate', 300, function () {
// $form.data('bootstrapValidator').enableFieldValidators(field_name, false);
$form.data('bootstrapValidator').updateStatus(field_name, 'INVALID', 'phone');
});
}
}
}
{/block}
function emailValidationCallback(response) {
if (response) {
const $email_field = $('.smartform-email');
if (!$email_field.length) {
return;
}
const email_field_name = $email_field.attr('name');
const $validation_message_element = $('[data-smartform-email-validation-message]');
const messages = {
'BAD_DOMAIN': "{t}Špatně zadaná doména.{/t}",
'MAILBOX_NOT_FOUND': "{t}E-mailová schránka neexistuje.{/t}"
};
const message = messages[response.result.flags[0]];
if (message) {
if (!$validation_message_element.length) {
$email_field.after("<p class='help-block' data-smartform-email-validation-message>" + message + "</p>");
} else {
$validation_message_element.text(message);
}
} else {
$validation_message_element.hide();
}
switch (response.result.resultType) {
case 'EXISTS':
case 'UNKNOWN':
$validation_message_element.hide();
$form.data('bootstrapValidator').enableFieldValidators(email_field_name, true);
break;
case 'NOT_EXISTS':
$validation_message_element.show();
wpj.domUtils.resetTimer('smartFormPhoneRevalidate', 200, function () {
$form.data('bootstrapValidator').enableFieldValidators(email_field_name, false);
});
break;
default:
$validation_message_element.hide();
break;
}
}
}
$('body').on('countryChanged', function (e, data) {
if (data === undefined && e.detail) {
data = e.detail;
}
smartform.getInstance('smartform-instance-' + data['instance']).addressControl.setAllSuggestionsEnabled((data['country'] === 'CZ' || data['country'] === 'SK'));
smartform.getInstance('smartform-instance-' + data['instance']).companyControl.setAllSuggestionsEnabled((data['country'] === 'CZ'));
smartform.getInstance('smartform-instance-' + data['instance']).addressControl.setCountry(data['country']);
smartform.getInstance('smartform-instance-' + data['instance']).phoneControl.setDefaultPrefix(data['country']);
}).on('change blur', 'input[class*="smartform"]', function (e) {
var field = $(this).attr('name');
var $current_form = $(this).closest('form');
if (!$current_form.length) {
$current_form = $form;
}
wpj.domUtils.resetTimer('smartFormRevalidate', 200, function () {
if ($current_form.data('bootstrapValidator')) {
$current_form.data('bootstrapValidator').updateStatus(field, 'NOT_VALIDATED').revalidateField(field);
}
});
});
</script>
{/if}
{if $dbcfg.here_autocomplete == "Y"}
<script>
var iso_table = {
{foreach $contexts.country->getAll() as $country}
'{$country->getId()}': '{$country->getCode3()}',
{/foreach}
};
$('body')
.on('focus', '[data-field-invoice-street], [data-field-invoice-city], [data-field-invoice-zip], [data-field-invoice-countycode]', function (e) {
var $this = $(this);
var invoice_country = $('[name=\'invoice[country]\'], [name=\'country\']').val();
var resultsType = null;
if (typeof $this.attr('data-field-invoice-city') !== 'undefined') {
resultsType = "city";
} else if (typeof $this.attr('data-field-invoice-zip') !== 'undefined') {
resultsType = "postalCode";
} else if (typeof $this.attr('data-field-invoice-countycode') !== 'undefined') {
resultsType = "area";
}
$this.hereAddressAutocomplete({
input: $this,
type: 'invoice',
countryCode: iso_table[invoice_country],
resultsType: resultsType
});
})
.on('focus', '[data-field-delivery-street], [data-field-delivery-city], [data-field-delivery-zip], [data-field-delivery-countycode]', function (e) {
var $this = $(this);
var delivery_country = $('[name=\'delivery[country]\'], [name=\'dcountry\']').val();
var resultsType = null;
if (typeof $this.attr('data-field-delivery-city') !== 'undefined') {
resultsType = "city";
} else if (typeof $this.attr('data-field-delivery-zip') !== 'undefined') {
resultsType = "postalCode";
} else if (typeof $this.attr('data-field-delivery-countycode') !== 'undefined') {
resultsType = "area";
}
$this.hereAddressAutocomplete({
input: $this,
type: 'delivery',
countryCode: iso_table[delivery_country],
resultsType: resultsType
});
})
.on('change blur', '[data-field-delivery-street], [data-field-delivery-city], [data-field-delivery-zip], [data-field-invoice-city],[data-field-invoice-street], [data-field-invoice-zip]', function (e) {
var field = $(this).attr('name');
$form.data('bootstrapValidator').updateStatus(field, 'NOT_VALIDATED').revalidateField(field);
});
</script>
{/if}