<?php

/* Theme setup section
-------------------------------------------------------------------- */
if (!function_exists('seohub_sc_googlemap_theme_setup')) {
	add_action( 'seohub_action_before_init_theme', 'seohub_sc_googlemap_theme_setup' );
	function seohub_sc_googlemap_theme_setup() {
		add_action('seohub_action_shortcodes_list', 		'seohub_sc_googlemap_reg_shortcodes');
		if (function_exists('seohub_exists_visual_composer') && seohub_exists_visual_composer())
			add_action('seohub_action_shortcodes_list_vc','seohub_sc_googlemap_reg_shortcodes_vc');
	}
}



/* Shortcode implementation
-------------------------------------------------------------------- */

//[trx_googlemap id="unique_id" width="width_in_pixels_or_percent" height="height_in_pixels"]
//	[trx_googlemap_marker address="your_address"]
//[/trx_googlemap]

if (!function_exists('seohub_sc_googlemap')) {	
	function seohub_sc_googlemap($atts, $content = null) {
		if (seohub_in_shortcode_blogger()) return '';
		extract(seohub_html_decode(shortcode_atts(array(
			// Individual params
			"zoom" => 16,
			"style" => 'default',
			// Common params
			"id" => "",
			"class" => "",
			"css" => "",
			"animation" => "",
			"width" => "100%",
			"height" => "400",
			"top" => "",
			"bottom" => "",
			"left" => "",
			"right" => ""
		), $atts)));
		$class .= ($class ? ' ' : '') . seohub_get_css_position_as_classes($top, $right, $bottom, $left);
		$css .= seohub_get_css_dimensions_from_values($width, $height);
		if (empty($id)) $id = 'sc_googlemap_'.str_replace('.', '', mt_rand());
		if (empty($style)) $style = seohub_get_custom_option('googlemap_style');
		seohub_enqueue_script( 'googlemap', seohub_get_protocol().'://maps.google.com/maps/api/js?sensor=false', array(), null, true );
		seohub_enqueue_script( 'seohub-googlemap-script', seohub_get_file_url('js/core.googlemap.js'), array(), null, true );
		global $SEOHUB_GLOBALS;
		$SEOHUB_GLOBALS['sc_googlemap_markers'] = array();
		$content = do_shortcode($content);
		$output = '';
		if (count($SEOHUB_GLOBALS['sc_googlemap_markers']) == 0) {
			$SEOHUB_GLOBALS['sc_googlemap_markers'][] = array(
				'title' => seohub_get_custom_option('googlemap_title'),
				'description' => seohub_strmacros(seohub_get_custom_option('googlemap_description')),
				'latlng' => seohub_get_custom_option('googlemap_latlng'),
				'address' => seohub_get_custom_option('googlemap_address'),
				'point' => seohub_get_custom_option('googlemap_marker')
			);
		}
		$output .= '<div id="'.esc_attr($id).'"'
			. ' class="sc_googlemap'. (!empty($class) ? ' '.esc_attr($class) : '').'"'
			. ($css!='' ? ' style="'.esc_attr($css).'"' : '') 
			. (!seohub_param_is_off($animation) ? ' data-animation="'.esc_attr(seohub_get_animation_classes($animation)).'"' : '')
			. ' data-zoom="'.esc_attr($zoom).'"'
			. ' data-style="'.esc_attr($style).'"'
			. '>';
		$cnt = 0;
		foreach ($SEOHUB_GLOBALS['sc_googlemap_markers'] as $marker) {
			$cnt++;
			if (empty($marker['id'])) $marker['id'] = $id.'_'.$cnt;
			$output .= '<div id="'.esc_attr($marker['id']).'" class="sc_googlemap_marker"'
				. ' data-title="'.esc_attr($marker['title']).'"'
				. ' data-description="'.esc_attr(seohub_strmacros($marker['description'])).'"'
				. ' data-address="'.esc_attr($marker['address']).'"'
				. ' data-latlng="'.esc_attr($marker['latlng']).'"'
				. ' data-point="'.esc_attr($marker['point']).'"'
				. '></div>';
		}
		$output .= '</div>';
		return apply_filters('seohub_shortcode_output', $output, 'trx_googlemap', $atts, $content);
	}
	seohub_require_shortcode("trx_googlemap", "seohub_sc_googlemap");
}


if (!function_exists('seohub_sc_googlemap_marker')) {	
	function seohub_sc_googlemap_marker($atts, $content = null) {
		if (seohub_in_shortcode_blogger()) return '';
		extract(seohub_html_decode(shortcode_atts(array(
			// Individual params
			"title" => "",
			"address" => "",
			"latlng" => "",
			"point" => "",
			// Common params
			"id" => ""
		), $atts)));
		if (!empty($point)) {
			if ($point > 0) {
				$attach = wp_get_attachment_image_src( $point, 'full' );
				if (isset($attach[0]) && $attach[0]!='')
					$point = $attach[0];
			}
		}
		global $SEOHUB_GLOBALS;
		$SEOHUB_GLOBALS['sc_googlemap_markers'][] = array(
			'id' => $id,
			'title' => $title,
			'description' => do_shortcode($content),
			'latlng' => $latlng,
			'address' => $address,
			'point' => $point ? $point : seohub_get_custom_option('googlemap_marker')
		);
		return '';
	}
	seohub_require_shortcode("trx_googlemap_marker", "seohub_sc_googlemap_marker");
}



/* Add shortcode in the internal SC Builder
-------------------------------------------------------------------- */
if ( !function_exists( 'seohub_sc_googlemap_reg_shortcodes' ) ) {
	//add_action('seohub_action_shortcodes_list', 'seohub_sc_googlemap_reg_shortcodes');
	function seohub_sc_googlemap_reg_shortcodes() {
		global $SEOHUB_GLOBALS;
	
		$SEOHUB_GLOBALS['shortcodes']["trx_googlemap"] = array(
			"title" => esc_html__("Google map", "seohub"),
			"desc" => wp_kses( __("Insert Google map with specified markers", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
			"decorate" => false,
			"container" => true,
			"params" => array(
				"zoom" => array(
					"title" => esc_html__("Zoom", "seohub"),
					"desc" => wp_kses( __("Map zoom factor", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"divider" => true,
					"value" => 16,
					"min" => 1,
					"max" => 20,
					"type" => "spinner"
				),
				"style" => array(
					"title" => esc_html__("Map style", "seohub"),
					"desc" => wp_kses( __("Select map style", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"value" => "default",
					"type" => "checklist",
					"options" => $SEOHUB_GLOBALS['sc_params']['googlemap_styles']
				),
				"width" => seohub_shortcodes_width('100%'),
				"height" => seohub_shortcodes_height(240),
				"top" => $SEOHUB_GLOBALS['sc_params']['top'],
				"bottom" => $SEOHUB_GLOBALS['sc_params']['bottom'],
				"left" => $SEOHUB_GLOBALS['sc_params']['left'],
				"right" => $SEOHUB_GLOBALS['sc_params']['right'],
				"id" => $SEOHUB_GLOBALS['sc_params']['id'],
				"class" => $SEOHUB_GLOBALS['sc_params']['class'],
				"animation" => $SEOHUB_GLOBALS['sc_params']['animation'],
				"css" => $SEOHUB_GLOBALS['sc_params']['css']
			),
			"children" => array(
				"name" => "trx_googlemap_marker",
				"title" => esc_html__("Google map marker", "seohub"),
				"desc" => wp_kses( __("Google map marker", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
				"decorate" => false,
				"container" => true,
				"params" => array(
					"address" => array(
						"title" => esc_html__("Address", "seohub"),
						"desc" => wp_kses( __("Address of this marker", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
						"value" => "",
						"type" => "text"
					),
					"latlng" => array(
						"title" => esc_html__("Latitude and Longitude", "seohub"),
						"desc" => wp_kses( __("Comma separated marker's coorditanes (instead Address)", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
						"value" => "",
						"type" => "text"
					),
					"point" => array(
						"title" => esc_html__("URL for marker image file", "seohub"),
						"desc" => wp_kses( __("Select or upload image or write URL from other site for this marker. If empty - use default marker", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
						"readonly" => false,
						"value" => "",
						"type" => "media"
					),
					"title" => array(
						"title" => esc_html__("Title", "seohub"),
						"desc" => wp_kses( __("Title for this marker", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
						"value" => "",
						"type" => "text"
					),
					"_content_" => array(
						"title" => esc_html__("Description", "seohub"),
						"desc" => wp_kses( __("Description for this marker", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
						"rows" => 4,
						"value" => "",
						"type" => "textarea"
					),
					"id" => $SEOHUB_GLOBALS['sc_params']['id']
				)
			)
		);
	}
}


/* Add shortcode in the VC Builder
-------------------------------------------------------------------- */
if ( !function_exists( 'seohub_sc_googlemap_reg_shortcodes_vc' ) ) {
	//add_action('seohub_action_shortcodes_list_vc', 'seohub_sc_googlemap_reg_shortcodes_vc');
	function seohub_sc_googlemap_reg_shortcodes_vc() {
		global $SEOHUB_GLOBALS;
	
		vc_map( array(
			"base" => "trx_googlemap",
			"name" => esc_html__("Google map", "seohub"),
			"description" => wp_kses( __("Insert Google map with desired address or coordinates", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
			"category" => esc_html__('Content', 'js_composer'),
			'icon' => 'icon_trx_googlemap',
			"class" => "trx_sc_collection trx_sc_googlemap",
			"content_element" => true,
			"is_container" => true,
			"as_parent" => array('only' => 'trx_googlemap_marker'),
			"show_settings_on_create" => true,
			"params" => array(
				array(
					"param_name" => "zoom",
					"heading" => esc_html__("Zoom", "seohub"),
					"description" => wp_kses( __("Map zoom factor", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"admin_label" => true,
					"class" => "",
					"value" => "16",
					"type" => "textfield"
				),
				array(
					"param_name" => "style",
					"heading" => esc_html__("Style", "seohub"),
					"description" => wp_kses( __("Map custom style", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"admin_label" => true,
					"class" => "",
					"value" => array_flip($SEOHUB_GLOBALS['sc_params']['googlemap_styles']),
					"type" => "dropdown"
				),
				$SEOHUB_GLOBALS['vc_params']['id'],
				$SEOHUB_GLOBALS['vc_params']['class'],
				$SEOHUB_GLOBALS['vc_params']['animation'],
				$SEOHUB_GLOBALS['vc_params']['css'],
				seohub_vc_width('100%'),
				seohub_vc_height(240),
				$SEOHUB_GLOBALS['vc_params']['margin_top'],
				$SEOHUB_GLOBALS['vc_params']['margin_bottom'],
				$SEOHUB_GLOBALS['vc_params']['margin_left'],
				$SEOHUB_GLOBALS['vc_params']['margin_right']
			)
		) );
		
		vc_map( array(
			"base" => "trx_googlemap_marker",
			"name" => esc_html__("Googlemap marker", "seohub"),
			"description" => wp_kses( __("Insert new marker into Google map", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
			"class" => "trx_sc_collection trx_sc_googlemap_marker",
			'icon' => 'icon_trx_googlemap_marker',
			//"allowed_container_element" => 'vc_row',
			"show_settings_on_create" => true,
			"content_element" => true,
			"is_container" => true,
			"as_child" => array('only' => 'trx_googlemap'), // Use only|except attributes to limit parent (separate multiple values with comma)
			"params" => array(
				array(
					"param_name" => "address",
					"heading" => esc_html__("Address", "seohub"),
					"description" => wp_kses( __("Address of this marker", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"admin_label" => true,
					"class" => "",
					"value" => "",
					"type" => "textfield"
				),
				array(
					"param_name" => "latlng",
					"heading" => esc_html__("Latitude and Longitude", "seohub"),
					"description" => wp_kses( __("Comma separated marker's coorditanes (instead Address)", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"admin_label" => true,
					"class" => "",
					"value" => "",
					"type" => "textfield"
				),
				array(
					"param_name" => "title",
					"heading" => esc_html__("Title", "seohub"),
					"description" => wp_kses( __("Title for this marker", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"admin_label" => true,
					"class" => "",
					"value" => "",
					"type" => "textfield"
				),
				array(
					"param_name" => "point",
					"heading" => esc_html__("URL for marker image file", "seohub"),
					"description" => wp_kses( __("Select or upload image or write URL from other site for this marker. If empty - use default marker", "seohub"), $SEOHUB_GLOBALS['allowed_tags'] ),
					"class" => "",
					"value" => "",
					"type" => "attach_image"
				),
				$SEOHUB_GLOBALS['vc_params']['id']
			)
		) );
		
		class WPBakeryShortCode_Trx_Googlemap extends SEOHUB_VC_ShortCodeCollection {}
		class WPBakeryShortCode_Trx_Googlemap_Marker extends SEOHUB_VC_ShortCodeCollection {}
	}
}
?>