<?php
global $bp,$wpdb;

/**
 * Si el usuario que quieres ver es Privado, redirigimos al index
 */

if (xprofile_get_field_data('Privacidad', bp_current_user_id()) == 'Privado') {
    if ( bp_loggedin_user_id() != bp_current_user_id() ) {
        $site_url = site_url();
        wp_redirect ( $site_url );
    }
}

get_header();

locate_template( array( 'leftsidebar.php' ), true );

?>

    <div id="content">
    
        <div id="iron-encabezado-container">
            <?php
            
            if ($bp->current_action == "edit" || $bp->current_action == "change-avatar") {
                ?>
                <h2 class="iron-encabezado-seccion">
                    <?php echo __('Mi perfil','buddypress'); ?>
                </h2>
                <ul class="iron-encabezado-ul">
                    <li class="iron-encabezado-li">
                        <a <?php if ($bp->current_action=="edit") echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain(); ?>profile/edit">
                            <?php _e ('Editar perfil','buddypress'); ?>
                        </a>
                    </li>
                    <li class="iron-encabezado-li">&middot;&nbsp;
                        <a <?php if ($bp->current_action=="change-avatar") echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain(); ?>profile/change-avatar">
                            <?php _e ('Cambiar avatar','buddypress'); ?>
                        </a>
                    </li>
                    <li class="iron-encabezado-li">&middot;&nbsp;
                        <a <?php if ($bp->current_action=="settings") echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain(); ?>settings">
                            <?php _e ('Tu cuenta','buddypress'); ?>
                        </a>
                    </li>
                </ul>
            <?php
            } elseif ($bp->current_component == "messages") {
            ?>
                <h2 class="iron-encabezado-seccion">
                    <?php echo __('Mensajes','buddypress'); ?>
                </h2>
                <ul class="iron-encabezado-ul">
                    <li class="iron-encabezado-li <?php if ($bp->current_action=="inbox") echo ' selected'; ?>">
                        <a <?php if ($bp->current_action == 'inbox') echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain(); ?>messages/inbox">
                            <?php _e ('Recibidos','buddypress'); ?>
                        </a>
                    </li>
                    <li class="iron-encabezado-li <?php if ($bp->current_action=="sentbox") echo ' selected'; ?>">&middot;&nbsp;
                        <a <?php if ($bp->current_action == 'sentbox') echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain(); ?>messages/sentbox">
                            <?php _e ('Enviados','buddypress'); ?>
                        </a>
                    </li>
                    <li class="iron-encabezado-li <?php if ($bp->current_action=="compose") echo ' selected'; ?>">
                        <a id="new_msg_button" <?php if ($bp->current_action == 'compose') echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain(); ?>messages/compose">
                            <?php _e ('Nuevo','buddypress'); ?>
                        </a>
                    </li>
                </ul>
            <?php
            } elseif ($bp->current_component == "friends") {
                
                $nreqs = sizeof(friends_get_friendship_request_user_ids( bp_loggedin_user_id()));
                ?>
                
                <h2 class="iron-encabezado-seccion">
                    <?php echo __('Mis amigos','buddypress'); ?>
                </h2>
                <ul class="iron-encabezado-ul">
                    <li class="iron-encabezado-li">
                        <a <?php if ($bp->current_action == 'my-friends') echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain() . 'friends/' ?>">
                            <?php _e( 'Mis amigos', 'buddypress' ) ?>
                        </a>
                    </li>
                    <li class="iron-encabezado-li">&middot;&nbsp;
                        <a <?php if ($bp->current_action == 'requests') echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain() . 'friends/requests/' ?>">
                            <?php _e( 'Solicitudes', 'buddypress' ) ?> (<?php echo $nreqs; ?>)
                        </a>
                    </li>
                </ul>
            <?php
            } elseif ($bp->current_component == "groups") {
                
                if ($bp->current_action == "my-groups") {
                ?>
                    <h2 class="iron-encabezado-seccion">
                        <?php echo __('Mis grupos','buddypress'); ?>
                    </h2>
                <?php
                } else {
                ?>
                    <h2 class="iron-encabezado-seccion">
                        <?php echo __('Grupos','buddypress'); ?>
                    </h2>
                <?php
                }
                ?>
                <ul class="iron-encabezado-ul">
                    <?php
                    if (xprofile_get_field_data('Privacidad', bp_loggedin_user_id()) != 'Privado') {
                        ?>
                        <li class="iron-encabezado-li">
                            <a class="fancybox-groups" href="<?php echo bp_get_root_domain() . '/grupos/create/' ?>">
                                <?php _e( 'Crear grupo', 'buddypress' ) ?>
                            </a>
                        </li>
                    <?php
                    }
                    ?>
                    <li class="iron-encabezado-li">
                        <?php
                        if (xprofile_get_field_data('Privacidad', bp_loggedin_user_id()) != 'Privado') {
                        ?>
                        &middot;&nbsp;
                        <?php
                        }
                        ?>
                        <a <?php if (!$bp->current_action) echo 'class="selected"'; ?> href="<?php bp_root_domain() ?>/grupos">
                            <?php printf( __( 'Todos', 'buddypress' ) ) ?>
                        </a>
                    </li>
                    <li class="iron-encabezado-li">&middot;&nbsp;
                        <a <?php if ($bp->current_action == 'my-groups') echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain() . 'groups' ?>">
                            <?php printf( __( 'Mis grupos', 'buddypress' ) ) ?>
                        </a>
                    </li>
                    <li class="iron-encabezado-li">&middot;&nbsp;
                        <a <?php if ($bp->current_action == 'invites') echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain() . 'groups/invites/' ?>">
                            <?php _e( 'Invitaciones', 'buddypress' ) ?>
                        </a>
                    </li>
                </ul>
                <?php
                } else {
                    if (bp_loggedin_user_id() == bp_displayed_user_id()) :
                    ?>
                    <h2 class="iron-encabezado-seccion">
                        <?php echo __('Mi perfil','buddypress'); ?>
                    </h2>
                    <ul class="iron-encabezado-ul">
                        <li class="iron-encabezado-li">
                            <a <?php if ($bp->current_action=="edit") echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain(); ?>profile/edit">
                                <?php _e ('Editar perfil','buddypress'); ?>
                            </a>
                        </li>
                        <li class="iron-encabezado-li">&middot;&nbsp;
                            <a <?php if ($bp->current_action=="change-avatar") echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain(); ?>profile/change-avatar">
                                <?php _e ('Cambiar avatar','buddypress'); ?>
                            </a>
                        </li>
                        <li class="iron-encabezado-li">&middot;&nbsp;
                            <a <?php if ($bp->current_action=="settings") echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain(); ?>settings">
                                <?php _e ('Tu cuenta','buddypress'); ?>
                            </a>
                        </li>
                    </ul>
                    <?php
                    else :
                    ?>
                    <h2 class="iron-encabezado-seccion">
                        <?php echo __('Perfil de:','buddypress'); ?>
                    </h2>
                    <?php
                    endif;
                }
                ?>
        </div>
        
        <div class="padder">
        
        <div id="content-top"></div>
        
            <div id="content-middle">
            
            <?php
                do_action( 'bp_before_member_home_content' );
                
                if (
                        ($bp->current_component == "profile" &&
                        ($bp->current_action == "edit" || $bp->current_action == "change-avatar")) ||
                        ($bp->current_component == "friends") ||
                        ($bp->current_component == "messages") ||
                        ($bp->current_component == "groups")
                    ) {
                ?>
                
                    <div id="item-body">
                        <?php
                        do_action( 'bp_before_member_body' );
                        
                        if ( bp_is_user_activity() || !bp_current_component() ) :
                            locate_template( array( 'members/single/activity.php' ), true );
                            
                        elseif ( bp_is_user_friends() ) :
                            locate_template( array( 'members/single/friends.php' ), true );
                            
                        elseif ( bp_is_user_groups() ) :
                            locate_template( array( 'members/single/groups.php' ), true );
                            
                        elseif ( bp_is_user_messages() ) :
                            
                            locate_template( array( 'members/single/messages.php' ), true );
                            
                        elseif ( bp_is_user_profile() ) :
                            locate_template( array( 'members/single/profile.php' ), true );
                            
                        else :
                            
                            /**
                             * If nothing sticks, display error page
                             */
                            locate_template( array( '404.php' ), true );
                            
                        endif;
                        
                        do_action( 'bp_after_member_body' );
                        ?>
                    </div><!-- #item-body -->
                    
            <?php
                } elseif ($bp->current_component == 'activity' and $bp->current_action == 'mentions' ) {
            ?>
            
                <div id="content-middle-add">
                
                    <div id="item-header">
                        <?php
                        locate_template( array( 'members/single/member-header.php' ), true );
                        ?>
                    </div><!-- #item-header -->
                    
                    <?php
                        if ($bp->current_component == "activity") {
                            if (bp_is_friend( bp_displayed_user_id() ) == 'is_friend' || bp_displayed_user_id() == bp_loggedin_user_id()) {
                                locate_template( array( 'members/single/mentions.php' ), true );
                            } else {
                                
                                _e('Debes ser amigo de &eacute;ste usuario para ver toda su actividad.','buddypress');
                                _e('Aqu&iacute; te mostramos sus &uacute;ltimas actividades p&uacute;blicas.','buddypress');
                                ?>
                            <div class="clear">&nbsp;</div>
                        
                        <?php
                            }
                        }
                        ?>
                </div><!-- content-middle-add -->
                
            <?php
                } else {
            ?>
            
                <div id="content-middle-add">
                
                    <div id="item-header">
                        <?php
                        locate_template( array( 'members/single/member-header.php' ), true );
                        ?>
                    </div><!-- #item-header -->
                    
                    <?php
                        if ($bp->current_component == "activity") {
                            if (bp_is_friend( bp_displayed_user_id() ) == 'is_friend' || bp_displayed_user_id() == bp_loggedin_user_id()) {
                                locate_template( array( 'members/single/activity.php' ), true );
                            } else {
                                _e('Debes ser amigo de &eacute;ste usuario para ver toda su actividad.','buddypress');
                                _e('Aqu&iacute; te mostramos sus &uacute;ltimas actividades p&uacute;blicas.','buddypress');
                                ?>
                            <div class="clear">&nbsp;</div>
                            <?php
                            /**
                             * Mostramos la actividad para todos de este usuario
                             */
                            ?>
                            <div id="item-body">
                            
                                <?php
                                do_action( 'bp_before_member_body' );
                                ?>
                                <div class="activity-todos">
                                    <ul id="activity-stream" class="activity-list item-list">
                                    <?php
                                    
                                    $table  = $wpdb->prefix . 'bp_activity';
                                    $table2 = $wpdb->prefix . 'bp_activity_meta';
                                    $queryFull  = "
                                                SELECT
                                                    act.id,
                                                    act.user_id,
                                                    act.content,
                                                    act.date_recorded
                                                FROM ".$table." act
                                                
                                                INNER JOIN ".$table2." met ON
                                                        act.id = met.activity_id
                                                    AND
                                                        met.meta_key = 'privacity'
                                                    AND
                                                        met.meta_value = 'publico'
                                                WHERE
                                                    user_id = ". bp_current_user_id() ."
                                                AND
                                                    act.type='activity_update'
                                                ORDER BY date_recorded DESC
                                                LIMIT 10";
                                    
                                    $res = $wpdb->get_results($wpdb->prepare($queryFull));
                                    
                                    foreach ($res as $act) {
                                        
                                        $actclass = '';
                                        $userdata = get_userdata( $act->user_id );
                                        
                                        /**
                                         * Obtain post mood
                                         */
                                        $queryActivityMeta = "SELECT * FROM ".$table2." WHERE activity_id = ". $act->id;
                                        $results           = $wpdb->get_results($wpdb->prepare($queryActivityMeta));
                                        
                                        foreach ($results as $activity_meta) {
                                            if ($activity_meta->meta_key == "current_mood") {
                                                $post_mood=$activity_meta->meta_value;
                                            }
                                        }
                                        
                                        if ($post_mood == 'familiares') $actclass = 'ac01';
                                        if ($post_mood == 'sintomaticos') $actclass = 'ac03';
                                        if ($post_mood == 'reflexivos') $actclass = 'ac02';
                                        if ($post_mood == 'transgresores') $actclass = 'ac04';
                                        // End obtain post mood
                                        ?>
                                        <li id="activity-<?php echo $act->id; ?>">
                                        
                                            <div class="activity-avatar">
                                                <a href="<?php echo $userdata->user_url; ?>" title="<?php echo $userdata->user_nicename; ?>">
                                                    <?php echo bp_core_fetch_avatar( array( 'item_id' => $userdata->ID, 'type' => 'full', 'width' => 24, 'height' => 24 ) ); ?>
                                                </a>
                                            </div>
                                            
                                            <div class="activity-content">
                                            
                                                <div class="activity-header">
                                                    <a href="<?php echo home_url('/miembros/'.$userdata->nickname); ?>" title="<?php echo xprofile_get_field_data('Nombre', $userdata->ID); ?>"><?php echo xprofile_get_field_data('Nombre', $userdata->ID); ?></a><span id="mentions">@<?php echo $userdata->nickname; ?></span>
                                                    <span style="font-size: 9px;">&nbsp;&nbsp;<?php echo bp_core_time_since ($act->date_recorded); ?></span>
                                                </div>
                                                
                                                <div class="activity-inner <?php echo $actclass; ?>">
                                                <?php echo contenidoMuroCorto($act->content); ?>
                                                </div>
                                            
                                            </div>
                                        
                                        </li>
                                    <?php
                                    }
                                    ?>
                                    </ul>
                                </div> <!-- .activity-todos -->
                                <?php
                                do_action( 'bp_after_member_body' );
                                ?>
                            </div> <!-- #item-body -->
                            
                        <?php
                            }
                        }
                        ?>
                </div><!-- content-middle-add -->
                
            <?php
                }
                
                do_action( 'bp_after_member_home_content' );
            ?>
            </div> <!-- content-middle -->
            
        <div id="content-bottom"></div>
        
        </div><!-- .padder -->
        
    </div><!-- #content -->
    
<?php
locate_template( array( 'sidebar.php' ), true );

get_footer();
?>