<?php
/**
 * Widget: Events
 *
 * @package WordPress
 * @subpackage Charity
 * @since Charity 1.0
 */
?>
<?php


add_action('widgets_init', function(){return register_widget("wpl_sidebar_events_widget");});
class wpl_sidebar_events_widget extends WP_Widget {


	/*-----------------------------------------------------------------------------------*/
	/*	Widget actual processes
	/*-----------------------------------------------------------------------------------*/

	public function __construct() {
		parent::__construct(
	 		'wpl_sidebar_events_widget',
			__( 'WPL Events (Sidebar)', 'charity-wpl' ),
			array( 'description' => __( 'A widget to displaying Events', 'charity-wpl' ), )
		);
	}


	/*-----------------------------------------------------------------------------------*/
	/*	Outputs the options form on admin
	/*-----------------------------------------------------------------------------------*/

	public function form( $instance ) {

		if ( $instance ) {
			$title = esc_attr( $instance[ 'title' ] );
			$nr_posts = esc_attr( $instance[ 'nr_posts' ] );
			$display_style = esc_attr( $instance[ 'display_style' ] );
			$event_type = esc_attr( $instance[ 'event_type' ] );
		}
		else {
			$title = __( 'Events', 'charity-wpl' );
			$nr_posts = 3;
			$display_style = 'slider';
			$event_type = 'up';
		}
		 ?>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('display_style') ); ?>"><?php _e('Display Style:', 'charity-wpl'); ?> <br /> </label>
				<select id="<?php echo esc_attr( $this->get_field_id('display_style') ); ?>" name="<?php echo esc_attr( $this->get_field_name('display_style') ); ?>">
					<option value="slider" <?php selected( 'slider', $display_style ); ?>><?php _e('Slider', 'charity-wpl'); ?></option>
					<option value="list" <?php selected( 'list', $display_style ); ?>><?php _e('List', 'charity-wpl'); ?></option>
				</select>
			</p>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('event_type') ); ?>"><?php _e('Event type', 'charity-wpl'); ?> <br /> </label>
				<select id="<?php echo esc_attr( $this->get_field_id('event_type') ); ?>" name="<?php echo esc_attr( $this->get_field_name('event_type') ); ?>">
					<option value="up" <?php selected( 'up', $event_type ); ?>><?php _e('Upcoming Events', 'charity-wpl'); ?></option>
					<option value="past" <?php selected( 'past', $event_type ); ?>><?php _e('Past Events', 'charity-wpl'); ?></option>
				</select>
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"> <?php _e('Title:', 'charity-wpl'); ?> </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_html( $title ); ?>" />
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('nr_posts') ); ?>"> <?php _e('Number of Events:', 'charity-wpl'); ?> </label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('nr_posts') ); ?>" name="<?php echo esc_attr( $this->get_field_name('nr_posts') ); ?>" type="text" value="<?php echo esc_attr( $nr_posts ); ?>" />
				<p style="font-size: 10px; color: #999; margin: -10px 0 0 0px; padding: 0px;"> <?php _e('Number of Events you want to display', 'charity-wpl'); ?></p>
			</p>

			<br />
			<p style="font-size: 10px; color: #999; margin: -10px 0 0 0px; padding: 0px;">
				<?php printf( __( 'The ID of this widget is: %1$s %3$s %2$s', 'charity-wpl' ), '<strong>', '</strong>',  '#'.esc_attr( $this->id ) ); ?>
			</p>
			<br />
		<?php
	}


	/*-----------------------------------------------------------------------------------*/
	/*	Processes widget options to be saved
	/*-----------------------------------------------------------------------------------*/

	public function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = sanitize_text_field($new_instance['title']);
		$instance['nr_posts'] = sanitize_text_field($new_instance['nr_posts']);
		$instance['display_style'] = sanitize_text_field($new_instance['display_style']);
		$instance['event_type'] = sanitize_text_field($new_instance['event_type']);
		return $instance;
	}


	/*-----------------------------------------------------------------------------------*/
	/*	Outputs the content of the widget
	/*-----------------------------------------------------------------------------------*/

	public function widget( $args, $instance ) {
		global $post;
		extract( $args );
		$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance );
		$nr_posts = isset( $instance['nr_posts'] ) ? esc_attr( $instance['nr_posts'] ) : '';
		$display_style = isset( $instance['display_style'] ) ? esc_attr( $instance['display_style'] ) : '';
		$event_type = isset( $instance['event_type'] ) ? esc_attr( $instance['event_type'] ) : '';
		$wplook_datenow =  date("Y-m-d H:i");
		if ($event_type == 'up') {
			$wplook_datenow = new DateTime();
			$wplook_datenow->setDate( date("Y"), date("m"), date("d") );
			$gmt_offset = get_option('gmt_offset') - 4;
			$wplook_datenow->modify($gmt_offset.' hours');
			$wplook_datenow = $wplook_datenow->format('Y-m-d H:i');
		}
		$compare = ($event_type == 'up') ? '>=' : '<=';
		$compareorder = ($event_type == 'up') ? 'ASC' : 'DESC';

		// WP_Query arguments
		$args = array(
			'post_type'              => 'post_events',
			'post_status'            => 'publish',
			'pagination'             => false,
			'posts_per_page'         => $nr_posts,
			'orderby'				=> 'meta_value',
			'meta_key'				=> 'wpl_event_date_time',
			'order' => $compareorder,
			'meta_query' => array(
				array(
					'key'     => 'wpl_event_date_time',
					'value'   => $wplook_datenow,
					'compare' => $compare,
				),
			),
		);


		// The Query
		$the_query = null;
		$the_query = new WP_Query( $args ); ?>

	<?php if ( $display_style == 'list') : ?>
		<aside class="widget widget_posts">
			<h3 class="widget-title"><?php echo esc_html( $title ); ?></h3><!-- /.widget-title -->

			<ul class="list-popular-posts">
				<?php
					if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();  ?>
						<?php
							$event_meta		= get_post_meta(get_the_ID());
							$event_date 	= isset($event_meta['wpl_event_date_time'][0]) ? $event_meta['wpl_event_date_time'][0]  : false;
							$event_location = isset($event_meta['wpl_event_location'][0]) ? $event_meta['wpl_event_location'][0] : false;
						?>

							<li>
								<span>
									<?php
										if ( has_post_thumbnail() ) {
											the_post_thumbnail( 'small-event' );
											echo '<a href="'. esc_url( get_permalink() ) .'"><i class="fas fa-external-link-alt"></i></a>';
										}
									?>
								</span>

								<div>
									<h4>
										<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
									</h4>
									<?php if ($event_date )  { ?>
										<p><?php echo date( get_option( 'date_format' ), strtotime( $event_meta['wpl_event_date_time'][0] ) ); ?></p>
									<?php } ?>
								</div>
							</li>

						<?php endwhile; ?>
					<?php else : ?>
						<p><?php _e('Sorry, no events matched your criteria.', 'charity-wpl'); ?></p>
					<?php endif; ?>
					<?php wp_reset_postdata(); ?>
			</ul><!-- /.list-events -->
		</aside><!-- /.widget widget_events <?php echo esc_attr($this->id); ?> -->
	<?php else : ?>
		<aside class="widget widget_posts">
			<h3 class="widget-title"><?php echo esc_html( $title ); ?></h3><!-- /.widget-title -->

			<div class="slider-widget">
				<div class="slider-clip">
					<ul class="slides owl-carousel">
						<?php
							if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();  ?>
								<?php
									$event_meta		= get_post_meta(get_the_ID());
									$wpl_event_url = isset($event_meta['wpl_event_url'][0]) ? $event_meta['wpl_event_url'][0] : false;
									$wpl_event_eventbrite_url = isset($event_meta['wpl_event_eventbrite_url'][0]) ? $event_meta['wpl_event_eventbrite_url'][0] : "";
									$wpl_event_custom_url = isset($event_meta['wpl_event_custom_url'][0]) ? $event_meta['wpl_event_custom_url'][0] : "";
									$wpl_event_custom_label = isset($event_meta['wpl_event_custom_label'][0]) ? $event_meta['wpl_event_custom_label'][0] : "";
									$event_date 	= isset($event_meta['wpl_event_date_time'][0]) ? $event_meta['wpl_event_date_time'][0]  : false;
									$event_location = isset($event_meta['wpl_event_location'][0]) ? $event_meta['wpl_event_location'][0] : false;
								?>

									<li class="slide">
										<div class="event-small">
										<?php
											if ( has_post_thumbnail() ) {
												echo '<div class="event-image"><a href="'. esc_url( get_permalink() ) .'">';
												the_post_thumbnail( 'medium-event' );
												echo '</a>';
												echo '<div class="event-date">';
												echo '<small>'.date( 'M', strtotime( $event_meta['wpl_event_date_time'][0] ) ).'</small>';
												echo '<span>'.date( 'd', strtotime( $event_meta['wpl_event_date_time'][0] ) ).'</span>';
												echo '</div></div>';
											}
										?>

											<div class="event-content">
												<h4>
													<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
												</h4>

												<?php echo wplook_short_excerpt(ot_get_option('wpl_events_excerpt_limit')); ?>
											</div><!-- /.event-content -->

											<div class="event-actions">
												<a href="<?php the_permalink(); ?>" class="button"><?php _e('More', 'charity-wpl'); ?></a>

												<?php if ($wpl_event_url) { ?>
													<a class="button button-event-social button-facebook" href="<?php echo esc_url( $wpl_event_url ); ?>"><i class="fab fa-facebook"></i></a>
												<?php } ?>
												<?php if ($wpl_event_eventbrite_url) { ?>
													<a class="button button-event-social button-eventbrite" href="<?php echo esc_url( $wpl_event_eventbrite_url ); ?>"><i class="far fa-calendar-alt"></i></a>
												<?php } ?>
												<?php if ($wpl_event_custom_url) { ?>
													<a class="button button-event-social button-custom" href="<?php echo esc_url( $wpl_event_custom_url ); ?>"><i class="fas fa-globe"></i></a>
												<?php } ?>
											</div><!-- /.event-actions -->
										</div><!-- /.event-small -->
									</li><!-- /.slide -->

								<?php endwhile; ?>
							<?php else : ?>
								<p><?php _e('Sorry, no events matched your criteria.', 'charity-wpl'); ?></p>
							<?php endif; ?>
							<?php wp_reset_postdata(); ?>
					</ul><!-- /.list-events -->
				</div><!-- /.slider-clip -->
			</div><!-- /.slider-widget -->
		</aside><!-- /.widget widget_events <?php echo esc_attr($this->id); ?> -->
	<?php endif; ?>
		<?php
	}
}
?>
