<?php
/**
 *  twitter
 * 
 *  @author freshface
 */

// private register this widget for framework 
fEnv::addWidget('ffPopularPosts');

class ffPopularPosts extends frameworkWidget {
    /**
     *  Defining the widget options
     */

     protected $_widgetAdminTitle =       "Popular Posts - Custom Widget";
     protected $_widgetAdminDescription = "Display your popular posts";
     protected $_widgetWrapperClasses =   "";
     protected $_widgetFormSize =         frameworkWidget::WIDGET_FORM_SIZE_THIN;



     protected $options = array(
        array(  'id' => 'popular_title',
                'description' => 'Widget Title',
                'type' => 'text',  // [[ text, check, select ]]
                'default' => 'Popular'),

        array(  'id' => 'popular_categories',
                'description' => 'Category',
                'type' => 'select-wp-category',
                'include-values' => array(
                                array( 'name' => 'All top catagories', 'value' => '0' ),
                            ),
                'default' => 0),
         
        array(  'id' => 'popular_num_of_posts',
                'description' => 'Number of popular posts to show',
                'type' => 'text',  // [[ text, check, select ]]
                'default' => '5'),

     );

     private function _get_popular_posts( $instance ) {
     	$args = array('orderby' => 'comment_count', 'order'=>'desc', 'numberposts'=> $instance['popular_num_of_posts'], 'post_type'=>array('post','portfolio') );
     	if( !empty( $instance['popular_categories'] ) ) $args['category'] = $instance['popular_categories'];
     	$posts = get_posts( $args );
     	return $posts;
     }
     
    /**
     * Printing widget, called by wordpress
     */
    function _widget($args, $instance) {
        extract( $args );

        echo $before_widget;

        if( !empty( $instance['popular_title'] ) ) {
            $instance['popular_title'] = apply_filters('widget_title', $instance['popular_title'] );
        }

  		echo $before_title . $instance['popular_title'] . $after_title;

       $popularPosts = $this->_get_popular_posts( $instance );
       echo '<ul id="rp_posts_popular">';
		foreach( $popularPosts as $onePost ) {
			$gallery = ffGalleryCollection::getGallery( $onePost->ID );
			$featuredImage = $gallery->getFeaturedImage();
			
			echo '<li>';
				if( null == $featuredImage ) {
					$image = get_template_directory_uri().'/gfx/rp_thumb_blank.png';
				} else {
					$image = $featuredImage->image->resize(60,60,true);
				}
				
				echo '<a href="'.get_permalink($onePost->ID).'">'; 
					echo '<img class="rp_thumb" src="'.$image.'" alt="'.$onePost->post_title.'" />';   
				echo '</a>'; 
				
				echo '<a href="'.get_permalink($onePost->ID).'" class="rp_title">'.$onePost->post_title.'</a>';
				echo '<br />'; 
					echo '<span class="rp_date">';  // conopts
						echo date(get_option('lcp_translate_date'),strtotime($onePost->post_date)); 
					echo '</span>'; 
					echo '<div class="clear"></div>';
			echo '</li>';
		}
		echo '</ul>';
        
		echo $after_widget;        
    }
}
?>
