<?php

global $bp;

do_action( 'bp_before_member_header' );

$class = calculate_moodbar( bp_displayed_user_id() );
?>

<div id="item-header-avatar">
    
    <div class="item-avatar <?php echo $class; ?>">
        <a href="<?php bp_user_link() ?>">
            <?php bp_member_avatar( 'type=full&width=150&height=150' ); ?>
        </a>
        
        <div id="item-buttons">
            <?php
            /**
             * Friendship button
             */
            bp_add_friend_button($bp->displayed_user->id);
            
            /**
             *  Private Message button
             *  Only if is your friend
             */
            $checkfriends = friends_check_friendship_status(bp_loggedin_user_id(), bp_current_user_id());
            if ($checkfriends == 'is_friend' or current_user_can( 'manage_options' )) {
                
                bp_send_private_message_button();
            }
            ?>
        </div><!-- #item-buttons -->
        
    </div>
</div><!-- #item-header-avatar -->

<div id="item-header-content">
    
    <h2 class="fn">
        <a href="<?php bp_displayed_user_link() ?>">
            <?php bp_displayed_user_fullname() ?>
        </a>
        <span class="highlight">@<?php bp_displayed_user_username() ?></span>
    </h2>
    
    <span class="activity">
        <?php bp_last_activity( bp_displayed_user_id() ); ?>
    </span>
    
    <?php
        do_action( 'bp_before_member_header_meta' )
    ?>
    
    <div id="item-meta" style="#float:right; margin-top: 4px;">
        
        <?php
        
        if ( function_exists('xprofile_get_profile') ) :
        
            $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() ) :
                    ?>
            
            <div id="iron-profile-fields" style="clear: both; margin-top: 5px;">
            
                <table cellspacing="1" cellpadding="2">
                    <?php
                    
                    $x = 0;
                    
                    while ( bp_profile_fields() ) : bp_the_profile_field();
                        
                        if (bp_get_the_profile_field_name()=="Mi vista") continue;
                        
                        if (bp_get_the_profile_field_name()=="EstadoAnimo") continue;
                        
                        if (bp_get_the_profile_field_name()=="Privacidad") continue;
                        
                        if (bp_get_the_profile_field_name()=="auto_estado") continue;
                        
                        if ( get_current_user_id() == $bp->displayed_user->id) {
                            /**
                             * Ocultamos la Categoría a todos menos al usuario actual
                             */
                            if (bp_get_the_profile_field_name()=="Categoria") continue;
                        } else {
                            
                            if (bp_get_the_profile_field_name()=="Categoria") {
                                
                                $state_now = '';
                                
                                switch (trim(strip_tags(bp_get_the_profile_field_value()))) {
                                    case 'sintomaticos':
                                        $state_now = __('Necesito Apoyo', 'buddypress');
                                    break;
                                    case 'familiares':
                                        $state_now = __('Estoy contigo', 'buddypress');
                                    break;
                                    case 'transgresores':
                                        $state_now = __('Sigo Adelante', 'buddypress');
                                    break;
                                    case 'reflexivos':
                                        $state_now = __('Busco Respuestas', 'buddypress');
                                    break;
                                }
                                
                                echo "<tr>";
                                echo "<td style='width: 150px;'><b>". __('Estado de Animo', 'buddypress') ."</b></td>";
                                echo "<td>". $state_now . "</td>";
                                echo "</tr>";
                                
                                continue;
                            }
                        }
                        
                        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 "<tr>";
                                    echo "<td style=\"width:100px;\"><b>".__(bp_get_the_profile_field_name(), 'buddypress') ."</b></td>";
                                    echo "<td>". strip_tags(bp_get_the_profile_field_value()). "</td>";
                                    echo "</tr>";
                                    
                                } else {
                                    /**
                                     * MOSTRAMOS EL CAMPO EN FUNCION DE LA PROFILE PRIVACY
                                     * 0 = EVERYONE, 1 = MY FRIENDS, 2 = ONLY ME, 3 = FRIENDS AND THEIR FRIENDS
                                     * 0 = EVERYONE
                                     */
                                    if ($privacity == 0 and bp_get_the_profile_field_name() != 'Quién puede ver "Poblacion"?' and bp_get_the_profile_field_name() != 'Quién puede ver "Patologia"?' ) {
                                        
                                        if (bp_get_the_profile_field_name() != 'Apellidos') {
                                            echo "<tr>";
                                            echo "<td><b>". __( bp_get_the_profile_field_name(), 'buddypress') ."</b></td>";
                                            echo "<td>";
                                            
                                            if ("Otra patología" == eregi_replace("[\n|\r|\n\r]", '', strip_tags(bp_get_the_profile_field_value())) or "Sin Especificar" == strip_tags(bp_get_the_profile_field_value())) {
                                                
                                                if ("Otra patología" == eregi_replace("[\n|\r|\n\r]", '', strip_tags(bp_get_the_profile_field_value()))) {
                                                    
                                                    echo _e('Otra patolog&iacute;a','buddypress');
                                                    
                                                } else {
                                                    
                                                    echo _e('Sin Especificar','buddypress');
                                                    
                                                }
                                                
                                            } else {
                                                
                                                if ( bp_get_the_profile_field_name() == 'Patologia') {
                                                    
                                                      echo utf8_decode(strip_tags(bp_get_the_profile_field_value()));
                                                      
                                                } elseif (bp_get_the_profile_field_name() == "Pais") {
                                                    
                                                    echo validCodeCountry(utf8_decode(strip_tags(trim(bp_get_the_profile_field_value()))));
                                                } else {
                                                      echo strip_tags(bp_get_the_profile_field_value());
                                                      
                                                }
                                            }
                                            echo "</td>";
                                            //echo "<td>". strip_tags(utf8_decode(bp_get_the_profile_field_value())). "</td>";
                                            echo "</tr>";
                                        }
                                        
                                    }
                                    // 1 = MY FRIENDS
                                    elseif ($privacity == 1 && ( friends_check_friendship( $bp->displayed_user->id, $bp->loggedin_user->id ) || bp_is_my_profile() ) and bp_get_the_profile_field_name() != 'Quién puede ver "Poblacion"?' and bp_get_the_profile_field_name() != 'Quién puede ver "Patologia"?') {
                                        
                                        echo "<tr>";
                                        echo "<td><b>". bp_get_the_profile_field_name()."</b></td>";
                                         "<td>". strip_tags(bp_get_the_profile_field_value()). "</td>";
                                        echo "</tr>";
                                        
                                    }
                                    // 2 = ONLY ME
                                    elseif ($privacity == 2 && bp_is_my_profile() and bp_get_the_profile_field_name() != 'Quién puede ver "Poblacion"?' and bp_get_the_profile_field_name() != 'Quién puede ver "Patologia"?') {
                                        
                                        echo "<tr>";
                                        echo "<td><b>". bp_get_the_profile_field_name()."</b></td>";
                                        echo "<td>". strip_tags(bp_get_the_profile_field_value()). "</td>";
                                        echo "</tr>";
                                        
                                    }
                                    // 3 = FRIENDS AND THEIR FRIENDS
                                    elseif ($privacity == 3 && ( $isfriend == 1 || $isfriend_friend == 1 || bp_is_my_profile() || in_array($bp->loggedin_user->id, $friend_list)) and bp_get_the_profile_field_name() != 'Quién puede ver "Poblacion"?' and bp_get_the_profile_field_name() != 'Quién puede ver "Patologia"?' ) {
                                        
                                        echo "<tr>";
                                        echo "<td><b>". bp_get_the_profile_field_name()."</b></td>";
                                        echo "<td>". strip_tags(bp_get_the_profile_field_value()). "</td>";
                                        echo "</tr>";
                                        
                                    }
                                }
                                $x++;
                                
                            endif;
                            
                            do_action( 'bp_profile_field_item' );
                        endwhile;
                        ?>
                </table>
            </div> <!-- iron-profile-fields -->
            
            <?php
            endif;
            
            endwhile;
            
            endif;
            
            endif;
            
            do_action( 'bp_profile_header_meta' );
            
            ?>
    
    </div><!-- #item-meta -->
    
    <?php
    // ARITZ: PONEMOS EL MAPA DEL USUARIO
    $poblac = strip_tags(trim(xprofile_get_field_data( 'Poblacion', bp_displayed_user_id())));
    $poblac = str_replace ('  ','',$poblac);
    $poblac = urlencode($poblac);
    if ($poblac) {
    
        /**
         * Google maps code
         */
        $q    = $poblac;
        $near = $poblac;
        
        echo '<div id="iron-map" style="margin: auto;">';
        echo '
        <iframe style="margin-bottom: 10px;" width="315" height="250" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.es/maps?f=q&amp;source=s_q&amp;hl=es&amp;geocode=&amp;q='. $q .'&amp;aq=0&amp;ie=UTF8&amp;hq=&amp;hnear='. $near .'&amp;z=12&amp;iwloc=B&amp;output=embed"></iframe>
        ';
        echo '</div>';
    }
    ?>

</div><!-- #item-header-content -->

<?php
do_action( 'bp_after_member_header' );

do_action( 'template_notices' );
?>