Adds a postal formated address to the addresses data.
authorStéphane Jacob <jacou@melix.net>
Sat, 21 Feb 2009 16:03:38 +0000 (17:03 +0100)
committerStéphane Jacob <jacou@melix.net>
Sat, 21 Feb 2009 16:03:38 +0000 (17:03 +0100)
htdocs/javascript/profile.js
include/geocoding.inc.php
modules/profile/addresses.inc.php
templates/geoloc/form.address.tpl

index 19589e4..efcfe07 100644 (file)
@@ -343,6 +343,7 @@ function validGeoloc(id, geoloc)
 {
     if (geoloc == 1) {
         $('#addresses_' + id + '_cont').find('[name*=text]').val($('#addresses_' + id + '_cont').find('[name*=geoloc]').val());
+        $('#addresses_' + id + '_cont').find('[name*=postalText]').val($('#addresses_' + id + '_cont').find('[name*=geocodedPostalText]').val());
     }
     $('#addresses_' + id + '_cont').find('[name*=text]').removeClass('error');
     $('#addresses_' + id + '_cont').find('[name*=geoloc_choice]').val(geoloc);
index c888a85..8052070 100644 (file)
@@ -121,8 +121,8 @@ class GMapsGeocoder extends Geocoder {
     }
 
     public function stripGeocodingFromAddress(array $address) {
-        unset($address['geoloc'], $address['geoloc_choice'], $address['countryId'],
-              $address['country'], $address['administrativeAreaName'],
+        unset($address['geoloc'], $address['geoloc_choice'], $address['geocodedPostalText'],
+              $address['countryId'], $address['country'], $address['administrativeAreaName'],
               $address['subAdministrativeAreaName'], $address['localityName'],
               $address['thoroughfareName'], $address['postalCode']);
         $address['accuracy'] = 0;
@@ -163,7 +163,7 @@ class GMapsGeocoder extends Geocoder {
     // Prepares address to be geocoded
     private function prepareAddress($address) {
         $address['text'] = preg_replace('/\s*\n\s*/m', "\n", trim($address['text']));
-        // TODO: $address['postalAddress'] = getPostalAddress($address['text']);
+        $address['postalText'] = $this->getPostalAddress($address['text']);
         $address['updateTime'] = time();
         unset($address['changed']);
         return $address;
@@ -316,6 +316,7 @@ class GMapsGeocoder extends Geocoder {
         if ($extraLines) {
             $address['geoloc'] = $extraLines . "\n" . $address['geoloc'];
         }
+        $address['geocodedPostalText'] = $this->getPostalAddress($address['geoloc']);
         $geoloc = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"),
                                           array("", "\n"), $address['geoloc']));
         $text   = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"),
@@ -338,10 +339,55 @@ class GMapsGeocoder extends Geocoder {
         }
         if ($same) {
             $address['text'] = $address['geoloc'];
-            unset($address['geoloc']);
+            $address['postalText'] = $address['geocodedPostalText'];
+            unset($address['geoloc'], $address['geocodedPostalText']);
         }
     }
  
+    // 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 lign, 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 => $lign) {
+            $postalText .= (($i == 0) ? "" : "\n");
+            if (($length = strlen($lign)) > 32) {
+                $words = explode(" ", $lign);
+                $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 .= $lign;
+            }
+        }
+        return $postalText;
+    }
+
     // Search for the lign from the given address that is the closest to the geocoded thoroughfareName
     // and replaces the corresponding lign in the geocoded text by it.
     static protected function fixStreetNumber(&$address)
index 5388595..d073956 100644 (file)
@@ -126,7 +126,7 @@ class ProfileAddress extends ProfileGeocoding
                            VALUES  ({?}, 'home', {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
                                     {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?}, {?}, {?})",
                      S::i('uid'), $addrid, $flags, $address['accuracy'],
-                     $address['text'], 'postalText'/*$address['postalText']*/, $address['postalCode'], $address['localityId'],
+                     $address['text'], $address['postalText'], $address['postalCode'], $address['localityId'],
                      $address['subAdministrativeAreaId'], $address['administrativeAreaId'],
                      $address['countryId'], $address['latitude'], $address['longitude'],
                      $address['updateTime'], $address['pub'], $address['comment'],
index 0f5cc34..cf589af 100644 (file)
@@ -43,6 +43,7 @@
 {if $address.geoloc}
 <input type="hidden" name="{$prefname}[geoloc_choice]" value="1" />
 <input type="hidden" name="{$prefname}[geoloc]" value="{$address.geoloc}" />
+<input type="hidden" name="{$prefname}[geocodedPostalText]" value="{$address.geocodedPostalText}" />
 <input type="hidden" name="{$prefname}[updateTime]" value="{$address.updateTime}" />
 {/if}
 <input type="hidden" name="{$prefname}[type]" value="{$address.type}" />