<?php

// Featured TeoFooterPopularPosts Widget
class TeoFooterPopularPosts extends WP_Widget
{
    function TeoFooterPopularPosts(){
    $widget_ops = array('description' => 'Shows the most funny posts from last 7 days.');
    $control_ops = array('width' => 200, 'height' => 300);
    parent::__construct(false,$name='[Aruna] Footer Funny Stuff Last Week',$widget_ops,$control_ops);
    }

  /* Displays the Widget in the front-end */
    function widget($args, $instance){
    extract($args);

    $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
    $number = $instance['number'];

    echo $before_widget; 

    if ( $title != '' )
        echo $before_title . $title . $after_title;

    echo '<div class="post-preview">';
    add_filter('posts_orderby','teo_ordervotes');
    add_filter( 'posts_where', 'teo_filter_where' );
    $args = array();
    $args['post_type'] = 'post';
    $args['posts_per_page'] = $number;
    $args['meta_key'] = '_ratings_up';
    $args['meta_query'] = array(
        array('key' => '_ratings_up'),
        array('key' => '_ratings_down')
    );
    //performance improvements
    $args['no_found_rows'] = true;
    $args['update_post_term_cache'] = true;
    $args['update_post_meta_cache'] = true;

    $query = new WP_Query($args);
    $i = 1; //count the posts
    while($query->have_posts() ) : $query->the_post(); 
        global $post;

        echo '<div class="left">';
        $type = get_post_format($post->ID);
        $thumb = '';
        if($type == 'image' || $type == 'standard') {
            $thumb = get_post_meta($post->ID, '_standard_image', true);
        }
        elseif($type == 'gallery') {
            $thumb = get_post_meta($post->ID, '_slider_image1', true);
        }
        elseif($type == 'quote') {
            $quoteimg = get_post_meta($post->ID, '_quote_image', true);
            if($quoteimg) {
                $thumb = $quoteimg;
            }
        }
        if($thumb == '') {
            $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
        }
        if($thumb == '' && $type == 'video') {
            $thumb = get_template_directory_uri() . '/img/missing_video.png';
        }
        elseif($thumb == '' && ($type == 'image') || $type == 'standard') {
            $thumb = get_template_directory_uri() . '/img/missing_img.png';
        }
        elseif($thumb == '' && $type == 'gallery') {
            $thumb = get_template_directory_uri() . '/img/missing_slider.png';
        }
        if($thumb == '') {
            $thumb = get_template_directory_uri() . '/img/missing_img.png';
        }
        $resized = aq_resize($thumb, 82, 82, true);
        if($resized == '') {
            $resized = $thumb;
            //we can't resize the original image so we keep that one
        }
        echo '<figure class="small">
            <a href="' . get_permalink() . '">
                <img src="' . $resized . '" alt="' . get_the_title() . '"/>
            </a>
        </figure>';

        echo '</div>';
    $i++; endwhile;  wp_reset_postdata();
    remove_filter('posts_orderby','teo_ordervotes');
    remove_filter( 'posts_where', 'teo_filter_where' );
    echo '</div>';

    echo $after_widget;
  }

  /*Saves the settings. */
    function update($new_instance, $old_instance){
    $instance =  array();
    $instance['title'] = esc_attr($new_instance['title']);
    $instance['number'] = (int)$new_instance['number'];

    return $instance;
  }

  /*Creates the form for the widget in the back-end. */
    function form($instance){
    //Defaults
    $instance = wp_parse_args( (array) $instance, array('title'=> '', 'number' => 3 ) );

    $title = esc_attr($instance['title']);
    $number = (int) $instance['number'];
    
    echo '<p><label for="' . $this->get_field_id('title') . '">' . 'Title: ' . '</label><br /><input id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" value="'. esc_textarea($title)  . '" /></p>';
    echo '<p><label for="' . $this->get_field_id('number') . '">' . 'Number of posts: ' . '</label><input id="' . $this->get_field_id('number') . '" name="' . $this->get_field_name('number') . '" value="'. esc_textarea($number)  . '" /></p>';
  }

}// end TeoFooterPopularPosts class

// Featured TeoRandomPosts Widget
class TeoRandomPosts extends WP_Widget
{
    function TeoRandomPosts(){
    $widget_ops = array('description' => 'Shows random posts.');
    $control_ops = array('width' => 200, 'height' => 300);
    parent::__construct(false,$name='[Aruna] Random posts',$widget_ops,$control_ops);
    }

  /* Displays the Widget in the front-end */
    function widget($args, $instance){
    extract($args);

    $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
    $number = $instance['number'];

    echo $before_widget; 

    if ( $title != '' )
        echo $before_title . $title . $after_title;

    echo '<div class="post-preview">';
    $args = array();
    $args['post_type'] = 'post';
    $args['posts_per_page'] = $number;
    $args['orderby'] = 'rand';
    $args['order'] = 'asc';
    //performance improvements
    $args['no_found_rows'] = true;
    $args['update_post_term_cache'] = true;
    $args['update_post_meta_cache'] = true;

    $query = new WP_Query($args);
    $i = 1; //count the posts
    while($query->have_posts() ) : $query->the_post(); 
        global $post;
        if($i % 3 == 1) {
            echo '<div class="left">';
        }
        if($i % 3 == 0) {
            echo '<div class="right">';
        }
        $type = get_post_format($post->ID);
        $thumb = '';
        if($type == 'image' || $type == 'standard') {
            $thumb = get_post_meta($post->ID, '_standard_image', true);
        }
        elseif($type == 'gallery') {
            $thumb = get_post_meta($post->ID, '_slider_image1', true);
        }
        elseif($type == 'quote') {
            $quoteimg = get_post_meta($post->ID, '_quote_image', true);
            if($quoteimg) {
                $thumb = $quoteimg;
            }
        }
        if($thumb == '') {
            $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
        }
        if($thumb == '' && $type == 'video') {
            $thumb = get_template_directory_uri() . '/img/missing_video.png';
        }
        elseif($thumb == '' && ($type == 'image') || $type == 'standard') {
            $thumb = get_template_directory_uri() . '/img/missing_img.png';
        }
        elseif($thumb == '' && $type == 'gallery') {
            $thumb = get_template_directory_uri() . '/img/missing_slider.png';
        }
        if($thumb == '') {
            $thumb = get_template_directory_uri() . '/img/missing_img.png';
        }
        if($i % 3 == 1 || $i % 3 == 2) {
            $resized = aq_resize($thumb, 82, 82, true);
            if($resized == '') {
                $resized = $thumb;
                //we can't resize the original image so we keep that one
            }
            echo '<figure class="small">
                <a href="' . get_permalink() . '">
                    <img src="' . $resized . '" alt="' . get_the_title() . '"/>
                </a>
            </figure>'; 
        }
        else {
            $resized = aq_resize($thumb, 129, 129, true);
            if($resized == '') {
                $resized = $thumb;
                //we can't resize the original image so we keep that one
            }
            echo '<figure class="big">
                <a href="' . get_permalink() . '">
                    <img src="' . $resized . '" alt="' . get_the_title() . '"/>
                </a>
            </figure>'; 
        }

        if($i % 3 == 2 || $i % 3 == 0) {
            echo '</div>';
        }
    $i++; endwhile;  
    if($i % 3 == 2) {
        echo '</div>';
    }
    echo '</div>';

    echo $after_widget;
  }

  /*Saves the settings. */
    function update($new_instance, $old_instance){
    $instance =  array();
    $instance['title'] = esc_attr($new_instance['title']);
    $instance['number'] = (int)$new_instance['number'];

    return $instance;
  }

  /*Creates the form for the widget in the back-end. */
    function form($instance){
    //Defaults
    $instance = wp_parse_args( (array) $instance, array('title'=> '', 'number' => 3 ) );

    $title = esc_attr($instance['title']);
    $number = (int) $instance['number'];
    
    echo '<p><label for="' . $this->get_field_id('title') . '">' . 'Title: ' . '</label><br /><input id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" value="'. esc_textarea($title)  . '" /></p>';
    echo '<p><label for="' . $this->get_field_id('number') . '">' . 'Number of posts: ' . '</label><input id="' . $this->get_field_id('number') . '" name="' . $this->get_field_name('number') . '" value="'. esc_textarea($number)  . '" /></p>';
  }

}// end TeoRandomPosts class


// Featured TeoPopularAuthors Widget
class TeoPopularAuthors extends WP_Widget
{
    function TeoPopularAuthors(){
    $widget_ops = array('description' => 'Most active authors.');
    $control_ops = array('width' => 200, 'height' => 300);
    parent::__construct(false,$name='[Aruna] Top Authors',$widget_ops,$control_ops);
    }

  /* Displays the Widget in the front-end */
    function widget($args, $instance){
    extract($args);

    $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
    $number = $instance['number'];

    echo $before_widget; 

    if ( $title != '' )
        echo $before_title . $title . $after_title;

    $users = get_users('orderby=post_count&order=DESC&number=' . $number);

    ?>

    <div class="widget-content">
        <?php foreach($users as $user) { 
            $url = get_author_posts_url($user->ID); ?>
            <div class="joker">
                <figure>
                    <?php echo teo_get_avatar($user->ID, 70);?>
                </figure>
                <div class="text">
                    <div class="name"><a href="<?php echo $url?>"><?php truncate_text($user->display_name, 13);?></a></div>
                    <div class="likes"><?php printf(__('%d posts', 'Aruna'), count_user_posts($user->ID) );?></div>
                </div>
                <a href="<?php echo $url?>" class="btn btn-primary btn-block custom-button"><?php printf(__("See profile", 'Aruna'),  $user->display_name); ?></a>
            </div>
        <?php } ?>
    </div>

    <?php echo $after_widget;
  }

  /*Saves the settings. */
    function update($new_instance, $old_instance){
    $instance =  array();
    $instance['title'] = esc_attr($new_instance['title']);
    $instance['number'] = (int)$new_instance['number'];

    return $instance;
  }

  /*Creates the form for the widget in the back-end. */
    function form($instance){
    //Defaults
    $instance = wp_parse_args( (array) $instance, array('title'=> '', 'number' => 3 ) );

    $title = esc_attr($instance['title']);
    $number = (int) $instance['number'];
    
    echo '<p><label for="' . $this->get_field_id('title') . '">' . 'Title: ' . '</label><br /><input id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" value="'. esc_textarea($title)  . '" /></p>';
    echo '<p><label for="' . $this->get_field_id('number') . '">' . 'Number of authors: ' . '</label><input id="' . $this->get_field_id('number') . '" name="' . $this->get_field_name('number') . '" value="'. esc_textarea($number)  . '" /></p>';
  }

}// end TeoPopularAuthors class

// Featured TeoPopularPosts Widget
class TeoPopularPosts extends WP_Widget
{
    function TeoPopularPosts(){
    $widget_ops = array('description' => 'Most popular posts.');
    $control_ops = array('width' => 200, 'height' => 300);
    parent::__construct(false,$name='[Aruna] Most popular posts',$widget_ops,$control_ops);
    }

  /* Displays the Widget in the front-end */
    function widget($args, $instance){
    extract($args);

    $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
    $number = $instance['number'];

    echo $before_widget; 

    if ( $title != '' )
        echo $before_title . $title . $after_title;

    ?>

    <div class="widget-content">
        <div class="post-list">

    <?php
    add_filter('posts_orderby','teo_ordervotes');
    $args = array();
    $args['post_type'] = 'post';
    $args['posts_per_page'] = $number;
    $args['meta_key'] = '_ratings_up';
    $args['meta_query'] = array(
        array('key' => '_ratings_up'),
        array('key' => '_ratings_down')
    );
    //performance improvements
    $args['no_found_rows'] = true;
    $args['update_post_term_cache'] = true;
    $args['update_post_meta_cache'] = true;

    $query = new WP_Query($args);
    $i = 1; //count the posts
    while($query->have_posts() ) : $query->the_post(); 
        global $post;
        $type = get_post_format($post->ID);
        $thumb = '';
        if($type == 'image' || $type == 'standard') {
            $thumb = get_post_meta($post->ID, '_standard_image', true);
        }
        elseif($type == 'gallery') {
            $thumb = get_post_meta($post->ID, '_slider_image1', true);
        }
        if($thumb == '') {
            $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
        }
        if($thumb == '' && $type == 'video') {
            $thumb = get_template_directory_uri() . '/img/video_placeholder.jpg';
        }
        if($thumb == '') {
            $thumb = get_template_directory_uri() . '/img/missing_img.png';
        }
        $resized = aq_resize($thumb, 68, 63, true);
        if($resized == '') {
            $resized = $thumb;
            //we can't resize the original image so we keep that one
        }
    ?>
        <article>
            <?php if($resized != '') { ?>
                <figure>
                    <a href="<?php the_permalink();?>">
                        <img src="<?php echo $resized;?>" alt="<?php the_title();?>" />
                    </a>
                    <figcaption><?php if($i < 10) echo '0' . $i; else echo $i; ?></figcaption>
                </figure>
            <?php } ?>
            <div class="text">
                <h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3>
                <span class="date"><?php the_time("d.m");?></span>
            </div>
        </article>
    <?php $i++; endwhile; wp_reset_postdata(); remove_filter('posts_orderby','teo_ordervotes');?>

        </div>
    </div>

    <?php echo $after_widget;
  }

  /*Saves the settings. */
    function update($new_instance, $old_instance){
    $instance =  array();
    $instance['title'] = esc_attr($new_instance['title']);
    $instance['number'] = (int)$new_instance['number'];

    return $instance;
  }

  /*Creates the form for the widget in the back-end. */
    function form($instance){
    //Defaults
    $instance = wp_parse_args( (array) $instance, array('title'=> '', 'number' => 3 ) );

    $title = esc_attr($instance['title']);
    $number = (int) $instance['number'];
    
    echo '<p><label for="' . $this->get_field_id('title') . '">' . 'Title: ' . '</label><br /><input id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" value="'. esc_textarea($title)  . '" /></p>';
    echo '<p><label for="' . $this->get_field_id('number') . '">' . 'Number of posts: ' . '</label><input id="' . $this->get_field_id('number') . '" name="' . $this->get_field_name('number') . '" value="'. esc_textarea($number)  . '" /></p>';
  }

}// end TeoPopularPosts class


// TeoArunaSearch Widget
class TeoArunaSearch extends WP_Widget
{
    function TeoArunaSearch(){
    $widget_ops = array('description' => 'Search widget.');
    $control_ops = array('width' => 200, 'height' => 300);
    parent::__construct(false,$name='[Aruna] Search widget',$widget_ops,$control_ops);
    }

  /* Displays the Widget in the front-end */
    function widget($args, $instance){ 
    extract($args);

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

    echo $before_widget; 

    if ( $title != '' )
        echo $before_title . $title . $after_title;

    ?>

    <form action="<?php echo home_url(); ?>/">
        <div class="input-group search-widget">
            <input type="text" class="form-control" name="s" placeholder="<?php _e('Search something', 'Aruna');?>" />
            <span class="input-group-btn">
                <button class="btn btn-primary custom-button" name="submit" type="submit"><?php _e('Search', 'Aruna');?></button>
            </span>
        </div>
    </form>
        <?php 

    echo $after_widget;
  }

  /*Saves the settings. */
    function update($new_instance, $old_instance){
    $instance =  array();
    $instance['title'] = esc_attr($new_instance['title']);

    return $instance;
  }

  /*Creates the form for the widget in the back-end. */
    function form($instance){
    //Defaults
    $instance = wp_parse_args( (array) $instance, array('title' => '') );

    $title = esc_attr($instance['title']);
    
    echo '<p><label for="' . $this->get_field_id('title') . '">' . 'Title: ' . '</label><input id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" value="'. esc_textarea($title)  . '" /></p>';
  }

}// end TeoArunaSearch class

// TeoAccordionCategories Widget
class TeoAccordionCategories extends WP_Widget
{
    function TeoAccordionCategories(){
    $widget_ops = array('description' => 'Shows an accordion with multiple categories and posts.');
    $control_ops = array('width' => 200, 'height' => 300);
    parent::__construct(false,$name='[Aruna] Categories accordion',$widget_ops,$control_ops);
    }

  /* Displays the Widget in the front-end */
    function widget($args, $instance){
    extract($args);

    $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
    $number_posts = $instance['number_posts'];
    $categories = $instance['categories'];

    if($number_posts == 0) {
        $number_posts = 5;
    }

    echo $before_widget; 

    if ( $title != '' )
        echo $before_title . $title . $after_title;

    echo '<div class="categories-accordion">
        <div class="panel-group" id="accordion">';

    $i = 1;
    $j = 1;
    foreach($categories as $category) {
        if($i > 5) {
            $i = $i % 5;
        }
        $cat = get_category($category);
        echo '<div class="panel panel-default panel-color-' . $i . '">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse-' . $j . '">' . 
                        $cat->cat_name . '
                    </a>
                </h4>
            </div>
            <div id="collapse-' . $j . '" class="panel-collapse collapse">
                <div class="panel-body">
                    <ul>';
        $query = new WP_Query('posts_per_page=' . $number_posts . '&cat=' . $cat->term_id);
        while($query->have_posts() ) : $query->the_post(); 
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        endwhile; wp_reset_postdata();
        echo '</ul> </div>
            </div>
        </div>';
        $i++;
        $j++;
    }
    echo '</div>
    </div>';

    echo $after_widget;
  }

  /*Saves the settings. */
    function update($new_instance, $old_instance){
    $instance =  array();
    $instance['title'] = esc_attr($new_instance['title']);
    $instance['number_posts'] = (int)$new_instance['number_posts'];
    $instance['categories'] = $new_instance['categories'];

    return $instance;
  }

  /*Creates the form for the widget in the back-end. */
    function form($instance){
    //Defaults
    $instance = wp_parse_args( (array) $instance, array('number_posts'=> 5, 'title' => '', 'categories' => array() ) );

    $title = esc_attr($instance['title']);
    $number_posts = (int) $instance['number_posts'];
    $categories = (array) $instance['categories'];
    
    echo '<p><label for="' . $this->get_field_id('title') . '">' . 'Title: ' . '</label><input id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" value="'. esc_textarea($title)  . '" /></p>';
    echo '<p><label for="' . $this->get_field_id('number_posts') . '">' . 'Number of posts per category: ' . '</label><input id="' . $this->get_field_id('number_posts') . '" name="' . $this->get_field_name('number_posts') . '" value="'. esc_textarea($number_posts)  . '" /></p>';
    $cats = get_categories('hide_empty=0');
    ?>
    <p>
        <label>Categories to include: </label> <br />
        <?php foreach( $cats as $category ) { ?>
            <label>
                <input type="checkbox" name="<?php echo $this->get_field_name('categories'); ?>[]" value="<?php echo $category->cat_ID; ?>"  <?php if(in_array( $category->cat_ID, $categories ) ) echo 'checked="checked" '; ?> /> 
                <?php echo $category->cat_name; ?>
            </label> <br />
        <?php } ?>
    </p> 
    <?php
  }

}// end TeoAccordionCategories class


function VoxisWidgets() {
  register_widget('TeoArunaSearch');
  register_widget('TeoPopularAuthors');
  register_widget('TeoPopularPosts');
  register_widget('TeoFooterPopularPosts');
  register_widget('TeoRandomPosts');
  register_widget('TeoAccordionCategories');
}

add_action('widgets_init', 'VoxisWidgets');