Moves postal address formating to Address class.
authorStéphane Jacob <sj@m4x.org>
Sun, 28 Nov 2010 12:44:24 +0000 (13:44 +0100)
committerStéphane Jacob <sj@m4x.org>
Wed, 1 Dec 2010 12:37:46 +0000 (13:37 +0100)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
classes/address.php
classes/gmapsgeocoder.php
htdocs/javascript/profile.js
templates/geoloc/form.address.tpl

index a5c651c..61fba3d 100644 (file)
@@ -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);
index 405bad1..edae5c5 100644 (file)
@@ -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
index 7e6b683..dcd3fec 100644 (file)
@@ -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);
index 2ef9f3c..190441e 100644 (file)
@@ -45,7 +45,6 @@
 {if t($address.geocodedText)}
 <input type="hidden" name="{$prefname}[geocodeChosen]" value="1" />
 <input type="hidden" name="{$prefname}[geocodedText]" value="{$address.geocodedText}" />
-<input type="hidden" name="{$prefname}[geocodedPostalText]" value="{$address.geocodedPostalText}" />
 {/if}
 <input type="hidden" name="{$prefname}[accuracy]" value="{$address.accuracy}" />
 <input type="hidden" name="{$prefname}[postalText]" value="{$address.postalText}" />