<?php


class best_reviews_widget extends WP_Widget {


    /** constructor */
    function best_reviews_widget() {
        parent::WP_Widget(false, $name = __('( LioIT ) Best Reviews','lioit'));	
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {	
        extract( $args );
		global $wpdb, $smof_data;
		
        $title = apply_filters('widget_title', $instance['image']);
		$title = $instance['title'];
		$image = $instance['image'];
		$number = $instance['number'];
	
		
		echo $before_widget; ?>
  	<?php echo $before_title . $title . $after_title; ?>
	
	<ul class="bestreview">
		<?php  	
				$idObj = get_category_by_slug($image);
				$id = $idObj->term_id;
				$category_link = get_category_link( $id );
				$category_name = get_cat_name( $id );
		global $smof_data;
		global $post;
				$new = new WP_Query(array('showposts' => $number, 'meta_key' => 'lioit_final_score', 'orderby' => 'meta_value', 'cat' => $id, 'nopaging' => 0, 'post_status' => 'publish'));
				if ($new->have_posts()) : while ($new->have_posts()) : $new->the_post(); 
				
				
				
		?>
		<li class="post">
  	<div class="postThumb ">
      
       <a href="<?php the_permalink(); ?>" class="tooltip top" title="<?php the_title(); ?>"><?php the_post_thumbnail('main-block', array('title' => strip_tags(get_the_title()))); ?></a> </div>
    <!--End Post Thumb-->
    <div class="post-title">
      <aside><?php the_category(', ') ?></aside>
      <h6><a href="<?php the_permalink();?>" rel="bookmark" title="<?php the_title(); ?>" class="postlink"><?php 
											$badge_text = get_post_meta($post->ID, 'lioit_post_badge_text', true);
											$badge_color = get_post_meta($post->ID, 'lioit_post_badge_color', true);
										?>
      <?php if ($badge_text) { ?>
      <div class="articleType" style="background-color:<?php echo $badge_color; ?>;"><?php echo $badge_text; ?></div>
      <?php } ?><?php the_title(); ?></a></h6>
    </div>
    <div class="post-content">
   
      <aside class="post-meta">
  
      <?php
                                            $lioit_review_enable =  get_post_meta($post->ID, 'lioit_review_enable', true);
                                            $lioit_review_type =  get_post_meta($post->ID, 'lioit_review_type', true);
                                            $lioit_final_score =  get_post_meta($post->ID, 'lioit_final_score', true);
                                            $lioit_final_percentage = $lioit_final_score * 20 ;
                                        	?>
      <?php if($lioit_review_enable == 'enable'){ ?>
      <span class="lioit-rating lioit-rating-<?php echo esc_html($lioit_review_type); ?>"> <span style="width:<?php echo esc_html($lioit_final_percentage); ?>%;"></span> </span>
      <?php } ?>
      <span class="postViews"><?php echo getPostViews($post->ID); ?> </span> <span class="postComments"><?php comments_popup_link(__('<span class="icon"></span> 0 Comments', 'lioit'), __('<span class="icon"></span> 1 Comment', 'lioit'), __('<span class="icon"></span> % Comments', 'lioit')); ?></span>
      </aside>
    </div>
  </li><?php endwhile; endif; wp_reset_query(); ?>
	</ul>
	
  	<?php echo $after_widget;
	}

  // Save the settings for this instance
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['image'] = strip_tags($new_instance['image']);
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['number'] =  strip_tags($new_instance['number']);
		
		return $instance;
	}

  // Display the widget form in the admin interface
	function form( $instance ) {
		
		$defaults = array('title' => 'Best Reviews');
		$instance = wp_parse_args((array) $instance, $defaults);
?>
    	<p>
        	<label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
        	<input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_html($title); ?>" />
        </p>
		
		<p>
			<label for="<?php echo $this->get_field_id('image'); ?>">Category Slug:				
				<input class="widefat" id="<?php echo $this->get_field_id('image'); ?>" name="<?php echo $this->get_field_name('image'); ?>" type="text" value="<?php echo $instance['image']; ?>" />
			</label>
		</p>
		
		<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of reviews to show:', 'lioit'); ?></label>
		<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $instance['number']; ?>" size="3" /><br />
		<small><?php _e('(at most 15)', 'lioit'); ?></small></p>
		

	

        <?php 
    }


} // class utopian_recent_posts
// register Recent Posts widget
add_action('widgets_init', create_function('', 'return register_widget("best_reviews_widget");'));
