<?php
add_action('widgets_init', 'news_in_pics_widget');

function news_in_pics_widget() {
    register_widget('news_in_pics');
}

class news_in_pics extends WP_Widget {

    function news_in_pics() {
        $widget_ops = array('classname' => 'w-pictures');
        $control_ops = array('width' => 250, 'height' => 350, 'id_base' => 'id-widget-news-pics');
        $this->WP_Widget('id-widget-news-pics', theme_name . ' - News in Pictures', $widget_ops, $control_ops);
    }

    function widget($args, $instance) {
        global $post;
        extract($args);

        $title = apply_filters('widget_title', $instance['box_title']);
        
        $no_of_posts = $instance['no_of_posts'];
        if(!isset($no_of_posts) || $no_of_posts == '' || $no_of_posts <= 0 ){ $no_of_posts = 3; }
        
        //convert $cat_id to an array
        $cat_id = explode(',', $instance['cat_id']); 
        
        $posts_order = $instance['order'];
        if(!isset($posts_order) || $posts_order == '') { $posts_order = 'latest'; }
        
        echo $before_widget;
        
        if ($title) :
            echo $before_title;
            echo $title;
            echo $after_title;
        endif;

        // Draw Last Posts
        if ($cat_id == '' || $cat_id[0] == '') {
            switch ($posts_order) {
                case 'latest':
                    $wp_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => $no_of_posts, 'post_status' => 'publish'));
                    break;

                case 'random':
                    $wp_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => $no_of_posts, 'orderby' => 'rand', 'post_status' => 'publish'));
                    break;
            }
        } else {
            switch ($posts_order) {
                case 'latest':
                    $wp_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => $no_of_posts, 'category__in' => $cat_id, 'post_status' => 'publish'));
                    break;

                case 'random':
                    $wp_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => $no_of_posts, 'category__in' => $cat_id, 'orderby' => 'rand', 'post_status' => 'publish'));
                    break;
            }
        }

        if ($wp_query->have_posts()) : ?>
            <ul class="posts-in-images clearfix">

                <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

                    <li rel="tooltip" data-toggle="tooltip" data-placement="top" data-original-title="<?php the_title(); ?>" title="">
                        <a href="<?php the_permalink(); ?>">
                            <div class="thumb-effect">
                                <div class="mask"></div>
                                <?php serpentsoft_grab_post_banner_figure($post->ID, array(), 'small'); ?>
                            </div>
                        </a>
                    </li>

                <?php endwhile; ?>

            </ul>

        <?php endif; ?>

        <?php echo $after_widget;
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['box_title'] = strip_tags($new_instance['box_title']);
        $instance['no_of_posts'] = strip_tags($new_instance['no_of_posts']);

        $instance['cat_id'] = implode(',', $new_instance['cat_id']);

        $instance['order'] = strip_tags($new_instance['order']);
        return $instance;
    }

    function form($instance) {
        $defaults = array('box_title' => __('News in Pictures', 'serpentsoft'), 'cat_id'  => '', 'no_of_posts' => '9', 'order' => 'latest' );
        $instance = wp_parse_args((array) $instance, $defaults);

        $obj_categories = get_categories(array('hide_empty' => 0, 'hierarchical' => 1));
        $categories = array();

        foreach ($obj_categories as $indv_cat) {
            $categories[$indv_cat->cat_ID] = $indv_cat->cat_name;
        } ?>

        <p>
            <label for="<?php echo $this->get_field_id('box_title'); ?>"><?php _e('Title', 'serpentsoft'); ?> :</label>

            <input id="<?php echo $this->get_field_id('box_title'); ?>" name="<?php echo $this->get_field_name('box_title'); ?>" value="<?php echo $instance['box_title']; ?>" class="widefat" type="text" />
        </p>

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

        <p>
            <style>
                .select-newsInPic-cat-width{
                    width: 160px;
                    height: 130px !important;
                }
            </style>
            
            <?php $cat_ids = explode(',', $instance['cat_id']); ?>
            <label for="<?php echo $this->get_field_id('cat_id'); ?>"><?php _e('Category', 'serpentsoft'); ?> :</label>

            <select id="<?php echo $this->get_field_id('cat_id'); ?>[]" name="<?php echo $this->get_field_name('cat_id'); ?>[]" multiple="multiple" class="select-newsInPic-cat-width">
                <?php foreach ($categories as $key => $value) { ?>
                    <option value="<?php echo $key; ?>" <?php if( in_array($key, $cat_ids) ){ echo ' selected="selected"'; } ?>><?php echo $value; ?></option>
                <?php } ?>
            </select>
        </p>
        
        <p>
            <label for="<?php echo $this->get_field_id('order'); ?>"><?php _e('Order', 'serpentsoft'); ?> :</label>

            <select id="<?php echo $this->get_field_id('order'); ?>" name="<?php echo $this->get_field_name('order'); ?>" >
                <option value="latest" <?php if ($instance['order'] == 'latest') echo "selected=\"selected\""; else echo ""; ?>><?php _e('Most Recent', 'serpentsoft') ?></option>
                
                <option value="random" <?php if ($instance['order'] == 'random') echo "selected=\"selected\""; else echo ""; ?>><?php _e('Random', 'serpentsoft') ?></option>
            </select>
        </p>

<?php } } ?>