<?php

/**

 * The template for displaying Search Results pages

 *

 * @package WordPress

 * @subpackage Twenty_Twelve

 * @since Twenty Twelve 1.0

 */

global $wpdb;

$word = "no_pattern_matched";

if(preg_match('/([1-9])?[\s]?([a-zA-Z]{1,})[\s]*(\d*)[:]?(\d*)/',$_REQUEST['s'], $matches)){
	$word = $matches[1]." ".$matches[2];
	$chapter = ($matches[3] != "")?$matches[3]:1;
	$verus = ($matches[4] != "")?$matches[4]:1;
}

$regexp = "^".$word.".* ".$chapter.":".$verus."$";

/*
	$searchTerm = $_REQUEST['s'];
	preg_match('/[^\d]+/',$searchTerm, $textMatch);
	preg_match('/\d+/', $searchTerm,$numMatch,PREG_OFFSET_CAPTURE,3);
	print_r($textMatch);
	print_r($numMatch);

	if( count($numMatch) > 0)
	{
		$searchTerm =  str_replace(" ","s",strstr($searchTerm, $numMatch[0][0], true))." ".strstr($searchTerm, $numMatch[0][0]);
	}

echo $searchTerm;
echo "<br>";
$sArr = preg_split("/[\s:]+/", $searchTerm);
$word = "xxxxx%";
$chapter = 1;
$verus = 1;

if(count($sArr) == 1)
{
	$word = $sArr[0];
}

if(count($sArr) == 2)
{
	$word = $sArr[0];
	$chapter = $sArr[1];
}

if(count($sArr) == 3)
{
	$word = $sArr[0];
	$chapter = $sArr[1];
	$verus = $sArr[2];
}

$word = preg_replace("/^(\d+)/", "$1.?", $word);
echo $regexp = "^".$word.".* ".$chapter.":".$verus."$";*/

$word = "no_pattern_matched";

if(preg_match('/([1-9])?[\s]?([a-zA-z]{1,})[\s]*(\d*)[:]?(\d*)/',$_REQUEST['s'], $matches)){
	$word = (($matches[1]!="")?$matches[1]." ":"").$matches[2];
	$chapter = ($matches[3] != "")?$matches[3]:1;
	$verus = ($matches[4] != "")?$matches[4]:1;
}

$regexp = "^".$word.".* ".$chapter.":".$verus."$";

$querystr = "SELECT * from $wpdb->posts where post_title regexp '".$regexp."' and post_status = 'publish' LIMIT 0,1";

	  $result_content = $wpdb->get_results($querystr);

	  if(count($result_content) > 0)
	  {

	  	$post = $result_content[0];
	  	$isChapter = (preg_match("/-/",$post->post_name))?true:false;
	  	if($isChapter)
	  	{
	  		$breakout = explode("-", $post->post_name);
	  		$url = str_replace($post->post_name, $breakout[1], get_permalink($post->ID))."?v=".$post->post_name;
	  		wp_redirect($url);
	  		exit;
	  	}
	  }

get_header(); ?>

	<section id="primary" class="fixed-width">
		<div id="content" role="main" style="font-family:<?php echo $font_style;?>;font-size:<?php echo $bible_font_size;?>;">
		<?php
			$limit	=	5*get_query_var( 'paged',0 );
			$search=esc_attr(get_search_query());
			$search_arr	=	explode(" ",$search);
			global $wpdb;
        $cateid = get_site_option('symatic_strong_cat');

			// $querystr = "
				// SELECT $wpdb->posts.*
				// FROM $wpdb->posts
				// WHERE $wpdb->posts.ID =  '$search'
				// OR ".get_like_string($search_arr,'post_title')."
				// OR ".get_like_string($search_arr,'post_content')."
			 // ";

			$querystr="SELECT *	FROM $wpdb->posts JOIN $wpdb->term_relationships 
						ON `bible_term_relationships`.object_id = bible_posts.ID
						WHERE $wpdb->term_relationships.term_taxonomy_id=4
						AND  (".get_like_string($search_arr,'post_content')."
						OR ".get_like_string($search_arr,'post_title').")
						AND $wpdb->posts.post_status = 'publish'";

			 $pageposts = $wpdb->get_results($querystr, OBJECT);
		?>

		<?php query_posts($query_args);?>

		 <?php if (!empty($pageposts)): ?>

				<?php global $post; ?>

			<header class="page-header">
				<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentytwelve' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
			</header>

			<?php //twentytwelve_content_nav( 'nav-above' ); ?>

			<?php /* Start the Loop */ ?>

			<?php foreach ($pageposts as $post):
                 $cats = wp_get_post_categories($post->ID);
                 if(in_array($cateid, $cats)) continue;
                 ?>

					<?php setup_postdata($post); ?>

					<?php get_template_part( 'content', 'search'); ?>

			<?php endforeach; ?>

			<?php //twentytwelve_content_nav( 'nav-below' ); ?>

		<?php else : ?>

			<article id="post-0" class="post no-results not-found">
				<header class="entry-header">
					<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
				</header>

				<div class="entry-content">
					<p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'twentytwelve' ); ?></p>
					<?php get_search_form(); ?>
				</div><!-- .entry-content -->
			</article><!-- #post-0 -->

		<?php endif; ?>

		</div><!-- #content -->
	</section><!-- #primary -->

<?php //get_sidebar(); ?>

<?php get_footer(); ?>