Skip to content

Commit

Permalink
Fix issue with address not getting formatted correctly
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com>
  • Loading branch information
sampoyigi committed Feb 22, 2022
1 parent ec41dcd commit 0576d56
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 27 deletions.
8 changes: 1 addition & 7 deletions app/system/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,7 @@ protected function registerSingletons()
});

App::singleton('country', function ($app) {
$country = new Libraries\Country;

$country->setDefaultFormat("{address_1}\n{address_2}\n{city} {postcode}\n{state}\n{country}", [
'{address_1}', '{address_2}', '{city}', '{postcode}', '{state}', '{country}',
]);

return $country;
return new Libraries\Country;
});

App::instance('path.uploads', base_path(Config::get('system.assets.media.path', 'assets/media/uploads')));
Expand Down
45 changes: 25 additions & 20 deletions app/system/libraries/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class Country

const ISO_CODE_3 = 3;

protected $defaultFormat = [];
protected $defaultFormat = [
"{address_1}\n{address_2}\n{city} {postcode}\n{state}\n{country}",
];

protected $requiredAddressKeys = [
'address_1',
Expand All @@ -28,15 +30,19 @@ class Country

public function addressFormat($address, $useLineBreaks = TRUE)
{
[$format, $placeholders] = $this->getDefaultFormat();
$format = $this->getDefaultFormat();

$address = $this->evalAddress($address);

// Override format if present in address array
if (!empty($address['format']))
$format = $address['format'];

$formattedAddress = str_replace(['\r\n', '\r', '\n'], '<br />',
preg_replace(['/\s\s+/', '/\r\r+/', '/\n\n+/'], '<br />',
trim(str_replace($placeholders, $this->evalAddress($address), $format))
trim(str_replace([
'{address_1}', '{address_2}', '{city}', '{postcode}', '{state}', '{country}',
], array_except($address, 'format'), $format))
)
);

Expand Down Expand Up @@ -79,12 +85,10 @@ public function getCountryNameByCode($isoCodeTwo)

public function getDefaultFormat()
{
return $this->defaultFormat;
}
if ($defaultCountry = Countries_model::getDefault())
return $defaultCountry->format;

public function setDefaultFormat($format, $placeholders = [])
{
$this->defaultFormat = [$format, $placeholders];
return $this->defaultFormat;
}

public function listAll($column = null, $key = 'country_id')
Expand All @@ -104,28 +108,29 @@ protected function evalAddress($address)

$result = [];
foreach ($this->requiredAddressKeys as $key) {
$value = isset($address[$key]) ? $address[$key] : '';

if ($key == 'country') {
$value = $this->processCountryValue($value);
$this->processCountryValue($address[$key], $result);
}
else {
$result[$key] = $address[$key] ?? '';
}

$result[$key] = $value;
}

return $result;
}

protected function processCountryValue($country)
protected function processCountryValue($country, &$result)
{
if (is_numeric($country)) {
return $this->getCountryNameById($country);
if (!is_string($country) && isset($country['country_name'])) {
$result['country'] = $country['country_name'];
$result['format'] = $country['format'];
}
elseif (!is_string($country) && isset($country['country_name'])) {
return $country['country_name'];
elseif (is_numeric($country)) {
if ($countryModel = $this->countriesCollection->find($country)) {
$result['country'] = $countryModel->country_name;
$result['format'] = $countryModel->format;
}
}

return $country;
}

protected function loadCountries()
Expand Down
41 changes: 41 additions & 0 deletions app/system/models/Countries_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Igniter\Flame\Database\Model;
use Igniter\Flame\Database\Traits\Sortable;
use Igniter\Flame\Exception\ValidationException;

/**
* Countries Model Class
Expand Down Expand Up @@ -39,11 +40,51 @@ class Countries_model extends Model

public $timestamps = TRUE;

/**
* @var self Default country cache.
*/
protected static $defaultCountry;

public static function getDropdownOptions()
{
return static::isEnabled()->dropdown('country_name');
}

public function makeDefault()
{
if (!$this->status) {
throw new ValidationException(['status' => sprintf(
lang('admin::lang.alert_error_set_default'), $this->country_name
)]);
}

setting('country_id', $this->country_id);
setting()->save();
}

/**
* Returns the default currency defined.
* @return self
*/
public static function getDefault()
{
if (self::$defaultCountry !== null) {
return self::$defaultCountry;
}

$defaultCountry = self::isEnabled()
->where('country_id', setting('country_id'))
->first();

if (!$defaultCountry) {
if ($defaultCountry = self::whereIsEnabled()->first()) {
$defaultCountry->makeDefault();
}
}

return self::$defaultCountry = $defaultCountry;
}

//
// Scopes
//
Expand Down

0 comments on commit 0576d56

Please sign in to comment.