<?php

class Moesia_Contact extends WP_Widget {

// constructor
    function moesia_contact() {
		$widget_ops = array('classname' => 'moesia_contact_widget', 'description' => __( 'Display a Contact Form 7 and a Google Map', 'moesia') );
        parent::__construct(false, $name = __('Moesia FP: Contact', 'moesia'), $widget_ops);
		$this->alt_option_name = 'moesia_contact_widget';
		
		add_action( 'save_post', array($this, 'flush_widget_cache') );
		add_action( 'deleted_post', array($this, 'flush_widget_cache') );
		add_action( 'switch_theme', array($this, 'flush_widget_cache') );		
    }
	
	// widget form creation
	function form($instance) {

	// Check values
		$title     	= isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
		$form   	= isset( $instance['form'] ) ? absint( $instance['form'] ) : '';
		$street  	= isset( $instance['street'] ) ? esc_html( $instance['street'] ) : '';
		$phone   	= isset( $instance['phone'] ) ? esc_html( $instance['phone'] ) : '';
		$email    	= isset( $instance['email'] ) ? esc_html( $instance['email'] ) : '';	
		$image_uri 	= isset( $instance['image_uri'] ) ? esc_url( $instance['image_uri'] ) : '';
		$video_uri_mp4  = isset( $instance['video_uri_mp4'] ) ? esc_url_raw( $instance['video_uri_mp4'] ) : '';
		$video_uri_webm  = isset( $instance['video_uri_webm'] ) ? esc_url_raw( $instance['video_uri_webm'] ) : '';			
	?>

	<p>
	<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'moesia'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
	</p>	

	<!-- Contact form id -->
	<p>
	<label for="<?php echo $this->get_field_id('form'); ?>"><?php _e('ID for your form built with Contact Form 7', 'moesia'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('form'); ?>" name="<?php echo $this->get_field_name('form'); ?>" type="text" value="<?php echo $form; ?>" />
	</p>

	<p><label for="<?php echo $this->get_field_id( 'street' ); ?>"><?php _e( 'Enter your address for the Google Map marker', 'moesia' ); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id( 'street' ); ?>" name="<?php echo $this->get_field_name( 'street' ); ?>" type="text" value="<?php echo $street; ?>" size="3" /></p>

	<h3><?php _e('Image background', 'moesia'); ?></h3>
    <?php
        if ( $image_uri != '' ) :
           echo '<p><img class="custom_media_image" src="' . $image_uri . '" style="max-width:100px;" /></p>';
        endif;
    ?>
    <p><label for="<?php echo $this->get_field_id('image_uri'); ?>"><?php _e('[DEPRECATED - Go to Edit Row > Theme > Background image] Upload an image for the background if you want. It will get a parallax effect.', 'moesia'); ?></label></p> 
    <p><input type="button" class="button button-primary custom_media_button" id="custom_media_button" name="<?php echo $this->get_field_name('image_uri'); ?>" value="Upload Image" style="margin-top:5px;" /></p>
	<p><input class="widefat custom_media_url" id="<?php echo $this->get_field_id( 'image_uri' ); ?>" name="<?php echo $this->get_field_name( 'image_uri' ); ?>" type="text" value="<?php echo $image_uri; ?>" size="3" /></p>	

	<h3><?php _e('Video background', 'moesia'); ?></h3>
	<p>
	<label for="<?php echo $this->get_field_id('video_uri_mp4'); ?>"><?php _e('Enter the full link to your video [mp4 format]', 'moesia'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('video_uri_mp4'); ?>" name="<?php echo $this->get_field_name('video_uri_mp4'); ?>" type="text" value="<?php echo $video_uri_mp4; ?>" />
	</p>
	<p>
	<label for="<?php echo $this->get_field_id('video_uri_webm'); ?>"><?php _e('Enter the full link to your video [webm format]', 'moesia'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('video_uri_webm'); ?>" name="<?php echo $this->get_field_name('video_uri_webm'); ?>" type="text" value="<?php echo $video_uri_webm; ?>" />
	</p>

	<?php
	}

	// update widget
	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['title'] 		= strip_tags($new_instance['title']);
		$instance['form'] 		= intval($new_instance['form']);
		$instance['street'] 	= strip_tags($new_instance['street']);
	    $instance['image_uri'] 	= esc_url_raw( $new_instance['image_uri'] );
		$instance['video_uri_mp4'] = esc_url_raw( $new_instance['video_uri_mp4'] );
		$instance['video_uri_webm'] = esc_url_raw( $new_instance['video_uri_webm'] );	    			
		$this->flush_widget_cache();

		$alloptions = wp_cache_get( 'alloptions', 'options' );
		if ( isset($alloptions['moesia_contact']) )
			delete_option('moesia_contact');		  
		  
		return $instance;
	}
	
	function flush_widget_cache() {
		wp_cache_delete('moesia_contact', 'widget');
	}
	
	// display widget
	function widget($args, $instance) {
		$cache = array();
		if ( ! $this->is_preview() ) {
			$cache = wp_cache_get( 'moesia_contact', 'widget' );
		}

		if ( ! is_array( $cache ) ) {
			$cache = array();
		}

		if ( ! isset( $args['widget_id'] ) ) {
			$args['widget_id'] = $this->id;
		}

		if ( isset( $cache[ $args['widget_id'] ] ) ) {
			echo $cache[ $args['widget_id'] ];
			return;
		}

		ob_start();
		extract($args);

		$title 			= ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Get in touch', 'moesia' );
		$title 			= apply_filters( 'widget_title', $title, $instance, $this->id_base );
		$form   		= isset( $instance['form'] ) ? absint( $instance['form'] ) : '';
		$street   		= isset( $instance['street'] ) ? esc_html( $instance['street'] ) : '';
		$image_uri 		= isset( $instance['image_uri'] ) ? esc_url($instance['image_uri']) : '';
		$video_uri_mp4 = isset( $instance['video_uri_mp4'] ) ? esc_url($instance['video_uri_mp4']) : '';
		$video_uri_webm = isset( $instance['video_uri_webm'] ) ? esc_url($instance['video_uri_webm']) : '';				

?>
		<section id="contact" class="contact-area">
			<div class="container">
				<?php if ( $title ) echo $before_title . '<span class="wow bounce">' . $title . '</span>' . $after_title; ?>
				<?php if ($street && $form) : ?>
					<?php $columns = 'col-md-6'; ?>
				<?php else : ?>
					<?php $columns = ''; ?>
				<?php endif; ?>		
				<?php if ($street !='') : ?>
					<div id="map" class="map wow fadeInLeft <?php echo $columns; ?>"></div>
				<?php endif; ?>	
				<?php if ($form !='') : ?>
					<div class="contact wow fadeInRight <?php echo $columns; ?>">
						<?php echo do_shortcode('[contact-form-7 id="' . absint($form) . '"]'); ?>
					</div>
				<?php endif; ?>																				
			</div>
			<?php if (($image_uri != '') && ($video_uri_mp4 =='')) : ?>
				<style type="text/css">
					.contact-area {
					    display: block;			    
						background: url(<?php echo $image_uri; ?>) no-repeat;
						background-position: center top;
						background-attachment: fixed;
						background-size: cover;
						z-index: -1;
						position: static;
					}
				</style>
			<?php endif; ?>
			<script>		
			   jQuery(function($) {
					$('#map').gmap3({
					 map: {
					    options: {
					    	maxZoom: 16,
					    	streetViewControl: false,
					    	navigationControl: false,
					    }  
					 },
					 marker:{
					    address: "<?php echo $street; ?>",
					 }
					},
					"autofit" );
			    });
		    </script>
			<?php if ($video_uri_mp4 !='') : ?>
				<video id="videobg" preload="auto" autoplay="true" loop="loop" muted="muted" volume="0">
				     <source src="<?php echo esc_url($video_uri_mp4); ?>" type="video/mp4">
				     <source src="<?php echo esc_url($video_uri_webm); ?>" type="video/webm">
				</video>
			<?php endif; ?>		    										
		</section>				
	<?php

		if ( ! $this->is_preview() ) {
			$cache[ $args['widget_id'] ] = ob_get_flush();
			wp_cache_set( 'moesia_contact', $cache, 'widget' );
		} else {
			ob_end_flush();
		}
	}
	
}