<?php
/**
 * Popular_Posts Widget Class
 */
class Theme_Widget_Popular_Posts extends WP_Widget {

	function Theme_Widget_Popular_Posts() {
		$widget_ops = array('classname' => 'widget_popular_posts', 'description' => __( "Displays the popular posts on your site", THEME_ADMIN_DOMAIN) );
		$this->WP_Widget('popular_posts', THEME_SLUG.' - '.__('Popular Posts', THEME_ADMIN_DOMAIN), $widget_ops);
		$this->alt_option_name = 'widget_popular_posts';

		add_action( 'save_post', array(&$this, 'flush_widget_cache') );
		add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
		add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
	}

	function get_all_author(){
		global $wpdb;
		$order = 'user_id';
		$user_ids = $wpdb->get_col($wpdb->prepare("SELECT $wpdb->usermeta.user_id FROM $wpdb->usermeta where meta_key='wp_user_level' and meta_value>=1 ORDER BY %s ASC",$order));

		foreach($user_ids as $user_id) :
			$user = get_userdata($user_id);
			$all_authors[$user_id] = $user->display_name;
		endforeach;
		return $all_authors;
	}
	
	function widget($args, $instance) {
		$cache = wp_cache_get('theme_widget_popular_posts', 'widget');

		if ( !is_array($cache) )
			$cache = array();

		if ( isset($cache[$args['widget_id']]) ) {
			echo $cache[$args['widget_id']];
			return;
		}

		ob_start();
		extract($args);

		$title = apply_filters('widget_title', empty($instance['title']) ? __('Popular Posts', THEME_FRONT_DOMAIN) : $instance['title'], $instance, $this->id_base);
		if ( !$number = (int) $instance['number'] )
			$number = 10;
		else if ( $number < 1 )
			$number = 1;
		else if ( $number > 15 )
			$number = 15;
		
		if ( !$desc_length = (int) $instance['desc_length'] )
			$desc_length = 80;
		else if ( $desc_length < 1 )
			$desc_length = 1;
		if ( !$title_length = (int) $instance['title_length'] )
			$title_length = '';
		else if ( $title_length < 1 )
			$title_length = '';
		$disable_thumbnail = $instance['disable_thumbnail'] ? '1' : '0';
		$use_lightbox = $instance['use_lightbox'] ? '1' : '0';
		$display_extra_type = $instance['display_extra_type'] ? $instance['display_extra_type'] :'time';
		if($display_extra_type == 'both'){
			$display_extra_type = array('time','description');
		}else{
			$display_extra_type = array($display_extra_type);
		}
		$query = array('showposts' => $number, 'nopaging' => 0, 'orderby'=> 'comment_count', 'post_status' => 'publish', 'ignore_sticky_posts' => 1);
		if(!empty($instance['cat'])){
			$query['cat'] = implode(',', $instance['cat']);
		}
		if(!empty($instance['authors'])){
			$query['author'] = implode(',', $instance['authors']);
		}
		$exclude_cats = theme_get_option('blog','exclude_categorys');
		if(!empty($exclude_cats)){
			$query['category__not_in'] = $exclude_cats;
		}
		$r = new WP_Query($query);
		if ($r->have_posts()) :
?>
		<?php echo $before_widget; ?>
		<?php if ( $title ) echo $before_title . $title . $after_title; ?>
		<ul class="posts_list">
<?php  while ($r->have_posts()) : $r->the_post(); ?>
			<li>
<?php if(!$disable_thumbnail):?>

<?php if($use_lightbox):?>

<?php if (has_post_thumbnail() ): ?>
				<a class="thumbnail" rel="prettyPhoto" href="<?php $thumb =  wp_get_attachment_image_src(get_post_thumbnail_id(),'full',true); echo $thumb[0];?>" title="<?php the_title();?>">
<?php elseif(theme_get_option('blog','display_default_thumbnail')):
		$default_thumbnail_custom = theme_get_option('blog','default_thumbnail_custom');
		$image = theme_parse_image($default_thumbnail_custom,'widget_posts_thumbnail.png',false);
		$resize = theme_resize($image['media'],65,65,$image['method']);
?>
				<a class="thumbnail" rel="prettyPhoto" href="<?php echo $resize['url'];?>" title="<?php the_title();?>">
<?php endif;//end has_post_thumbnail ?>

<?php else:?>
				<a class="thumbnail" href="<?php echo get_permalink() ?>" title="<?php the_title();?>">
<?php endif;//use_lightbox ?>

<?php if (has_post_thumbnail() ): ?>

					<?php the_post_thumbnail(array(65,65),array('title'=>get_the_title(),'alt'=>get_the_title())); ?>	
<?php elseif(theme_get_option('blog','display_default_thumbnail')):
		$default_thumbnail_custom = theme_get_option('blog','default_thumbnail_custom');
		$image = theme_parse_image($default_thumbnail_custom,'widget_posts_thumbnail.png',false);
		$resize = theme_resize($image['media'],65,65,$image['method']);
?>
					<img src="<?php echo $resize['url']?>" width="65" height="65" title="<?php the_title();?>" alt="<?php the_title();?>"/>
<?php endif;//end has_post_thumbnail ?>
				</a>

<?php endif;//disable_thumbnail ?>

				<div class="post_extra_info">
						<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) { if($title_length && mb_strlen(get_the_title())>$title_length){echo mb_substr(get_the_title(),0,$title_length).'...';}else{the_title();} }else the_ID(); ?></a>
<?php if(in_array('time', $display_extra_type)):?>
					<time datetime="<?php the_time('Y-m-d') ?>"><?php echo get_the_date(); ?></time>
<?php endif;?>
<?php if(in_array('description', $display_extra_type)):?>
					<p><?php echo wp_html_excerpt(get_the_excerpt(),$desc_length);?>...</p>
<?php endif;//end display extra type ?>
				</div>
				<div class="clear"></div>
			</li>
<?php endwhile; ?>
		</ul>
		<?php echo $after_widget; ?>
<?php
		// Reset the global $the_post as this query will have stomped on it
		wp_reset_query();

		endif;

		$cache[$args['widget_id']] = ob_get_flush();
		wp_cache_set('theme_widget_popular_posts', $cache, 'widget');
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['number'] = (int) $new_instance['number'];
		$instance['desc_length'] = (int) $new_instance['desc_length'];
		$instance['disable_thumbnail'] = !empty($new_instance['disable_thumbnail']) ? 1 : 0;
		$instance['use_lightbox'] = !empty($new_instance['use_lightbox']) ? 1 : 0;
		$instance['display_extra_type'] = $new_instance['display_extra_type'];
		$instance['cat'] = $new_instance['cat'];
		$instance['title_length'] = (int) $new_instance['title_length'];
		$instance['authors'] = $new_instance['authors'];
		
		$this->flush_widget_cache();

		$alloptions = wp_cache_get( 'alloptions', 'options' );
		if ( isset($alloptions['theme_widget_popular_posts']) )
			delete_option('theme_widget_popular_posts');

		return $instance;
	}

	function flush_widget_cache() {
		wp_cache_delete('theme_widget_popular_posts', 'widget');
	}

	function form( $instance ) {
		$title = isset($instance['title']) ? esc_attr($instance['title']) : '';
		$disable_thumbnail = isset( $instance['disable_thumbnail'] ) ? (bool) $instance['disable_thumbnail'] : false;
		$use_lightbox = isset( $instance['use_lightbox'] ) ? (bool) $instance['use_lightbox'] : false;
		$display_extra_type = isset( $instance['display_extra_type'] ) ? $instance['display_extra_type'] : 'time';
		$cat = isset($instance['cat']) ? $instance['cat'] : array();
		$authors = isset($instance['authors']) ? $instance['authors'] : array();
		$authors_list = $this->get_all_author();
		
		if ( !isset($instance['number']) || !$number = (int) $instance['number'] )
			$number = 5;

		if ( !isset($instance['desc_length']) || !$desc_length = (int) $instance['desc_length'] )
			$desc_length = 80;
		if ( !isset($instance['title_length']) || !$title_length = (int) $instance['title_length'] )
			$title_length = '';
		$categories = get_categories('orderby=name&hide_empty=0');

?>
		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:',THEME_ADMIN_DOMAIN); ?></label>
		<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>

		<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:', THEME_ADMIN_DOMAIN); ?></label>
		<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>

		<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('disable_thumbnail'); ?>" name="<?php echo $this->get_field_name('disable_thumbnail'); ?>"<?php checked( $disable_thumbnail ); ?> />
		<label for="<?php echo $this->get_field_id('disable_thumbnail'); ?>"><?php _e( 'Disable Post Thumbnail?' , THEME_ADMIN_DOMAIN); ?></label></p>

		<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('use_lightbox'); ?>" name="<?php echo $this->get_field_name('use_lightbox'); ?>"<?php checked( $use_lightbox ); ?> />
		<label for="<?php echo $this->get_field_id('use_lightbox'); ?>"><?php _e( 'Use Lightbox?', THEME_ADMIN_DOMAIN ); ?></label></p>
			
		<p><label for="<?php echo $this->get_field_id('title_length'); ?>"><?php _e('Length of Title to show:', THEME_ADMIN_DOMAIN); ?></label>
		<input id="<?php echo $this->get_field_id('title_length'); ?>" name="<?php echo $this->get_field_name('title_length'); ?>" type="text" value="<?php echo $title_length; ?>" size="3" /></p>
		<p>
			<label for="<?php echo $this->get_field_id('display_extra_type'); ?>"><?php _e( 'Display Extra infomation type:', THEME_ADMIN_DOMAIN ); ?></label>
			<select name="<?php echo $this->get_field_name('display_extra_type'); ?>" id="<?php echo $this->get_field_id('display_extra_type'); ?>" class="widefat">
				<option value="time"<?php selected($display_extra_type,'time');?>><?php _e( 'Time', THEME_ADMIN_DOMAIN ); ?></option>
				<option value="description"<?php selected($display_extra_type,'description');?>><?php _e( 'Description', THEME_ADMIN_DOMAIN ); ?></option>
				<option value="both"<?php selected($display_extra_type,'both');?>><?php _e( 'Time and Description', THEME_ADMIN_DOMAIN ); ?></option>
				<option value="none"<?php selected($display_extra_type,'none');?>><?php _e( 'None', THEME_ADMIN_DOMAIN ); ?></option>
			</select>
		</p>
		
		<p><label for="<?php echo $this->get_field_id('desc_length'); ?>"><?php _e('Length of Description to show:', THEME_ADMIN_DOMAIN); ?></label>
		<input id="<?php echo $this->get_field_id('desc_length'); ?>" name="<?php echo $this->get_field_name('desc_length'); ?>" type="text" value="<?php echo $desc_length; ?>" size="3" /></p>

		<p>
			<label for="<?php echo $this->get_field_id('cat'); ?>"><?php _e( 'Categorys:' , THEME_ADMIN_DOMAIN); ?></label>
			<select style="height:5.5em" name="<?php echo $this->get_field_name('cat'); ?>[]" id="<?php echo $this->get_field_id('cat'); ?>" class="widefat" multiple="multiple">
				<?php foreach($categories as $category):?>
				<option value="<?php echo $category->term_id;?>"<?php echo in_array($category->term_id, $cat)? ' selected="selected"':'';?>><?php echo $category->name;?></option>
				<?php endforeach;?>
			</select>
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('authors'); ?>"><?php _e( 'Authors:' , THEME_ADMIN_DOMAIN); ?></label>
			<select style="height:5.5em" name="<?php echo $this->get_field_name('authors'); ?>[]" id="<?php echo $this->get_field_id('authors'); ?>" class="widefat" multiple="multiple">
				<?php foreach($authors_list as $user_id => $display_name):?>
				<option value="<?php echo $user_id;?>"<?php echo in_array($user_id, $authors)? ' selected="selected"':'';?>><?php echo $display_name;?></option>
				<?php endforeach;?>
			</select>
		</p>
<?php
	}
}
