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

class Summer_Camp_Contact_Widget extends WP_Widget {
	function __construct() {
		parent::__construct(
			'Summer_Camp_Contact_Widget', 
			__('VW Contact us', 'summer-camp'),
			array( 'description' => __( 'Widget for contact us section in sidebar', 'summer-camp' ), ) 
		);
	}
	
	public function widget( $summer_camp_args, $summer_camp_instance ) {
		?>
		<aside class="widget">
			<?php
			$summer_camp_title = isset( $summer_camp_instance['title'] ) ? $summer_camp_instance['title'] : '';
			$summer_camp_phone = isset( $summer_camp_instance['phone'] ) ? $summer_camp_instance['phone'] : '';
			$summer_camp_email = isset( $summer_camp_instance['email'] ) ? $summer_camp_instance['email'] : '';
			$summer_camp_address = isset( $summer_camp_instance['address'] ) ? $summer_camp_instance['address'] : '';
			$summer_camp_timing = isset( $summer_camp_instance['timing'] ) ? $summer_camp_instance['timing'] : '';
			$summer_camp_longitude = isset( $summer_camp_instance['longitude'] ) ? $summer_camp_instance['longitude'] : '';
			$summer_camp_latitude = isset( $summer_camp_instance['latitude'] ) ? $summer_camp_instance['latitude'] : '';
			$summer_camp_contact_form = isset( $summer_camp_instance['contact_form'] ) ? $summer_camp_instance['contact_form'] : '';

	        echo '<div class="custom-contact-us">';
	        if(!empty($summer_camp_title) ){ ?><h3 class="custom_title"><?php echo esc_html($summer_camp_title); ?></h3><?php } ?>
		        <?php if(!empty($summer_camp_phone) ){ ?><p><span class="custom_details"><?php esc_html_e('Phone Number: ','summer-camp'); ?></span><span class="custom_desc"><?php echo esc_html($summer_camp_phone); ?></span></p><?php } ?>
		        <?php if(!empty($summer_camp_email) ){ ?><p><span class="custom_details"><?php esc_html_e('Email ID: ','summer-camp'); ?></span><span class="custom_desc"><?php echo esc_html($summer_camp_email); ?></span></p><?php } ?>
		        <?php if(!empty($summer_camp_address) ){ ?><p><span class="custom_details"><?php esc_html_e('Address: ','summer-camp'); ?></span><span class="custom_desc"><?php echo esc_html($summer_camp_address); ?></span></p><?php } ?> 
		        <?php if(!empty($summer_camp_timing) ){ ?><p><span class="custom_details"><?php esc_html_e('Opening Time: ','summer-camp'); ?></span><span class="custom_desc"><?php echo esc_html($summer_camp_timing); ?></span></p><?php } ?>
		        <?php if(!empty($summer_camp_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($summer_camp_longitude); ?>,<?php echo esc_html($summer_camp_latitude); ?>&hl=es;z=14&amp;output=embed"></embed><?php } ?>
		        <?php if(!empty($summer_camp_contact_form) ){ ?><?php echo do_shortcode($summer_camp_contact_form); ?><?php } ?>
	        <?php echo '</div>';
			?>
		</aside>
		<?php
	}
	
	// Widget Backend 
	public function form( $summer_camp_instance ) {

		$summer_camp_title= ''; $summer_camp_phone= ''; $summer_camp_email = ''; $summer_camp_address = ''; $summer_camp_timing = ''; $summer_camp_longitude = ''; $summer_camp_latitude = ''; $summer_camp_contact_form = ''; 
		
		$summer_camp_title = isset( $summer_camp_instance['title'] ) ? $summer_camp_instance['title'] : '';
		$summer_camp_phone = isset( $summer_camp_instance['phone'] ) ? $summer_camp_instance['phone'] : '';
		$summer_camp_email = isset( $summer_camp_instance['email'] ) ? $summer_camp_instance['email'] : '';
		$summer_camp_address = isset( $summer_camp_instance['address'] ) ? $summer_camp_instance['address'] : '';
		$summer_camp_timing = isset( $summer_camp_instance['timing'] ) ? $summer_camp_instance['timing'] : '';
		$summer_camp_longitude = isset( $summer_camp_instance['longitude'] ) ? $summer_camp_instance['longitude'] : '';
		$summer_camp_latitude = isset( $summer_camp_instance['latitude'] ) ? $summer_camp_instance['latitude'] : '';
		$summer_camp_contact_form = isset( $summer_camp_instance['contact_form'] ) ? $summer_camp_instance['contact_form'] : '';
		
		?>

		<p>
        	<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:','summer-camp'); ?></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($summer_camp_title); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('phone')); ?>"><?php esc_html_e('Phone Number:','summer-camp'); ?></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($summer_camp_phone); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('email')); ?>"><?php esc_html_e('Email id:','summer-camp'); ?></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($summer_camp_email); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('address')); ?>"><?php esc_html_e('Address:','summer-camp'); ?></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($summer_camp_address); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('timing')); ?>"><?php esc_html_e('Opening Time:','summer-camp'); ?></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($summer_camp_timing); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('longitude')); ?>"><?php esc_html_e('Longitude:','summer-camp'); ?></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($summer_camp_longitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('latitude')); ?>"><?php esc_html_e('Latitude:','summer-camp'); ?></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($summer_camp_latitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('contact_form')); ?>"><?php esc_html_e('Contact Form Shortcode:','summer-camp'); ?></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($summer_camp_contact_form); ?>">
    	</p>
		
		<?php 
	}
	
	// Updating widget replacing old instances with new
	public function update( $summer_camp_new_instance, $summer_camp_old_instance ) {
		$summer_camp_instance = array();	
		$summer_camp_instance['title'] = (!empty($summer_camp_new_instance['title']) ) ? strip_tags($summer_camp_new_instance['title']) : '';
		$summer_camp_instance['phone'] = (!empty($summer_camp_new_instance['phone']) ) ? summer_camp_sanitize_phone_number($summer_camp_new_instance['phone']) : '';
		$summer_camp_instance['email'] = (!empty($summer_camp_new_instance['email']) ) ? sanitize_email($summer_camp_new_instance['email']) : '';
		$summer_camp_instance['address'] = (!empty($summer_camp_new_instance['address']) ) ? strip_tags($summer_camp_new_instance['address']) : '';
		$summer_camp_instance['timing'] = (!empty($summer_camp_new_instance['timing']) ) ? strip_tags($summer_camp_new_instance['timing']) : '';
		$summer_camp_instance['longitude'] = (!empty($summer_camp_new_instance['longitude']) ) ? strip_tags($summer_camp_new_instance['longitude']) : '';
		$summer_camp_instance['latitude'] = (!empty($summer_camp_new_instance['latitude']) ) ? strip_tags($summer_camp_new_instance['latitude']) : '';
		$summer_camp_instance['contact_form'] = (!empty($summer_camp_new_instance['contact_form']) ) ? strip_tags($summer_camp_new_instance['contact_form']) : '';
        
		return $summer_camp_instance;
	}
}
// Register and load the widget
function summer_camp_contact_custom_load_widget() {
	register_widget( 'Summer_Camp_Contact_Widget' );
}
add_action( 'widgets_init', 'summer_camp_contact_custom_load_widget' );