<?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 preguntas','buddypress');
                } else {
                    echo __('Pregunta a todos','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-preguntatodos" class="button" href="<?php echo site_url() .'/questions-modal/?action=create'; ?>">
                            <?php _e( 'Crear pregunta', '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() ?>questions">
                        <?php echo  __( 'Todas', '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() ?>questions/?sc=mine">
                        <?php echo  __( 'Mis preguntas', 'buddypress' ); ?>
                    </a>
                </li>
            </ul>
        </div>
        
        <div class="padder">
        
            <div id="content-top"></div>
            
                <div id="content-middle">
                
                    <div class="page" id="iron-preguntatodos-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) );
                         */
                        
                        $addquery = "";
                        if ($_GET['sc'] == 'mine') {
                            $addquery = ' AND post_author='. get_current_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='pregunta'
                        ORDER BY 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-preguntatodos" style="font-size: 11px;" href="'. site_url() .'/questions-modal/?action=update&id='. $post->ID .'">'. __ ('Editar','buddypress') .'</a>';
                                                echo '&nbsp;&middot;&nbsp;<a style="font-size: 11px;" href="'. site_url() .'/questions/?action=delete&id='. $post->ID .'" onClick="if(!confirm(\''. __ ('Eliminar Pregunta ?','buddypress') .'\')) {return false;}">'. __ ('Eliminar','buddypress') .'</a>';
                                            }
                                            ?>
                                        </p>
                                        
                                        <p class="post-content">
                                            <?php
                                            if (strlen(strip_tags($post->post_content))>200) {
                                                echo strip_tags(substr($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>
                                        </p>
                                    </div> <!-- .post-content -->
                                
                                </div> <!-- .post -->
                            <?php }
                        }
                        else {
                            _e('No se han encontrado preguntas.','buddypress');
                        }
                        ?>
                    
                    </div> <!-- .page -->
                
                 </div> <!-- content-middle -->
            
            <div id="content-bottom"></div>
        
        </div><!-- .padder -->
    </div><!-- #content -->
    
<?php
locate_template( array( 'sidebar.php' ), true );

get_footer();
?>