<?php
/**
 * Deals Widget
 * @package Bloggit WordPress Theme
 * @since 1.0
 * @author WPExplorer : http://www.wpexplorer.com
 * @copyright Copyright (c) 2012, WPExplorer
 * @link http://www.wpexplorer.com
 * @author WPExplorer : http://www.wpexplorer.com
 */
 
class wpex_post_slider_widget extends WP_Widget {
	
    /** constructor */
    function wpex_post_slider_widget() {
        parent::WP_Widget(false, $name = __('Symple - Posts Slider','wpex'), array( 'description' => __( 'Easily add a slider with your custom post type of choice.', 'wpex' ) ) );
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {		
        extract( $args );
        $title = apply_filters('widget_title', $instance['title']);
        $number = apply_filters('widget_number', $instance['number']);
		$order = apply_filters('widget_order', $instance['order']);
		$post_type = apply_filters('widget_post_type', $instance['post_type']); 
		$img_width = apply_filters('widget_width', $instance['img_width']);
		$img_height = apply_filters('widget_img_height', $instance['img_height']);
		$img_hard_crop = apply_filters('widget_img_hard_crop', $instance['img_hard_crop']);
		
              echo $before_widget;
					if ( $title )
                        echo $before_title . $title . $after_title; ?>
							<div class="wpex-widget-slider posts-slider-widget flex-loading flexslider">
                           		<ul class="slides">
									<?php
									// Load necessary slider scripts
									wp_enqueue_script('flexslider', WPEX_JS_DIR .'/flexslider.js', array('jquery'), '2', true);
									wp_enqueue_script('wpex-slider-widget', get_template_directory_uri() .'/js/slider-widget.js', array('jquery','flexslider'), true);
                                    
									// Get posts
									global $post;
									$myposts = get_posts( array(
                                        'post_type' => $post_type,
                                        'numberposts' => $number,
                                        'order' => 'post_date',
                                        'orderby' => $order,
										'no_found_rows' => true
                                    ) );
									
									// Loop through posts
                                    foreach( $myposts as $post ) : setup_postdata($post);
										if( has_post_thumbnail() ) {  ?>
											<li>
												<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="wpex-widget-slider-link">
													<span class="wpex-widget-slider-title"><?php the_title(); ?></span>
													<img src="<?php echo aq_resize( wp_get_attachment_url( get_post_thumbnail_id() ), $img_width, $img_height, $img_hard_crop ); ?>" alt="<?php the_title(); ?>" />
												</a>
											</li>
										<?php }
									endforeach; wp_reset_postdata(); ?>
                            	</ul>
							</div>
              <?php echo $after_widget;
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {				
	$instance = $old_instance;
	$instance['title'] = strip_tags($new_instance['title']);
	$instance['number'] = strip_tags($new_instance['number']);
	$instance['order'] = strip_tags($new_instance['order']);
	$instance['post_type'] = strip_tags($new_instance['post_type']);
	$instance['img_width'] = strip_tags($new_instance['img_width']);
	$instance['img_height'] = strip_tags($new_instance['img_height']);
	$instance['img_hard_crop'] = strip_tags($new_instance['img_hard_crop']);
        return $instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {	
	    $instance = wp_parse_args( (array) $instance, array(
			'title' => __('Recent Posts','wpex'),
			'post_type' => 'post',
			'number' => '4',
			'order' => 'ASC',
			'img_width' => '400',
			'img_height' => '300',
			'img_hard_crop' => 'true'
		));					
        $title = esc_attr( $instance['title'] );
        $number = esc_attr( $instance['number'] );
		$order = esc_attr( $instance['order'] );
		$post_type = esc_attr( $instance['post_type'] );
		$img_width = esc_attr( $instance['img_width'] );
		$img_height = esc_attr( $instance['img_height'] );
		$img_hard_crop = esc_attr( $instance['img_hard_crop'] ); ?>
        
         <p>
          <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'wpex'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title','wpex'); ?>" type="text" value="<?php echo $title; ?>" />
        </p>
        
		<p>
        <label for="<?php echo $this->get_field_id('post_type'); ?>"><?php _e('Post Type?', 'wpex'); ?></label> 
        <br />
        <select class='wpex-select' name="<?php echo $this->get_field_name('post_type'); ?>" id="<?php echo $this->get_field_id('post_type'); ?>">
            <option value="post" <?php if($post_type == 'post') { ?>selected="selected"<?php } ?>><?php _e('Post', 'wpex'); ?></option>
            <?php
            //get post_typeonomies
            $args=array('public' => true,'_builtin' => false, 'exclude_from_search' => false); 
            $output = 'names'; // or objects
            $operator = 'and'; // 'and' or 'or'
            $get_post_types = get_post_types($args,$output,$operator); 
            foreach ($get_post_types as $get_post_type ) {
				if( $get_post_type != 'post' && $get_post_type !== 'faq' ){ ?>
                <option value="<?php echo $get_post_type; ?>" id="<?php $get_post_type; ?>" <?php if($post_type == $get_post_type) { ?>selected="selected"<?php } ?>><?php echo $get_post_type; ?></option>
            <?php } } ?>
        </select>
        </p>
        
        <p>
        <label for="<?php echo $this->get_field_id('order'); ?>"><?php _e('Random or Recent?', 'wpex'); ?></label>
        <br />
        <select class='wpex-select' name="<?php echo $this->get_field_name('order'); ?>" id="<?php echo $this->get_field_id('order'); ?>">
        <option value="ASC" <?php if($order == 'ASC') { ?>selected="selected"<?php } ?>><?php _e('Recent', 'wpex'); ?></option>
        <option value="rand" <?php if($order == 'rand') { ?>selected="selected"<?php } ?>><?php _e('Random', 'wpex'); ?></option>
        </select>
        </p>
        
		<p>
          <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number to Show:', 'wpex'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
        </p>
        
		<p>
          <label for="<?php echo $this->get_field_id('img_width'); ?>"><?php _e('Image Width:', 'wpex'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('img_width'); ?>" name="<?php echo $this->get_field_name('img_width'); ?>" type="text" value="<?php echo $img_width; ?>" />
        </p>
        
		<p>
          <label for="<?php echo $this->get_field_id('img_height'); ?>"><?php _e('Image Height:', 'wpex'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('img_height'); ?>" name="<?php echo $this->get_field_name('img_height'); ?>" type="text" value="<?php echo $img_height; ?>" />
        </p>
        
		<p>
        <label for="<?php echo $this->get_field_id('img_hard_crop'); ?>"><?php _e('Hard Crop Image?', 'wpex'); ?></label>
        <br />
        <select class='wpex-select' name="<?php echo $this->get_field_name('img_hard_crop'); ?>" id="<?php echo $this->get_field_id('img_hard_crop'); ?>">
        <option value="true" <?php if($img_hard_crop == 'true') { ?>selected="selected"<?php } ?>><?php _e('True', 'wpex'); ?></option>
        <option value="false" <?php if($img_hard_crop == 'false') { ?>selected="selected"<?php } ?>><?php _e('False', 'wpex'); ?></option>
        </select>
        </p>

        

        <?php
    }


} // class wpex_post_slider_widget
// register Recent Posts widget
add_action('widgets_init', create_function('', 'return register_widget("wpex_post_slider_widget");')); ?>