<?php
/*-----------------------------------------------------------------------------------

CLASS INFORMATION

Description: A custom WooThemes Archives widget.
Date Created: 2011-04.
Last Modified: 2011-04-26.
Author: WooThemes.
Since: 1.0.0


TABLE OF CONTENTS

- function (constructor)
- function widget ()
- function update ()
- function form ()

- Register the widget on `widgets_init`.

-----------------------------------------------------------------------------------*/

class Woo_Widget_Archives extends WP_Widget {

	/*----------------------------------------
	  Constructor.
	  ----------------------------------------
	  
	  * The constructor. Sets up the widget.
	----------------------------------------*/

	function Woo_Widget_Archives () {
		
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'widget_woo_archives', 'description' => __( 'A custom WooThemes archives widget.', 'woothemes' ) );

		/* Widget control settings. */
		$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'woo_archives' );

		/* Create the widget. */
		$this->WP_Widget( 'woo_archives', __('Woo - Archives', 'woothemes' ), $widget_ops, $control_ops );
		
	} // End Constructor

	/*----------------------------------------
	  widget()
	  ----------------------------------------
	  
	  * Displays the widget on the frontend.
	----------------------------------------*/

	function widget( $args, $instance ) {  
		
		$html = '';
		
		extract( $args, EXTR_SKIP );
		
		/* Our variables from the widget settings. */
		$title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base );
		$category_id = apply_filters('widget_title', $instance['category_id'], $instance, $this->id_base );
			
		/* Before widget (defined by themes). */
		echo $before_widget;

		/* Display the widget title if one was input (before and after defined by themes). */
		if ( $title ) {
		
			echo $before_title . $title . $after_title;
		
		} // End IF Statement
		
		/* Widget content. */
		
		// Add actions for plugins/themes to hook onto.
		do_action( 'widget_woo_archives_top' );
		
		global $post;
		
		$query = 'post_type=post&posts_per_page=-1';
		if ( $category_id != 0 ) {
			$query .= '&cat=' . $category_id;
		}
		$myposts = get_posts( $query );
					
		$dates_array 			= array();
		$year_array 			= array();
		$i 						= 0;
		$prev_post_ts    		= null;
		$prev_post_year  		= null;
		$distance_multiplier 	=  9;
		
		if ( count( $myposts ) > 0 ) {
			foreach( $myposts as $post ) {
				setup_postdata( $post );
				
				$post_ts    = strtotime( $post->post_date );
				$post_year  = date( 'Y', $post_ts );
				$post_month = date( 'm', $post_ts );
				
				// Handle the first year as a special case
				if ( is_null( $prev_post_year ) ) {
					$working_year = $post_year;
?>
<div class="archive">
	
	<div class="fl"><span class="archive_year"><a href="<?php echo get_year_link($working_year); ?>"><?php echo $working_year; ?></a></span></div>
	
	<div class="fr">
	
		<ul class="archives_list">
<?php
				} else if ( $prev_post_year != $post_year ) {
?>
		</ul>
	
	</div>
	
	<div class="fix"></div>

</div><!--/.archive-->
<?php			
	$working_year  =  $prev_post_year;
	// Print year headings until we reach the post year
	while ( $working_year > $post_year ) {
		$working_year--;
?>
		<div class="archive">
		<div class="fl"><span class="archive_year"><a href="<?php echo get_year_link($working_year); ?>"><?php echo $working_year; ?></a></span></div>
		<?php if ( $working_year > $post_year ) { ?>
			<div class="fr"></div>
			<div class="fix"></div>
			</div><!--/.archive-->
		<?php } ?>
<?php
	}
// Open a new ordered list
?>
<div class="fr">

<ul class="archives_list">
<?php
				} // End IF Statement
				
				// Compute difference in days
				if ( ! is_null( $prev_post_ts ) && $prev_post_year == $post_year ) {
					$dates_diff  =  ( date( 'z', $prev_post_ts ) - date( 'z', $post_ts ) ) * $distance_multiplier;
				}
				else {
					$dates_diff  =  0;
				}
				
				// jump to next iteneration if month has already been counted 
				if ($prev_post_month == $post_month) continue;
?>
<li>
<a href="<?php echo get_month_link($working_year,get_the_time( 'm' )); ?>"><?php the_time( 'M' ); ?></a>
<?php
		// For subsequent iterations
		$prev_post_ts    =  $post_ts;
		$prev_post_year  =  $post_year;
		$prev_post_month = $post_month;

			} // End FOREACH Loop
			// If we've processed at least *one* post, close the ordered list
			if ( ! is_null( $prev_post_ts ) ) {
?>
</div>	<!-- fr -->
</ul> <!-- archives_list -->
<div class="fix"></div>
</div> <!-- archive -->
<?php
			}
			
		} // End IF Statement
		
		// Add actions for plugins/themes to hook onto.
		do_action( 'widget_woo_archives_bottom' );

		/* After widget (defined by themes). */
		echo $after_widget;

	} // End widget()

	/*----------------------------------------
	  update()
	  ----------------------------------------
	
	* Function to update the settings from
	* the form() function.
	
	* Params:
	* - Array $new_instance
	* - Array $old_instance
	----------------------------------------*/
	
	function update ( $new_instance, $old_instance ) {
		
		$instance = $old_instance;

		/* Strip tags for title and name to remove HTML (important for text inputs). */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance["category_id"] = intval($new_instance["category_id"]);
		
		return $instance;
		
	} // End update()
	
   /*----------------------------------------
	 form()
	 ----------------------------------------
	  
	  * The form on the widget control in the
	  * widget administration area.
	  
	  * Make use of the get_field_id() and 
	  * get_field_name() function when creating
	  * your form elements. This handles the confusing stuff.
	  
	  * Params:
	  * - Array $instance
	----------------------------------------*/

   function form( $instance ) {       
   
       /* Set up some default widget settings. */
		$defaults = array(
						'title' => __( 'Archives', 'woothemes' ),
						'category_id' => 0
					);

		$instance = wp_parse_args( (array) $instance, $defaults );		
		
		$instance = wp_parse_args( $instance, array_fill_keys( array( 'title', 'category_id' ), array('', 0) ) );
?>
       <!-- Widget Title: Text Input -->
       <p>
	   	   <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title (optional):', 'woothemes' ); ?></label>
	       <input type="text" name="<?php echo $this->get_field_name( 'title' ); ?>"  value="<?php echo $instance['title']; ?>" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" />
       </p>
	   
        <?php
		$category_id = $instance['category_id'];
        echo "<p>";
        echo "<label for=\"" . $this->get_field_id("category_id") . "\">";
		_e( 'Category' );
		echo ":<br/>";
        echo "<select name=\"" . $this->get_field_name("category_id") . 
             "\" id=\"" . $this->get_field_id("category_id") . "\">";
        $categories=  get_categories("orderby=ID&order=asc"); 
        foreach ($categories as $cat) {
            $option = "<option value=\"" . $cat->cat_ID . "\"";
            if ($cat->cat_ID == $category_id)
            {
                $option .= "selected=\"selected\" ";
            }
            $option .= ">";
            $option .= $cat->cat_ID . " : " . $cat->cat_name . " (" . $cat->count . ")";
            $option .= "</option>";
            echo $option;
        }
        echo "</select>";
        echo "</label></p>";
		?>
	   
<?php
	} // End form()
	
} // End Class

/*----------------------------------------
  Register the widget on `widgets_init`.
  ----------------------------------------
  
  * Registers this widget.
----------------------------------------*/

add_action( 'widgets_init', create_function( '', 'return register_widget("Woo_Widget_Archives");' ), 1 ); 
?>