<?php
/**
 * Custom Contact us Widget
 */

class Forest_Jungle_Safari_Contact_Widget extends WP_Widget {
	function __construct() {
		parent::__construct(
			'Forest_Jungle_Safari_Contact_Widget', 
			__('VW Contact us', 'forest-jungle-safari'),
			array( 'description' => __( 'Widget for contact us section in sidebar', 'forest-jungle-safari' ), ) 
		);
	}
	
	public function widget( $forest_jungle_safari_args, $forest_jungle_safari_instance ) {
		?>
		<aside class="widget">
			<?php
			$forest_jungle_safari_title = isset( $forest_jungle_safari_instance['title'] ) ? $forest_jungle_safari_instance['title'] : '';
			$forest_jungle_safari_phone = isset( $forest_jungle_safari_instance['phone'] ) ? $forest_jungle_safari_instance['phone'] : '';
			$forest_jungle_safari_email = isset( $forest_jungle_safari_instance['email'] ) ? $forest_jungle_safari_instance['email'] : '';
			$forest_jungle_safari_address = isset( $forest_jungle_safari_instance['address'] ) ? $forest_jungle_safari_instance['address'] : '';
			$forest_jungle_safari_timing = isset( $forest_jungle_safari_instance['timing'] ) ? $forest_jungle_safari_instance['timing'] : '';
			$forest_jungle_safari_longitude = isset( $forest_jungle_safari_instance['longitude'] ) ? $forest_jungle_safari_instance['longitude'] : '';
			$forest_jungle_safari_latitude = isset( $forest_jungle_safari_instance['latitude'] ) ? $forest_jungle_safari_instance['latitude'] : '';
			$forest_jungle_safari_contact_form = isset( $forest_jungle_safari_instance['contact_form'] ) ? $forest_jungle_safari_instance['contact_form'] : '';

	        echo '<div class="custom-contact-us">';
	        if(!empty($forest_jungle_safari_title) ){ ?><h3 class="custom_title"><?php echo esc_html($forest_jungle_safari_title); ?></h3><?php } ?>
		        <?php if(!empty($forest_jungle_safari_phone) ){ ?><p><span class="custom_details"><?php esc_html_e('Phone Number: ','forest-jungle-safari'); ?></span><span class="custom_desc"><?php echo esc_html($forest_jungle_safari_phone); ?></span></p><?php } ?>
		        <?php if(!empty($forest_jungle_safari_email) ){ ?><p><span class="custom_details"><?php esc_html_e('Email ID: ','forest-jungle-safari'); ?></span><span class="custom_desc"><?php echo esc_html($forest_jungle_safari_email); ?></span></p><?php } ?>
		        <?php if(!empty($forest_jungle_safari_address) ){ ?><p><span class="custom_details"><?php esc_html_e('Address: ','forest-jungle-safari'); ?></span><span class="custom_desc"><?php echo esc_html($forest_jungle_safari_address); ?></span></p><?php } ?> 
		        <?php if(!empty($forest_jungle_safari_timing) ){ ?><p><span class="custom_details"><?php esc_html_e('Opening Time: ','forest-jungle-safari'); ?></span><span class="custom_desc"><?php echo esc_html($forest_jungle_safari_timing); ?></span></p><?php } ?>
		        <?php if(!empty($forest_jungle_safari_longitude) ){ ?><embed width="100%" height="200px" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?q=<?php echo esc_html($forest_jungle_safari_longitude); ?>,<?php echo esc_html($forest_jungle_safari_latitude); ?>&hl=es;z=14&amp;output=embed"></embed><?php } ?>
		        <?php if(!empty($forest_jungle_safari_contact_form) ){ ?><?php echo do_shortcode($forest_jungle_safari_contact_form); ?><?php } ?>
	        <?php echo '</div>';
			?>
		</aside>
		<?php
	}
	
	// Widget Backend 
	public function form( $forest_jungle_safari_instance ) {

		$forest_jungle_safari_title= ''; $forest_jungle_safari_phone= ''; $forest_jungle_safari_email = ''; $forest_jungle_safari_address = ''; $forest_jungle_safari_timing = ''; $forest_jungle_safari_longitude = ''; $forest_jungle_safari_latitude = ''; $forest_jungle_safari_contact_form = ''; 
		
		$forest_jungle_safari_title = isset( $forest_jungle_safari_instance['title'] ) ? $forest_jungle_safari_instance['title'] : '';
		$forest_jungle_safari_phone = isset( $forest_jungle_safari_instance['phone'] ) ? $forest_jungle_safari_instance['phone'] : '';
		$forest_jungle_safari_email = isset( $forest_jungle_safari_instance['email'] ) ? $forest_jungle_safari_instance['email'] : '';
		$forest_jungle_safari_address = isset( $forest_jungle_safari_instance['address'] ) ? $forest_jungle_safari_instance['address'] : '';
		$forest_jungle_safari_timing = isset( $forest_jungle_safari_instance['timing'] ) ? $forest_jungle_safari_instance['timing'] : '';
		$forest_jungle_safari_longitude = isset( $forest_jungle_safari_instance['longitude'] ) ? $forest_jungle_safari_instance['longitude'] : '';
		$forest_jungle_safari_latitude = isset( $forest_jungle_safari_instance['latitude'] ) ? $forest_jungle_safari_instance['latitude'] : '';
		$forest_jungle_safari_contact_form = isset( $forest_jungle_safari_instance['contact_form'] ) ? $forest_jungle_safari_instance['contact_form'] : '';
		
		?>

		<p>
        	<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:','forest-jungle-safari'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($forest_jungle_safari_title); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('phone')); ?>"><?php esc_html_e('Phone Number:','forest-jungle-safari'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('phone')); ?>" name="<?php echo esc_attr($this->get_field_name('phone')); ?>" type="text" value="<?php echo esc_attr($forest_jungle_safari_phone); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('email')); ?>"><?php esc_html_e('Email id:','forest-jungle-safari'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('email')); ?>" name="<?php echo esc_attr($this->get_field_name('email')); ?>" type="text" value="<?php echo esc_attr($forest_jungle_safari_email); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('address')); ?>"><?php esc_html_e('Address:','forest-jungle-safari'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('address')); ?>" name="<?php echo esc_attr($this->get_field_name('address')); ?>" type="text" value="<?php echo esc_attr($forest_jungle_safari_address); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('timing')); ?>"><?php esc_html_e('Opening Time:','forest-jungle-safari'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('timing')); ?>" name="<?php echo esc_attr($this->get_field_name('timing')); ?>" type="text" value="<?php echo esc_attr($forest_jungle_safari_timing); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('longitude')); ?>"><?php esc_html_e('Longitude:','forest-jungle-safari'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('longitude')); ?>" name="<?php echo esc_attr($this->get_field_name('longitude')); ?>" type="text" value="<?php echo esc_attr($forest_jungle_safari_longitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('latitude')); ?>"><?php esc_html_e('Latitude:','forest-jungle-safari'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('latitude')); ?>" name="<?php echo esc_attr($this->get_field_name('latitude')); ?>" type="text" value="<?php echo esc_attr($forest_jungle_safari_latitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('contact_form')); ?>"><?php esc_html_e('Contact Form Shortcode:','forest-jungle-safari'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('contact_form')); ?>" name="<?php echo esc_attr($this->get_field_name('contact_form')); ?>" type="text" value="<?php echo esc_attr($forest_jungle_safari_contact_form); ?>">
    	</p>
		
		<?php 
	}
	
	// Updating widget replacing old instances with new
	public function update( $forest_jungle_safari_new_instance, $forest_jungle_safari_old_instance ) {
		$forest_jungle_safari_instance = array();	
		$forest_jungle_safari_instance['title'] = (!empty($forest_jungle_safari_new_instance['title']) ) ? strip_tags($forest_jungle_safari_new_instance['title']) : '';
		$forest_jungle_safari_instance['phone'] = (!empty($forest_jungle_safari_new_instance['phone']) ) ? forest_jungle_safari_sanitize_phone_number($forest_jungle_safari_new_instance['phone']) : '';
		$forest_jungle_safari_instance['email'] = (!empty($forest_jungle_safari_new_instance['email']) ) ? sanitize_email($forest_jungle_safari_new_instance['email']) : '';
		$forest_jungle_safari_instance['address'] = (!empty($forest_jungle_safari_new_instance['address']) ) ? strip_tags($forest_jungle_safari_new_instance['address']) : '';
		$forest_jungle_safari_instance['timing'] = (!empty($forest_jungle_safari_new_instance['timing']) ) ? strip_tags($forest_jungle_safari_new_instance['timing']) : '';
		$forest_jungle_safari_instance['longitude'] = (!empty($forest_jungle_safari_new_instance['longitude']) ) ? strip_tags($forest_jungle_safari_new_instance['longitude']) : '';
		$forest_jungle_safari_instance['latitude'] = (!empty($forest_jungle_safari_new_instance['latitude']) ) ? strip_tags($forest_jungle_safari_new_instance['latitude']) : '';
		$forest_jungle_safari_instance['contact_form'] = (!empty($forest_jungle_safari_new_instance['contact_form']) ) ? strip_tags($forest_jungle_safari_new_instance['contact_form']) : '';
        
		return $forest_jungle_safari_instance;
	}
}
// Register and load the widget
function forest_jungle_safari_contact_custom_load_widget() {
	register_widget( 'Forest_Jungle_Safari_Contact_Widget' );
}
add_action( 'widgets_init', 'forest_jungle_safari_contact_custom_load_widget' );