<?php
   add_action('widgets_init', 'tigu_tabs_load_widgets');
   function tigu_tabs_load_widgets()
   {
      register_widget('Tigu_Tabs_Widget');
   }
      class Tigu_Tabs_Widget extends WP_Widget {
   function Tigu_Tabs_Widget()
   {
      $widget_ops = array(
     'classname' => 'tigu_tabs',
      'description' => 'Latest Posts, Most Popular posts, Most Viewed/Played and Random Posts.');
   $control_ops = array(
  'id_base' => 'tigu_tabs-widget');
   $this->WP_Widget('tigu_tabs-widget', 'GAMELEON: Homepage Tabs', $widget_ops, $control_ops);
   }
   function widget($args, $instance)
   {
      extract($args);

/* User selected settings. */
      $posts = $instance['posts'];
      $exclude_cat = !empty($instance['exclude_cat']) ? $instance['exclude_cat'] : NULL;
      $exclude_cat_pop = !empty($instance['exclude_cat_pop']) ? $instance['exclude_cat_pop'] : NULL;
      $exclude_cat_most = !empty($instance['exclude_cat_most']) ? $instance['exclude_cat_most'] : NULL;
      $exclude_cat_rand = !empty($instance['exclude_cat_rand']) ? $instance['exclude_cat_rand'] : NULL;
      $recent_posts = isset($instance['show_recent_posts']) ? 'true' : 'false';
      $random_posts = isset($instance['show_random_posts']) ? 'true' : 'false';
      $show_popular_posts = isset($instance['show_popular_posts']) ? 'true' : 'false';
      $show_recent_posts = isset($instance['show_recent_posts']) ? 'true' : 'false';
      $show_random_posts = isset($instance['show_random_posts']) ? 'true' : 'false';
      $show_getMostPlayed = isset($instance['show_getMostPlayed']) ? 'true' : 'false';
      echo $before_widget;
?>

<!-- START WIDGET -->

      <div class="tabs-wrapper">
      <ul class="tabs">
<?php if($show_recent_posts == 'true'): ?><li><a href="#tab1"><?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?><?php _e('Latest Posts', 'Gameleon'); ?><?php else: ?><?php _e('Latest Games', 'Gameleon'); ?><?php endif; ?></a></li><?php endif; ?>
<?php if($show_popular_posts == 'true'): ?><li><a href="#tab2"><?php _e('Most Popular', 'Gameleon'); ?></a></li><?php endif; ?>
<?php if($show_getMostPlayed == 'true'): ?><li><a href="#tab3"><?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?><?php _e('Most Viewed', 'Gameleon'); ?><?php else: ?><?php _e('Most Played', 'Gameleon'); ?><?php endif; ?></a></li><?php endif; ?>
<?php if($show_random_posts == 'true'): ?><li><a href="#tab4"><?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?><?php _e('Random Posts', 'Gameleon'); ?><?php else: ?><?php _e('Random Games', 'Gameleon'); ?><?php endif; ?></a></li><?php endif; ?>
      </ul>
<div class="tabs-back">
      <div class="homepage-tabs_container" style="height:176px;">
<?php if($show_recent_posts == 'true'): ?>
      <div id="tab1" class="tab_content" style="padding:15px 0px 10px 4px; overflow:hidden">
<?php
       $show_post = 4;
       $recent_posts = new WP_Query();
       $recent_posts->query('showposts='.$show_post.'&orderby=date&cat='.$exclude_cat.'');
       while ($recent_posts->have_posts()) : $recent_posts->the_post();
?>
      <div class="homepage-block-tabs">
<?php if( has_post_thumbnail() || is_myarcade_game() ): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'hometabs-thumb'); ?>
<?php if ( empty($image[0]) ) $image[0] = myarcade_get_thumbnail_url (); ?>


      <div class="grid-image block"  style="height:176px;">
<div class="shadow_01">
<a class=<?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?>"hover_play_small-wp"<?php else: ?>"hover_play_small"<?php endif; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<img src="<?php echo $image[0]; ?>" width="137" height="100" alt="<?php the_title(); ?>" />
</a>
</div>
<div class="datetabs"><?php the_time('F j, Y - G:i') ?></div>
<h2><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<?php if(get_option('tigu_shorten_titles') == 'true'): ?>
<?php $shorttitle = substr(the_title('','',FALSE),0,20); ?><?php echo $shorttitle; if (strlen($shorttitle) >19){ echo '&hellip;'; } ?>
<?php else: ?>
<?php the_title(); ?>
<?php endif; ?>
</a></h2>
      </div>
<?php else: ?>
      <div class="grid-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumbnail.png" width="137" height="100" alt="<?php the_title(); ?>" />
</a></div>
<?php endif; ?>
      </div>
<?php endwhile; wp_reset_query(); ?>
      </div>
<?php endif; ?>
<?php if($show_popular_posts == 'true'): ?>
      <div id="tab2" class="tab_content" style="padding:15px 0px 10px 4px; overflow:hidden">
<?php
      $show_post = 4;
      $popular_posts = new WP_Query();
      $popular_posts->query('showposts='.$show_post.'&orderby=comment_count&cat='.$exclude_cat_pop.'');
      while ($popular_posts->have_posts()) : $popular_posts->the_post();
?>
      <div class="homepage-block-tabs">
<?php if( has_post_thumbnail() || is_myarcade_game() ): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'hometabs-thumb'); ?>
<?php if ( empty($image[0]) ) $image[0] = myarcade_get_thumbnail_url (); ?>
      <div class="grid-image block"  style="height:176px;">
<div class="shadow_01">
<a class=<?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?>"hover_play_small-wp"<?php else: ?>"hover_play_small"<?php endif; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<img src="<?php echo $image[0]; ?>" width="137" height="100" alt="<?php the_title(); ?>" />
</a>
</div>
<div class="datetabs">
<?php if( get_option('tigu_comments_type', 'WordPress') == 'WordPress') : ?>
<?php comments_number(__( 'no comments', 'Gameleon'), __('one comment', 'Gameleon'), __('% comments', 'Gameleon'));?>
<?php else: ?>
<fb:comments-count href="<?php echo get_permalink($post->ID); ?>"></fb:comments-count> <?php _e('COMMENTS', 'Gameleon'); ?>
<?php endif; ?>

</div>
<h2><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>

<?php if(get_option('tigu_shorten_titles') == 'true'): ?>
<?php $shorttitle = substr(the_title('','',FALSE),0,20); ?><?php echo $shorttitle; if (strlen($shorttitle) >19){ echo '&hellip;'; } ?>
<?php else: ?>
<?php the_title(); ?>
<?php endif; ?>
</a></h2>

      </div>
<?php else: ?>
      <div class="grid-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>

<img src="<?php echo get_template_directory_uri(); ?>/images/thumbnail.png" width="137" height="100" alt="<?php the_title(); ?>" />

</a>
      </div>
<?php endif; ?>
      </div>
<?php endwhile; wp_reset_query(); ?>
      </div>
<?php endif; ?>
<?php if($show_getMostPlayed == 'true'): ?>
      <div id="tab3" class="tab_content" style="padding:15px 0px 10px 4px; overflow:hidden">
<?php
      query_posts('meta_key=post_views_count&posts_per_page=4&orderby=meta_value_num&cat='.$exclude_cat_most.'');
      if (have_posts()) :
      while (have_posts()) :
      the_post();
?>
      <div class="homepage-block-tabs">
<?php if( has_post_thumbnail() || is_myarcade_game() ): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'hometabs-thumb'); ?>
<?php if ( empty($image[0]) ) $image[0] = myarcade_get_thumbnail_url (); ?>
      <div class="grid-image block"  style="height:176px;">
<div class="shadow_01">
<a class=<?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?>"hover_play_small-wp"<?php else: ?>"hover_play_small"<?php endif; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<img src="<?php echo $image[0]; ?>" width="137" height="100" alt="<?php the_title(); ?>" />
</a>
</div>
<div class="datetabs">
<?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?>
<?php _e('Total Views', 'Gameleon'); ?>: <?php echo getMostPlayed(get_the_ID()); ?> 
<?php else: ?>
<?php _e('Played', 'Gameleon'); ?>: <?php echo getMostPlayed(get_the_ID()); ?> <?php _e('times', 'Gameleon'); ?>
<?php endif; ?>

</div>
<h2><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<?php if(get_option('tigu_shorten_titles') == 'true'): ?>
<?php $shorttitle = substr(the_title('','',FALSE),0,20); ?><?php echo $shorttitle; if (strlen($shorttitle) >19){ echo '&hellip;'; } ?>
<?php else: ?>
<?php the_title(); ?>
<?php endif; ?>
</a></h2>

      </div>
<?php else: ?>
      <div class="grid-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumbnail.png" width="137" height="100" alt="<?php the_title(); ?>" />
</a></div>
<?php endif; ?>
      </div>
<?php endwhile; endif; wp_reset_query(); ?>
      </div>
<?php endif; ?>
<?php if($show_random_posts == 'true'): ?>
      <div id="tab4" class="tab_content" style="padding:15px 0px 10px 4px; overflow:hidden">
<?php
      $show_post = 4;
      $random_posts = new WP_Query();
      $random_posts->query('showposts='.$show_post.'&orderby=rand&cat='.$exclude_cat_rand.'');
      while ($random_posts->have_posts()) : $random_posts->the_post();
?>
      <div class="homepage-block-tabs">
<?php if( has_post_thumbnail() || is_myarcade_game() ): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'hometabs-thumb'); ?>
<?php if ( empty($image[0]) ) $image[0] = myarcade_get_thumbnail_url (); ?>
      <div class="grid-image block"  style="height:176px;">

<div class="shadow_01">
<a class=<?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?>"hover_play_small-wp"<?php else: ?>"hover_play_small"<?php endif; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<img src="<?php echo $image[0]; ?>" width="137" height="100" alt="<?php the_title(); ?>" />
</a>
</div>
<div class="datetabs">
<?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?>
<?php _e('Total Views', 'Gameleon'); ?>: <?php echo getMostPlayed(get_the_ID()); ?> 
<?php else: ?>
<?php _e('Played', 'Gameleon'); ?>: <?php echo getMostPlayed(get_the_ID()); ?> <?php _e('times', 'Gameleon'); ?>
<?php endif; ?>

</div>
<h2><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>

<?php if(get_option('tigu_shorten_titles') == 'true'): ?>
<?php $shorttitle = substr(the_title('','',FALSE),0,20); ?><?php echo $shorttitle; if (strlen($shorttitle) >19){ echo '&hellip;'; } ?>
<?php else: ?>
<?php the_title(); ?>
<?php endif; ?>
</a></h2>

      </div>
<?php else: ?>
      <div class="grid-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumbnail.png" width="137" height="100" alt="<?php the_title(); ?>" />
</a></div>
<?php endif; ?>
      </div>
<?php endwhile; wp_reset_query(); ?>
      </div>
<?php endif; ?>
      </div>
      </div>
</div>
<!-- END WIDGET -->

<?php echo $after_widget;
   }
   function update($new_instance, $old_instance)
   {
      $instance = $old_instance;
      $instance['posts'] = $new_instance['posts'];
      $instance['exclude_cat'] =  $new_instance['exclude_cat'];
      $instance['exclude_cat_pop'] =  $new_instance['exclude_cat_pop'];
      $instance['exclude_cat_most'] =  $new_instance['exclude_cat_most'];
      $instance['exclude_cat_rand'] =  $new_instance['exclude_cat_rand'];
      $instance['getMostPlayed'] = $new_instance['getMostPlayed'];
      $instance['show_getMostPlayed'] = $new_instance['show_getMostPlayed'];
      $instance['popular_posts'] = $new_instance['popular_posts'];
      $instance['show_popular_posts'] = $new_instance['show_popular_posts'];
      $instance['show_recent_posts'] = $new_instance['show_recent_posts'];
      $instance['show_random_posts'] = $new_instance['show_random_posts'];
      return $instance;
   }
   function form($instance)
   {
      $defaults = array(
      'exclude_cat' => '',
      'exclude_cat_pop' => '',
      'exclude_cat_most' => '',
      'exclude_cat_rand' => '',
      'show_popular_posts' => 'on',
      'show_recent_posts' => 'on',
      'show_getMostPlayed' =>  'on',
      'show_random_posts' => 'on');
      $instance = wp_parse_args((array) $instance, $defaults); ?>

<!-- START ADMIN WIDGETS AREA -->

    <p>
      <input class="checkbox" type="checkbox" <?php checked($instance['show_recent_posts'], 'on'); ?> id="<?php echo $this->get_field_id('show_recent_posts'); ?>" name="<?php echo $this->get_field_name('show_recent_posts'); ?>" />
      <label for="<?php echo $this->get_field_id('show_recent_posts'); ?>"><?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?>Show Latest Posts<?php else: ?>Show Latest Games<?php endif; ?></label>
    </p>
 <p>
          <label for="<?php echo $this->get_field_id('exclude_cat'); ?>">Exclude categories by ID:</label>
         <input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('exclude_cat'); ?>" name="<?php echo $this->get_field_name('exclude_cat'); ?>" value="<?php echo $instance['exclude_cat']; ?>" />
     <br />
      <small>You may exclude some categories by typing their IDs, with minus sign in front of them, separated by comma and no spaces. Example: -3,-4,-21 for multiple IDs and just -3 without comma, for one ID only. Just leave it empty if you do not need to exclude any categories.</small>
   </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 if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?>Show Popular Posts<?php else: ?>Show Popular Games<?php endif; ?></label>
    </p>
 <p>
          <label for="<?php echo $this->get_field_id('exclude_cat_pop'); ?>">Exclude categories by ID:</label>
         <input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('exclude_cat_pop'); ?>" name="<?php echo $this->get_field_name('exclude_cat_pop'); ?>" value="<?php echo $instance['exclude_cat_pop']; ?>" />
     <br />
      <small>You may exclude some categories by typing their IDs, with minus sign in front of them, separated by comma and no spaces. Example: -3,-4,-21 for multiple IDs and just -3 without comma, for one ID only. Just leave it empty if you do not need to exclude any categories.</small>
    </p>
    <p>
      <input class="checkbox" type="checkbox" <?php checked($instance['show_getMostPlayed'], 'on'); ?> id="<?php echo $this->get_field_id('show_getMostPlayed'); ?>" name="<?php echo $this->get_field_name('show_getMostPlayed'); ?>" />
      <label for="<?php echo $this->get_field_id('show_getMostPlayed'); ?>"><?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?>Show Most Viewed Posts<?php else: ?>Show Most Played Games<?php endif; ?></label>
    </p>
 <p>
          <label for="<?php echo $this->get_field_id('exclude_cat_most'); ?>">Exclude categories by ID:</label>
         <input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('exclude_cat_most'); ?>" name="<?php echo $this->get_field_name('exclude_cat_most'); ?>" value="<?php echo $instance['exclude_cat_most']; ?>" />
     <br />
      <small>You may exclude some categories by typing their IDs, with minus sign in front of them, separated by comma and no spaces. Example: -3,-4,-21 for multiple IDs and just -3 without comma, for one ID only. Just leave it empty if you do not need to exclude any categories.</small>
      </p>
    <p>
      <input class="checkbox" type="checkbox" <?php checked($instance['show_random_posts'], 'on'); ?> id="<?php echo $this->get_field_id('show_random_posts'); ?>" name="<?php echo $this->get_field_name('show_random_posts'); ?>" />
      <label for="<?php echo $this->get_field_id('show_random_posts'); ?>"><?php if( get_option('tigu_template_type', 'WordPres Default') == 'WordPres Default') : ?>Show Random Posts<?php else: ?>Show Random Games<?php endif; ?></label>
    </p>
 <p>
          <label for="<?php echo $this->get_field_id('exclude_cat_rand'); ?>">Exclude categories by ID:</label>
         <input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('exclude_cat_rand'); ?>" name="<?php echo $this->get_field_name('exclude_cat_rand'); ?>" value="<?php echo $instance['exclude_cat_rand']; ?>" />
     <br />
     <small>You may exclude some categories by typing their IDs, with minus sign in front of them, separated by comma and no spaces. Example: -3,-4,-21 for multiple IDs and just -3 without comma, for one ID only. Just leave it empty if you do not need to exclude any categories.</small>
    </p>

<!-- END ADMIN WIDGETS AREA -->

<?php }
}
?>