<?php
global $wpdb;
/**
 * Template Name: CustomSearch
 * Description: CustomSearch
 **/

get_header();

$_POST['search-terms'] = strip_tags ($_POST['search-terms']);

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

    <div id="content">
    
        <div id="iron-encabezado-container">
        
            <h2 class="iron-encabezado-seccion">
                <?php echo __('B&uacute;squeda','buddypress'); ?>
            </h2>
            
            <ul class="iron-encabezado-ul">
                <li class="iron-encabezado-li">
                    <a href="<?php bp_root_domain() ?>/">
                        <?php echo  __( 'Volver', 'buddypress' ); ?>
                    </a>
                </li>
            </ul>
            
        </div>
        
        <div class="padder">
        
            <div id="content-top"></div>
            
            <div id="content-middle">
            
                <div id="content-middle-add">
                
                    <div class="page" id="iron-search-container">
                    
                        <?php if ($_POST['search-terms']) : ?>
                        
                            <h3>
                                <?php
                                echo __ ('Resultados para la b&uacute;squeda', 'buddypress');
                                echo ': <em>'. $_POST['search-terms'].'</em>';
                                ?>
                            </h3>
                            
                            <?php
                            global $wpdb;
                            
                            $content = '';
                            
                            // Buscamos en testimonios
                            $querySearchTestimo  = 'SELECT 
                                                        post_title, 
                                                        post_name, 
                                                        post_content 
                                                    FROM
                                                        '. $wpdb->posts .', '. $wpdb->postmeta .'
                                                    WHERE
                                                        ID = post_id
                                                    AND
                                                        meta_value = "testimonio"
                                                    AND
                                                        post_status = "publish"
                                                    AND
                                                        (post_content LIKE "%'. $_POST['search-terms'] .'%" OR post_title LIKE "%'. $_POST['search-terms'] .'%")';
                            
                            $results_testimonios = $wpdb->get_results ($querySearchTestimo);
                            
                            $testimonios_count   = count($results_testimonios);
                            
                            if ($testimonios_count>0) {
                                
                                $content .= '<p style="margin: 20px 0px;">&middot;&nbsp;' . __ ('Resultados en <b>testimonios</b>: ','buddypress') . count($results_testimonios) . '</p>';
                                
                                foreach ($results_testimonios as $result) {
                                    $content .= '<p style="margin: 0px 10px; display: block;">';
                                    $content .= '<b><a href="'. site_url() . '/' . $result->post_name . '?search=' . $_POST['search-terms'] . '"> * '. $result->post_title .'</a></b>';
                                    $content .= '</p>';
                                    
                                    // marcamos el termino buscado en negrita y subrayado
                                    $excerpt      = trim( strip_tags( bp_create_excerpt( $result->post_content, 170 ) ) );
                                    
                                    $finalcontent = str_replace ($_POST['search-terms'],'<b><u>'. $_POST['search-terms'] .'</u></b>', $excerpt);
                                    
                                    $content .= '<span style="margin: 10px 20px; display: block; font-size: 9px;">';
                                    $content .= $finalcontent;
                                    $content .= '</span>';
                                }
                            }
                            
                            // Buscamos en preguntas
                            $querySearchPregun = '  SELECT 
                                                        post_title, 
                                                        post_name, 
                                                        post_content 
                                                    FROM
                                                        '. $wpdb->posts .', '. $wpdb->postmeta .'
                                                    WHERE 
                                                        ID = post_id
                                                    AND
                                                        meta_value = "pregunta"
                                                    AND
                                                        post_status = "publish"
                                                    AND
                                                        (post_content LIKE "%'. $_POST['search-terms'] .'%" OR post_title LIKE "%'. $_POST['search-terms'] .'%")
                                                 ';
                            
                            $results_preguntas = $wpdb->get_results($querySearchPregun);
                            
                            $preguntas_count   = count($results_preguntas);
                            
                            if ($preguntas_count>0) {
                             
                                $content .= '<p style="margin: 20px 0px;">&middot;&nbsp;' . __ ('Resultados en <b>preguntas</b>: ','buddypress') . count($results_preguntas). '</p>';
                                
                                foreach ($results_preguntas as $result) {
                                    $content .= '<p style="margin: 0px 10px; display: block;">';
                                    $content .= '<b><a href="'. site_url() . '/' . $result->post_name . '?search=' . $_POST['search-terms'] . '"> * '. $result->post_title .'</a></b>';
                                    $content .= '</p>';
                                    
                                    // marcamos el termino buscado en negrita y subrayado
                                    $excerpt = trim( strip_tags( bp_create_excerpt( $result->post_content, 170 ) ) );
                                    $finalcontent = str_replace ($_POST['search-terms'],'<b><u>'. $_POST['search-terms'] .'</u></b>', $excerpt);
                                    
                                    $content .= '<span style="margin: 10px 20px; display: block; font-size: 9px;">';
                                    $content .= $finalcontent;
                                    $content .= '</span>';
                                }
                            }
                            
                            // Buscamos en eventos
                            $table            = $wpdb->prefix . 'jet_events';
                            $querySearchEvent = '
                                                 SELECT 
                                                     name, 
                                                     slug, 
                                                     description 
                                                 FROM 
                                                     '. $table .'
                                                WHERE 
                                                    name
                                                LIKE
                                                    "%'. $_POST['search-terms'] .'%" OR description LIKE "%'. $_POST['search-terms'] .'%"
                                                ';
                            
                            $results_eventos  = $wpdb->get_results ($querySearchEvent);
                            
                            $eventos_count    = count($results_eventos);
                            
                            if ($eventos_count>0) {
                                
                                $content .= '<p style="margin: 20px 0px;">&middot;&nbsp;' . __ ('Resultados en <b>eventos</b>: ','buddypress') . count($results_eventos). '</p>'; 
                                 
                                foreach ($results_eventos as $result) {
                                    
                                    $content .= '<p style="margin: 0px 10px; display: block;">';
                                    $content .= '<b><a href="'. site_url() . '/eventos/?view=' . $result->slug .'&search=' . $_POST['search-terms'] . '"> * '. $result->name .'</a></b>';
                                    $content .= '</p>';
                                    
                                    // marcamos el termino buscado en negrita y subrayado
                                    $excerpt = trim( strip_tags( bp_create_excerpt( $result->description, 170 ) ) );
                                    $finalcontent = str_replace ($_POST['search-terms'],'<b><u>'. $_POST['search-terms'] .'</u></b>', $excerpt);
                                    
                                    $content .= '<span style="margin: 10px 20px; display: block; font-size: 9px;">';
                                    $content .= $finalcontent;
                                    $content .= '</span>';
                                }
                            }
                            
                            // Buscamos en grupos
                            $table            = $wpdb->prefix . 'bp_groups';
                            $querySearchGroup = '
                                                SELECT 
                                                    name, 
                                                    slug, 
                                                    description 
                                                FROM 
                                                    '. $table .'
                                                WHERE 
                                                    name 
                                                LIKE
                                                    "%'. $_POST['search-terms'] .'%" OR description LIKE "%'. $_POST['search-terms'] .'%"
                                                ';
                            
                            $results_grupos   = $wpdb->get_results ($querySearchGroup);
                            
                            $grupos_count     = count($results_grupos);
                            
                            if ($grupos_count>0) {
                            
                                $content .= '<p style="margin: 20px 0px;">&middot;&nbsp;' . __ ('Resultados en <b>grupos</b>: ','buddypress') . count($results_grupos) . '</p>';
                                
                                foreach ($results_grupos as $result) {
                                    
                                    $content .= '<p style="margin: 0px 10px;"><b><a href="'. site_url() . '/grupos/' . $result->slug .'?search=' . $_POST['search-terms'] . '"> * '. $result->name .'</a></b></p>';
                                    
                                    // marcamos el termino buscado en negrita y subrayado
                                    $excerpt = trim( strip_tags( bp_create_excerpt( $result->description, 170 ) ) );
                                    $finalcontent = str_replace ($_POST['search-terms'],'<b><u>'. $_POST['search-terms'] .'</u></b>', $excerpt);
                                    
                                    $content .= '<span style="margin: 10px 20px; display: block; font-size: 9px;">';
                                    $content .= $finalcontent;
                                    $content .= '</span>';
                                }
                            }
                            
                            // Buscamos en usuarios
                            $table            =$wpdb->prefix . 'bp_xprofile_data';
                            $querySearchUsers = '
                                                SELECT
                                                    display_name,
                                                    user_url,
                                                    user_nicename
                                                FROM
                                                    '. $wpdb->users .', '. $table .'
                                                WHERE
                                                    display_name
                                                LIKE
                                                    "%'. $_POST['search-terms'] .'%"
                                                AND
                                                    '. $wpdb->users .'.ID = user_id
                                                AND
                                                    field_id = 15
                                                AND
                                                    value != "Privado"
                                                ';
                            
                            $results_miembros = $wpdb->get_results($querySearchUsers);
                            
                            $members_count    = count($results_miembros);
                            if ($members_count>0) {
                                
                                $content .= '<p style="margin: 20px 0px;">&middot;&nbsp;' . __ ('Resultados en <b>miembros</b>: ','buddypress') . count($results_miembros) . '</p>';
                                
                                foreach ($results_miembros as $result) {
                                    $content .= '<p style="margin: 0px 10px;"><b><a href="'. site_url() .'/miembros/'. $result->user_nicename .'"> * '. $result->display_name .'</a></b></p>';
                                }
                            }
                            
                            $querySearchActivityUpdate = "
                                                            SELECT
                                                                id,
                                                                user_id,
                                                                content,
                                                                component
                                                            FROM
                                                                `wp_bp_activity`
                                                            WHERE
                                                            (
                                                                `type` = 'activity_update'
                                                            OR
                                                                `type` = 'activity_comment'
                                                            )
                                                            AND
                                                            (
                                                                `component` = 'activity'
                                                            OR
                                                                `component` = 'groups'
                                                            )
                                                            AND
                                                                secondary_item_id = '0'
                                                            AND
                                                                item_id = '0'
                                                            AND
                                                                hide_sitewide != '1'
                                                            AND
                                                                user_id in (".implode(',',friends_get_friend_user_ids(wp_get_current_user()->ID)) . ',' . wp_get_current_user()->ID . ")
                                                            AND
                                                                content LIKE '%" . $_POST['search-terms'] . "%'
                                                        ";
                            
                            $Results_ActivityUpdate    = $wpdb->get_results($querySearchActivityUpdate);
                            
                            $activity_count = count($Results_ActivityUpdate);
                            
                            if ($activity_count > 0) {
                                
                                $content .= '<p style="margin: 20px 0px;">&middot;&nbsp;' . __ ('Resultados en <b>publicaciones</b>: ','buddypress') . count($Results_ActivityUpdate) . '</p>';
                                
                                foreach ($Results_ActivityUpdate as $result) {
                                    
                                    $content .= '<p style="margin: 0px 10px;"><b><a href="'. site_url() .'/miembros/' . get_userdata($result->user_id)->user_nicename . '"> * '. xprofile_get_field_data('Nombre', $result->user_id) .'</a></b></p>';
                                    
                                    // marcamos el termino buscado en negrita y subrayado
                                    $excerpt = trim( strip_tags( bp_create_excerpt( $result->content, 20 ) ) );
                                    $finalcontent = str_replace ($_POST['search-terms'],'<b><u>'. $_POST['search-terms'] .'</u></b>', $excerpt);
                                    
                                    $content .= '<span style="margin: 10px 20px; display: block; font-size: 9px;">';
                                    $content .= '<a href="'. site_url() .'/miembros/' . get_userdata($result->user_id)->user_nicename . '/activity/' . $result->id . '?search=' . $_POST['search-terms'] . '">';
                                    $content .= $finalcontent;
                                    $content .= '</a>';
                                    $content .= '</span>';
                                    
                                }
                                
                            }
                            
                            $total_results = $members_count + $grupos_count + $eventos_count + $preguntas_count + $testimonios_count + $activity_count;
                            
                            if ($total_results>0) {
                                echo '<p><em>' . $total_results . __ (' resultados','buddypress') .  '</em></p>';
                                echo $content;
                            } else {
                                echo '<p>' . __ ('No se han encontrado resultados para tu b&uacute;squeda.','buddypress') . '</p>';
                            }
                            
                        else :
                        
                            echo '<p>' . __ ('Por favor, introduce el t&eacute;rmino a buscar.','buddypress') . '</p>';
                        
                        endif;
                        ?>
                        
                    </div> <!-- .page -->
                    
                </div> <!-- #content-middle-add -->
                
            </div> <!-- content-middle -->
            
            <div id="content-bottom"></div>
            
        </div><!-- .padder -->
    </div><!-- #content -->

<?php
locate_template( array( 'sidebar.php' ), true );

get_footer(); ?>