<?php

// Do not load directly...
if ( ! defined( 'ABSPATH' ) ) { die( 'Direct access forbidden.' ); }

/* Kentooz Framework widget for Tab sidebar. */
class ktz_tabs_widget extends WP_Widget {
	
	function __construct()
	{
		$widget_ops = array( 'classname' => 'ktz_tabber', 'description' => __( 'Widget tab for display comment, popular and tags.',ktz_admin_textdomain) );
		parent::__construct( 'ktz_tabber', __( 'KTZ tabs',ktz_admin_textdomain), $widget_ops );
	}
	
	function widget($args, $instance)
	{
		extract($args);
		$posts = $instance['posts'];
		$comments = $instance['comments'];
		$tags_count = $instance['tags'];
		$show_popular_posts = isset($instance['show_popular_posts']) ? 'true' : 'false';
		$show_comments = isset($instance['show_comments']) ? 'true' : 'false';
		$show_tags = isset($instance['show_tags']) ? 'true' : 'false';
		echo $before_widget;
		?>
			<ul class="nav nav-tabs" id="ktzsbtab">
				<?php if($show_popular_posts == 'true'): ?><li><a href="#post-tabs" data-toggle="tab"><h5><?php _e('Popular', ktz_admin_textdomain); ?></h5></a></li><?php endif; ?>
				<?php if($show_comments == 'true'): ?><li><a href="#comments-tabs" data-toggle="tab"><h5><?php _e('Comments', ktz_admin_textdomain); ?></h5></a></li><?php endif; ?>
				<?php if($show_tags == 'true'): ?><li><a href="#tags-tabs" data-toggle="tab"><h5><?php _e('Tags', ktz_admin_textdomain); ?></h5></a></li><?php endif; ?>
			</ul>
			<div class="tab-content">
			<?php if($show_popular_posts == 'true'):
			$ktzpop = new WP_Query('showposts='.$posts.'&orderby=comment_count&order=DESC');
			if ($ktzpop->have_posts()) : global $post; ?>
            <div class="tab-pane" id="post-tabs"><ul>
				<?php  while ($ktzpop->have_posts()) : $ktzpop->the_post(); $format = get_post_format();
					?>
					<li class="clearfix">
					<?php echo ktz_featured_img( 50, 50, 'thumbnail btn-box' ); // width, height, class	?>
					<div class="ktz-titlecust"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></div>
					<small><?php _e('Posted on:', ktz_admin_textdomain); ?> <?php the_time('j F Y') ?></small>
					</li> 
				    <?php endwhile; ?>
            </ul></div>
            <?php
			wp_reset_query();  // Restore global post data stomped by the_post()
			endif; ?>
			<?php endif; ?>
			<?php if($show_comments == 'true'): ?>
			<div class="tab-pane" id="comments-tabs"><ul>
			<?php
			$number = $instance['comments'];
			global $wpdb;
			$recent_comments = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_author_email, comment_date_gmt, comment_approved, comment_type, comment_author_url, SUBSTRING(comment_content,1,30) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT $number";
			$the_comments = $wpdb->get_results($recent_comments);
			foreach ($the_comments as $comment) { ?>
			<li class="clearfix">
				<?php echo get_avatar($comment, '50'); ?>
				<div class="ktz-titlecust"><a href="<?php echo get_permalink($comment->ID); ?>#comment-<?php echo $comment->comment_ID; ?>" title="<?php echo strip_tags($comment->comment_author); ?> on <?php echo $comment->post_title; ?>"><?php echo strip_tags($comment->com_excerpt); ?></a></div>
				<small><?php _e('Comment by:', ktz_admin_textdomain); ?> <?php echo strip_tags($comment->comment_author); ?></small>
			</li>
			<?php } ?>
			</ul>
			</div>
			<?php endif; ?>
				
			<?php if($show_tags == 'true'): ?>
			<div class="tab-pane" id="tags-tabs">
				<?php wp_tag_cloud( apply_filters('widget_tag_cloud_args', array('smallest'=>10,'largest'=>10,'number'=> $tags_count,'format'=>'list') ) ); ?>
			</div>
			<?php endif; ?>
		</div>
		<!-- END WIDGET -->
		<?php
		echo $after_widget;
	}
	
	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		
		$instance['posts'] = $new_instance['posts'];
		$instance['comments'] = $new_instance['comments'];
		$instance['tags'] = $new_instance['tags'];
		$instance['show_popular_posts'] = $new_instance['show_popular_posts'];
		$instance['show_comments'] = $new_instance['show_comments'];
		$instance['show_tags'] = $new_instance['show_tags'];
		
		return $instance;
	}

	function form($instance)
	{
		$defaults = array('posts' => 3, 'comments' => '3', 'tags' => 20, 'show_popular_posts' => 'on', 'show_comments' => 'on', 'show_tags' =>  'on');
		$instance = wp_parse_args((array) $instance, $defaults); ?>
		<p>
			<label for="<?php echo $this->get_field_id('posts'); ?>"><?php _e( 'Number of posts:',ktz_admin_textdomain ); ?></label>
			<input class="widefat" type="text" style="width: 30px;" id="<?php echo $this->get_field_id('posts'); ?>" name="<?php echo $this->get_field_name('posts'); ?>" value="<?php echo $instance['posts']; ?>" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('comments'); ?>"><?php _e( 'Number of comments:',ktz_admin_textdomain ); ?></label>
			<input class="widefat" type="text" style="width: 30px;" id="<?php echo $this->get_field_id('comments'); ?>" name="<?php echo $this->get_field_name('comments'); ?>" value="<?php echo $instance['comments']; ?>" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('tags'); ?>"><?php _e( 'Number of tags:',ktz_admin_textdomain ); ?></label>
			<input class="widefat" type="text" style="width: 30px;" id="<?php echo $this->get_field_id('tags'); ?>" name="<?php echo $this->get_field_name('tags'); ?>" value="<?php echo $instance['tags']; ?>" />
		</p>
		<p>
			<input class="checkbox" type="checkbox" <?php checked($instance['show_popular_posts'], 'on'); ?> id="<?php echo $this->get_field_id('show_popular_posts'); ?>" name="<?php echo $this->get_field_name('show_popular_posts'); ?>" /> 
			<label for="<?php echo $this->get_field_id('show_popular_posts'); ?>"><?php _e( 'Show popular posts',ktz_admin_textdomain ); ?></label>
		</p>
		<p>
			<input class="checkbox" type="checkbox" <?php checked($instance['show_comments'], 'on'); ?> id="<?php echo $this->get_field_id('show_comments'); ?>" name="<?php echo $this->get_field_name('show_comments'); ?>" /> 
			<label for="<?php echo $this->get_field_id('show_comments'); ?>"><?php _e( 'Show comments',ktz_admin_textdomain ); ?></label>
		</p>
		<p>
			<input class="checkbox" type="checkbox" <?php checked($instance['show_tags'], 'on'); ?> id="<?php echo $this->get_field_id('show_tags'); ?>" name="<?php echo $this->get_field_name('show_tags'); ?>" /> 
			<label for="<?php echo $this->get_field_id('show_tags'); ?>"><?php _e( 'Show tags',ktz_admin_textdomain ); ?></label>
		</p>
	<?php }
}