<?php
	/*
	** File: search_results
	** Description: results of a search
	** Version: $Revision: 1.13 $
	** Created: 06/10/1999
	** Author: Leon Atkinson, Shannon -jj Behrens
	** Email: leon@clearink.com, shannon.behrens@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 2000/02/02 18:59:50 $
	**
	** Log : $Log: search_results,v $
	** Log : Revision 1.13  2000/02/02 18:59:50  leon
	** Log : Fixing bugs reported by Ted Henigson.
	** Log :
	** Log : Revision 1.12  1999/11/08 22:32:30  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.11  1999/11/04 21:35:45  leon
	** Log : Cleaned up the use of paths.  Paths are now all set in index.php3 as
	** Log : constants.  Example settings for Apache/Unix, Apache/Win32, and IIS.
	** Log :
	** Log : Revision 1.10  1999/10/13 22:19:08  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.9  1999/10/05 22:47:37  leon
	** Log : Added protection for the case when a screen appears in ScreenInfo, but
	** Log : doesn't exist on the disk.
	** Log :
	** Log : Revision 1.8  1999/09/09 20:34:20  leon
	** Log : Added support for an index-based search.
	** Log : Constants in global_settings controls whether the index is used or
	** Log : not.  You can build/re-build the index through a link on the
	** Log : admin menu.
	** Log :
	** Log : Revision 1.7  1999/08/18 22:32:22  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.6  1999/08/03 20:01:24  leon
	** Log : Updated search functionality
	** Log : You can now search on screens.  A constant controls whether you want
	** Log : to pull screen modules into memory to search on the contents.  Otherwise
	** Log : the search is only made on the meta tags as defined in ScreenInfo.
	** Log :
	** Log : Revision 1.5  1999/07/30 19:09:42  jj
	** Log : Used tokenizing for advanced searches, like was already done for simple searches.
	** Log :
	** Log : Revision 1.4  1999/07/28 19:27:34  leon
	** Log : Mass update based on code donation from a live project
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	//time before search
	print("\n\n<!-- ".microtime()."-->\n\n");


	$MatchedSomething = FALSE;
	$SearchResultsOkay = TRUE;


	if($SearchType == 'simple')
	{
		//use keywords for all the fields in the advance form
		$inputScreenKeywords = $inputKeywords;
		$inputDescription = $inputKeywords;
		$inputName = $inputKeywords;
	}


	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ functions


	
	/* 
	** Function: outputMatch
	** Input: $itemID, $itemName, $itemDescription, [$itemThumbnail]
	** Output: None (uses print)
	** Description: Once a matching item has been found, you may call this 
	**   function to output the matching item's details. 
	*/
	function outputMatch($itemID, $itemName, $itemDescription, 
		$itemThumbnail="")
	{
		print("<A HREF=\"" . ScreenURL("item") . 
			"&item=$itemID\">");
		print("<B>" . prepareText($itemName) . "</B>");
		print("</A>");
		print("<BR>\n");
		/* Output the thumbnail, if one exists. */ 
		if($itemThumbnail != "")
		{
			$thumbnailPath = "images/items/$itemThumbnail";
			if( file_exists($thumbnailPath) )
			{
				$imageSize = getimagesize($thumbnailPath);
				print("<A HREF=\"" . ScreenURL("item") . 
					"&item=$itemID\">");
				print("<IMG SRC=\"$thumbnailPath\" " . 
					"$imageSize[3] BORDER=\"0\" ALIGN=\"left\" " . 
					"VSPACE=\"10\" HSPACE=\"10\">\n");	
				print("</A>");
			}
		}
		print($itemDescription);
		print("<BR>\n<BR CLEAR=\"all\">\n");
	}



	/*
	** Function: search
	** Input: String $searchIn, String $searchForList (comma or space separated
	**   list of words).
	** Output: Boolean
	** Description: Tokenize $searchForList, and see if any of the words in 
	**   $searchForList are in $searchIn.  If so, return true.  Otherwise, 
	**   return false. Ignores letter case as well as punctuation.
	*/
	function search($searchIn, $searchForList)
	{
		for ($token = strtok($searchForList, ", ") ; 
			$token != "" ; 
			$token = strtok(", ") )
		{
			/* Strip bad characters. */
			$token = eregi_replace("[^A-Z]", "", $token);
			
			/* See if we have a match. */
			if(($token != "") AND (eregi($token, $searchIn)))
			{
				return TRUE;
			}
		}
		return FALSE;
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main



	/*
	** Since we'll be using eregi for search functionality, we might as well
	** just go ahead a retrieve all the items from the database.
	*/
	$Query = "SELECT ID, Name, Description, Thumbnail, Keywords ";
	$Query .= "FROM item ";
	$Query .= "WHERE Active = 'Y' ";
	$Query .= "ORDER BY DisplayPrecedence, Name ";

	/* Execute the query. */
	if ( !($DatabaseResult = mysql_query($Query, $DatabaseLink)) )
	{
		print("<!-- " . mysql_errno() . ": " . mysql_error() . 
			"   Query was: " . $Query . " -->\n");
		print("Unable to query the database. Please check the html comments for debugging information.<BR>\n");
		$SearchResultsOkay = FALSE;
	}
	else 
	{
		/*
		** This probably shouldn't go wrong, but just in case: check to 
		** make sure there is something in the table.
		*/
		if ( !mysql_num_rows($DatabaseResult) )
		{	
			print("The \"item\" table appears to be empty. Check the html comments for debugging information.<BR>\n");
			print("<!-- Here's the query: $Query -->\n");
			$SearchResultsOkay = FALSE;
		}
	}


	while($DatabaseRow = mysql_fetch_row($DatabaseResult))
	{
		list ($item_ID, $item_Name, $item_Description, $item_Thumbnail,
			$item_Keywords) = $DatabaseRow;

		if ( search($item_Name, $inputName) ||
			search($item_Description, $inputDescription) ||
			search($item_Keywords, $inputKeywords) )
		{
			$MatchedSomething = TRUE;
			outputMatch($item_ID, $item_Name, $item_Description, 
				$item_Thumbnail);
		}
		/* else, just move on to the next item. */
	}



	if(SEARCH_INDEX)
	{

		include(APPLICATION_ROOT . "/modules/include/ScreenIndex");
	
		/*
		** Loop through keywords submitted from search_form
		** If we recognize a keyword, save the screens as matches
		*/
		for($keyword = strtok($inputScreenKeywords, ", "); 
			$keyword != ""; 
			$keyword = strtok(", ") )
		{
			//Strip bad characters
			$keyword = eregi_replace("[^A-Z]", "", $keyword);
	
			// See if we recognize this keyword
			if(is_array($ScreenIndex[$keyword]))
			{
				//we do, add screen to list of matches
				for(reset($ScreenIndex[$keyword]); 
					$screen = key($ScreenIndex[$keyword]); 
					next($ScreenIndex[$keyword]))
				{
					$Match[$screen] += $ScreenIndex[$keyword][$screen];
				}
			}
	
		}
	
		if(is_array($Match))
		{
			$MatchedSomething = TRUE;
		
			for(reset($Match); $screen=key($Match); next($Match))
			{
				print("(" . $Match[$screen] . ")");
				print("<A HREF=\"".ScreenURL($screen)."\">");
				print($ScreenInfo[$screen][0]);
				print("</A><BR>\n");
				print($ScreenInfo[$screen][1] . "");
				print("<BR>\n");
				print("<BR>\n");
			}
		}

	
	}
	else
	{

		/*
		** Loop through ScreenInfo
		*/
		$tempContent = "";
		for(reset($ScreenInfo); $key = key($ScreenInfo); next($ScreenInfo))
		{
			if(SEARCH_SCREENS AND (file_exists(APPLICATION_ROOT . "/modules/screens/$key")))
			{
				//grab file, put it in one variable
				$tempContent = file(APPLICATION_ROOT . "/modules/screens/$key");
				$tempContent = implode(" ", $tempContent);		
			}
			
			if(search($ScreenInfo[$key][1], $inputScreenKeywords) OR
				search($ScreenInfo[$key][2], $inputScreenKeywords) OR
				search($tempContent, $inputScreenKeywords)
				)
			{
				print("<A HREF=\"".ScreenURL($key)."\">");
				print($ScreenInfo[$key][0]);
				print("</A><BR>\n");
				print($ScreenInfo[$key][1] . "<BR>\n");
				print("<BR>\n");
	
				$MatchedSomething = TRUE;
			}
	
		}
	}


	/* 
	** If the query was valid, but we had no decent search results, tell the
	** user so.
	*/
	if ($SearchResultsOkay && !$MatchedSomething)
	{
		print("<I>" . L_SEARCHRESULTS_NOMATCHESFOUND . "</I><BR>\n");
	}

	print("<A HREF=\"".ScreenURL('search_form')."\">");
	print(L_SEARCHRESULTS_TRYANOTHERSEARCH);
	print("</A><BR>\n");


	//time after search
	print("\n\n<!-- ".microtime()."-->\n\n");
?>
