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

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

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

     protected $_widgetAdminTitle =       "Twitter - Custom Widget";
     protected $_widgetAdminDescription = "Display your latest Tweets";
     protected $_widgetWrapperClasses =   "";
     protected $_widgetFormSize =         frameworkWidget::WIDGET_FORM_SIZE_THIN;



     protected $options = array(
        array(  'id' => 'title',
                'description' => 'Title',
                'type' => 'text',
                'default' => 'LATEST TWEETS'),
                

        array(  'id' => 'username',
                'description' => 'Your twitter username',
                'type' => 'text',
                'default' => ''),
                

        array(  'id' => 'number',
                'description' => 'Number of tweets to show',
                'type' => 'text',
                'default' => 5),

     );

    /**
     * Printing widget, called by wordpress
     */
    function _widget($args, $instance) {

        extract( $args );

        echo $before_widget;

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

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

        $twitterFeed = new ffTwitterFeeder( $instance['username'], $instance['number'] );

        echo '<div class="tweets">';
        echo '<ul class="tweet_list">';
        foreach ( $twitterFeed->getTwitterFeed() as $key=>$feed) {
            echo '<li>';
            echo '<span class="tweet_avatar" style="visibility: visible; background-position: 0px 3px;"></span>';

            echo '<span class="tweet_time">';
            echo '<a href="http://twitter.com/' . $feed->user->screen_name . '/status/' . $feed->id_str . '" title="view tweet on twitter">';
            echo human_time_diff( strtotime( $feed->created_at ) );
            echo ' ago</a>';
            echo '</span>';

            echo '<span class="tweet_join"></span>';

            echo '<span class="tweet_text">';
            echo preg_replace('!((?:www|http://|https://)[^ ]+)!', '<a href="\1">\1</a>', $feed->text);
            echo '</span>';

            echo '</li>';
        }
        echo '</ul>';
        echo '</div>';

        echo $after_widget;        
    }
    
    protected function doAdditionalUpdates( $new_instance ){
        $twitterFeed = new ffTwitterFeeder( $new_instance['username'], $new_instance['number'] );
        $twitterFeed->actualizeTwitterFeed();
    }
}
?>
