<?php
/* ====================
Seditio - Website engine
Copyright Neocrome
http://www.neocrome.net
[BEGIN_SED]
File=plugins/xsearch/xsearch.admin.php
Version=1.0.0
Updated=2009-jan-13
Type=Plugin
Author=Dmitry Lapin
Description=
[END_SED]
[BEGIN_SED_EXTPLUGIN]
Code=xsearch
Part=admin
File=xsearch.admin
Hooks=tools
Tags=
Order=10
[END_SED_EXTPLUGIN]
==================== */

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

require_once("plugins/xsearch/inc/xsearch.common.php");
$xsearch = new XTemplate('plugins/xsearch/tpl/xsearch.tpl');

$sql_tmp = sed_sql_query("SELECT config_value FROM $db_config WHERE config_name = 'xfulltext' LIMIT 1");
$installed = (sed_sql_numrows($sql_tmp)>0) ? $installed=true : $installed=false;

$act = sed_import('act', 'G', 'ALP', 7);

if ($act == 'index')
{
	$whom = sed_import('whom', 'G', 'ALP', 5);
	$start = sed_import('start', 'G', 'INT');
	if(empty($start)) { $start = 0; }
	
	// Ñëîâàðè íóæíî çàãðóçèòü èìåííî çäåñü,
	// ÷òîáû íå ãðóçèòü êàæäûé ðàç ïðè âûçîâå textToBaseFormWords
	//prep_dict_MEM(""); // äëÿ rumor
	$dict_bundle = new phpMorphy_FilesBundle("plugins/xsearch/inc/phpmorphy/dicts", "rus");
	$morphy = new phpMorphy($dict_bundle, array("storage" => PHPMORPHY_STORAGE_MEM, "with_gramtab" => false, "predict_by_suffix" => true, "predict_by_db" => true));
	$stemmer = new Lingua_Stem_Ru();
	
	switch ($whom)
	{
		case 'pages':
			$sql = sed_sql_query("SELECT SQL_CALC_FOUND_ROWS
						page_id, page_title, page_text
						FROM $db_pages
						WHERE 1 ORDER BY page_id
						LIMIT $start,$step");
			$items = sed_sql_query("SELECT FOUND_ROWS()");
			$items = sed_sql_result($items, 0, 0);
					
			while ($res = sed_sql_fetcharray($sql))
			{
				$title_index = textToBaseFormWords($res['page_title'], "selective");
				$title_index = implode(" ", $title_index);
				
				$text_index = textToBaseFormWords($res['page_text'], "selective");
				$text_index = implode(" ", $text_index);
				
				$sql2 = sed_sql_query("UPDATE $db_pages SET
						page_index = '".sed_sql_prep($text_index)."',
						title_index = '".sed_sql_prep($title_index)."'
						WHERE page_id='".$res['page_id']."'");
			}
			
			if ( ($items > $step) && ($items - $start >= $step) )
			{
				$start += $step;
				$xsearch-> assign(array(
					"WHOM" => $whom,
					"START" => $start,
					"NUM_INDEXED" => $start-1,
					"NUM_ALL" => $items
					));
				$xsearch->parse('XSEARCH.ADMIN.INDEXING');
			}
			else
			{
				$xsearch-> assign(array(
					'ADMIN_MESSAGE' => $L['xsearch_index_successful']
					));
				$xsearch->parse('XSEARCH.ADMIN.MESSAGE');
			}
			break;
		
		case 'forum':

			// ÈÍÄÅÊÑÀÖÈß ÑÎÎÁÙÅÍÈÉ
			$sql = sed_sql_query("SELECT SQL_CALC_FOUND_ROWS
						fp_id, fp_text
						FROM $db_forum_posts
						WHERE 1 ORDER BY fp_id
						LIMIT $start,$step");
			$items = sed_sql_query("SELECT FOUND_ROWS()");
			$items = sed_sql_result($items, 0, 0);
					
			while ($res = sed_sql_fetcharray($sql))
			{
				$words = textToBaseFormWords($res['fp_text'], "selective");
				$words = implode(" ", $words);
				
				$sql2 = sed_sql_query("UPDATE $db_forum_posts SET
						fp_index = '".sed_sql_prep($words)."'
						WHERE fp_id='".$res['fp_id']."'");
			}
			
			if ( ($items > $step) && ($items - $start >= $step) )
			{
				$start += $step;
				$xsearch-> assign(array(
					"WHOM" => $whom,
					"START" => $start,
					"NUM_ALL" => $items
					));
				$xsearch->parse('XSEARCH.ADMIN.INDEXING');
			}
			else
			{
				$xsearch-> assign(array(
					"WHOM" => "fttit",
					"START" => $items,
					"NUM_ALL" => $items
					));
				$xsearch->parse('XSEARCH.ADMIN.INDEXING');
			}
			break;	
		
		case 'fttit':
		
			//ÈÍÄÅÊÑÀÖÈß ÇÀÃÎËÎÂÊÎÂ ÒÅÌ ÔÎÐÓÌÀ
			$sql = sed_sql_query("SELECT ft_id, ft_title
						FROM $db_forum_topics
						WHERE 1 ORDER BY ft_id");
			$items = sed_sql_query("SELECT FOUND_ROWS()");
			$items = sed_sql_result($items, 0, 0);
					
			while ($res = sed_sql_fetcharray($sql))
			{
				$words = textToBaseFormWords($res['ft_title'], "selective");
				$words = implode(" ", $words);
				
				$sql2 = sed_sql_query("UPDATE $db_forum_topics SET
						ft_index = '".sed_sql_prep($words)."'
						WHERE ft_id='".$res['ft_id']."'");
			}
			
			$xsearch-> assign(array(
					'ADMIN_MESSAGE' => $L['xsearch_index_successful']
					));
				$xsearch->parse('XSEARCH.ADMIN.MESSAGE');
				
			break;
	}
	
}
else if ($act == 'install')
{
	
	if (sed_sql_numrows($sql_tmp)>0)
	{
		$xsearch-> assign(array(
				'ADMIN_MESSAGE' => $L['xsearch_install_already']
				));
			$xsearch->parse('XSEARCH.ADMIN.MESSAGE');
	}
	else
	{
		$sql_tmp = sed_sql_query("ALTER TABLE `$db_forum_posts` ADD `fp_index` TEXT NOT NULL");
		$sql_tmp = sed_sql_query("ALTER TABLE `$db_forum_posts` ADD FULLTEXT (`fp_index`)");
		$sql_tmp = sed_sql_query("ALTER TABLE `$db_forum_topics` ADD `ft_index` TEXT NOT NULL");
		$sql_tmp = sed_sql_query("ALTER TABLE `$db_forum_topics` ADD FULLTEXT (`ft_index`)");
		$sql_tmp = sed_sql_query("ALTER TABLE `$db_pages` ADD `page_index` TEXT NOT NULL");
		$sql_tmp = sed_sql_query("ALTER TABLE `$db_pages` ADD `title_index` TEXT NOT NULL");
		$sql_tmp = sed_sql_query("ALTER TABLE `$db_pages` ADD FULLTEXT (`page_index`)");
		$sql_tmp = sed_sql_query("ALTER TABLE `$db_pages` ADD FULLTEXT (`title_index`)");
						
		$sql_tmp = sed_sql_query("INSERT INTO $db_config (config_owner, config_cat, config_order, config_name, config_type, config_value, config_default, config_text) VALUES ('xxsearch', 'xxsearch', '1', 'xfulltext', '1', '1', '1', 'xxsearch')");
		
		$xsearch-> assign(array(
				'ADMIN_MESSAGE' => $L['xsearch_install_successful']
				));
		$xsearch->parse('XSEARCH.ADMIN.MESSAGE');
	}
}

$install = ($installed) ? "" : "<li><a href=\"admin.php?m=tools&p=xsearch&act=install\">".$L['xsearch_install']."</a></li>";
$xsearch->assign("INSTALL", $install);

$xsearch->parse('XSEARCH.ADMIN.MAIN');
$xsearch->parse('XSEARCH.ADMIN');	
$plugin_body = $xsearch->text('XSEARCH.ADMIN');
?>