<?php

global $bp,$wpdb;

get_header();

locate_template( array( 'leftsidebar.php' ), true );
?>
    
    <div id="content">
        
        <div id="iron-encabezado-container">
            <h2 class="iron-encabezado-seccion">
                <?php
                if ($_GET['sc']=='mine') {
                    echo __('Mis testimonios','buddypress');
                } else {
                    echo __('Testimonios','buddypress');
                }
                ?>
            </h2>
            <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-testimonies" class="button" href="<?php echo site_url() .'/testimonies-modal/?action=create'; ?>">
                            <?php _e( 'Crear testimonio', '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 ($_GET['sc'] != 'mine') echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain() ?>testimonies">
                            <?php echo  __( 'Todos', 'buddypress' ); ?>
                        </a>
                    </li>
                    <li class="iron-encabezado-li">
                        &middot;&nbsp;
                        <a <?php if ($_GET['sc'] == 'mine') echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain() ?>testimonies/?sc=mine">
                            <?php echo  __( 'Mis testimonios', 'buddypress' ); ?>
                        </a>
                    </li>
            </ul>
        </div>
        
        <div class="padder">
        
        <div id="content-top"></div>
        
            <div id="content-middle">
                
                <div class="page" id="iron-testimonies-container">
                    
                    <?php
                    /**
                     * Obtenemos el listado de posts
                     * numberposts es el num que queremos recoger
                     * offset es a partir de cual
                     * $list = get_posts( array ('numberposts'=> 15, 'offset'=> 0) );
                     * 
                     * Listado de amigos y usuario actual
                     */
                    
                    $friend_list = bp_get_friend_ids( bp_loggedin_user_id() );
                    $friend_list = $friend_list . ',' . bp_loggedin_user_id();
                    $addquery    = "";
                    
                    if ($_GET['sc'] == 'mine') {
                        
                        $addquery = ' AND post_author='. bp_loggedin_user_id();
                        
                        $list = $wpdb->get_results("SELECT * FROM ". $wpdb->posts .", ".$wpdb->postmeta."
                        WHERE ID=post_id AND post_type='post' AND post_status='publish' ".$addquery."
                        AND meta_key='_wp_page_type' AND meta_value='testimonio'
                        ORDER BY post_date DESC");
                    } else {
                        
                        /**
                         * Aqui cogemos los publicos y los de nuestros amigos y el ususario logueado
                         */
                        
                        $list = $wpdb->get_results("SELECT p.* FROM ". $wpdb->posts ." p
                        INNER JOIN ".$wpdb->postmeta." pm ON pm.post_id = p.ID AND pm.meta_key='_wp_page_type' AND pm.meta_value='testimonio'
                        INNER JOIN ".$wpdb->postmeta." pmm ON pmm.post_id = p.ID AND pmm.meta_key='iron-privacity' AND
                        (pmm.meta_value='publico' OR (pmm.meta_value='amigos' AND p.post_author IN (". $friend_list.")))
                        WHERE p.post_type='post' AND p.post_status='publish' ORDER BY p.post_date DESC");
                    }
                    
                    if (sizeof($list)>0) {
                        foreach ($list as $post) {
                            
                            /**
                             * Calcular estado de animo
                             */
                            $class = calculate_moodbar( $post->post_author );
                            ?>
                            <div class="post" id="post-<?php the_ID(); ?>">
                                
                                <div class="author-box <?php echo $class; ?>">
                                    <a href="<?php echo bp_core_get_user_domain($post->post_author); ?>">
                                        <?php echo get_avatar( $post->post_author, '50' ); ?>
                                    </a>
                                </div>
                                
                                <div class="post-content">
                                
                                    <p class="post-title">
                                        <a href="<?php echo $post->guid; ?>" title="<?php echo $post->post_title; ?>">
                                            <?php echo $post->post_title; ?>
                                        </a>
                                    </p>
                                    
                                    <p class="post-user">
                                        <?php
                                        printf( __( 'por %s', 'buddypress' ), bp_core_get_userlink( $post->post_author ) );
                                        ?>
                                        <span id="mentions">
                                            @<?php echo get_userdata( $post->post_author )->user_login ?>
                                        </span>
                                        <?php
                                        echo __ (' el ','buddypress') . date ("d/m/Y", strtotime ($post->post_date));
                                        
                                        /**
                                         * Si es el autor mostramos enlaces "Editar" y "Eliminar"
                                         */
                                        if ( get_current_user_id() == $post->post_author ) {
                                            echo '&middot;&nbsp;<a class="fancybox-testimonies" style="font-size: 11px;" href="'. site_url() .'/testimonies-modal/?action=update&id='. $post->ID .'">'. __ ('Editar','buddypress') .'</a>';
                                            echo '&nbsp;&middot;&nbsp;<a style="font-size: 11px;" href="'. site_url() .'/testimonies/?action=delete&id='. $post->ID .'" onClick="if(!confirm(\''. __ ('Eliminar Testimonio ?','buddypress') .'\')) {return false;}">'. __ ('Eliminar','buddypress') .'</a>';
                                        }
                                        ?>
                                    </p>
                                    
                                    <p class="post-content">
                                        <?php
                                        /**
                                         * Debug
                                         * $priv = get_post_meta($post->ID, 'iron-privacity');
                                         * echo "priv: ".$priv[0];
                                         */
                                        
                                        if (strlen(strip_tags($post->post_content))>200) {
                                            echo substr(strip_tags($post->post_content),0,200);
                                            echo '<br />&nbsp;<br />...&nbsp;<a href="'. $post->guid .'" title="'. __ ('continuar leyendo','buddypress') . $post->post_title .'">'. __ ('continuar leyendo','buddypress') .'</a>';
                                        }
                                        else
                                            echo $post->post_content;
                                        ?>
                                    </p>
                                    
                                    <p class="postmetadata">
                                        <span class="comments">
                                            <?php
                                            comments_popup_link( __( 'No Comments &#187;', 'buddypress' ), __( '1 Comment &#187;', 'buddypress' ), __( '% Comments &#187;', 'buddypress' ) );
                                            ?>
                                        </span>
                                        <?php
                                        /*
                                        // Mostrar tags
                                        $tags = wp_get_post_tags ($post->ID);
                                        $tags_show='';
                                        
                                        if (count($tags)>0) {
                                            foreach ($tags as $tag) {
                                                $tags_show .= $tag->name . ', ';
                                            }
                                        
                                        _e ('Etiquetas: ', 'buddypress');
                                        echo substr ($tags_show, 0, strlen($tags_show)-2);
                                        }
                                        */
                                        ?>
                                    </p>
                                    
                                </div> <!-- .post-content -->
                            
                            </div> <!-- .post -->
                        <?php }
                    }
                    else
                        _e('No se han encontrado testimonios.', 'buddypress');
                    ?>
                
                </div><!-- .page -->
            
            </div><!-- content-middle -->
            
        <div id="content-bottom"></div>
        
        </div><!-- .padder -->
    </div><!-- #content -->
    
<?php
locate_template( array( 'sidebar.php' ), true );

get_footer();
?>