From 928c303e472d8f2e5192c01ae9f48309e9dfdcce Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Jacob?= Date: Sat, 25 Dec 2010 18:19:07 +0100 Subject: [PATCH] Improves country detection for postal addresses formatting. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Jacob --- classes/address.php | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/classes/address.php b/classes/address.php index d7f7970..7757e02 100644 --- a/classes/address.php +++ b/classes/address.php @@ -547,19 +547,26 @@ class Address $arrayText = explode("\n", $text); $arrayText = array_map('trim', $arrayText); - // Search for country. - $countries = DirEnum::getOptions(DirEnum::COUNTRIES); - $countries = array_map('replace_accent', $countries); - $countries = array_map('strtoupper', $countries); + // Formats according to country rules. Thus we first identify the + // country, then apply corresponding formatting or translate country + // into default language. $count = count($arrayText); if (in_array(strtoupper($this->countryId), Address::$formattings)) { $text = call_user_func(array($this, 'formatPostalAddress' . strtoupper($this->countryId)), $arrayText); - } elseif (array_key_exists($arrayText[$count - 1], Address::$formattings)) { - $text = call_user_func(array($this, 'formatPostalAddress' . Address::$formattings[$arrayText[$count - 1]]), $arrayText); - } elseif (!in_array($arrayText[$count - 1], $countries)) { - $text = $this->formatPostalAddressFR($arrayText); } else { - $text = implode("\n", $arrayText); + list($countryId, $country) = XDB::fetchOneRow('SELECT gc.iso_3166_1_a2, gc.country + FROM geoloc_countries AS gc + INNER JOIN geoloc_languages AS gl ON (gc.iso_3166_1_a2 = gl.iso_3166_1_a2) + WHERE gc.iso_3166_1_a2 = {?} OR gl.countryPlain = {?} OR gc.countryPlain = {?}', + $this->countryId, $arrayText[$count - 1], $arrayText[$count - 1]); + if (is_null($countryId)) { + $text = $this->formatPostalAddressFR($arrayText); + } elseif (in_array(strtoupper($countryId), Address::$formattings)) { + $text = call_user_func(array($this, 'formatPostalAddress' . strtoupper($countryId)), $arrayText); + } else { + $arrayText[$count - 1] = mb_strtoupper(replace_accent($country)); + $text = implode("\n", $arrayText); + } } $this->postalText = $text; -- 2.1.4