<?php
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //
$xoopsOption['pagetype'] = "search";

include("mainfile.php");
include_once(XOOPS_ROOT_PATH."/class/xoopsmodule.php");
include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");

$action = isset($HTTP_POST_VARS['action']) ? trim($HTTP_POST_VARS['action']) : "search";
$query = isset($HTTP_POST_VARS['query']) ? trim($HTTP_POST_VARS['query']) : "";
$andor = isset($HTTP_POST_VARS['andor']) ? trim($HTTP_POST_VARS['andor']) : "AND";
$mids = isset($HTTP_POST_VARS['mids']) ? $HTTP_POST_VARS['mids'] : array();
$mid = isset($HTTP_POST_VARS['mid']) ? intval($HTTP_POST_VARS['mid']) : 0;
$start = isset($HTTP_POST_VARS['start']) ? intval($HTTP_POST_VARS['start']) : 0;

if ( $action == "results" && $query == "" ) {
	redirect_header("search.php",1,_SR_PLZENTER);
	exit();
}

if ( $action == "showall" && ($query == "" || empty($mid)) ) {
	redirect_header("search.php",1,_SR_PLZENTER);
	exit();
}

if ( $andor != "OR" && $andor != "exact" && $andor != "AND" ) {
	$andor = "AND";
}

if ( $action == "results" ) {
	$myts =& MyTextSanitizer::getInstance();
	if ( !is_array($mids) || count($mids) == 0 ) {
		$mids = array();
		$mids = XoopsModule::getHasSearchModulesList(false);
	}
	if ( $andor != "exact" ) {
		$queries = split(" ",$myts->oopsAddSlashes($query));
	} else {
		$queries = array($myts->oopsAddSlashes($query));
	}
	include(XOOPS_ROOT_PATH."/header.php");
	OpenTable();
	echo "<h3>"._SR_SEARCHRESULTS."</h3>\n";
	foreach ( $mids as $mid ) {
		$mid = intval($mid);
		$module = new XoopsModule($mid);
		$results = array();
		$results = $module->search($queries, $andor, 5, 0);
		echo "<p><h4>".$module->name()."</h4>";
		$count = count($results);
		if ( !is_array($results) || $count == 0 ) {
			echo _SR_NOMATCH."</p>";
		} else {
			for ( $i = 0; $i < $count; $i++ ) {
				if ( isset($results[$i]['image']) || $results[$i]['image'] != "" ) {
					echo "<img src='modules/".$module->dirname()."/".$results[$i]['image']."' alt='".$module->name()."' />&nbsp;";
				} else {
					echo "<img src='images/icons/posticon.gif' alt='".$module->name()."' width='14' height='11' />&nbsp;";
				}
				echo "<b><a href='modules/".$module->dirname()."/".$results[$i]['link']."'>".$myts->makeTboxData4Show($results[$i]['title'])."</a></b><br />\n";
				echo "<small>";
				$results[$i]['uid'] = intval($results[$i]['uid']);
				if ( !empty($results[$i]['uid']) ) {
					$uname = XoopsUser::getUnameFromId($results[$i]['uid']);
					echo "&nbsp;&nbsp;<a href='".XOOPS_URL."/userinfo.php?uid=".$results[$i]['uid']."'>".$uname."</a>\n";
				}
				echo $results[$i]['time'] ? " (". formatTimestamp(intval($results[$i]['time'])).")" : "";
				echo "</small><br />\n";
			}
			if ( $count == 5 ) {
				echo "<br /><form action='search.php' method='post' name='showall".$mid."'>
				<input type='hidden' value='".$myts->makeTboxData4PreviewInForm($query)."' name='query' />
				<input type='hidden' name='mid' value='".$mid."' />
				<input type='hidden' name='action' value='showall' />
				<input type='hidden' name='andor' value='".$andor."' />
				<a href='#$mid' onclick=\"javascript:document.showall".$mid.".submit();\">"._SR_SHOWALLR."</a></form></p>";
			}
		}
	}
	CloseTable();
}

if ( $action =="search" || $action =="results" ) {
	if ( $action == "search" ) {
		include(XOOPS_ROOT_PATH."/header.php");
	}
	OpenTable();
	include("include/searchform.php");
	$search_form->display();
	CloseTable();
}

if ( $action == "showall" ) {
	$myts =& MyTextSanitizer::getInstance();
	if ( $andor != "exact" ){
		$queries = split(" ",$myts->oopsAddSlashes($query));
	} else {
		$queries = array($myts->oopsAddSlashes($query));
	}
	include(XOOPS_ROOT_PATH."/header.php");
	$module = new XoopsModule($mid);
	$results = array();
	$results = $module->search($queries, $andor);
	$count = count($results);
	if ( $count - $start > 20 ) {
		$limit = $start + 20;
	} else { 
		$limit = $count;
	}
	OpenTable();
	echo "<h3>"._SR_SEARCHRESULTS."</h3>\n";
	printf(_SR_FOUND,$count);
	echo "<br />";
	printf(_SR_SHOWING, $start+1, $limit);
	echo "<p><h4>".$module->name()."</h4>";
	for ( $i = $start; $i < $limit; $i++ ) {
		if ( isset($results[$i]['image']) && $results[$i]['image'] != "" ) {
			echo "<img src='modules/".$module->dirname()."/".$results[$i]['image']."' alt='".$module->name()."' />&nbsp;";
		} else {
			echo "<img src='images/icons/posticon.gif' alt='".$module->name()."' width='14' height='11' />&nbsp;";
		}
		echo "<b><a href='modules/".$module->dirname()."/".$results[$i]['link']."'>".$myts->makeTboxData4Show($results[$i]['title'])."</a></b><br />\n";
		echo "<small>";
		$results[$i]['uid'] = intval($results[$i]['uid']);
		if ( !empty($results[$i]['uid']) ) {
			$uname = XoopsUser::getUnameFromId($results[$i]['uid']);
			echo "&nbsp;&nbsp;<a href='".XOOPS_URL."/userinfo.php?uid=".$results[$i]['uid']."'>".$uname."</a>\n";
		}
		echo $results[$i]['time'] ? " (". formatTimestamp(intval($results[$i]['time'])).")" : "";
		echo "</small><br />\n";
	}
	echo "</p><table><tr>";
	if ( $start > 0 ) {
		$prev = $start - 20;
		echo "<td align='left'><form action='search.php' method='post' name='showprev'>
		<input type='hidden' value='".$myts->makeTboxData4PreviewInForm($query)."' name='query' />
		<input type='hidden' name='mid' value='".$mid."' />
		<input type='hidden' name='action' value='showall' />
		<input type='hidden' name='andor' value='".$andor."' />
		<input type='hidden' name='start' value='".$prev."' />
		<a href='#$prev' onclick=\"javascript:document.showprev.submit();\">"._SR_PREVIOUS."</a></form></td>\n";
	}
	echo "<td>&nbsp;&nbsp;</td>";
	if ( $count - $start > 20 ) {
		$next = $start + 20;
		echo "<td align='right'><form action='search.php' method='post' name='shownext'>
		<input type='hidden' value='".$myts->makeTboxData4PreviewInForm($query)."' name='query' />
		<input type='hidden' name='mid' value='".$mid."' />
		<input type='hidden' name='action' value='showall' />
		<input type='hidden' name='andor' value='".$andor."' />
		<input type='hidden' name='start' value='".$next."' />
		<a href='#$next' onclick=\"javascript:document.shownext.submit();\">"._SR_NEXT."</a></form></td>\n";
	}
	echo "</tr></table><p>\n";
	include("include/searchform.php");
	$search_form->display();
	echo "</p>";
	CloseTable();
}
	
include(XOOPS_ROOT_PATH."/footer.php");

?>