<?php

class Theme_Widget_ExArchives extends WP_Widget {

	var $prefix;
	var $textdomain;

	/**
	 * Set up the widget's unique name, ID, class, description, and other options.
	 * @since 0.6
	 */
	function Theme_Widget_ExArchives() {
		$this->prefix = THEME_SLUG;
		$this->textdomain = THEME_ADMIN_DOMAIN;

		$widget_ops = array( 'classname' => 'exarchives', 'description' => __( 'An advanced widget that gives you total control over the output of your archives.', $this->textdomain ) );
		$control_ops = array( 'width' => 700, 'height' => 350,  );
		$this->WP_Widget( "{$this->prefix}-archives", __( 'symplex-Archives', $this->textdomain ), $widget_ops, $control_ops );
	}

	/**
	 * Outputs the widget based on the arguments input through the widget controls.
	 * @since 0.6
	 */
	function widget( $args, $instance ) {
		extract( $args );

		$type = $instance['type']; 
		$format = $instance['format'];
		$before = null;
		$after = null;
		$limit = (int)$instance['limit'];

		$show_post_count = isset( $instance['show_post_count'] ) ? $instance['show_post_count'] : false;
		
		if ( !$limit )
			$limit = '';
		if ($format == 'custom') {
			$before = $instance['before'];
			$after  = $instance['after'];
		}
		$args = array(
			'type' => $type,
			'limit' => $limit,
			'format' => $format,
			'before' => $before,
			'after' => $after,
			'show_post_count' => $show_post_count,
			'echo' => 0,
		);

		echo $before_widget;

		if ( $instance['title'] )
			echo $before_title . apply_filters( 'widget_title', $instance['title'] ) . $after_title;

		$archives = str_replace( array( "\r", "\n", "\t" ), '', wp_get_archives( $args ) );

		if ( 'option' == $format ) {

			if ( 'yearly' == $type )
				$option_title = __( 'Select Year',$this->textdomain );
			elseif ( 'monthly' == $type )
				$option_title = __( 'Select Month',$this->textdomain );
			elseif ( 'weekly' == $type )
				$option_title = __( 'Select Week',$this->textdomain );
			elseif ( 'daily' == $type )
				$option_title = __( 'Select Day',$this->textdomain );
			elseif ( 'postbypost' == $type || 'alpha' == $type )
				$option_title = __( 'Select Post',$this->textdomain );

			echo '<select name="archive-dropdown" onchange=\'document.location.href=this.options[this.selectedIndex].value;\'>';
			echo '<option value="">' . esc_attr( $option_title ) . '</option>';
			echo $archives;
			echo '</select>';
		}
		elseif ( 'html' == $format ) {
			echo '<ul class="archives">' . $archives . '</ul>';
		}
		else {
			echo $archives;
		}

		echo $after_widget;
	}

	/**
	 * Updates the widget control options for the particular instance of the widget.
	 * @since 0.6
	 */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['before'] = $new_instance['before'];
		$instance['after'] = $new_instance['after'] ;
		$instance['limit'] = strip_tags( $new_instance['limit'] );
		$instance['type'] = $new_instance['type'];
		$instance['format'] = $new_instance['format'];
		$instance['show_post_count'] = ! empty( $new_instance['show_post_count'] ) ? 1 : 0 ;

		return $instance;
	}

	/**
	 * Displays the widget control options in the Widgets admin screen.
	 * @since 0.6
	 */
	function form( $instance ) {

		//Defaults
		$title              = ! empty( $instance['title'] )           ? esc_attr( $instance['title'] ) : THEME_SLUG.'-archives';
		$before        	    = ! empty( $instance['before'] )          ? esc_attr( $instance['before'] ) : '';
		$after              = ! empty( $instance['after'] )           ? esc_attr( $instance['after'] ) : '';
		$limit 		        = ! empty( $instance['limit'] )           ? esc_attr( $instance['limit'] ) : '';
		$type 		        = isset( $instance['type'] )              ? esc_attr( $instance['type'] ) : '';
		$format             = isset( $instance['format'] )            ? esc_attr( $instance['format'] ) : '';
		$show_post_count    = isset( $instance['show_post_count'] )   ? 1 : 0;
?>

		<div style="float:left;width:48%;">
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', $this->textdomain ); ?></label>
			<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $title; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'limit' ); ?>"><?php _e( 'Limit:', $this->textdomain ); ?> <code>limit</code></label>
			<input type="text" id="<?php echo $this->get_field_id( 'limit' ); ?>" name="<?php echo $this->get_field_name( 'limit' ); ?>" value="<?php echo $limit; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'type' ); ?>"><?php _e( 'Type:',$this->textdomain ); ?> <code>type</code></label> 
			<select id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" class="widefat" style="width:100%;">
				<option <?php if ( 'yearly' == $type ) echo 'selected="selected"'; ?>>yearly</option>
				<option <?php if ( 'monthly' == $type ) echo 'selected="selected"'; ?>>monthly</option>
				<option <?php if ( 'weekly' == $type ) echo 'selected="selected"'; ?>>weekly</option>
				<option <?php if ( 'daily' == $type ) echo 'selected="selected"'; ?>>daily</option>
				<option value="postbypost" <?php if ( 'postbypost' == $type ) echo 'selected="selected"'; ?>>post order by date</option>
				<option value="alpha"<?php if ( 'alpha' == $type ) echo 'selected="selected"'; ?>>post order by title</option>
			</select>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'format' ); ?>"><?php _e( 'Format:',$this->textdomain ); ?> <code>format</code></label> 
			<select id="<?php echo $this->get_field_id( 'format' ); ?>" name="<?php echo $this->get_field_name( 'format' ); ?>" class="widefat" style="width:100%;">
				<option <?php if ( 'html' == $format ) echo 'selected="selected"'; ?>>html</option>
				<option <?php if ( 'option' == $format ) echo 'selected="selected"'; ?>>option</option>
				<option <?php if ( 'links' == $format ) echo 'selected="selected"'; ?>>links</option>
				<option <?php if ( 'custom' == $format ) echo 'selected="selected"'; ?>>custom</option>
			</select>
		</p>
		</div>

		<div style="float:right;width:48%;">
		<p>
			<label for="<?php echo $this->get_field_id( 'before' ); ?>"><?php _e( 'Before:', $this->textdomain ); ?> <code>before</code></label>
			<input type="text" id="<?php echo $this->get_field_id( 'before' ); ?>" name="<?php echo $this->get_field_name( 'before' ); ?>" value="<?php echo $before; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'after' ); ?>"><?php _e( 'After:', $this->textdomain ); ?> <code>after</code></label>
			<input type="text" id="<?php echo $this->get_field_id( 'after' ); ?>" name="<?php echo $this->get_field_name( 'after' ); ?>" value="<?php echo $after; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'show_post_count' ); ?>">
			<input class="checkbox" type="checkbox" <?php checked( $show_post_count, true ); ?> id="<?php echo $this->get_field_id( 'show_post_count' ); ?>" name="<?php echo $this->get_field_name( 'show_post_count' ); ?>" /> <?php _e( 'Show post count?', $this->textdomain ); ?> <code>show_post_count</code></label>
		</p>
		</div>
		<div style="clear:both;">&nbsp;</div>
	<?php
	}
}

?>
