Просмотр исходного кода

#1519 Location improvements: map object

Alex Trofimov 5 лет назад
Родитель
Сommit
6b04f4ce75

+ 91 - 0
inc/classes/BxDolLocationMap.php

@@ -0,0 +1,91 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaCore UNA Core
+ * @{
+ */
+
+/**
+ * This class allow different implementations for displaying maps.
+ *
+ * Add record to sys_objects_location_map table, like you are doing this for Comments or Voting objects:
+ * - object: your object name, usually it is in the following format - vendor prefix, underscore, module prefix;
+ * - module: module name
+ * - title: translatable title
+ * - class_name: user defined class name which is derived from BxDolLocationField.
+ * - class_file: the location of the user defined class, leave it empty if class is located in system folders.
+ */
+class BxDolLocationMap extends BxDolFactory implements iBxDolFactoryObject
+{
+	protected $_oDb;
+	protected $_sObject;
+    protected $_aObject;
+    protected $_oTemplate;
+
+    /**
+     * Constructor
+     */
+    protected function __construct($aObject, $oTemplate = null)
+    {
+        parent::__construct();
+
+        $this->_aObject = $aObject;
+        $this->_sObject = $aObject['object'];
+        $this->_oTemplate = $oTemplate ? $oTemplate : BxDolTemplate::getInstance();
+        $this->_oDb = new BxDolLocationMapQuery($this->_aObject);
+    }
+
+   /**
+     * Get object instance by object name
+     * @param $sObject object name
+     * @return object instance or false on error
+     */
+    public static function getObjectInstance($sObject, $oTemplate = null)
+    {
+        if(isset($GLOBALS['bxDolClasses']['BxDolLocationMap!' . $sObject]))
+            return $GLOBALS['bxDolClasses']['BxDolLocationMap!' . $sObject];
+
+        $aObject = BxDolLocationMapQuery::getLocationMapObject($sObject);
+        if (!$aObject || !is_array($aObject))
+            return false;
+
+        $sClass = 'BxDolLocationMap';
+        if(!empty($aObject['class_name'])) {
+            $sClass = $aObject['class_name'];
+            if(!empty($aObject['class_file']))
+                require_once(BX_DIRECTORY_PATH_ROOT . $aObject['class_file']);
+        }        
+
+        $o = new $sClass($aObject, $oTemplate);
+        return ($GLOBALS['bxDolClasses']['BxDolLocationMap!' . $sObject] = $o);
+    }
+
+    /**
+     * Get current object name
+     */
+    public function getObjectName()
+    {
+        return $this->_aObject['object'];
+    }
+
+    /**
+     * Get location map for single address
+     * @param $aLocation location array with the following indexes: 
+     *          lat, lng, country, state, city, zip, street, street_number
+     * @param $sLocationHtml formatted address string
+     * @param $aParams some specific params
+     */
+    public function getMapSingle($aLocation, $sLocationHtml = '', $aParams = array())
+    {
+        // override this method
+        return '';
+    }
+
+    public function addCssJs()
+    {
+    }
+}
+
+/** @} */

+ 53 - 0
inc/classes/BxDolLocationMapGoogleStatic.php

@@ -0,0 +1,53 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaCore UNA Core
+ * @{
+ */
+
+class BxDolLocationMapGoogleStatic extends BxDolLocationMap
+{
+    /**
+     * Get location map for single address
+     * @param $aLocation location array with the following indexes: 
+     *          lat, lng, country, state, city, zip, street, street_number
+     * @param $sLocationHtml formatted address string
+     * @param $aParams some specific params
+     */
+    public function getMapSingle($aLocation, $sLocationHtml = '', $aParams = array())
+    {
+        $sMapSize = '1000x144';
+        if (!empty($aParams['location_map_google_static_size']))
+            $sMapSize = $aParams['location_map_google_static_size'];
+
+        $sZoom = '7';    
+        if (!empty($aParams['location_map_google_static_zoom']))
+            $sZoom = $aParams['location_map_google_static_zoom'];
+
+        $sMapType = 'roadmap';    
+        if (!empty($aParams['location_map_google_static_maptype']))
+            $sMapType = $aParams['location_map_google_static_maptype'];
+
+		$sMapKey = trim(getParam('sys_maps_api_key'));
+        $sLocationEncoded = rawurlencode(trim(strip_tags($sLocationHtml)));
+        $iScale = isset($_COOKIE['devicePixelRatio']) && (int)$_COOKIE['devicePixelRatio'] >= 2 ? 2 : 1;
+        $sLang = bx_lang_name();
+
+        $this->addCssJs();
+
+        return $this->_oTemplate->parseHtmlByName('location_map_google_static.html', array (
+            'map_img' => bx_proto() . '://maps.googleapis.com/maps/api/staticmap?center=' . $sLocationEncoded . '&zoom=' . $sZoom . '&size=' . $sMapSize . '&maptype=' . $sMapType . '&markers=size:small%7C' . $sLocationEncoded . '&scale=' . $iScale . '&language=' . $sLang  . ($sMapKey ? '&key=' . $sMapKey : ''),
+            'location_string' => $sLocationHtml,
+        ));
+    }
+
+    public function addCssJs()
+    {
+        parent::addCssJs();
+        $this->_oTemplate->addCss('location_map_google_static.css');
+    }
+}
+
+/** @} */

+ 33 - 0
inc/classes/BxDolLocationMapQuery.php

@@ -0,0 +1,33 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaCore UNA Core
+ * @{
+ */
+
+class BxDolLocationMapQuery extends BxDolDb
+{
+
+    public function __construct($aObject = array())
+    {
+        parent::__construct();
+        $this->_aObject = $aObject;
+    }
+
+    static public function getLocationMapObject($sObject)
+    {
+        $oDb = BxDolDb::getInstance();
+
+        $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_location_map` WHERE `object` = ?", $sObject);
+
+        $aObject = $oDb->getRow($sQuery);
+        if(!$aObject || !is_array($aObject))
+            return false;
+
+        return $aObject;
+    }
+}
+
+/** @} */

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
install/sql/system.sql


+ 15 - 0
template/css/location_map_google_static.css

@@ -0,0 +1,15 @@
+.bx-metatags-locations-map {
+    position:relative;
+    height:144px;
+    background-position:center center;
+    background-repeat:no-repeat;
+    background-size:cover;
+}
+
+.bx-metatags-locations-map-string {
+    position:absolute;
+    bottom:0;
+    left:0;
+    text-shadow:1px 1px 0.125rem #fff;
+    background-color:rgba(255,255,255,0.7);
+}

+ 0 - 15
template/css/metatags.css

@@ -6,18 +6,3 @@
     line-height:2rem;
     margin: 0 0.25rem 0 0;
 }
-
-.bx-metatags-locations-map {
-    position:relative;
-    height:144px;
-    background-position:center center;
-    background-repeat:no-repeat;
-}
-
-.bx-metatags-locations-map-string {
-    position:absolute;
-    bottom:0;
-    left:0;
-    text-shadow:1px 1px 0.125rem #fff;
-    background-color:rgba(255,255,255,0.7);
-}

+ 3 - 0
template/location_map_google_static.html

@@ -0,0 +1,3 @@
+<div class="bx-metatags-locations-map bx-def-round-corners bx-def-color-bg-box" style="background-image:url(__map_img__);">
+    <div class="bx-metatags-locations-map-string bx-def-font-grayed bx-def-padding-sec bx-def-round-corners">__location_string__</div>
+</div>

+ 10 - 11
template/scripts/BxBaseMetatags.php

@@ -99,23 +99,22 @@ class BxBaseMetatags extends BxDolMetatags
         return $this->_oTemplate->parseHtmlByName('metatags_keywords_cloud.html', $aVars);
     }
 
-	public function getLocationsMap($iId, $sMapSize = '1000x144')
+	public function getLocationsMap($iId, $aParams = array())
     {
+        $aLocation = $this->locationGet($iId);
+        if(!$aLocation)
+            return '';
+
         $sLocationHtml = $this->locationsString($iId);
         if(!$sLocationHtml)
 			return '';
 
-		$sMapKey = trim(getParam('sys_maps_api_key'));
-        $sLocationEncoded = rawurlencode(strip_tags($sLocationHtml));
-        $sProto = (0 === strncmp('https', BX_DOL_URL_ROOT, 5)) ? 'https' : 'http';
-        $iScale = isset($_COOKIE['devicePixelRatio']) && (int)$_COOKIE['devicePixelRatio'] >= 2 ? 2 : 1;
-        $sLang = bx_lang_name();
+        // TODO: add param for map object
+        $o = BxDolLocationMap::getObjectInstance('sys_google_static',  $this->_oTemplate);
+        if(!$o)
+			return '';
 
-        $this->addCssJs();
-        return $this->_oTemplate->parseHtmlByName('metatags_locations_map.html', array (
-            'map_img' => $sProto . '://maps.googleapis.com/maps/api/staticmap?center=' . $sLocationEncoded . '&zoom=7&size=' . $sMapSize . '&maptype=roadmap&markers=size:small%7C' . $sLocationEncoded . '&scale=' . $iScale . '&language=' . $sLang  . ($sMapKey ? '&key=' . $sMapKey : ''),
-            'location_string' => $sLocationHtml,
-        ));
+        return $o->getMapSingle($aLocation, $sLocationHtml, $aParams);
     }
 
     public function addCssJs()

+ 2 - 2
template/scripts/BxBaseServiceMetatags.php

@@ -69,9 +69,9 @@ class BxBaseServiceMetatags extends BxDol
      * @param $iId content id
      * @return map HTML string
      */
-    public function serviceLocationsMap($sObject, $iId, $sMapSize = '1000x144')
+    public function serviceLocationsMap($sObject, $iId, $aParams = array())
     {
-        return BxDolMetatags::getObjectInstance($sObject)->getLocationsMap($iId, $sMapSize);
+        return BxDolMetatags::getObjectInstance($sObject)->getLocationsMap($iId, $aParams);
     }
     
 	/**

Некоторые файлы не были показаны из-за большого количества измененных файлов