From: Stéphane Jacob Date: Sun, 28 Nov 2010 12:44:24 +0000 (+0100) Subject: Moves postal address formating to Address class. X-Git-Tag: xorg/1.0.2~120 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=59e8fb002f615cb6ff7a861c06a1d01d8905375e;p=platal.git Moves postal address formating to Address class. Signed-off-by: Stéphane Jacob --- diff --git a/classes/address.php b/classes/address.php index a5c651c..61fba3d 100644 --- a/classes/address.php +++ b/classes/address.php @@ -74,7 +74,6 @@ class Address public $east = null; public $west = null; public $geocodedText = null; - public $geocodedPostalText = null; public $geocodeChosen = null; // Database's field required for both 'home' and 'job' addresses. @@ -142,11 +141,56 @@ class Address return ($this->flags != null && $this->flags->hasFlag($flag)); } + // Returns the address formated for postal use. + // The main rules are (cf AFNOR XPZ 10-011): + // -everything in upper case; + // -if there are more then than 38 characters in a line, split it; + // -if there are more then than 32 characters in the description of the "street", use abbreviations. + public function formatPostalAddress() { + static $abbreviations = array( + 'IMPASSE' => 'IMP', + 'RUE' => 'R', + 'AVENUE' => 'AV', + 'BOULEVARD' => 'BVD', + 'ROUTE' => 'R', + 'STREET' => 'ST', + 'ROAD' => 'RD', + ); + + $text = strtoupper($text); + $arrayText = explode("\n", $text); + $postalText = ''; + + foreach ($arrayText as $i => $line) { + $postalText .= (($i == 0) ? '' : "\n"); + if (($length = strlen($line)) > 32) { + $words = explode(' ', $line); + $count = 0; + foreach ($words as $word) { + if (isset($abbreviations[$word])) { + $word = $abbreviations[$word]; + } + if ($count + ($wordLength = strlen($word)) <= 38) { + $postalText .= (($count == 0) ? '' : ' ') . $word; + $count += (($count == 0) ? 0 : 1) + $wordLength; + } else { + $postalText .= "\n" . $word; + $count = strlen($word); + } + } + } else { + $postalText .= $line; + } + } + $this->postalText = $postalText; + } + public function format(array $format = array()) { if (empty($format)) { $format['requireGeocoding'] = false; $format['stripGeocoding'] = false; + $format['postalText'] = false; } $this->text = trim($this->text); if ($this->removed == 1) { @@ -170,6 +214,9 @@ class Address $mailer->send(); } } + if ($format['postalText']) { + $this->formatPostalAddress(); + } if ($this->countryId == '') { $this->countryId = null; } @@ -204,7 +251,6 @@ class Address ); if (!is_null($this->geocodedText)) { $address['geocodedText'] = $this->geocodedText; - $address['geocodedPostalText'] = $this->geocodedPostalText; $address['geocodeChosen'] = $this->geocodeChosen; } @@ -262,7 +308,7 @@ class Address { static $areas = array('administrativeArea', 'subAdministrativeArea', 'locality'); - $this->format(); + $this->format(array('postalText')); if (!$this->isEmpty()) { foreach ($areas as $area) { Geocoder::getAreaId($this, $area); diff --git a/classes/gmapsgeocoder.php b/classes/gmapsgeocoder.php index 405bad1..edae5c5 100644 --- a/classes/gmapsgeocoder.php +++ b/classes/gmapsgeocoder.php @@ -64,7 +64,6 @@ class GMapsGeocoder extends Geocoder { public function stripGeocodingFromAddress(Address &$address) { $address->geocodedText = null; - $address->geocodedPostalText = null; $address->geoloc_choice = null; $address->countryId = null; $address->country = null; @@ -96,7 +95,6 @@ class GMapsGeocoder extends Geocoder { // Prepares address to be geocoded private function prepareAddress(Address &$address) { $address->text = preg_replace('/\s*\n\s*/m', "\n", trim($address->text)); - $address->postalText = $this->getPostalAddress($address->text); } // Builds the Google Maps geocoder url to fetch information about @p address. @@ -245,7 +243,6 @@ class GMapsGeocoder extends Geocoder { if ($extraLines) { $address->geocodedText = $extraLines . "\n" . $address->geocodedText; } - $address->geocodedPostalText = $this->getPostalAddress($address->geocodedText); $geoloc = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), array('', "\n"), $address->geocodedText)); $text = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), @@ -273,57 +270,10 @@ class GMapsGeocoder extends Geocoder { if ($same) { $address->geocodedText = null; - $address->geocodedPostalText = null; } else { $address->geocodedText = str_replace("\n", "\r\n", $address->geocodedText); - $address->geocodedPostalText = str_replace("\n", "\r\n", $address->geocodedPostalText); } $address->text = str_replace("\n", "\r\n", $address->text); - $address->postalText = str_replace("\n", "\r\n", $address->postalText); - } - - // Returns the address formated for postal use. - // The main rules are (cf AFNOR XPZ 10-011): - // -everything in upper case; - // -if there are more then than 38 characters in a line, split it; - // -if there are more then than 32 characters in the description of the "street", use abbreviations. - private function getPostalAddress($text) { - static $abbreviations = array( - 'IMPASSE' => 'IMP', - 'RUE' => 'R', - 'AVENUE' => 'AV', - 'BOULEVARD' => 'BVD', - 'ROUTE' => 'R', - 'STREET' => 'ST', - 'ROAD' => 'RD', - ); - - $text = strtoupper($text); - $arrayText = explode("\n", $text); - $postalText = ''; - - foreach ($arrayText as $i => $line) { - $postalText .= (($i == 0) ? '' : "\n"); - if (($length = strlen($line)) > 32) { - $words = explode(' ', $line); - $count = 0; - foreach ($words as $word) { - if (isset($abbreviations[$word])) { - $word = $abbreviations[$word]; - } - if ($count + ($wordLength = strlen($word)) <= 38) { - $postalText .= (($count == 0) ? '' : ' ') . $word; - $count += (($count == 0) ? 0 : 1) + $wordLength; - } else { - $postalText .= "\n" . $word; - $count = strlen($word); - } - } - } else { - $postalText .= $line; - } - } - return $postalText; } // Trims the name of the real country if it contains an ISO 3166-1 non-country diff --git a/htdocs/javascript/profile.js b/htdocs/javascript/profile.js index 7e6b683..dcd3fec 100644 --- a/htdocs/javascript/profile.js +++ b/htdocs/javascript/profile.js @@ -327,11 +327,10 @@ function validGeoloc(prefid, id, geoloc) { if (geoloc == 1) { $('#' + prefid + '_cont').find('[name*=text]').val($('#' + prefid + '_cont').find('[name*=geocodedText]').val()); - $('#' + prefid + '_cont').find('[name*=postalText]').val($('#' + prefid + '_cont').find('[name*=geocodedPostalText]').val()); + $('#' + prefid + '_cont').find('[name*=postalText]').val(''); } if (geoloc > 0) { $('#' + prefid + '_cont').find("[name*='[geocodedText]']").remove(); - $('#' + prefid + '_cont').find("[name*='[geocodedPostalText]']").remove(); } $('#' + prefid + '_cont').find('[name*=text]').removeClass('error'); $('#' + prefid + '_cont').find('[name*=geocodeChosen]').val(geoloc); diff --git a/templates/geoloc/form.address.tpl b/templates/geoloc/form.address.tpl index 2ef9f3c..190441e 100644 --- a/templates/geoloc/form.address.tpl +++ b/templates/geoloc/form.address.tpl @@ -45,7 +45,6 @@ {if t($address.geocodedText)} - {/if}