<?php
/**
 * @package Flatnux_module_forum
 * @author Alessandro Vernassa <speleoalex@gmail.com>
 * @copyright Copyright (c) 2003-2005
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License
 * 
 */
defined('_FNEXEC') or die('Restricted access');
/**
 *
 * @global array $_FN
 * @param array $tosearch_array
 * @param string $method
 * @param array $sectionvalues
 * @param int $maxres
 * @return array 
 */
function FNSEARCH_module_forum($tosearch_array,$method,$sectionvalues,$maxres)
{

	global $_FN;
	$results=array();
	$count=0;
	
//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php",$sectionvalues['id']); //load config in section
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir_prefix=$config['forumdir'];
	if ($forumdir_prefix == "")
	{
		$forumdir_prefix=$sectionvalues['id'];
	}	
//--------------------config---------------------------------------------------<	
	
	$langs=$_FN['listlanguages'];
	if (count($langs) > 1)
		$multilanguage=true;
	else
		$multilanguage=false;
	$userlevel=FN_GetUserLevel($_FN['user']);
	$count=0;
	foreach ($langs as $l)
	{
		$forumdir = $forumdir_prefix;
		if ($multilanguage)
			$forumdir = $forumdir_prefix."_$l";
		$tablename=$forumdir . "_posts";
		$db=new XMLDatabase("fndatabase",$_FN['datadir']);
		$query="SELECT DISTINCT topicid,text FROM $tablename WHERE ";
		$m="";
		foreach ($tosearch_array as $q)
		{
			if ($q != "")
			{
				$query .= "$m ( text LIKE '%$q%' OR subject LIKE '%$q%' )";
				$m=$method;
			}
		}
		$posts=$db->Query($query);
		$table_forum_topics=new XMLTable("fndatabase",$forumdir . "_topics",$_FN['datadir']);
		if (is_array($posts))
		{
			$slang=$l;
			$tablearguments=new XMLTable("fndatabase",$forumdir . "_arguments",$_FN['datadir']);
			foreach ($posts as $th)
			{
				$topicid=$th['topicid'];
				$topic=$table_forum_topics->GetRecordByPk($topicid);
				if ($topic['hidden'] == 1 && !FN_IsAdmin())
					continue;
				//associo il livello del topic a quello dell' argomento
				$argumentvalues=$tablearguments->GetRecordByPk($topic['argument']);
				$topic['level']=$argumentvalues['level'];
				if ($topic['level'] == "")
					$topic['level']=-1;
				if ($topic['level'] == "")
					$topic['level']=-1;
				if ($topic['level'] > $userlevel)
					continue;
				$st="";
				if ($topic['hidden'] == 1)
					$st="style=\"text-decoration: line-through;\" ";

				$title = $topic['subject'] . " ". FN_GetDateTime($topic['time']);
				
				$charsetFrom = FN_i18n("_CHARSET",$l);
				$title = htmlentities($title,ENT_QUOTES,$charsetFrom) ;
				if ($multilanguage)
				{
					$link = FN_RewriteLink("index.php?mod={$sectionvalues['id']}&amp;op=readtopic&amp;topic=" . $topic['unirecid'] );
					$title .= "&nbsp;<img alt=\"$l\" height=\"10\" style=\"border:0px;\" src=\"" . FN_FromTheme("images/flags/$l.png") . "\" />";
				}
				else
				{
					$link = FN_RewriteLink("index.php?mod={$sectionvalues['id']}&amp;op=readtopic&amp;lang=$l&amp;topic=" . $topic['unirecid'] );
				}
				
				$results[$count]['link'] = $link;
				$results[$count]['title'] = $sectionvalues['title'].": ". $title;
				$results[$count]['text'] = substr(strip_tags($th['text']), 0, 100);
				$results[$count]['text'] =  htmlentities($results[$count]['text'],ENT_QUOTES,$charsetFrom) ;
			
				
				$count++;
			}
		}
	}
	return $results;
}
?>