Jelajahi Sumber

#644 Location field related enhancements

Alex Trofimov 9 tahun lalu
induk
melakukan
37b45d5258
2 mengubah file dengan 60 tambahan dan 1 penghapusan
  1. 53 0
      inc/classes/BxDolMetatags.php
  2. 7 1
      template/scripts/BxBaseFormView.php

+ 53 - 0
inc/classes/BxDolMetatags.php

@@ -447,6 +447,59 @@ class BxDolMetatags extends BxDolFactory implements iBxDolFactoryObject
         );
     }
 
+    /**
+     * Parse google's formatted address components into array with the followin indexes:
+     * lat, lng, country, state, city, zip, street, street_number
+     */ 
+    public static function locationsParseAddressComponents($aAdress, $sPrefix = '')
+    {
+        if (!isset($aAdress['address_components']))
+            return false;
+        if (!isset($aAdress['geometry']['location']))
+            return false;
+
+        $aRet = array(
+            $sPrefix . 'lat' => $aAdress['geometry']['location']['lat'],
+            $sPrefix . 'lng' => $aAdress['geometry']['location']['lng'],
+        );
+
+        $aMap = array(
+            $sPrefix . 'city' => 'locality',
+            $sPrefix . 'state' => 'administrative_area_level_1',
+            $sPrefix . 'country' => 'country',
+            $sPrefix . 'zip' => 'postal_code',
+            $sPrefix . 'street' => 'route',
+            $sPrefix . 'street_number' => 'street_number'        
+        );
+
+        $aAdressComponents = $aAdress['address_components'];
+        
+        foreach ($aAdressComponents as $r) {
+            if (!isset($r['types']))
+                continue;
+            foreach ($aMap as $sKey => $sName) {
+                
+                if ('locality' == $sName && !in_array($sName, $r['types']))
+                    $sName = 'postal_town';
+                
+                if ('administrative_area_level_1' == $sName && !in_array($sName, $r['types']))
+                    $sName = 'administrative_area_level_2';
+
+                if (in_array($sName, $r['types'])) {
+                    $sIndex = 'route' == $sName || 'locality' == $sName ? 'long_name' : 'short_name';
+                    $aRet[$sKey] = $r[$sIndex];
+                }
+
+                if (in_array('locality', $r['types']))
+                    $aRet[$sPrefix . 'city'] = $r['short_name'];
+                else if (in_array('country', $r['types']))
+                    $aRet[$sPrefix . 'country'] = $r['short_name'];
+            }
+        }
+        
+        return $aRet;
+    }
+    
     /**
      * Add location for the content from POST data
      * @param $iId content id

+ 7 - 1
template/scripts/BxBaseFormView.php

@@ -1224,12 +1224,18 @@ BLAH;
                 $aInput['checked'] = true;
             else
                 $aInput['checked'] = $this->getCleanValue($aInput['name'] . '_lat') && $this->getCleanValue($aInput['name'] . '_lng') ? 1 : 0;
-            $aVars['input'] = $isManualInput ? $this->genInputStandard($aInput) : $this->genInputSwitcher($aInput);
+            $aVars['input'] = $this->genInputSwitcher($aInput);
         }
 
         return $this->oTemplate->parseHtmlByName('form_field_location.html', $aVars);
     }
 
+    public function setLocationVal ($aInput, $sIndex, $sVal)
+    {
+        $s = $aInput['name'] . '_' . $sIndex;
+        $this->_aSpecificValues[$s] = $sVal;
+    }
+
     protected function getLocationVal ($aInput, $sIndex) 
     {
         $s = $aInput['name'] . '_' . $sIndex;