<?php
/*=================================================
	Project: phpGedView
	File: search.php
	Author: John Finlay
	Comments:
		Parses gedcom file and searches based on user query.

	Change Log:
		6/6/02 - File Created
		6/14/02 - Fixed bug in general search where a hit in the last record
			of the gedcom file would send the program into and infinite loop
		7/3/02 - Modified soundex search to use the indilist.ind index
		7/8/02 - Changed to use the indexes in the general search
		1/8/03 - Added firstname to soundex search

	phpGedView: Genealogy Viewer
    Copyright (C) 2002 to 2003  John Finlay and Others

    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

===================================================*/
# $Id: search.php,v 1.90 2004/12/08 21:14:25 yalnifj Exp $

require("config.php");

// Remove slashes
if (isset($query)) {
	$myquery = $query;
	$query = stripslashes($query);
	$myquery = stripslashes($myquery);
}

if (!isset($action)) $action="";

if ($action=="general") {
	//-- perform the search
	if ((isset($query)) && ($query!="")) {
		if (strlen($query) == 1) $query = preg_replace(array("/\?/", "/\[/", "/\|/", "/\*/"), array("\\\?","\\\[","\\\|", "\\\\\*") , $query);
		$query = preg_replace("/\s+/", ".*", $query);

		// -- array of names
		$myindilist = array();
		$mysourcelist = array();
		$myfamlist = array();

		$myindilist = search_indis($query);
		uasort($myindilist, "itemsort");

		$mysourcelist = search_sources($query);
		uasort($mysourcelist, "itemsort");
		reset($mysourcelist);

		$myfamlist = search_fams($query);
		uasort($myfamlist, "itemsort");
		reset($myfamlist);
		
		//-- if only 1 item is returned, automatically forward to that item
		if ((count($myindilist)==1)&&(count($myfamlist)==0)&&(count($mysourcelist)==0)) {
			foreach($myindilist as $pid=>$indi) {
				header("Location: individual.php?pid=".$pid."&ged=".$indi["file"]);
				exit;
			}
		}
		if ((count($myindilist)==0)&&(count($myfamlist)==1)&&(count($mysourcelist)==0)) {
			foreach($myfamlist as $famid=>$fam) {
				header("Location: family.php?famid=".$famid."&ged=".$fam["file"]);
				exit;
			}
		}
		if ((count($myindilist)==0)&&(count($myfamlist)==0)&&(count($mysourcelist)==1)) {
			foreach($mysourcelist as $sid=>$source) {
				header("Location: source.php?sid=".$sid."&ged=".$source["file"]);
				exit;
			}
		}
	}
}

if ($action=="soundex") {
	if ((!empty($lastname))||(!empty($firstname))||(!empty($place))) {
		$indilist = get_indi_list();
		$sindilist = array();
		// -- only get the names who match soundex
		foreach ($indilist as $key => $value) {
			$save = false;
			$value["name"] = check_NN($value["name"]);
			$names = preg_split("/,/", $value["name"]);
			$savel=false;
			if (!empty($lastname)) {
				$names[0] = preg_replace("/ [jJsS][rR]\.?/", "", $names[0]);
				if (soundex($names[0])==soundex($lastname)) $savel = true;
				if ($savel) $save=true;
			}
			$savef=false;
			if (!empty($firstname)) {
				$firstnames = preg_split("/\s/", trim($firstname));
				if (isset($names[1])) $fnames = preg_split("/\s/", trim($names[1]));
				else $fnames = preg_split("/\s/", trim($names[0]));
				for($i=0; $i<count($fnames); $i++) {
					for($j=0; $j<count($firstnames); $j++) {
						if (soundex($fnames[$i])==soundex($firstnames[$j])) $savef = true;
					}
				}
				if (($savel==true || empty($lastname)) && $savef==true) $save = true;
				else $save = false;
			}
			if ((!empty($place))||(!empty($year))) {
				$indirec = find_person_record($key);
				if (!empty($place)) {
					$savep=false;
					$pt = preg_match_all("/\d PLAC (.*)/i", $indirec, $match, PREG_PATTERN_ORDER );
					if ($pt>0) {
						$places = array();
						for ($pp=0; $pp<count($match[1]); $pp++){
							$places[$pp] =preg_split("/,\s/", trim($match[1][$pp]));
						}
						$cp=count($places);
						for($p=0; $p<$cp; $p++) {
							for($pp=0; $pp<count($places[$p]); $pp++) {
								if (soundex(trim($places[$p][$pp]))==soundex(trim($place))) $savep = true;
							}
						}
					}
					if (($savel==true || empty($lastname)) && ($savef==true || empty($firstname)) && $savep==true) $save = true;
					else $save = false;
				}
				if (!empty($year) && $save==true) {
					$yt = preg_match("/\d DATE (.*$year.*)/i", $indirec, $match);
					if ($yt==0) $save = false;
				}
			}
			if ($save) $sindilist["$key"] = $value;
		}

		uasort($sindilist, "itemsort");
		reset($sindilist);

		//-- if only 1 item is returned, automatically forward to that item
		if (count($sindilist)==1) {
			foreach($sindilist as $pid=>$indi) {
				header("Location: individual.php?pid=".$pid."&ged=".$indi["file"]);
				exit;
			}
		}
	}
}

print_header($pgv_lang["search"]);
?>
<script language="JavaScript" type="text/javascript">
<!--
	function checknames(frm) {
		if (frm.year.value!="") {
			message=true;
			if (frm.firstname.value!="") message=false;
			if (frm.lastname.value!="") message=false;
			if (frm.place.value!="") message=false;
			if (message) {
				alert("<?php print $pgv_lang["invalid_search_input"]?>");
				frm.firstname.focus();
				return false;
			}
		}
		return true;
	}
//-->
</script>
<?php
print "\n\t<table class=\"list_table, $TEXT_DIRECTION\" width=\"100%\" border=\"0\"><tr><td style=\"padding: 10px;\" valign=\"top\" width=\"50%\">\n\t\t";
	print "\n\t<h2>".$pgv_lang["search_gedcom"]."</h2>\n\t";
	print "<form method=\"get\" action=\"search.php\">";
	print "<table class=\"list_table\"><tr><td class=\"list_label\" style=\"padding: 5px;\">";
	print $pgv_lang["enter_terms"];
	print "</td><td class=\"list_value\" style=\"padding: 5px;\">";
	print "<input type=\"hidden\" name=\"action\" value=\"general\" />";
	print "<input tabindex=\"1\" type=\"text\" name=\"query\" value=\"";
	if ($action=="general" && isset($myquery)) print $myquery;
	else print "";
	print "\" />";
	print_help_link("search_enter_terms_help", "qm");
	print "</td><td class=\"list_value\" style=\"vertical-align: middle; text-align: center; padding: 5px;\">";
	print "<input tabindex=\"2\" type=\"submit\" value=\"".$pgv_lang["search"]."\" />";
	print "</td></tr></table>";
	print "</form>";
	print "</td>";
	print "<td style=\"padding: 10px;\">";
	print "<form method=\"get\" onsubmit=\""?>return checknames(this);<?php print " \" action=\"search.php\">"; 
	print "<input type=\"hidden\" name=\"action\" value=\"soundex\" />";
	print "<table class=\"list_table\"><tr><td colspan=\"3\">";
	print "<span class=\"label\">".$pgv_lang["soundex_search"]."</span>";
	print_help_link("soundex_search_help", "qm");
	print "</td></tr><tr><td class=\"list_label\" width=\"35%\">";
	print $pgv_lang["firstname_search"];
	print "</td><td class=\"list_value\">";
	print "<input tabindex=\"3\" type=\"text\" name=\"firstname\" value=\"";
	if ($action=="soundex") print $firstname;
	print "\" />";
		
	print "</td><td class=\"list_value\" style=\"vertical-align: middle; text-align: center; padding: 5px;\" rowspan=\"4\">";
	print "<input tabindex=\"7\" type=\"submit\" value=\"";
	print $pgv_lang["search"];
	print "\" />";
	
	print "</td></tr><tr><td class=\"list_label\">";
	print $pgv_lang["lastname_search"];
	print "</td><td class=\"list_value\"><input tabindex=\"4\" type=\"text\" name=\"lastname\" value=\"";
	if ($action=="soundex") print $lastname;
	print "\" /></td></tr>";
	print "<tr><td class=\"list_label\">";
	print $pgv_lang["search_place"];
	print "</td><td class=\"list_value\"><input tabindex=\"5\" type=\"text\" name=\"place\" value=\"";
	if ($action=="soundex") print $place;
	print "\" /></td></tr>";
	print "<tr><td class=\"list_label\">";
	print $pgv_lang["search_year"];
	print "</td><td class=\"list_value\"><input tabindex=\"6\" type=\"text\" name=\"year\" value=\"";
	if ($action=="soundex") print $year;
	print "\" /></td>";
	print "</tr></table>";
print "</form>";
print "</td></tr></table>";


// ---- section to search and display results on a general keyword search
if ($action=="general") {
	if ((isset($query)) && ($query!="")) {
		print "<br />";
		print "\n\t<div class=\"center\"><table class=\"list_table $TEXT_DIRECTION\">\n\t\t<tr>";
		if (count($myindilist)>0) print "<td class=\"list_label\"><img src=\"".$PGV_IMAGE_DIR."/".$PGV_IMAGES["indis"]["large"]."\" border=\"0\" width=\"25\" alt=\"\" /> ".$pgv_lang["people"]."</td>";
		if (count($myfamlist)>0) print "<td class=\"list_label\"><img src=\"".$PGV_IMAGE_DIR."/".$PGV_IMAGES["sfamily"]["large"]."\" border=\"0\" width=\"25\" alt=\"\" /> ".$pgv_lang["families"]."</td>";
		if (count($mysourcelist)>0) print "<td class=\"list_label\"><img src=\"".$PGV_IMAGE_DIR."/".$PGV_IMAGES["source"]["large"]."\" border=\"0\" width=\"25\" alt=\"\" /> ".$pgv_lang["sources"]."</td>";
		print "</tr>\n\t\t<tr>";
		$i=0;
		$cti=count($myindilist);
		if ($cti>0) {
			$indi_private=0;
			$indi_hide=0;
			print "<td class=\"list_value_wrap\"><ul>";
			foreach ($myindilist as $key => $value) {
				$value["name"] = check_NN($value["name"]);
				//-- make sure that the data that was searched on is not in a private part of the record
				if (!displayDetailsById($key) && showLivingNameById($key)) {
					//-- any record that is not a FAMC, FAMS is private
					$record = get_sub_record(1, "1 NAME", $value["gedcom"]);
					if (preg_match("/".$query."/i", $record)>0) print_list_person($key, array($value["name"], $value["file"]));
					else $indi_hide++;
				}
		    	else print_list_person($key, array($value["name"], $value["file"]));
		    	print "\n";
		    	$i++;
			}
			print "\n\t\t</ul></td>";
		}
		$i=0;
		$ctf=count($myfamlist);
		if ($ctf>0) {
			$fam_private=0;
			$fam_hide=0;
			print "\n\t\t<td class=\"list_value_wrap\"><ul>";
			foreach ($myfamlist as $key => $value) {
				$name = preg_replace(array("/ [jJsS][rR]\.?,/", "/ I+,/","/^[a-z. ]*/"), array(",",",",""), $value["name"]);
				$names = preg_split("/[,+]/", $name);
				$value["name"] = check_NN($names);
			    print_list_family($key, array($value["name"], $value["file"]));
			    $i++;
			}
			print "\n\t\t</ul></td>";
		}
		$i=0;
		$cts=count($mysourcelist);
		if ($cts>0) {
			print "\n\t\t<td class=\"list_value_wrap\"><ul>";
			foreach ($mysourcelist as $key => $value) {
				print "<li type=\"circle\">";
			    print "\n\t\t\t<a href=\"source.php?sid=$key&amp;ged=".$value["file"]."\"><span class=\"list_item\">".$value["name"]."</span></a>\n";
			    print "</li>\n";
			    $i++;
			}
			print "\n\t\t</ul></td>";
		}
		print "</tr><tr>\n\t";
		if ($cti > 0 || $cts > 0 || $ctf > 0) {
			if ($cti > 0) {
				$cti -= $indi_hide;
				print "<td>".$pgv_lang["total_indis"]." ".$cti;
				if ($indi_private>0) print "  (".$pgv_lang["private"]." ".$indi_private.")";
				if ($indi_hide>0) print "  --  ".$pgv_lang["hidden"]." ".$indi_hide;
				if ($indi_private>0 || $indi_hide>0) print_help_link("privacy_error_help", "qm");
				print "</td>";
			}
			if ($ctf > 0) {
				$ctf -= $fam_hide;
				print "<td>".$pgv_lang["total_fams"]." ".$ctf;
				if ($fam_private>0) print "  (".$pgv_lang["private"]." ".$fam_private.")";
				if ($fam_hide>0) print "  --  ".$pgv_lang["hidden"]." ".$fam_hide;
				if ($fam_private>0 || $fam_hide>0) print_help_link("privacy_error_help", "qm");
				print "</td>";
			}
			if ($cts > 0) print "<td>".$pgv_lang["total_sources"]." ".$cts."</td>";
			if ($cti > 0 || $cts > 0 || $ctf > 0) print "</tr>\n\t";
		}
		else print "<td class=\"warning\" style=\" text-align: center;\"><i>".$pgv_lang["no_results"]."</i><br /></td></tr>\n\t\t";
		print "</table></div>";
	}
	else print "<br /><div class=\"warning\" style=\" text-align: center;\"><i>".$pgv_lang["no_results"]."</i><br /></div>\n\t\t";
}

// ----- section to search and display results for a Soundex last name search
if ($action=="soundex") {
	if ((!empty($lastname))||(!empty($firstname)) ||(!empty($place))) {
//		print "<div class=\"center\"><span class=\"label\">".$pgv_lang["search_results"]."</span>";
		print "<div class=\"center\"><br />";
		print "\n\t<table class=\"list_table $TEXT_DIRECTION\">\n\t\t<tr>\n\t\t";
		$i=0;
		$ct=count($sindilist);
		if ($ct > 0) {
			$indi_private=0;
			$indi_hide=0;
			print "<td colspan=\"2\" class=\"list_label\"><img src=\"".$PGV_IMAGE_DIR."/".$PGV_IMAGES["indis"]["large"]."\" border=\"0\" width=\"25\" alt=\"\" /> ".$pgv_lang["people"]."</td></tr>";
			print "<tr>\n\t\t<td class=\"list_value_wrap\"><ul>";
		
			foreach ($sindilist as $key => $value) {
				print_list_person($key, array($value["name"], $value["file"]));
				print "\n";
				if ($i == floor($ct / 2) && $ct>9) print "\n\t\t</ul></td>\n\t\t<td class=\"list_value_wrap\"><ul>";
				$i++;
			}
			print "\n\t\t</ul></td>\n\t\t</tr>\n\t";
			if ($i>0) {
				print "<tr><td ";
				if ($ct > 9) print "colspan=\"2\">";
				else print ">";
				$ct -= $indi_hide;
				print $pgv_lang["total_indis"]." ".$ct;
				if ($indi_private>0) print "  (".$pgv_lang["private"]." ".$indi_private.")";
				if ($indi_hide>0) print "  --  ".$pgv_lang["hidden"]." ".$indi_hide;
				if ($indi_private>0 || $indi_hide>0) print_help_link("privacy_error_help", "qm");
				print "</td></tr>";
			}
		}
		else print "<td class=\"warning\" style=\" text-align: center;\"><i>".$pgv_lang["no_results"]."</i></td></tr>\n\t\t";
		print "</table></div>";
	}
	else print "<br /><div class=\"warning\" style=\" text-align: center;\"><i>".$pgv_lang["no_results"]."</i><br /></div>\n\t\t";
}
print "<br /><br /><br />";
print_footer();
?>
