Skip to content

Commit

Permalink
Merge pull request #67 from JerrySmidt/3.1.9
Browse files Browse the repository at this point in the history
3.1.9
  • Loading branch information
JerrySmidt authored Jan 5, 2023
2 parents 717a27f + e28db69 commit 52a2627
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 41 deletions.
5 changes: 5 additions & 0 deletions Block/Onepage/LayoutProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public function process($jsLayout)
continue;
}

// Make sure form fields exist.
if (!isset($billingForm['children']['form-fields'])) {
continue;
}

// Billing fields
$billingForm['children']['form-fields']['children'] += $this->_updateCustomScope($autofillFields, $billingForm['dataScopePrefix']);
$billingForm['children']['form-fields']['children'] = $this->_changeAddressFieldsPosition($billingForm['children']['form-fields']['children']);
Expand Down
2 changes: 1 addition & 1 deletion Helper/ApiClientHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function getApiClient(): PostcodeApiClient
public function getJsinit(): array
{
$settings = [
'supported_countries' => array_column($this->_storeConfigHelper->getSupportedCountries(), 'iso2'),
'enabled_countries' => $this->_storeConfigHelper->getEnabledCountries(),
'nl_input_behavior' => $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['nl_input_behavior']) ?? \Flekto\Postcode\Model\Config\Source\NlInputBehavior::ZIP_HOUSE,
'show_hide_address_fields' => $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['show_hide_address_fields']) ?? \Flekto\Postcode\Model\Config\Source\ShowHideAddressFields::SHOW,
'base_url' => $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB),
Expand Down
55 changes: 48 additions & 7 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,78 @@

namespace Flekto\Postcode\Helper;

use Flekto\Postcode\Helper\StoreConfigHelper;
use Flekto\Postcode\Model\Config\Source\NlInputBehavior;
use Flekto\Postcode\Model\Config\Source\ShowHideAddressFields;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\ScopeInterface;

class Data extends AbstractHelper
{
/**
* Constructor
*
* @access public
* @param Context $context
* @param StoreConfigHelper $storeConfigHelper
* @return void
*/
public function __construct(
Context $context,
StoreConfigHelper $storeConfigHelper
) {
$this->_storeConfigHelper = $storeConfigHelper;
parent::__construct($context);
}

/**
* Check if formatted output is disabled.
*
* @access public
* @return bool
*/
public function isFormattedOutputDisabled() {
return
$this->isDisabled()
|| ShowHideAddressFields::FORMAT != $this->scopeConfig->getValue(StoreConfigHelper::PATH['show_hide_address_fields'], ScopeInterface::SCOPE_STORE);
|| ShowHideAddressFields::FORMAT != $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['show_hide_address_fields']);
}

/**
* Check if Dutch API component is disabled.
*
* @access public
* @return bool
*/
public function isNlComponentDisabled() {
return
$this->isDisabled()
|| NlInputBehavior::ZIP_HOUSE != $this->scopeConfig->getValue(StoreConfigHelper::PATH['nl_input_behavior'], ScopeInterface::SCOPE_STORE);
|| false === in_array('NL', $this->_storeConfigHelper->getEnabledCountries())
|| NlInputBehavior::ZIP_HOUSE != $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['nl_input_behavior']);
}

/**
* Check if the module is disabled.
*
* @access public
* @return bool
*/
public function isDisabled() {
return
false === $this->scopeConfig->isSetFlag('postcodenl_api/general/enabled', ScopeInterface::SCOPE_STORE)
|| ApiClientHelper::API_ACCOUNT_STATUS_ACTIVE != $this->scopeConfig->getValue(StoreConfigHelper::PATH['account_status'], ScopeInterface::SCOPE_STORE);
false === $this->_storeConfigHelper->isSetFlag(StoreConfigHelper::PATH['enabled'])
|| ApiClientHelper::API_ACCOUNT_STATUS_ACTIVE != $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['account_status']);
}

/**
* Check if autofill bypass is disabled.
*
* @access public
* @return bool
*/
public function isAutofillBypassDisabled() {
return
$this->isDisabled()
|| ShowHideAddressFields::SHOW == $this->scopeConfig->getValue(StoreConfigHelper::PATH['show_hide_address_fields'], ScopeInterface::SCOPE_STORE)
|| $this->scopeConfig->isSetFlag(StoreConfigHelper::PATH['allow_autofill_bypass'], ScopeInterface::SCOPE_STORE) === false;
|| ShowHideAddressFields::SHOW == $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['show_hide_address_fields'])
|| $this->_storeConfigHelper->isSetFlag(StoreConfigHelper::PATH['allow_autofill_bypass']) === false;
}

}
19 changes: 19 additions & 0 deletions Helper/StoreConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class StoreConfigHelper extends AbstractHelper

// Advanced
'api_debug' => 'postcodenl_api/advanced_config/api_debug',
'disabled_countries' => 'postcodenl_api/advanced_config/disabled_countries',

// Status
'module_version' => 'postcodenl_api/status/module_version',
Expand Down Expand Up @@ -73,6 +74,24 @@ public function getSupportedCountries(): array
return json_decode($this->getValue(static::PATH['supported_countries']) ?? '[]');
}

/**
* Get supported countries, excluding disabled countries.
*
* @access public
* @return array
*/
public function getEnabledCountries(): array
{
$supported = array_column($this->getSupportedCountries(), 'iso2');
$disabled = $this->getValue(static::PATH['disabled_countries']);

if (empty($disabled)) {
return $supported;
}

return array_values(array_diff($supported, explode(',', $disabled)));
}

/**
* Check if API credentials are set.
*
Expand Down
29 changes: 29 additions & 0 deletions Model/Config/Source/DisabledCountries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Flekto\Postcode\Model\Config\Source;

use Flekto\Postcode\Helper\StoreConfigHelper;

class DisabledCountries implements \Magento\Framework\Option\ArrayInterface
{
/**
* @param StoreConfigHelper $storeConfigHelper
*/
public function __construct(StoreConfigHelper $storeConfigHelper) {
$this->_storeConfigHelper = $storeConfigHelper;
}

public function toOptionArray()
{
$options = [];

foreach ($this->_storeConfigHelper->getSupportedCountries() as $country) {
$options[] = [
'value' => $country->iso2,
'label' => $country->name,
];
}

return $options;
}
}
7 changes: 6 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@
<group id="advanced_config" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label">
<label>Advanced Configuration</label>
<fieldset_css>postcode-eu-api-group postcode-eu-api-group-advanced</fieldset_css>
<field id="api_debug" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="90" translate="label" type="select" canRestore="1">
<field id="disabled_countries" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="90" translate="label" type="multiselect" canRestore="1">
<label>Countries to exclude</label>
<comment>Selected countries will have Address API disabled.</comment>
<source_model>Flekto\Postcode\Model\Config\Source\DisabledCountries</source_model>
</field>
<field id="api_debug" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="100" translate="label" type="select" canRestore="1">
<label>Debug</label>
<comment/>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
Expand Down
2 changes: 1 addition & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<default>
<postcodenl_api>
<status>
<module_version>3.1.8</module_version>
<module_version>3.1.9</module_version>
<account_status>new</account_status>
</status>
<general>
Expand Down
1 change: 1 addition & 0 deletions i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@
"An error has occurred while retrieving address data. Please contact us if the problem persists.","An error has occurred while retrieving address data. Please contact us if the problem persists."
"Please select a house number.","Please select a house number."
"Enter an address","Enter an address"
"Please select an address from metropolitan France.","Please select an address from metropolitan France."
1 change: 1 addition & 0 deletions i18n/nl_NL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@
"An error has occurred while retrieving address data. Please contact us if the problem persists.","Er is een fout opgetreden bij het ophalen van adres gegevens. Neem contact met ons op als het probleem blijft optreden."
"Please select a house number.","Selecteer alstublieft een huisnummer."
"Enter an address","Vul een adres in"
"Please select an address from metropolitan France.","Selecteer een adres uit Europees Frankrijk."
17 changes: 9 additions & 8 deletions view/frontend/web/js/form/element/address-autofill-intl.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ define([

this.additionalClasses['loading'] = this.loading;
this.address.subscribe(this.setInputAddress.bind(this));
this.validation['validate-overseas-france'] = {component: this};

return this;
},
Expand All @@ -38,26 +39,26 @@ define([
onChangeCountry: function (countryCode) {
this.reset();

if (this.settings.nl_input_behavior === 'zip_house' && countryCode === 'NL') {
const isEnabled = this.isEnabledCountry(countryCode);

if (isEnabled && this.settings.nl_input_behavior === 'zip_house' && countryCode === 'NL') {
this.visible(false);
return;
}

const isSupported = this.isSupportedCountry(countryCode);

this.visible(isSupported);
this.toggleFields(!isSupported, true);
this.visible(isEnabled);
this.toggleFields(!isEnabled, true);

if (isSupported && this.intlAutocompleteInstance !== null) {
if (isEnabled && this.intlAutocompleteInstance !== null) {
// Reset address fields on country change.
this.resetInputAddress();
this.intlAutocompleteInstance.reset();
this.intlAutocompleteInstance.setCountry(countryCode);
}
},

isSupportedCountry: function (countryCode) {
return this.settings.supported_countries.indexOf(countryCode) > -1;
isEnabledCountry: function (countryCode) {
return this.settings.enabled_countries.indexOf(countryCode) > -1;
},

isValid: function () {
Expand Down
2 changes: 1 addition & 1 deletion view/frontend/web/js/ko/bindings/init-intl-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ define([
viewModel.address(result[0]);
viewModel.toggleFields(true);
viewModel.loading(false);
viewModel.error(false);
viewModel.validate();
});
}
});
Expand Down
Loading

0 comments on commit 52a2627

Please sign in to comment.