<?php
function bpmag_remove_search_dropdown($select_html) {
    return '';
}
// Remove Buddypress search drowpdown for selecting members etc
add_filter("bp_search_form_type_select", "bpmag_remove_search_dropdown");


remove_action('init', 'bp_core_action_search_site', 5); //force buddypress to not process the search/redirect
add_action('init', 'bp_buddydev_search', 10); // custom handler for the search

function bp_buddydev_search() {
    global $bp;
    if ($bp->current_component == BP_SEARCH_SLUG)//if thids is search page
        bp_core_load_template(apply_filters('bp_core_template_search_template', 'search-single')); //load the single searh template
}

add_action("advance-search", "bpmag_show_search_results", 1); //highest priority

/* we just need to filter the query and change search_term=The search text */

function bpmag_show_search_results() {
    //filter the ajaxquerystring
    add_filter("bp_ajax_querystring", "bpmag_global_search_qs", 100, 2);
}

//show the search results for member*/
function bpmag_show_member_search() {
    /*if (!bp_has_members( bp_ajax_querystring( 'members' ) ) ) : return; endif;*/
    ?>
    <h2><?php _e("Member results", "bpmag"); ?></h2>
    <div id="members-dir-list" class="members dir-list">
        <?php locate_template(array('members/members-loop.php'), true); ?>
        <?php global $members_template; ?>
        <?php if ($members_template->total_member_count > 18): ?>
            <a href="<?php echo bp_get_root_domain() . '/' . BP_MEMBERS_SLUG . '/?s=' . $_REQUEST['s'] ?>" ><?php _e(sprintf("View all %d matched members", $members_template->total_member_count), "bpmag"); ?></a>
        <?php endif; ?>
    </div><!-- #members-dir-list -->    
    <div class="clear"></div>
    <hr/>

    <?php
}

//Hook Member results to search page
add_action("advance-search", "bpmag_show_member_search", 30); //the priority defines where in page this result will show up(the order of member search in other searchs)

function bpmag_show_groups_search() {
    /*if (!bp_has_groups( bp_ajax_querystring( 'groups' ) ) ) : return; endif;    */
    ?>
    <h2><?php _e("My favorite bands results", "bpmag"); ?></h2>
    <div id="groups-dir-list" class="groups dir-list">
        <?php locate_template(array('groups/groups-loop.php'), true); ?>
        <?php global $groups_template; ?>
        <?php if ($groups_template->total_group_count > 18): ?>
            <a href="<?php echo bp_get_root_domain() . '/' . BP_GROUPS_SLUG . '/?s=' . $_REQUEST['s'] ?>" ><?php _e(sprintf("View all %d matched groups", $groups_template->total_group_count > 18), "bpmag"); ?></a>
        <?php endif; ?>
    </div><!-- #groups-dir-list -->
    <div class="clear"></div>
    <hr/>

    <?php
}

//Hook Groups results to search page
if (bp_is_active('groups'))
    add_action("advance-search", "bpmag_show_groups_search", 20);

/**
 *
 * Show blog posts in search
 */
function bpmag_show_site_blog_search() {?>
    <h2><?php _e("News results results", "bpmag"); ?></h2>
    <?php if (!have_posts()) : ?>
	<div id="message" class="info">
		<p><?php _e( "Sorry, no posts were found.", 'buddypress' ); ?></p>
	</div>
        <div class="clear"></div>
        <hr/>
    <?php return; endif; ?>
    <?php
        $args = $GLOBALS['wp_query']->query;
        $cat_concerts = get_category_by_slug(CONCERTS_CATEGORY_SLUG);
        $cat_musicnews = get_category_by_slug(MUSICNEWS_CATEGORY_SLUG);
        if ($cat_concerts && $cat_musicnews) : 
            $args['cat'] = implode(',', array($cat_concerts->term_id, $cat_musicnews->term_id));
            query_posts($args);
        endif;
    ?>    
    <div class="blog-search-result search-result">
        <?php locate_template(array('post-loop.php'), true); ?>
    </div>
    <div class="clear"></div>

    <?php
    if ($cat_concerts && $cat_musicnews) : 
        wp_reset_query(); 
    endif;
}

//Hook Blog Post results to search page
add_action("advance-search", "bpmag_show_site_blog_search", 40);

//show forums search
function bpmag_show_forums_search() {
    ?>
    <h2><?php _e("Forum results", "bpmag"); ?></h2>
    <div class="forums-search-result search-result">        
        <?php locate_template(array('forums/forums-loop.php'), true); ?>
        <a href="<?php echo bp_get_root_domain() . '/' . BP_FORUMS_SLUG . '/?s=' . $_REQUEST['s'] ?>" ><?php _e("View All matched forum posts", "bpmag"); ?></a>
    </div>
    <div class="clear"></div>
    <hr/>    
    <?php
}

//Hook Forums results to search page
if (bp_is_active('forums') && bp_is_active('groups') && ( function_exists('bp_forums_is_installed_correctly') && !(int) bp_get_option('bp-disable-forum-directory') ) && bp_forums_is_installed_correctly())
    add_action("advance-search", "bpmag_show_forums_search", 50);

//show blogs search result

function bpmag_show_blogs_search() {
    ?>
    <h2><?php _e("News results", "bpmag"); ?></h2>
    <div class="blogs-search-result search-result">        
        <?php locate_template(array('post-loop.php'), true); ?>
        <a href="<?php echo bp_get_root_domain() . '/' . BP_BLOGS_SLUG . '/?s=' . $_REQUEST['s'] ?>" ><?php _e("View All matched Blogs", "bpmag"); ?></a>
    </div>
    <div class="clear"></div>
    <hr/>

    <?php
}

//Hook Blogs results to search page if blogs comonent is active
if (bp_is_active('blogs'))
    add_action("advance-search", "bpmag_show_blogs_search", 10);

//modify the query string with the search term
function bpmag_global_search_qs() {
    return "search_terms=" . $_REQUEST['s'];
}

function bpmag_is_advance_search() {
    global $bp;
    if ($bp->current_component == BP_SEARCH_SLUG)
        return true;
    return false;
}

//show forums search
function bpmag_show_dedications_search() {?>
    <h2><?php _e("Dedication results", "bpmag"); ?></h2>
    <?php if (!have_posts()) : ?>
    <div id="message" class="info">
            <p><?php _e( "Sorry, no posts were found.", 'buddypress' ); ?></p>
    </div>
    <div class="clear"></div>
    <hr/>
    <?php return; endif; ?>

    <?php
        $args = $GLOBALS['wp_query']->query;
        $args['category_name'] = DEDICATION_CATEGORY_SLUG;
        query_posts($args);        
    ?>
    
    <div class="blogs-search-result search-result">
        <?php locate_template(array('post-loop-dedication.php'), true); ?>
    </div>
    <div class="clear"></div>
    <hr/>

    <?php
     wp_reset_query(); 
}

//Hook Forums results to search page
if (bp_is_active(BP_DEDICATION_SLUG))
    add_action("advance-search", "bpmag_show_dedications_search", 10);
?>