<?php

/**

 * Plugin Name: Homepage Two-Column Widget

 */



add_action( 'widgets_init', 'cat_load_widgets' );



function cat_load_widgets() {

	register_widget( 'cat_widget' );

}



class cat_widget extends WP_Widget {



	/**

	 * Widget setup.

	 */

	function cat_widget() {

		/* Widget settings. */

		$widget_ops = array( 'classname' => 'cat_widget', 'description' => __('A widget that displays 4 posts from a category of your choice.', 'lioit') );



		/* Widget control settings. */

		$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'cat_widget' );



		/* Create the widget. */

		$this->WP_Widget( 'cat_widget', __('( LioIT ) Cat Widget', 'lioit'), $widget_ops, $control_ops );

	}



	/**

	 * How to display the widget on the screen.

	 */

	function widget( $args, $instance ) {

		extract( $args );



		/* Our variables from the widget settings. */

		$title = apply_filters('widget_title', $instance['title'] );

		$categories = $instance['categories'];






		?>

<div class="lioLatestPosts mainCatWidget">
<div class="LioLatestCat">
  <h2><a href="<?php echo get_category_link( $categories ); ?>"><?php echo $title; ?></a></h2>
  <ul class="LioCatPosts">
    <?php global $post; $recent = new WP_Query(array( 'cat' => $categories, 'showposts' => '4'  )); while($recent->have_posts()) : $recent->the_post();?>
    <li> 
      <h5><?php $type_badge = get_post_meta($post->ID, 'lioit_post_type', true);?>
        
      <a href="<?php the_permalink() ?>" class="lioCatPostTitle">
        <div class="<?php echo $type_badge; ?> articleType"></div><?php the_title(); ?>
      </a>
      </h5>
      
      <div class="LioCatPostsThumb">
        
        
        <?php $img = catch_that_image($post->ID); ?>
        <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>">
        <img src="<?php echo Lioit_URI; ?>/scripts/timthumb.php?src=<?php echo $img ?>&amp;w=62&amp;h=62&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
        </a>
        
      </div>
      <p><?php echo excerpt(10); ?></p>
      <div class="lioPosMeta"> <span class="postDate">
        <?php the_time('F d, Y'); ?>
        </span> <span class="postComments"><a href="<?php comments_link();?>">
        <?php comments_number('No Comments','1 Comment','% Comments');?>
        </a></span> </div>
    </li>
    <?php endwhile; ?>
  </ul>
</div>
</div>
<?php



		/* After widget (defined by themes). */

	

	}



	/**

	 * Update the widget settings.

	 */

	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['categories'] = $new_instance['categories'];





		return $instance;

	}





	function form( $instance ) {



		/* Set up some default widget settings. */

		$defaults = array( 'title' => __('Widget Title' , 'lioit'), 'categories' => 4);

		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

<!-- Widget Title: Text Input -->

<p>
  <label for="<?php echo $this->get_field_id( 'title' ); ?>">
    <?php _e('Title:','lioit'); ?>
  </label>
  <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
</p>

<!-- Category -->

<p>
  <label for="<?php echo $this->get_field_id('categories'); ?>"><?php _e('Select Category:' , 'lioit'); ?></label>
  <select id="<?php echo $this->get_field_id('categories'); ?>" name="<?php echo $this->get_field_name('categories'); ?>" style="width:100%;">
    <option value='all' <?php if ('all' == $instance['categories']) echo 'selected="selected"'; ?>><?php _e('All Categories' , 'lioit'); ?></option>
    <?php $categories = get_categories('hide_empty=0&depth=1&type=post'); ?>
    <?php foreach($categories as $category) { ?>
    <option value='<?php echo $category->term_id; ?>' <?php if ($category->term_id == $instance['categories']) echo 'selected="selected"'; ?>><?php echo $category->cat_name; ?></option>
    <?php } ?>
  </select>
</p>
<?php

	}

}



?>
