Execute($sql_query);
while (!$rs_app->EOF) {
	$variables[$rs_app->fields['name']] = $rs_app->fields['value'];
	$rs_app->MoveNext();
}
// Template
$output = new Template();
//Function to sort the results array
function ArraySort($a,$subkey,$order) {
	foreach($a as $k=>$v) {
		$b[$k] = strtolower($v[$subkey]);
	}
	if ($order == "desc") {
		arsort($b);
	} else {
		asort($b);
	}
	foreach($b as $key=>$val) {
		$c[] = $a[$key];
	}
	return $c;
}
//Function to get path
function GetLink($structure_id,$content_id) {
	if ($structure_id == "") {
		$sql_cs_query = 'SELECT * FROM bCMS_contents_to_structures cs INNER JOIN bCMS_elements e on cs.structure_id=e.structure_id INNER JOIN bCMS_applications_listing al on al.content_id=e.element_type_id WHERE cs.structure_id != 1 AND cs.content_id='.$content_id;
		$rs_cs = $GLOBALS['ddb']->Execute($sql_cs_query);
		$structure_id = $rs_cs->fields['structure_id'];
		$content_type = "news";
	}
	$sql_query = 'SELECT * FROM bCMS_structures WHERE structure_id='.$structure_id;
	$rs = $GLOBALS['ddb']->Execute($sql_query);
	$rs->fields['content_id'] = $content_id;
	if (!$rs->EOF) {
		if (($rs->fields['structure_id'] !=0 )and($rs->fields['structure_id'] !=1)) {
			$navigations = GetLink($rs->fields['parent_id'],$content_id);
			$navigations .= $rs->fields['directory'].'/';
			//$prevname = $rs->fields['directory'];
		}
	} else if($structure_id != "0") $navigations = "mobile";
	if ($content_type == "news") $navigations .= "index.php?content_id=" . $content_id;
	if ($structure_id == "") $navigations = "mobile";
	return $navigations;
}
$whereclause = "WHERE ((s.display = 'Show') or (s.display IS NULL)) AND ((s.report = 'Enabled') or (s.report IS NULL)) AND c.display = 'Show' AND ";
if ($_REQUEST['search-terms'] != "") {
	//$whereclause .= "((p.body LIKE '%".$_REQUEST['search-terms']."%') or (c.name LIKE '%".$_REQUEST['search-terms']."%'))";
	$whereclause .= " ((match(p.body) against (\"+".$_REQUEST['search-terms']."\" IN BOOLEAN MODE)) or (match(c.name) against (\"+".$_REQUEST['search-terms']."\" IN BOOLEAN MODE)) or (match(s.keywords) against (\"+".$_REQUEST['search-terms']."\" IN BOOLEAN MODE)))";
} else {
	$errors['message'] = 'No term submitted';
}
if (empty($errors)) {
	$sql_query_search = 'SELECT p.page_id, p.body, p.page_number, p.date_modified, p.display as pagedisplay, c.content_id, c.name AS contentname, c.redirect as contentredirect, c.description, c.date_start, c.date_stamp, c.display as contentdisplay, e.element_type_id, e.structure_id, s.structure_id, s.parent_id, s.order_id, s.directory, s.keywords, s.redirect, s.name AS structurename, s.status, s.display as structuredisplay FROM bCMS_contents c LEFT JOIN bCMS_pages p ON p.content_id=c.content_id LEFT JOIN bCMS_elements e ON e.element_type_id=c.content_id LEFT JOIN bCMS_structures s ON e.structure_id=s.structure_id '.$whereclause . ' ORDER BY s.parent_id DESC';
	$rs_search = $GLOBALS['ddb']->Execute($sql_query_search);
	//echo $sql_query_search;
	$count = 0;
	while (!$rs_search->EOF) {
		if ($rs_search->fields['structurename'] == "") {
			$rs_search->fields['name'] = $rs_search->fields['contentname'];
		} else {
			$rs_search->fields['name'] = $rs_search->fields['structurename'];
		}
		$body2 = str_ireplace($_REQUEST['search-terms'], "", $rs_search->fields['body']);
		$content2 = str_ireplace($_REQUEST['search-terms'], "", $rs_search->fields['name']);
		$keywords2 = str_ireplace($_REQUEST['search-terms'], "", $rs_search->fields['keywords']);
		$stringlength = strlen($_REQUEST['search-terms']);
		$pagelength1 = strlen($rs_search->fields['body']);
		$pagelength2 = strlen($body2);
		$contentlength1 = strlen($rs_search->fields['name']);
		$contentlength2 = strlen($content2);
		$keywordslength1 = strlen($rs_search->fields['keywords']);
		$keywordslength2 = strlen($keywords2);
		$matches1 = ($pagelength1 - $pagelength2) / $stringlength;
		$matches2 = ($contentlength1 - $contentlength2) / $stringlength;
		$matches3 = ($keywordslength1 - $keywordslength2) / $stringlength;
		$rs_search->fields['matches'] = $matches1 + $matches2 + $matches3;
		$rs_search->fields['body'] = strip_tags($rs_search->fields['body']);
		$rs_search->fields['name'] = strip_tags($rs_search->fields['name']);
		if ($rs_search->fields['name'] == "Overview") {
			$sql_query_name = 'SELECT name FROM bCMS_structures WHERE structure_id='.$rs_search->fields['parent_id'];
			$rs_name = $GLOBALS['ddb']->Execute($sql_query_name);
			$rs_search->fields['name'] = $rs_name->fields['name'] . " - " . $rs_search->fields['name'];
		}
		$rs_search->fields['body'] = str_ireplace(" ", " ", $rs_search->fields['body']);
		//Find first occurrence of term
		$first = stripos($rs_search->fields['body'], $_REQUEST['search-terms']);
		if ($first < 250) {
		$rs_search->fields['body'] = substr($rs_search->fields['body'], 0, 255);
		} else {
		$rs_search->fields['body'] = "..." . substr($rs_search->fields['body'], $first-50, 200);
		}
		$rs_search->fields['name'] = substr($rs_search->fields['name'], 0, 255);
		//$rs_search->fields['name'] = preg_replace("/\b".preg_quote($_REQUEST['search-terms'])."\b/i", "$0", $rs_search->fields['name']);
		//$rs_search->fields['body'] = preg_replace("/\b".preg_quote($_REQUEST['search-terms'])."\b/i", "$0", $rs_search->fields['body']);
		if ($rs_search->fields['body'] != "") $rs_search->fields['body'] .= "...";
		if ($rs_search->fields['status'] == "") $rs_search->fields['date_modified'] = $rs_search->fields['date_stamp'];
		//Get link for result
		$rs_search->fields['link'] = GetLink($rs_search->fields['structure_id'],$rs_search->fields['content_id']);
		//Give results a weight for relevancy sorting (higher weight = higher on the results list)
		$rs_search->fields['display_weight'] = $rs_search->fields['matches'];
		if ($rs_search->fields['structure_id'] > 0) $rs_search->fields['display_weight'] = $rs_search->fields['display_weight'] + 0.5;
		if ($matches2 > 0) $rs_search->fields['display_weight'] = $rs_search->fields['display_weight'] + 5.5;
		if ($first < 250) $rs_search->fields['display_weight'] = $rs_search->fields['display_weight'] + 1.5;
		$separate_content = strpos($rs_search->fields['link'], "content_id");
		if ($separate_content === false) $rs_search->fields['display_weight'] = $rs_search->fields['display_weight'] + 15.5;
		//Push search matches on structure names to top of relevancy list
		if (($separate_content === false) and ($matches2 > 0)) $rs_search->fields['display_weight'] = $rs_search->fields['display_weight'] + 55.5;
		if ($rs_search->fields['contentredirect'] != "") $rs_search->fields['link'] = $rs_search->fields['contentredirect'];
		//Count slashes for sitemap display
		$slashcount = substr_count($rs_search->fields['link'], '/');
		$rs_search->fields['sitemap_level'] = $slashcount;
		//only include if not mobile and page redirect results and add others to array
		$mobile = strpos($rs_search->fields['link'], "mobile");
		if (($mobile === false) and (trim($rs_search->fields['redirect']) == "")) {
			if ($rs_search->fields['matches'] > 0) {
				$list_search_results[] = $rs_search->fields;
				$count++;
				//echo $rs_search->fields['link']."---".$count."
";
			}
		}
		$rs_search->MoveNext();
	}
	if ($_REQUEST['sort'] == "date") {
		$list_search_results = ArraySort($list_search_results,'date_modified','desc');
	} else if($_REQUEST['sort'] == "match") {
		$list_search_results = ArraySort($list_search_results,'matches','desc');
	} else {
		$list_search_results = ArraySort($list_search_results,'display_weight','desc');
	}
	if ($count == 0) $errors['message'] = '
No results found. Try again in the box above or try Google site search.
'; if ($_REQUEST['sort'] == "match") { $sort_display = ""; } else if ($_REQUEST['sort'] == "date") { $sort_display = ""; } else { $sort_display = ""; } $total_time = round((microtime(1) - $start), 4); $originalArray = $list_search_results; $newArray = array(); $usedArray = array(); foreach ( $originalArray AS $line ) { if ( !in_array($line['link'], $usedArray) ) { $usedArray[] = $line['link']; $newArray[] = $line; } else $count--; } $list_search_results = $newArray; unset($newArray,$usedArray); //Get and display page info $total_pages = ceil($count / $results_per_page); if ($total_pages > 1) { if ($_REQUEST['page'] > 1) $display_page_links .= "« Previous "; for ($i = 1; $i <= $total_pages; $i++) { if ($_REQUEST['page'] == $i) { $display_page_links .= "".$i." "; } else { $display_page_links .= "".$i." "; } } if (($total_pages > 1) and ($_REQUEST['page'] != $total_pages)) $display_page_links .= "Next » "; } $results_portion1 = (($_REQUEST['page']-1) * $results_per_page) +1; if ($_REQUEST['page'] == $total_pages) { $results_portion2 = $count; } else { $results_portion2 = $_REQUEST['page'] * $results_per_page; } //print_r($list_search_results); $output->AssignVariable('list_search_results',$list_search_results); $output->AssignVariable('message',$errors['message']); $output->AssignVariable('results_num',$count); $output->AssignVariable('results_time',"Results ".$results_portion1."-".$results_portion2." of ".$count." for \"".$_REQUEST['search-terms']."\"