Advertisement
Guest User

Perch Custom Map Field

a guest
Jan 25th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.33 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Perch Map fieldtype extended by Oliver Lowe
  5.  *
  6.  * The aim is to move the field hint up the page to a more useful position
  7.  * underneath the address field, and above the main map.
  8.  *
  9.  * Also adds static map attributes and static-map class to static map.
  10.  */
  11. class PerchFieldType_gmap extends PerchFieldType_map
  12. {
  13.     public function add_page_resources()
  14.     {
  15.         $Perch = Perch::fetch();
  16.         $Perch->add_foot_content('<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key='.$this->_get_api_key().'"></script>');
  17.         $Perch->add_javascript(PERCH_LOGINPATH.'/core/assets/js/maps.js');
  18.     }
  19.  
  20.     /**
  21.      * Render the map in the admin screen.
  22.      * Move amny hints to below the search box, not at the bottom of the map.
  23.      *
  24.      * @param  array  $details [description]
  25.      * @return [type]          [description]
  26.      */
  27.     public function render_inputs($details=array())
  28.     {
  29.         $s = $this->Form->text($this->Tag->input_id().'_adr', $this->Form->get((isset($details[$this->Tag->input_id()])? $details[$this->Tag->input_id()] : array()), 'adr', $this->Tag->default()), 'map_adr');
  30.  
  31.         // This is the only change from the initial map.
  32.         if ($this->Tag->help()) {
  33.             $s .= $this->Form->translated_hint($this->Tag->help());
  34.         }
  35.         // Remove the help no we've loutput it to avoid outputting twice.
  36.         unset($this->Tag->attributes['help']);
  37.  
  38.         $s .= '<br>';
  39.  
  40.         // Carry on, nothing to see here...
  41.         $s .= '<div class="map" data-btn-label="'.PerchLang::get('Find').'" data-mapid="'.PerchUtil::html($this->Tag->input_id(), true).'" data-width="'.($this->Tag->width() ? $this->Tag->width() : '640').'" data-height="'.($this->Tag->height() ? $this->Tag->height() : '480').'">';
  42.             if (isset($details[$this->Tag->input_id()]['admin_html'])) {
  43.                 $s .= $details[$this->Tag->input_id()]['admin_html'];
  44.                 $s .= $this->Form->hidden($this->Tag->input_id().'_lat',  $details[$this->Tag->input_id()]['lat']);
  45.                 $s .= $this->Form->hidden($this->Tag->input_id().'_lng',  $details[$this->Tag->input_id()]['lng']);
  46.                 $s .= $this->Form->hidden($this->Tag->input_id().'_clat', $details[$this->Tag->input_id()]['clat']);
  47.                 $s .= $this->Form->hidden($this->Tag->input_id().'_clng', $details[$this->Tag->input_id()]['clng']);
  48.                 $s .= $this->Form->hidden($this->Tag->input_id().'_type', $details[$this->Tag->input_id()]['type']);
  49.                 $s .= $this->Form->hidden($this->Tag->input_id().'_zoom', $details[$this->Tag->input_id()]['zoom']);
  50.             }
  51.         $s .= '</div>';
  52.  
  53.         return $s;
  54.     }
  55.  
  56.     /**
  57.      * Get the data for output.
  58.      *
  59.      * Address 'directions' and 'address' output mode
  60.      *
  61.      * @param  boolean $raw [description]
  62.      * @return [type]       [description]
  63.      */
  64.     public function get_processed($raw = false)
  65.     {
  66.         if ($raw === false) {
  67.             $raw = $this->get_raw();
  68.         }
  69.  
  70.         if (is_array($raw)) {
  71.             switch ($this->Tag->output()) {
  72.                 case 'address':
  73.                     return $raw['adr'];
  74.                     break;
  75.  
  76.                 case 'directions':
  77.                     $directionsURL = "https://www.google.com/maps/dir/Current+Location/";
  78.                     $adr = urlencode($raw['adr']);
  79.  
  80.                     return $directionsURL . $adr;
  81.                     break;
  82.             }
  83.  
  84.             return $raw['html'];
  85.         }
  86.     }
  87.  
  88.     /**
  89.      * Add static width attributes to default method
  90.      *
  91.      * @param  [type] $id    [description]
  92.      * @param  [type] $tag   [description]
  93.      * @param  [type] $value [description]
  94.      * @return [type]        [description]
  95.      */
  96.     private function _process_map($id, $tag, $value)
  97.     {
  98.         $out = array();
  99.  
  100.         if (isset($value['adr'])) {
  101.  
  102.             $out['adr']     = $value['adr'];
  103.             $out['_title']  = $value['adr'];
  104.             $out['_default']= $value['adr'];
  105.  
  106.             if (!isset($value['lat'])) {
  107.  
  108.                 $lat = false;
  109.                 $lng = false;
  110.  
  111.                 $path = '/maps/api/geocode/json?address='.urlencode($value['adr']).'&sensor=false&key='.$this->_get_api_key();
  112.                 $result = PerchUtil::http_get_request('http://', 'maps.googleapis.com', $path);
  113.                 if ($result) {
  114.                     $result = PerchUtil::json_safe_decode($result, true);
  115.                     //PerchUtil::debug($result);
  116.                     if ($result['status']=='OK') {
  117.                         if (isset($result['results'][0]['geometry']['location']['lat'])) {
  118.                             $lat = $result['results'][0]['geometry']['location']['lat'];
  119.                             $lng = $result['results'][0]['geometry']['location']['lng'];
  120.                         }
  121.                     }
  122.                 }
  123.             }else{
  124.                 $lat = $value['lat'];
  125.                 $lng = $value['lng'];
  126.             }
  127.  
  128.             $out['lat'] = $lat;
  129.             $out['lng'] = $lng;
  130.  
  131.  
  132.             if (!isset($value['clat'])) {
  133.                 $clat = $lat;
  134.                 $clng = $lng;
  135.             }else{
  136.                 $clat = $value['clat'];
  137.                 $clng = $value['clng'];
  138.             }
  139.  
  140.             $out['clat'] = $clat;
  141.             $out['clng'] = $clng;
  142.  
  143.             if (!isset($value['zoom'])) {
  144.                 if ($tag->zoom()) {
  145.                     $zoom = $tag->zoom();
  146.                 }else{
  147.                     $zoom = 15;
  148.                 }
  149.             }else{
  150.                 $zoom = $value['zoom'];
  151.             }
  152.  
  153.             if (!isset($value['type'])) {
  154.                 if ($tag->type()) {
  155.                     $type = $tag->type();
  156.                 }else{
  157.                     $type = 'roadmap';
  158.                 }
  159.             }else{
  160.                 $type = $value['type'];
  161.             }
  162.  
  163.  
  164.             $adr    = $value['adr'];
  165.  
  166.             if (PERCH_RWD) {
  167.                 $width  = ($tag->width() ? $tag->width() : '');
  168.                 $height = ($tag->height() ? $tag->height() : '');
  169.             }else{
  170.                 $width  = ($tag->width() ? $tag->width() : '460');
  171.                 $height = ($tag->height() ? $tag->height() : '320');
  172.             }
  173.  
  174.             // OL Add static width attributes
  175.             $static_width  = ($tag->static_width()  == '' ? '460' : $tag->static_width());
  176.             $static_height = ($tag->static_height() == '' ? '320' : $tag->static_height());
  177.             $static_zoom = ($tag->static_zoom() == '' ? $zoom : $tag->static_zoom());
  178.  
  179.             $out['zoom'] = $zoom;
  180.             $out['type'] = $type;
  181.  
  182.             $r  = '<img id="cmsmap'.PerchUtil::html($id).'" src="//maps.google.com/maps/api/staticmap';
  183.  
  184.             $r  .= '?key='.PerchUtil::html($this->_get_api_key(), true).'&amp;center='.$clat.','.$clng.'&amp;size='.$static_width.'x'.$static_height.'&amp;scale=2&amp;zoom='.$static_zoom.'&amp;maptype='.$type;
  185.  
  186.             if ($lat && $lng)   $r .= '&amp;markers=color:red|color:red|'.$lat.','.$lng;
  187.             $r  .= '" ';
  188.  
  189.             if ($tag->class())  $r .= ' class="'.PerchUtil::html($tag->class()).'"';
  190.  
  191.             $r  .= ' width="'.$static_width.'" height="'.$static_height.'" alt="'.PerchUtil::html($adr).'" />';
  192.  
  193.             $out['admin_html'] = $r;
  194.  
  195.             $map_js_path = PerchUtil::html(PERCH_LOGINPATH).'/core/assets/js/public_maps.min.js';
  196.  
  197.             if (defined('PERCH_MAP_JS') && PERCH_MAP_JS) {
  198.                 $map_js_path = PerchUtil::html(PERCH_MAP_JS);
  199.             }
  200.  
  201.             // JavaScript
  202.             $r .= '<script type="text/javascript">/* <![CDATA[ */ ';
  203.             $r .= "if(typeof CMSMap =='undefined'){var CMSMap={};CMSMap.maps=[];document.write('<scr'+'ipt type=\"text\/javascript\" src=\"".$map_js_path."\"><'+'\/sc'+'ript>');}";
  204.             $r .= "CMSMap.maps.push({'mapid':'cmsmap".PerchUtil::html($id)."','width':'".$width."','height':'".$height."','type':'".$type."','zoom':'".$zoom."','adr':'".addslashes(PerchUtil::html($adr))."','lat':'".$lat."','lng':'".$lng."','clat':'".$clat."','clng':'".$clng."'});";
  205.             $r .= "CMSMap.key='".PerchUtil::html($this->_get_api_key(), true)."';";
  206.             $r .= '/* ]]> */';
  207.             $r .= '</script>';
  208.  
  209.  
  210.             if (defined('PERCH_XHTML_MARKUP') && PERCH_XHTML_MARKUP==false) {
  211.                 $r = str_replace('/>', '>', $r);
  212.             }
  213.  
  214.             $out['html'] = $r;
  215.         }
  216.  
  217.         return $out;
  218.     }
  219.  
  220.     /**
  221.      * Need to reimplement this method as its a private method on parent class.
  222.      *
  223.      * @return [type] [description]
  224.      */
  225.     private function _get_api_key()
  226.     {
  227.         if (!defined('PERCH_GMAPS_API_KEY')) {
  228.             return null;
  229.         }
  230.  
  231.         return PERCH_GMAPS_API_KEY;
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement