<?php

/*------------------------------------------*/
/* Hermes: Events           */
/*------------------------------------------*/
 
add_action('widgets_init', create_function('', 'return register_widget("hermes_widget_events");'));

class hermes_widget_events extends WP_Widget {
	
	function hermes_widget_events() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'hermes-events', 'description' => 'Displays events according to chosen criteria.' );

		/* Widget control settings. */
		$control_ops = array( 'id_base' => 'hermes-widget-events' );

		/* Create the widget. */
		$this->WP_Widget( 'hermes-widget-events', 'Hermes: Events', $widget_ops, $control_ops );
	}
	
	function widget( $args, $instance ) {
		
		extract( $args );

		/* User-selected settings. */
		$title 	= apply_filters('widget_title', $instance['title'] );
		$link_to_page	= $instance['link_to_page'];
		$link_to_page = hermes_wpml_pageid($link_to_page); // make it WPML-friendly
		$show_count	= $instance['show_count'];
		$photo_size = $instance['photo_size'];
		$show_photo = $instance['show_photo'];
		$show_date = $instance['show_date'];
		$show_date_year = $instance['show_date_year'];
		
		/* Before widget (defined by themes). */
		echo $before_widget;
		
		/* Title of widget (before and after defined by themes). */
	
		if ( $title ) { 
			echo $before_title;
			
			if ($link_to_page) {
				echo '<a href="' . get_page_link( $link_to_page ) . '">'.$title.'</a>';
			}
			else {
				echo $title;
			}
			
			echo $after_title;
		}
		

		$current_time = current_time('mysql');
		list( $today_year, $today_month, $today_day, $hour, $minute, $second ) = split( '([^0-9])', $current_time );
		$current_timestamp = $today_year . $today_month . $today_day . $hour . $minute;
		
		$meta_query = array(
			array(
				'key' => '_start_eventtimestamp',
				'value' => $current_timestamp,
				'compare' => '>'
			)
		);
		
		$args = array( 
			'post_type' => 'event',
			'meta_query' => $meta_query,
			'meta_key' => '_start_eventtimestamp',
			'orderby'=> 'meta_value_num',
			'order' => 'ASC',
			'posts_per_page' => $show_count,
		);
	
		$events = new WP_Query( $args ); 
		
		if ($events->have_posts()) {
			$i = 0;
			?>
	
				<ul class="hermes-posts hermes-events">

		<?php
		while ( $events->have_posts() ) : $events->the_post(); global $post;

		unset($same_day,$same_time,$parentMeta); 
		
		$parentMeta = get_post_custom();
		$event_start_day = $parentMeta['_start_day'][0];
		$event_start_month = $parentMeta['_start_month'][0];
		$event_start_year = $parentMeta['_start_year'][0];
		$event_end_day = $parentMeta['_end_day'][0];
		$event_end_month = $parentMeta['_end_month'][0];
		$event_end_year = $parentMeta['_end_year'][0];
		$event_start_hour = $parentMeta['_start_hour'][0];
		$event_start_minute = $parentMeta['_start_minute'][0];
		$event_end_hour = $parentMeta['_end_hour'][0];
		$event_end_minute = $parentMeta['_end_minute'][0];
		
		$metaDateStart = "$event_start_day/$event_start_month/$event_start_year";
		$metaDateEnd = "$event_end_day/$event_end_month/$event_end_year";
		if ($event_start_hour != '00' && $event_start_minute != '00') {
			$metaTimeStart = "$event_start_hour:$event_start_minute";
		}
		$metaTimeEnd = "$event_end_hour:$event_end_minute";
		$isoDateStart = "$event_start_year-$event_start_month-$event_start_day";
		$isoDateEnd = "$event_end_year-$event_end_month-$event_end_day";
		
		if ($metaDateEnd && ($metaDateEnd != $metaDateStart)) {
			$metaDate = "$metaDateStart - $metaDateEnd";
		}
		else {
			$metaDate = "$metaDateStart";
		}
		
		$day_start = date("j", mktime(0,0,0,$event_start_month, $event_start_day, $event_start_year));
		$month_start = date("F", mktime(0,0,0,$event_start_month, $event_start_day, $event_start_year));
		$year_start = date("Y", mktime(0,0,0,$event_start_month, $event_start_day, $event_start_year));
		$day_end = date("j", mktime(0,0,0,$event_end_month, $event_end_day, $event_end_year));
		$month_end = date("F", mktime(0,0,0,$event_end_month, $event_end_day, $event_end_year));
		$year_end = date("Y", mktime(0,0,0,$event_end_month, $event_end_day, $event_end_year));
		
		if (($day_start == $day_end) && ($month_start == $month_end)) {
			$same_day = true;
		}
		
		if ($same_day && ($event_start_hour == $event_end_hour) && ($event_start_minute == $event_end_minute)) {
			$same_time = true;
		}
		$classes = array('hermes-post','hermes-event');

		?>
		
		<li <?php post_class($classes); ?>>
	
			<?php
			if ($show_photo == 'on') {
				if ($photo_size == 'thumb-recent-posts') {
					get_the_image( array( 'size' => 'thumb-recent-posts', 'width' => 60, 'before' => '<div class="post-cover">', 'after' => '</div><!-- end .post-cover -->' ) );
				} else {
					get_the_image( array( 'size' => 'thumb-loop-main', 'width' => 130, 'height' => 80, 'before' => '<div class="post-cover">', 'after' => '</div><!-- end .post-cover -->' ) );
				}
			}
			?>
			
			<div class="post-excerpt<?php if ($show_photo != 'on') { echo ' excerpt-full'; } ?>">
				<h2 class="title-post title-s"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'hermes_textdomain' ), the_title_attribute( 'echo=0' ) ); ?>"><?php the_title(); ?></a></h2>
				<?php if ($show_date == 'on' && $metaDate) { ?>
				<p class="postmeta"><span class="meta-date">
					<span class="value value-date"><?php echo $day_start; ?></span>
					<span class="value value-month"><?php echo $month_start; ?></span>

					<?php if ($show_date_year == 'on') { ?>
					<span class="value value-year"><?php echo $year_start; ?></span>
					<?php } ?>
					
					<?php if ($same_day == false && $same_time == false) { ?>
					<?php _e('at', 'hermes_textdomain');?> <span class="value value-time"><?php echo "$event_start_hour:$event_start_minute"; ?></span>
					<?php } ?>

					<?php if ($same_day == false) { ?>
					<span class="value value-date"> &mdash; <?php echo $day_end; ?></span>
					<span class="value value-month"><?php echo $month_end; ?></span>
					<?php if ($show_date_year == 'on') { ?>
					<span class="value value-year"><?php echo $year_end; ?></span>
					<?php } // if show year ?>

					<?php if ($same_time == false) { ?>
					<?php _e('at', 'hermes_textdomain');?> <span class="value value-time"><?php echo "$event_end_hour:$event_end_minute"; ?></span>
					<?php } ?>

					<?php } // if same day == false ?>

					<?php if ($same_day == true && $same_time == false) { ?>
					<?php _e('at', 'hermes_textdomain');?> <span class="value value-time"><?php echo "$event_start_hour:$event_start_minute - $event_end_hour:$event_end_minute"; ?></span>
					<?php } // if same day == true and same time == false ?>

				</span></p><!-- .meta-date -->
				<?php } ?>
				<?php the_excerpt(); ?>
			</div><!-- end .post-excerpt -->
			<div class="cleaner">&nbsp;</div>
			
		</li><!-- end .hermes-post -->
		<?php endwhile; 
		wp_reset_query();
		?>
		</ul><!-- end .hermes-posts .hermes-events -->
		<?php
		}

		/* After widget (defined by themes). */
		echo $after_widget;
	}
	
	
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags (if needed) and update the widget settings. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['link_to_page'] = strip_tags( $new_instance['link_to_page'] );
		$instance['show_count'] = (int)strip_tags( $new_instance['show_count'] );
		$instance['photo_size'] = $new_instance['photo_size'];
		$instance['show_photo'] = $new_instance['show_photo'];
		$instance['show_date'] = $new_instance['show_date'];
		$instance['show_date_year'] = $new_instance['show_date_year'];
		return $instance;
	}
	
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array( 'title' => 'Widget Title', 'show_count' => 1, 'show_photo' => 'on', 'show_date' => 'on', 'show_date_year' => 'on');
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>" style="display: block; font-size: 11px; font-weight: bold; margin: 0 0 5px;"><?php _e('Widget Title', 'hermes_textdomain'); ?>:</label>
			<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" type="text" class="widefat" style="padding: 7px 5px; font-size: 11px;" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'link_to_page' ); ?>"><?php _e('Link Widget Title to Page', 'hermes_textdomain'); ?>:</label><br />
			<select id="<?php echo $this->get_field_id('link_to_page'); ?>" name="<?php echo $this->get_field_name('link_to_page'); ?>" class="widefat" style="font-size: 11px;">
				<option value="0"><?php _e('Choose page', 'hermes_textdomain'); ?>:</option>
				<?php
				$pages = get_pages();
				
				foreach ($pages as $page) {
				$option = '<option value="'.$page->ID;
				if ($page->ID == $instance['link_to_page']) { $option .='" selected="selected';}
				$option .= '">';
				$option .= $page->post_title;
				$option .= '</option>';
				echo $option;
				}
			?>
			</select>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'photo_size' ); ?>"><?php _e('Photo Size', 'hermes_textdomain'); ?>:</label><br />
			<select id="<?php echo $this->get_field_id( 'photo_size' ); ?>" name="<?php echo $this->get_field_name( 'photo_size' ); ?>" class="widefat" style="font-size: 11px;">
				<option value="thumb-recent-posts"<?php if (!$instance['photo_size'] || $instance['photo_size'] == 'thumb-recent-posts') { echo ' selected="selected"';} ?>>Small</option>
				<option value="thumb-loop-main"<?php if ($instance['photo_size'] == 'thumb-loop-main') { echo ' selected="selected"';} ?>>Normal</option>
			</select>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'show_count' ); ?>" style="display: block; font-size: 11px; font-weight: bold; margin: 0 0 5px;"><?php _e('How many events to display', 'hermes_textdomain'); ?></label>
			<input id="<?php echo $this->get_field_id( 'show_count' ); ?>" name="<?php echo $this->get_field_name( 'show_count' ); ?>" value="<?php echo $instance['show_count']; ?>" type="text" style="padding: 7px 5px; width: 40px;" />
		</p>

		<p>
			<input class="checkbox" type="checkbox" id="<?php echo $this->get_field_id('show_photo'); ?>" name="<?php echo $this->get_field_name('show_photo'); ?>" <?php if ($instance['show_photo'] == 'on') { echo ' checked="checked"';  } ?> />
			<label for="<?php echo $this->get_field_id( 'show_photo' ); ?>" style="font-size: 11px; font-weight: bold; margin: 0 0 5px;"><?php _e('Display Featured Image', 'hermes_textdomain'); ?></label>
		</p>

		<p>
			<input class="checkbox" type="checkbox" id="<?php echo $this->get_field_id('show_date'); ?>" name="<?php echo $this->get_field_name('show_date'); ?>" <?php if ($instance['show_date'] == 'on') { echo ' checked="checked"';  } ?> />
			<label for="<?php echo $this->get_field_id( 'show_date' ); ?>" style="font-size: 11px; font-weight: bold; margin: 0 0 5px;"><?php _e('Display Event Date', 'hermes_textdomain'); ?></label>
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" id="<?php echo $this->get_field_id('show_date_year'); ?>" name="<?php echo $this->get_field_name('show_date_year'); ?>" <?php if ($instance['show_date_year'] == 'on') { echo ' checked="checked"';  } ?> />
			<label for="<?php echo $this->get_field_id( 'show_date_year' ); ?>" style="font-size: 11px; font-weight: bold; margin: 0 0 5px;"><?php _e('Display Event Year', 'hermes_textdomain'); ?></label>
		</p>
		
		<?php
	}
}