Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Perch Map fieldtype extended by Oliver Lowe
- *
- * The aim is to move the field hint up the page to a more useful position
- * underneath the address field, and above the main map.
- *
- * Also adds static map attributes and static-map class to static map.
- */
- class PerchFieldType_gmap extends PerchFieldType_map
- {
- public function add_page_resources()
- {
- $Perch = Perch::fetch();
- $Perch->add_foot_content('<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key='.$this->_get_api_key().'"></script>');
- $Perch->add_javascript(PERCH_LOGINPATH.'/core/assets/js/maps.js');
- }
- /**
- * Render the map in the admin screen.
- * Move amny hints to below the search box, not at the bottom of the map.
- *
- * @param array $details [description]
- * @return [type] [description]
- */
- public function render_inputs($details=array())
- {
- $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');
- // This is the only change from the initial map.
- if ($this->Tag->help()) {
- $s .= $this->Form->translated_hint($this->Tag->help());
- }
- // Remove the help no we've loutput it to avoid outputting twice.
- unset($this->Tag->attributes['help']);
- $s .= '<br>';
- // Carry on, nothing to see here...
- $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').'">';
- if (isset($details[$this->Tag->input_id()]['admin_html'])) {
- $s .= $details[$this->Tag->input_id()]['admin_html'];
- $s .= $this->Form->hidden($this->Tag->input_id().'_lat', $details[$this->Tag->input_id()]['lat']);
- $s .= $this->Form->hidden($this->Tag->input_id().'_lng', $details[$this->Tag->input_id()]['lng']);
- $s .= $this->Form->hidden($this->Tag->input_id().'_clat', $details[$this->Tag->input_id()]['clat']);
- $s .= $this->Form->hidden($this->Tag->input_id().'_clng', $details[$this->Tag->input_id()]['clng']);
- $s .= $this->Form->hidden($this->Tag->input_id().'_type', $details[$this->Tag->input_id()]['type']);
- $s .= $this->Form->hidden($this->Tag->input_id().'_zoom', $details[$this->Tag->input_id()]['zoom']);
- }
- $s .= '</div>';
- return $s;
- }
- /**
- * Get the data for output.
- *
- * Address 'directions' and 'address' output mode
- *
- * @param boolean $raw [description]
- * @return [type] [description]
- */
- public function get_processed($raw = false)
- {
- if ($raw === false) {
- $raw = $this->get_raw();
- }
- if (is_array($raw)) {
- switch ($this->Tag->output()) {
- case 'address':
- return $raw['adr'];
- break;
- case 'directions':
- $directionsURL = "https://www.google.com/maps/dir/Current+Location/";
- $adr = urlencode($raw['adr']);
- return $directionsURL . $adr;
- break;
- }
- return $raw['html'];
- }
- }
- /**
- * Add static width attributes to default method
- *
- * @param [type] $id [description]
- * @param [type] $tag [description]
- * @param [type] $value [description]
- * @return [type] [description]
- */
- private function _process_map($id, $tag, $value)
- {
- $out = array();
- if (isset($value['adr'])) {
- $out['adr'] = $value['adr'];
- $out['_title'] = $value['adr'];
- $out['_default']= $value['adr'];
- if (!isset($value['lat'])) {
- $lat = false;
- $lng = false;
- $path = '/maps/api/geocode/json?address='.urlencode($value['adr']).'&sensor=false&key='.$this->_get_api_key();
- $result = PerchUtil::http_get_request('http://', 'maps.googleapis.com', $path);
- if ($result) {
- $result = PerchUtil::json_safe_decode($result, true);
- //PerchUtil::debug($result);
- if ($result['status']=='OK') {
- if (isset($result['results'][0]['geometry']['location']['lat'])) {
- $lat = $result['results'][0]['geometry']['location']['lat'];
- $lng = $result['results'][0]['geometry']['location']['lng'];
- }
- }
- }
- }else{
- $lat = $value['lat'];
- $lng = $value['lng'];
- }
- $out['lat'] = $lat;
- $out['lng'] = $lng;
- if (!isset($value['clat'])) {
- $clat = $lat;
- $clng = $lng;
- }else{
- $clat = $value['clat'];
- $clng = $value['clng'];
- }
- $out['clat'] = $clat;
- $out['clng'] = $clng;
- if (!isset($value['zoom'])) {
- if ($tag->zoom()) {
- $zoom = $tag->zoom();
- }else{
- $zoom = 15;
- }
- }else{
- $zoom = $value['zoom'];
- }
- if (!isset($value['type'])) {
- if ($tag->type()) {
- $type = $tag->type();
- }else{
- $type = 'roadmap';
- }
- }else{
- $type = $value['type'];
- }
- $adr = $value['adr'];
- if (PERCH_RWD) {
- $width = ($tag->width() ? $tag->width() : '');
- $height = ($tag->height() ? $tag->height() : '');
- }else{
- $width = ($tag->width() ? $tag->width() : '460');
- $height = ($tag->height() ? $tag->height() : '320');
- }
- // OL Add static width attributes
- $static_width = ($tag->static_width() == '' ? '460' : $tag->static_width());
- $static_height = ($tag->static_height() == '' ? '320' : $tag->static_height());
- $static_zoom = ($tag->static_zoom() == '' ? $zoom : $tag->static_zoom());
- $out['zoom'] = $zoom;
- $out['type'] = $type;
- $r = '<img id="cmsmap'.PerchUtil::html($id).'" src="//maps.google.com/maps/api/staticmap';
- $r .= '?key='.PerchUtil::html($this->_get_api_key(), true).'&center='.$clat.','.$clng.'&size='.$static_width.'x'.$static_height.'&scale=2&zoom='.$static_zoom.'&maptype='.$type;
- if ($lat && $lng) $r .= '&markers=color:red|color:red|'.$lat.','.$lng;
- $r .= '" ';
- if ($tag->class()) $r .= ' class="'.PerchUtil::html($tag->class()).'"';
- $r .= ' width="'.$static_width.'" height="'.$static_height.'" alt="'.PerchUtil::html($adr).'" />';
- $out['admin_html'] = $r;
- $map_js_path = PerchUtil::html(PERCH_LOGINPATH).'/core/assets/js/public_maps.min.js';
- if (defined('PERCH_MAP_JS') && PERCH_MAP_JS) {
- $map_js_path = PerchUtil::html(PERCH_MAP_JS);
- }
- // JavaScript
- $r .= '<script type="text/javascript">/* <![CDATA[ */ ';
- $r .= "if(typeof CMSMap =='undefined'){var CMSMap={};CMSMap.maps=[];document.write('<scr'+'ipt type=\"text\/javascript\" src=\"".$map_js_path."\"><'+'\/sc'+'ript>');}";
- $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."'});";
- $r .= "CMSMap.key='".PerchUtil::html($this->_get_api_key(), true)."';";
- $r .= '/* ]]> */';
- $r .= '</script>';
- if (defined('PERCH_XHTML_MARKUP') && PERCH_XHTML_MARKUP==false) {
- $r = str_replace('/>', '>', $r);
- }
- $out['html'] = $r;
- }
- return $out;
- }
- /**
- * Need to reimplement this method as its a private method on parent class.
- *
- * @return [type] [description]
- */
- private function _get_api_key()
- {
- if (!defined('PERCH_GMAPS_API_KEY')) {
- return null;
- }
- return PERCH_GMAPS_API_KEY;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement