<?PHP
/* ====================
Seditio - Website engine
Copyright Neocrome
http://www.neocrome.net
[BEGIN_SED]
File=plugins/xsearch/xsearch.php
Version=1.0.0
Updated=2009-jan-20
Type=Plugin
Author=Dmitry Lapin
Description=Search engine for Seditio CMS
[END_SED]
[BEGIN_SED_EXTPLUGIN]
Code=xsearch
Part=main
File=xsearch
Hooks=standalone
Tags=
Order=15
[END_SED_EXTPLUGIN]
==================== */

if (!defined('SED_CODE') || !defined('SED_PLUG')) { die('Wrong URL.'); }

require_once("plugins/xsearch/inc/xsearch.common.php");

$xsearch = new XTemplate('plugins/xsearch/tpl/xsearch.tpl');

$sq = sed_import('sq','P','TXT',70);
if (empty($sq)) { $sq = sed_import('sq','G','TXT',70); }
$type = sed_import('type','P','TXT',5);
if (empty($type)) { $type = sed_import('type','G','TXT',5); }
if (empty($type)) { $type = "all"; $typecheck['all'] = "checked=\"checked\""; }
$start = sed_import('start','P','INT');
if (empty($start)) { $start = sed_import('start','G','INT'); }
if (empty($start)) { $start = 0; }


if (!empty($sq))
{
	$xsearch->assign("SQ",$sq);
	
	if ( ($swords = sq_processing($sq)) == false ) 
	{ 
		$xsearch->assign("ERROR_BODY",$error);
		$xsearch->parse('XSEARCH.ERROR'); 
	}
	else // Processing successful. It's correct query. || Обработка запроса прошла успешно. Запрос соответствует нашим требованиям.
	{
		$num = count($swords);
		for ($i=0; $i<$num; $i++)
		{
			$swords_b .= ">".$swords[$i];
			if ($i!=$num-1) $swords_b .= " ";
		}
		$swords = implode(" ", $swords);
				
		// Type of query: only pages / forums or all || Тип запроса: по страницам / по форумам или везде.
		switch ($type)
		{
			case 'pages':
				if ($cfg['disable_page']) { $error = $L['xsearch_error_pagesoff']; break; }
				$plugin_title = $L['xsearch_pages_search'];
				break;
			
			case 'forum':
				if ($cfg['disable_forums']) { $error = $L['xsearch_error_forumoff']; break; }
				$plugin_title = $L['xsearch_forum_search'];
				break;
			
			default:
				$type = "all";
				$plugin_title = $L['xsearch_title_alltab'];
				break;
		} // end switch ($type)
		
		if ( ($type=="pages" || $type=="all") && !$cfg['disable_page'] )
		{
			$descselect1 = ($cfg['plugin']['xsearch']['showtext']) ? "p." . $cfg['plugin']['xsearch']['pagedesc'] . ", " : "";
			$sql1 = "SELECT SQL_CALC_FOUND_ROWS
				p.page_id, p.page_title, p.page_alias, p.page_cat, s.structure_title, $descselect1
				(100*(MATCH (p.title_index) AGAINST ('>\"".$swords."\" +(".$swords_b.")' IN BOOLEAN MODE)) + 
				MATCH (p.page_index) AGAINST ('>\"".$swords."\" +(".$swords_b.")' IN BOOLEAN MODE)) as relev
				FROM `$db_pages` p
				LEFT JOIN `$db_structure` AS s ON p.page_cat=s.structure_code
				WHERE p.page_state!=1
				AND (MATCH (p.title_index) AGAINST ('>\"".$swords."\" +(".$swords_b.")' IN BOOLEAN MODE)) > 0.5 OR 
				(MATCH (p.page_index) AGAINST ('>\"".$swords."\" +(".$swords_b.")' IN BOOLEAN MODE)) > 0.5
				GROUP BY p.page_id ORDER BY relev DESC
				LIMIT $start,".$cfg['plugin']['xsearch']['maxitems']."";
		}
		if ( ($type=="forum" || $type=="all") && !$cfg['disable_forums'] )
		{
			$descselect2 = ($cfg['plugin']['xsearch']['showtext']) ? "p.fp_text, " : "";
			$sql2 = "SELECT SQL_CALC_FOUND_ROWS
				p.fp_topicid, p.fp_sectionid, $descselect2 t.ft_title, s.fs_title, 
				(SUM(MATCH (p.fp_index) AGAINST ('>\"".$swords."\" +(".$swords_b.")' IN BOOLEAN MODE)) + 
				100 * (MATCH (t.ft_index) AGAINST ('>\"".$swords."\" +(".$swords_b.")' IN BOOLEAN MODE))) AS relev
				FROM `$db_forum_topics` t, `$db_forum_posts` p
				LEFT JOIN `$db_forum_sections` AS s ON s.fs_id=p.fp_sectionid
				WHERE p.fp_topicid=t.ft_id AND t.ft_mode != 1 
				AND ((MATCH (p.fp_index) AGAINST ('>\"".$swords."\" +(".$swords_b.")' IN BOOLEAN MODE)) > 0.5 OR 
				(MATCH (t.ft_index) AGAINST ('>\"".$swords."\" +(".$swords_b.")' IN BOOLEAN MODE)) > 0.5) 
				GROUP BY p.fp_topicid ORDER BY relev DESC
				LIMIT $start,".$cfg['plugin']['xsearch']['maxitems']."";
		}
		
		if ( $error != false ) 
		{ 
			$xsearch->assign("ERROR_BODY", $error);
			$xsearch->parse('XSEARCH.ERROR'); 
		}
		else // It's ok, let's start searching || Все системы в норме, ключ на старт
		{
			$typecheck[$type] = "checked=\"checked\"";
			
			if ( !empty($sql1) )
			{
				$sql1 = sed_sql_query($sql1);
				$items1 = sed_sql_query("SELECT FOUND_ROWS()");
				$items1 = sed_sql_result($items1, 0, 0);
				$pagnav = build_pagnav($items1,$sq,$type);
				
				while ($res = sed_sql_fetcharray($sql1))
				{
					$name = sed_cc($res['page_title']);
					$link = (empty($res['page_alias'])) ? $cfg['mainurl']."/page.php?id=".$res['page_id'] : $cfg['mainurl']."/page.php?al=".$res['page_alias'];
					$name = "<a href=\"".$link."\">".$name."</a>";
					$link = $L['xsearch_cat']."<a href=\"list.php?c=".$res['page_cat']."\">".$res['structure_title']."</a>";
					$text = ($cfg['plugin']['xsearch']['showtext']) ? substr(sed_cc($res[$cfg['plugin']['xsearch']['pagedesc']]),0,$cfg['plugin']['xsearch']['maxtext']) : "";

					$xsearch-> assign(array(
						'RESULT_TITLE' => $name ,
						'RESULT_LINK' => $link,
						'RESULT_TEXT' => $text,
						));		

					$xsearch->parse('XSEARCH.RESULTS.ITEM');
				}
				$xsearch-> assign(array(
					"PAGNAV" => $pagnav,
					"RESULT_NAME" => $L['xsearch_pages_search'],
					"RESULT_COUNT" => $L['xsearch_found'] . $items1
				)); 
				$xsearch->parse('XSEARCH.RESULTS');
			}
			
			if ( !empty($sql2) )
			{
				$sql2 = sed_sql_query($sql2);
				$items2 = sed_sql_query("SELECT FOUND_ROWS()");
				$items2 = sed_sql_result($items2, 0, 0);
				$pagnav = build_pagnav($items2,$sq,$type);
				
				while ($res = sed_sql_fetcharray($sql2))
				{
					$name = sed_cc($res['ft_title']);
					$link = $cfg['mainurl']."/forums.php?m=posts&amp;q=".$res['fp_topicid'];
					$name = "<a href=\"".$link."\">".$name."</a>";
					$link = $L['Forums']. ": <a href=\"forums.php?m=topics&s=".$res['fp_sectionid']."\">".$res['fs_title']."</a>";
					$text = ($cfg['plugin']['xsearch']['showtext']) ? substr(sed_cc($res['fp_text']),0,$cfg['plugin']['xsearch']['maxtext']) : "";

					$xsearch-> assign(array(
						'RESULT_TITLE' => $name ,
						'RESULT_LINK' => $link,
						'RESULT_TEXT' => $text,
						));		
					$xsearch->parse('XSEARCH.RESULTS.ITEM');
				}
				$xsearch-> assign(array(
					"PAGNAV" => $pagnav,
					"RESULT_NAME" => $L['xsearch_forum_search'],
					"RESULT_COUNT" => $L['xsearch_found'] . $items2
				)); 
				$xsearch->parse('XSEARCH.RESULTS');
			}
		} // $error == false
	} // sq_processing($sq)) is ok	
} // end if (!empty($sq))

$type = ($cfg['disable_page']) ? "" : "<input id=\"type1\" name=\"type\" type=\"radio\" value=\"pages\" ".$typecheck['pages']." />".$L['xsearch_search_pages'];
$type .= ($cfg['disable_forums']) ? "" : "<input id=\"type2\" name=\"type\" type=\"radio\" value=\"forum\" ".$typecheck['forum']." />".$L['xsearch_search_forum'];
$type .= "<input id=\"type3\" name=\"type\" type=\"radio\" value=\"all\" ".$typecheck['all']." />".$L['xsearch_search_all'];
$xsearch-> assign(array(
		'SEARCH_TYPE' => $type
		));
$xsearch->parse('XSEARCH');
$plugin_body = $xsearch->text('XSEARCH');

?>