<?php
/**
 * Plugin Name: Irontec - Friends Widget
 * Plugin URI: http://www.irontec.com
 * Description: Adds a custom friends widget
 * Version: 0.1
 * Author: Irontec
 * Author URI: http://www.irontec.com
 * License: GNU General Public License 2.0 (GPL) http://www.gnu.org/licenses/gpl.html
 */

/**
 * DEFINIMOS NUESTROS CUSTOM SLUGS
 */
if ( !defined( 'BP_IRON_MEMBERSMODAL_SLUG' ) ) define( 'BP_IRON_MEMBERSMODAL_SLUG', 'members-modal' );

function my_setup_root_iron_membersmodal () {
    bp_core_add_root_component( BP_IRON_MEMBERSMODAL_SLUG );
}

add_action( 'bp_setup_root_components', 'my_setup_root_iron_membersmodal' );

function my_action_iron_membersmodal() {
    global $bp;

    if ( $bp->current_component == BP_IRON_MEMBERSMODAL_SLUG ) {
        do_action( 'my_action_iron_membersmodal' );
        bp_core_load_template( apply_filters( 'bp_core_template_iron_amigos', 'members-modal/index' ) );
    }
}

add_action( 'wp', 'my_action_iron_membersmodal', 1 );

class Irontec_Friends_Widget extends WP_Widget {
    /** constructor */
    function Irontec_Friends_Widget() {
        parent::WP_Widget(false, $name = 'Irontec Friends');
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {
        global $bp;
        extract( $args );
        ?>

        <?php
        // Un miembro privado no ve este widget
        if (xprofile_get_field_data('Privacidad', bp_loggedin_user_id()) == 'Privado') return false;
        ?>

        <?php echo $before_widget; ?>

        <?php
        // change the title
        $title = apply_filters('widget_title', $instance['title']);
        if ( $title ) {
            $before_title = str_replace ('widgettitle','widgettitle friends', $before_title);
            echo $before_title;
            echo '<a href="'. bp_loggedin_user_domain() .'friends" title="'. __('Amigos', 'buddypress') .'">';
            echo __('Amigos', 'buddypress');
            echo '</a>';
            echo $after_title;
        }

        // links content
        $links_content = "";
        if ($instance['links_align'] == "right")
            $links_content .= '<div class="irontec-widget-links-padder" style="text-align: right">';
        else
            $links_content .= '<div class="irontec-widget-links-padder">';

        if ($instance['display_showall'] == 1) {
            $links_content .= '<a href="'. bp_loggedin_user_domain() .'friends" title="'.__('Ver todos','buddypress').'">';
            $links_content .= __('Ver todos','buddypress');
            $links_content .= '</a>'; 
        }
        if ($instance['display_showall'] == 1 && $instance['display_search'] == 1)
            $links_content .= '&nbsp;&middot;&nbsp;';
        if ($instance['display_search'] == 1) {
            $links_content .= '<a id="fancybox-friends" href="'. site_url() .'/members-modal" title="'.__('Buscar usuarios','buddypress').'">';
            $links_content .= __('Buscar usuarios','buddypress');
            $links_content .= '</a>';
        }
        $links_content .= '</div>';

        // main content
        $main_content = "";
        $list = bp_get_friend_ids( $bp->loggedin_user->id );
        $list = explode(",", $list);
        $nitems = sizeof($list);
        $avatar_size = substr($instance['avatar_size'],0,2);
        if (!$avatar_size) $avatar_size = "40";

        $main_content .= '<div class="irontec-widget-padder">';

        if ($list[0]) {
            for ($i=0; $i<$nitems; $i++) {
                $main_content .= '<a href="'. bp_core_get_user_domain( $list[$i] ) .'" title="'. bp_core_get_user_displayname($list[$i]) .'">' . get_avatar( $list[$i], $avatar_size ) . '</a>';

                // Check online icon
                // Checks users last activity and if less than 5 minute have passed, display online icon
                date_default_timezone_set('Europe/Madrid');
                $online_time = get_user_meta(bp_get_activity_user_id(), 'last_activity');
                $online_time2 = strtotime($online_time[0]) + 300;
            
                if (time() <= $online_time2)
                    $main_content .= '<img class="check-online" src="'. get_stylesheet_directory_uri() . '/_inc/images/user_online.png" />';
                /*
                else
                    $main_content .= '<img class="check-online" src="'. get_stylesheet_directory_uri() . '/_inc/images/user_offline.gif" />';
                */

                if ($instance['number']==$i) break;
            }
        }
        else {
            $main_content .= '<span class="nodata" style="font-size: 10px;">';
            $main_content .= __('Actualmente no tienes amigos !','buddypress');
            $main_content .= '</span>';
        }

        $main_content .= '</div>';

        // Add some CSS styling
        // You can move this to your theme's css if you hate inline styling
        /*
        echo "<style>
        .irontec-widget-padder { overflow: hidden; padding: 8px; }
        .irontec-widget-links-padder { overflow: hidden; padding: 4px 0px; }
        </style>";
        */

        // Display the content
        if ($instance['links_valign'] == "top")
            echo $links_content . $main_content;
        else
            echo $main_content . $links_content;
        ?>

        <?php echo $after_widget; ?>

    <?php
    }

    /** @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['avatar_size'] = strip_tags($new_instance['avatar_size']);
        $instance['display_showall'] = intval($new_instance['display_showall']);
        $instance['display_search'] = intval($new_instance['display_search']);
        $instance['links_valign'] = strip_tags($new_instance['links_valign']);
        $instance['links_align'] = strip_tags($new_instance['links_align']);

        return $instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {
        $title = esc_attr($instance['title']);
        $number = esc_attr($instance['number']);
        $avatar_size = esc_attr($instance['avatar_size']);
        $display_showall = esc_attr($instance['display_showall']);
        $display_search = esc_attr($instance['display_search']);
        $links_valign = esc_attr($instance['links_valign']);
        $links_align = esc_attr($instance['links_align']);
        ?>
        <p>
          <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
          <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php if ($title) echo $title; else echo "Your Friends"; ?>" />
        </p>
        <p>
          <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of items:'); ?></label>
          <input style="width:40px;" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php if ($number) echo $number; else echo "10"; ?>" maxlength="3" />
        </p>
        <p>
          <label for="<?php echo $this->get_field_id('avatar_size'); ?>"><?php _e('Avatar Size:'); ?></label>
          <input style="width:40px;" id="<?php echo $this->get_field_id('avatar_size'); ?>" name="<?php echo $this->get_field_name('avatar_size'); ?>" type="text" value="<?php if ($avatar_size) echo $avatar_size; else echo "40"; ?>" maxlength="3" />&nbsp;px
        </p>
        <p>
          <label for="<?php echo $this->get_field_id('links_align'); ?>"><?php _e('Links align:'); ?></label>
          <p>
          Left <input id="<?php echo $this->get_field_id('links_align'); ?>" name="<?php echo $this->get_field_name('links_align'); ?>" type="radio" value="left" <?php if ($links_align== "left") echo "checked"; ?> />&nbsp;
          Right <input id="<?php echo $this->get_field_id('links_align'); ?>" name="<?php echo $this->get_field_name('links_align'); ?>" type="radio" value="right" <?php if ($links_align == "right") echo "checked"; ?> />
          </p>
        </p>
        <p>
          <label for="<?php echo $this->get_field_id('links_valign'); ?>"><?php _e('Links v-align:'); ?></label>
          <p>
          Top <input id="<?php echo $this->get_field_id('links_valign'); ?>" name="<?php echo $this->get_field_name('links_valign'); ?>" type="radio" value="top" <?php if ($links_valign== "top") echo "checked"; ?> />&nbsp;
          Bottom <input id="<?php echo $this->get_field_id('links_valign'); ?>" name="<?php echo $this->get_field_name('links_valign'); ?>" type="radio" value="bottom" <?php if ($links_valign == "bottom") echo "checked"; ?> />
          </p>
        </p>
        <p>
          <label for="<?php echo $this->get_field_id('display_showall'); ?>"><?php _e('Display Show all link ?'); ?></label>
          <input id="<?php echo $this->get_field_id('display_showall'); ?>" name="<?php echo $this->get_field_name('display_showall'); ?>" type="checkbox" value="1" <?php if ($display_showall==1) echo "checked"; ?> />
        </p>
        <p>
          <label for="<?php echo $this->get_field_id('display_search'); ?>"><?php _e('Display Search link ?'); ?></label>
          <input id="<?php echo $this->get_field_id('display_search'); ?>" name="<?php echo $this->get_field_name('display_search'); ?>" type="checkbox" value="1" <?php if ($display_search==1) echo "checked"; ?> />
        </p>
    <?php }
}

add_action('widgets_init', create_function('', 'return register_widget("Irontec_Friends_Widget");'));
?>