<?
	# $Id: search.php3,v 1.12 2000/10/13 22:53:10 shevett Exp $
	#
	# search.php3 - does searches.
	#

	$scquery=db_query("select * from status");
	while ($row=db_fetch_array($scquery)) {
		$code = $row[flag];
		$value = $row[label];
		$scarray[$code] = $value;
	}

	# This search could be called from many locations.  Do
	# the search based on which dataset we're looking at.

	switch ($state[mdataset]) {
		case "slips" : {

			#
			# First run through and nab anywhere where the string 
			# matches in the 'problem' or 'summary' columns
			#
		
			$qstring = 	"select sid,problem,ctag,summary,status from slips where ".
									"(problem LIKE '%".$f_search."%' OR ".
									"ctag LIKE '%".$f_search."%' OR ".
									"summary LIKE '%".$f_search."%')";
			$q = db_query($qstring); 
			while ($results = db_fetch_array($q)) {
				$sidlist[$results[sid]] = $results[problem];
				$statlist[$results[sid]] = $results[status];
				$contlist[$results[sid]] = $results[ctag];
			}
		
			#
			# Now check out the followups.  Anyone in there?
			#
		
			$qstring = 	"select followups.f_sid,followups.f_text,".
									"slips.problem,slips.summary,slips.status,slips.ctag ".
									"from followups,slips where ".
									"followups.f_sid=slips.sid AND ".
									"(f_text LIKE '%".$f_search."%')";
			$q = db_query($qstring);
			while ($results = db_fetch_array($q)) {
				$sidlist[$results[f_sid]] = $results[problem];
				$statlist[$results[f_sid]] = $results[status];
				$contlist[$results[f_sid]] = $results[ctag];
			}
		
			$lastresult[0]=0;
			$lastresult[1]="Search found ".count($sidlist)." slips matching.";
			echo "<tr><td bgcolor=#bbbbbb align=center colspan=4>";
			echo "<h3>Search Results</h3><br>";
			echo "Your query <b>\" $f_search \"</b> on <b>Slips</b> returned <b>".count($sidlist)."</b> row(s).";
			echo "</td></tr>\n";
			echo "<tr bgcolor=#cccccc>";
			echo "<th align=left>Slip #</th>";
			echo "<th align=left>Status</th>";
			echo "<th align=left>Contact</th>";
			echo "<th align=left>Problem</th>";
			echo "</tr>\n";
			if (count($sidlist)) {
				ksort($sidlist);
				for(reset($sidlist); $key = key($sidlist); next($sidlist)) {
					echo "<tr bgcolor=#aaffaa>";
					echo "<td>";
					echo "<a href=\"nbrowse.php3?f_mdataset=slips&f_slipzoom=$key\">";
					echo $key."</a></td>";
					echo "<td>".$scarray[$statlist[$key]]."</td>";
					if ("$contlist[$key]") {
						echo "<td><a href=\"nbrowse.php3?f_mdataset=contacts&f_contact=$contlist[$key]&f_contzoom=$contlist[$key]&f_expand=1\">$contlist[$key]</td>";
					} else {
						echo "<td>&nbsp;</td>";
					}
					echo "<td>".$sidlist[$key]."</td>";
					echo "</tr>\n";
				}
			}
			break;;
		}
		case "resources" : {
			$qstring = 	"select r_tag,r_multi,r_serno,r_mctag,r_model,r_loc,r_desc,r_currtech,r_ctag from resources where ".
									"(r_tag LIKE '%${f_search}%' OR ".
									"r_serno LIKE '%${f_search}%' OR ".
									"r_mctag LIKE '%${f_search}%' OR ".
									"r_model LIKE '%${f_search}%' OR ".
									"r_loc LIKE '%${f_search}%' OR ".
									"r_desc LIKE '%${f_search}%' OR ".
									"r_currtech LIKE '%${f_search}%' OR ".
									"r_ctag LIKE '%${f_search}%') LIMIT 100";
			$q = db_query($qstring); 
			echo "<tr><td bgcolor=#bbbbbb align=center colspan=8>";
			echo "<b><font size=4>Search Results</font></b><br>";
			echo "Your query <b>\" $f_search \"</b> on <b>Resources</b> returned <b>".db_numrows($q)."</b> row(s).";
			echo "</td></tr>\n";
			echo "<tr bgcolor=#cccccc>";
			echo "<th align=left>Tag</th>";
			echo "<th align=left>Serno</th>";
			echo "<th align=left>Manuf</th>";
			echo "<th align=left>Model</th>";
			echo "<th align=left>Loc</th>";
			echo "<th align=left>Description</th>";
			echo "<th align=left>Tech</th>";
			echo "<th align=left>Contact</th>";
			echo "</tr>\n";
			while ($results = db_fetch_array($q)) {
				echo "<tr bgcolor=#aaffaa>";
				echo "<td>";
				if ($results[r_multi]) {
					echo "<a href=\"nbrowse.php3?f_mdataset=resources&f_resset=acc&f_resazoom=$results[r_tag]\">$results[r_tag]</a></td>";
				} else {
					echo "<a href=\"nbrowse.php3?f_mdataset=resources&f_resset=res&f_resrzoom=$results[r_tag]\">$results[r_tag]</a></td>";
				}
				echo "<td>$results[r_serno]</td>";
				echo "<td>$results[r_mctag]</td>";
				echo "<td>$results[r_model]</td>";
				echo "<td>$results[r_loc]</td>";
				echo "<td>$results[r_desc]</td>";
				echo "<td>$results[r_currtech]</td>";
				echo "<td>$results[r_ctag]</td>";
				echo "</tr>\n";
			}
		}
		case "contacts" : {
		$qstring = 	"select tag,name,company,comment from contacts where ".
								"(tag LIKE '%${f_search}%' OR ".
								"name LIKE '%${f_search}%' OR ".
								"company LIKE '%${f_search}%' OR ".
								"comment LIKE '%${f_search}%') limit 100 " ;
			$q = db_query($qstring); 
			echo "<tr><td bgcolor=#bbbbbb align=center colspan=3>";
			echo "<b><font size=4>Search Results</font></b><br>";
			echo "Your query <b>\" $f_search \"</b> on <b>Contacts</b> returned <b>".db_numrows($q)."</b> row(s).";
			echo "</td></tr>\n";
			echo "<tr bgcolor=#cccccc>";
			echo "<th align=left>Tag</th>";
			echo "<th align=left>Name</th>";
			echo "<th align=left>Company</th>";
			echo "</tr>\n";
			while ($results = db_fetch_array($q)) {
				echo "<tr bgcolor=#aaffaa>";
				echo "<td>";
				echo "<a href=\"nbrowse.php3?f_contact=$results[tag]&f_contzoom=$results[tag]&f_expand=1&f_stflag=M\">";
				echo "$results[tag]</a></td>";
				echo "<td>$results[name]</td>";
				echo "<td>$results[company]</td>";
				echo "</tr>\n";
			}
		}
}
?>
