<?php
do_action( 'bp_before_profile_loop_content' );

if ( function_exists('xprofile_get_profile') ) :
    
    global $bp; $user_settings = get_user_meta( $bp->displayed_user->id, 'bp_profile_privacy' );
    
    if ( bp_has_profile() ) :
    
        while ( bp_profile_groups() ) : bp_the_profile_group();
        
            if ( bp_profile_group_has_fields() ) :
            
                do_action( 'bp_before_profile_field_content' )
                ?>
                
                <div class="bp-widget <?php bp_the_profile_group_slug() ?>">
                    <?php
                    if ( 1 != bp_get_the_profile_group_id() ) {
                    ?>
                        <h4><?php bp_the_profile_group_name() ?></h4>
                    <?php
                    }
                    ?>
                    
                    <div id="iron-profile-avatar">
                        <?php
                        echo bp_core_fetch_avatar(
                                                    array(
                                                        'item_id' => $user_id,
                                                        'type'    => 'thumb',
                                                        'width'   => 80,
                                                        'height'  => 80
                                                        )
                                                );
                        ?>
                    </div>
                    
                    <div id="iron-profile-fields">
                        <?php
                        $x=0;
                        
                        while ( bp_profile_fields() ) : bp_the_profile_field();
                        
                            if ( bp_field_has_data() ) :
                                
                                $field_id  = bp_get_the_profile_field_id();
                                $privacity = $user_settings[$field_id];
                                $isfriend  = friends_check_friendship( $bp->displayed_user->id, $bp->loggedin_user->id);
                                
                                /**
                                 * ESTO COMPRUEBA SI ES AMIGO DE AMIGOS
                                 */
                                $friends         = bp_get_friend_ids( $bp->displayed_user->id );
                                $friend_list     = explode(",", $friends);
                                $isfriend_friend = 0;
                                
                                foreach ($friend_list as $friend_friend) {
                                    
                                    $friend2      = bp_get_friend_ids( $friend_friend[0] );
                                    $friend2_list = explode(",", $friend2);
                                    
                                    if (in_array($bp->loggedin_user->id, $friend2_list)) {
                                        $isfriend_friend = 1;
                                    }
                                    
                                }
                                
                                if ($x == 0) {
                                    echo '<p class="primary">';
                                    echo strip_tags(bp_get_the_profile_field_value());
                                    echo '</p>';
                                } else {
                                    /**
                                     * MOSTRAMOS EL CAMPO EN FUNCION DE LA PROFILE PRIVACY
                                     * 0 = EVERYONE, 1 = MY FRIENDS, 2 = ONLY ME, 3 = FRIENDS AND THEIR FRIENDS
                                     */
                                    if ($privacity == 0) {
                                        echo '<p class="secondary">';
                                        echo strip_tags(bp_get_the_profile_field_value());
                                        echo '</p>';
                                    } elseif ($privacity == 1 && ( friends_check_friendship( $bp->displayed_user->id, $bp->loggedin_user->id ) || bp_is_my_profile() )) {
                                        echo '<p class="secondary">';
                                        echo strip_tags(bp_get_the_profile_field_value());
                                        echo '</p>';
                                    } elseif ($privacity == 2 && bp_is_my_profile()) {
                                        echo '<p class="secondary">';
                                        echo strip_tags(bp_get_the_profile_field_value());
                                        echo '</p>';
                                    } elseif ($privacity == 3 && ( $isfriend == 1 || $isfriend_friend == 1 || bp_is_my_profile() || in_array($bp->loggedin_user->id, $friend_list))) {
                                        echo '<p class="secondary">';
                                        echo strip_tags(bp_get_the_profile_field_value());
                                        echo '</p>';
                                    }
                                }
                                $x++;
                                    
                            endif;
                            
                            do_action( 'bp_profile_field_item' );
                            
                        endwhile;
                        ?>
                    </div>
                    
                    <div id="iron-profile-map">
                    <?php //echo member_profile_locationMap(); ?>
                    </div>
                </div>
                
                <div id="item-buttons">
                    <?php
                    do_action( 'bp_member_header_actions' );
                    ?>
                </div><!-- #item-buttons -->
                
                <div style="clear: both;"></div>
                
                <div id="iron-profile-extensions">
                    <?php
                    echo "<h3>Amigos de ". bp_get_displayed_user_fullname() ."</h3>";
                    /**
                     * MOSTRAR LOS AMIGOS DEL USUARIO
                     */
                    $list = bp_get_friend_ids( $bp->displayed_user->id );
                    $list = explode(",", $list);
                    
                    if ($list[0]) {
                        
                        echo '<p class="iron-widget-p-2">';
                        
                        for ($i=0; $i<sizeof($list); $i++) {
                            echo '<a href="'. bp_core_get_user_domain( $list[$i] ) .'">' . get_avatar( $list[$i], $size = '32' ) . '</a>';
                        }
                        
                        echo '</p>';
                    } else {
                        
                        echo '<p class="iron-widget-p">Actualmente no tienes ning&uacute;n amigo.</p>';
                    }
                    ?>
                </div>
                
                <?php
                do_action( 'bp_after_profile_field_content' );
                
            endif;
        
        endwhile;
        
        do_action( 'bp_profile_field_buttons' );
        
    endif;
    
else :

/**
 * Just load the standard WP profile information, if BP extended profiles are not loaded.
 */
bp_core_get_wp_profile();

endif;

do_action( 'bp_after_profile_loop_content' );
?>