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

function serpentsoft_modern_widget_init() {
    register_widget('serpentsoft_modern_widget');
}

class serpentsoft_modern_widget extends WP_Widget {

    function serpentsoft_modern_widget() {
        $widget_ops = array('classname' => 'w-posts', 'description' => '');
        $control_ops = array('width' => 250, 'height' => 350, 'id_base' => 'id-widget-modern-posts');
        $this->WP_Widget('id-widget-modern-posts', theme_name . ' - Slide Posts', $widget_ops, $control_ops);
    }

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

        extract($args);
        $title = apply_filters('widget_title', $instance['title']);

        $title = $instance['title'];
        $cat_id = $instance['cat_id'];
        $posts_order = $instance['order'];
        $posts_num = $instance['postsnum'];
        $posts_per_page = $instance['postsperpage'];

        echo $before_widget;
        if ($title) :
            echo $before_title;
            echo $title;
            echo $after_title;
        endif;
        
        //$wp_query = '';
        $err_msg = 'Undefined Error';
        // All Categories
        //wp_reset_query();

        if ($cat_id == '0') {
            switch ($posts_order) {
                case 'dayposts': {
                        $today = getdate();
                        $wp_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => $posts_num, 'post_status' => 'publish', 'meta_key' => 'serpentsoft_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'year' => $today["year"], 'monthnum' => $today["mon"], 'day' => $today["mday"]));
                        if(!$wp_query->have_posts()){
                            $err_msg = 'There is no posts published today (' . $today["mday"] . '/' . $today["mon"] . '/' . $today["year"]  . ').';
                            wp_reset_query();
                        }
                    }
                    break;

                case 'mostviews': {
                        $wp_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => $posts_num, 'post_status' => 'publish', 'meta_key' => 'serpentsoft_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC'));
                    }
                    break;

                case 'latest': {
                        $wp_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => $posts_num, 'post_status' => 'publish'));
                    }
                    break;

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

        // Category ID
        else {
            switch ($posts_order) {
                case 'dayposts': {
                        $today = getdate();
                        $wp_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => $posts_num, 'post_status' => 'publish', 'cat' => $cat_id, 'meta_key' => 'serpentsoft_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'year' => $today["year"], 'monthnum' => $today["mon"], 'day' => $today["mday"]));
                        if(!$wp_query->have_posts()){
                            $err_msg = 'There is no posts published today (' . $today["mday"] . '/' . $today["mon"] . '/' . $today["year"]  . ').';
                            wp_reset_query();
                        }
                    }
                    break;

                case 'mostviews': {
                        $wp_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => $posts_num, 'post_status' => 'publish', 'cat' => $cat_id, 'meta_key' => 'serpentsoft_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC'));
                    }
                    break;

                case 'latest':
                    $wp_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => $posts_num, 'post_status' => 'publish', 'cat' => $cat_id));
                    //$wp_query = 'Latest News';
                    break;

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

        if($wp_query->have_posts()) : ?>

            <div class="posts-in-widget">
                <div class="row-fluid modern-items-list">

                    <div id="<?php echo $this->get_field_id('title'); ?>" class="flexslider">

                        <ul class="slides">

                            <?php $page_slide = 1; ?>

                            <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
                                <?php if ($page_slide == 1): ?>
                                    <li><ul class="items left clearfix">
                                        <?php endif; ?>                

                                        <li>
                                            <article class="fold-item">
                                                <div class="clearfix">
                                                    <?php serpentsoft_grab_post_banner_figure($post->ID, array('class' => 'post-img')); ?>

                                                    <div class="post-sitemap">
                                                        <span><i class="bo-icon-home bo-icon-white"></i><?php echo get_the_category_list(', ', 'multiple', $post->ID); ?></span>
                                                    </div>

                                                    <div class="description visible-part">
                                                        <h5 class="title"><?php the_title(); ?></h5>

                                                        <div class="info">
                                                            <span><i class="bo-icon-time bo-icon-white"></i><?php the_time(serpentsoft_getDateTimeFormat('dt')); ?></span>
                                                            <span><i class="bo-icon-comment bo-icon-white"></i><?php comments_number('0', '1', '%'); ?></span>
                                                            <span><i class="bo-icon-eye-open bo-icon-white"></i><?php echo serpentsoft_get_post_views($post->ID); ?></span>
                                                        </div>

                                                        <p class="text"><?php serpentsoft_cats_GetTheExcerpt(); ?></p>
                                                    </div>

                                                    <a href="<?php the_permalink(); ?>" class="more" title="<?php the_title(); ?>"></a>
                                                </div>
                                            </article>    
                                        </li>

                                        <?php if ($page_slide / $posts_per_page == 1): ?>
                                        </ul></li>
                                    <?php $page_slide = 0;
                                endif;
                                $page_slide++; ?>

                        <?php endwhile; ?>

                        </ul> <!-- End of UL Slides -->
                    </div> <!-- End of Flexslider -->

                </div> <!-- End of row-fluid modern-items-list -->
            </div> <!-- End of posts-in-widget -->

            <script type="text/javascript">
                //<![CDATA[
                jQuery('#<?php echo $this->get_field_id('title'); ?>').flexslider({
                    slideshow: false,
                    animation: "slide",              //String: Select your animation type, "fade" or "slide"
                    easing: "swing",                 //{NEW} String: Determines the easing method used in $jqSlider transitions. $jqSlider easing plugin is supported!
                    direction: "horizontal"            // String: Select the sliding direction, "horizontal" or "vertical"
                });
                //]]>
            </script>

            <?php else: echo $err_msg; endif;

        echo $after_widget;
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['postsnum'] = strip_tags($new_instance['postsnum']);
        $instance['postsperpage'] = strip_tags($new_instance['postsperpage']);

        $instance['cat_id'] = $new_instance['cat_id'];

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

    function form($instance) {
        $defaults = array('title' => __('Posts Of The Day', 'serpentsoft'), 'cat_id' => '0', 'postsnum' => '6', 'postsperpage' => '2', 'order' => 'dayposts');
        $instance = wp_parse_args((array) $instance, $defaults);

//        $obj_categories = get_categories(array('hide_empty' => 0, 'hierarchical' => 1));
//        $categories = array('0' => 'All Categories');
//
//        foreach ($obj_categories as $indv_cat) {
//            $categories[$indv_cat->cat_ID] = $indv_cat->cat_name;
//        }
        ?>

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

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

        <p>
            <label for="<?php echo $this->get_field_id('cat_id'); ?>"><?php _e('Category', 'serpentsoft'); ?> :</label>
            
            <style>
                .select-cat-width{
                    width: 160px;
                }
            </style>
            <?php $args_dropdown_categories = array(
                'show_option_all' => 'All Categories',
                'show_option_none' => '',
                'orderby' => 'ID',
                'order' => 'ASC',
                'show_count' => 0,
                'hide_empty' => 0,
                'child_of' => 0,
                'exclude' => '',
                'echo' => 1,
                'selected' => $instance['cat_id'],
                'hierarchical' => 1,
                'name' => $this->get_field_name('cat_id'),
                'id' => $this->get_field_id('cat_id'),
                'class' => 'select select-cat-width postform'
            );
            $select_categories = wp_dropdown_categories($args_dropdown_categories); ?>
            
        </p>

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

            <select id="<?php echo $this->get_field_id('order'); ?>" name="<?php echo $this->get_field_name('order'); ?>">
                <option value="dayposts" <?php if($instance['order'] == 'dayposts') { echo ' selected="selected"'; } ?>><?php _e('Posts Of The Day', 'serpentsoft'); ?></option>
                <option value="mostviews" <?php if($instance['order'] == 'mostviews') { echo ' selected="selected"'; } ?>><?php _e('Most Views', 'serpentsoft'); ?></option>
                <option value="latest" <?php if($instance['order'] == 'latest') { echo ' selected="selected"'; } ?>><?php _e('Recent', 'serpentsoft'); ?></option>
                <option value="random" <?php if($instance['order'] == 'random') { echo ' selected="selected"'; } ?>><?php _e('Random', 'serpentsoft'); ?></option>
            </select>
        </p>

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

        <p>
            <label for="<?php echo $this->get_field_id('postsperpage'); ?>"><?php _e('Posts for each slide', 'serpentsoft'); ?> :</label>
            <select id="<?php echo $this->get_field_id('postsperpage'); ?>" name="<?php echo $this->get_field_name('postsperpage'); ?>">
        <?php for ($i = 1; $i <= 4; $i++) { ?>
                    <option value="<?php echo $i; ?>" <?php if($instance['postsperpage'] == $i) { echo ' selected="selected"'; } else { echo ''; } ?>><?php echo $i; ?></option>
        <?php } ?>
            </select>
        </p>
<?php } } ?>