<?php
/*=================================================
	Project: phpGedView
	File: placelist.php
	Author: John Finlay
	Comments:
		Parses gedcom file and displays a place hierachy.

	Change Log:
		6/26/02 - File Created
		9/25/02 - Added support for language files
		10/18/02 - Rebuilt page to work better.
		12/27/02 - Made case insensitive

	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: placelist.php,v 1.89 2004/12/04 17:49:46 eikland Exp $

require("config.php");

function case_in_array($value, $array) {
	foreach($array as $key=>$val) {
		if (strcasecmp($value, $val)==0) return true;
	}
	return false;
}

if (empty($action)) $action = "find";
if (empty($display)) $display = "hierarchy";

if ($display=="hierarchy") print_header($pgv_lang["place_list"]);
else print_header($pgv_lang["place_list2"]);

print "\n\t<div class=\"center\">";
if ($display=="hierarchy") print "<h2>".$pgv_lang["place_list"]."</h2>\n\t";
else print "<h2>".$pgv_lang["place_list2"]."</h2>\n\t";

if (!isset($parent)) $parent=array();
// Remove slashes
foreach ($parent as $p => $child){
	$parent[$p] = stripslashes($child);
}

if (!isset($level)) {
	$level=0;
}

//-- extract the place form encoded in the gedcom
$header = find_gedcom_record("HEAD");
$hasplaceform = strpos($header, "1 PLAC");

//-- hierarchical display
if ($display=="hierarchy") {
//	print "<br /><br />";
	// -- array of names
	$placelist = array();
	$positions = array();
	$numfound = 0;
	get_place_list();
	// -- sort the array
	uasort($placelist, "itemsort");
	
	//-- create a query string for passing to search page
	$tempparent = array_reverse($parent);
	if (count($tempparent)>0) $squery = "&query=".urlencode($tempparent[0]);
	else $squery="";
	for($i=1; $i<$level; $i++) {
		$squery.=", ".urlencode($tempparent[$i]);
	}
	
	//-- if the number of places found is 0 then automatically redirect to search page
	if ($numfound==0) {
		$action="show";
	}
		
	// -- print the breadcrumb hierarchy
	$numls=0;
	if ($level>0) {
		//-- link to search results
		if ((($level>1)||($parent[0]!=""))&&($numfound>0)) {
			print $numfound."  ".$pgv_lang["connections"].": ";
	//		print_help_link("ppp_numfound_help", "qm");
	//		print "<br /><br />";
		}
		//-- breadcrumb
		$numls = count($parent)-1;
		$num_place="";
		//-- place and page text orientation is opposite -> top level added at the beginning of the place text
		print "<a href=\"placelist.php?level=0\">";
		if ($numls>=0 && (($TEXT_DIRECTION=="ltr" && hasRtLText($parent[$numls])) || ($TEXT_DIRECTION=="rtl" && !hasRtLText($parent[$numls])))) print $pgv_lang["top_level"].", ";
//		if ($numls>=0 && $TEXT_DIRECTION=="ltr" && (ord(substr($parent[$numls],0,2))=="215" || ord(substr($parent[$numls],strlen(trim($parent[$numls]))-2,2))=="215")) print $pgv_lang["top_level"].", ";
//	    else if ($numls>=0 && $TEXT_DIRECTION=="rtl" &&  ord(substr($parent[$numls],0,2))!="215" && ord(substr($parent[$numls],strlen(trim($parent[$numls]))-2,2))!="215")  print $pgv_lang["top_level"].", ";
		print "</a>";
	    for($i=$numls; $i>=0; $i--) {
			print "<a href=\"placelist.php?level=".($i+1)."&amp;";
			for ($j=0; $j<=$i; $j++) {
				$levels = preg_split ("/,/", trim($parent[$j]));
				// Routine for replacing ampersands
				foreach($levels as $pindex=>$ppart) {
					$ppart = urlencode($ppart);
					$ppart = preg_replace("/amp\%3B/", "", trim($ppart));
					print "&amp;parent[$j]=".$ppart;
				}
			}
//			if (ord(substr($parent[$i],0,2))=="215" || ord(substr($parent[$i],strlen(trim($parent[$i]))-2,2))=="215") print "\">&rlm;".$parent[$i]."&rlm;</a>";
 			print "\">".PrintReady($parent[$i])."</a>"; 
 			if ($i>0) print ", ";
 			else if (($TEXT_DIRECTION=="rtl" && hasRtLText($parent[$i])) || ($TEXT_DIRECTION=="ltr" &&  !hasRtLText($parent[$i])))  print ", ";            
// 			else if ($TEXT_DIRECTION=="rtl" && (ord(substr($parent[$i],0,2))=="215" || ord(substr($parent[$i],strlen(trim($parent[$i]))-2,2))=="215")) print ", ";
//			     else if ($TEXT_DIRECTION=="ltr" &&  ord(substr($parent[$i],0,2))!="215" && ord(substr($parent[$i],strlen(trim($parent[$i]))-2,2))!="215")  print ", ";            
			if (empty($num_place)) $num_place=$parent[$i];
		}
	}
	print "<a href=\"placelist.php?level=0\">";
	//-- place and page text orientation is the same -> top level added at the end of the place text
//	if ($numls>0 && $TEXT_DIRECTION=="rtl" && (ord(substr($parent[$numls],0,2))=="215" || ord(substr($parent[$numls],strlen(trim($parent[$numls]))-2,2))=="215")) print $pgv_lang["top_level"];
//	else if ($numls>0 && $TEXT_DIRECTION=="ltr" &&  ord(substr($parent[$numls],0,2))!="215" && ord(substr($parent[$numls],strlen(trim($parent[$numls]))-2,2))!="215")  print $pgv_lang["top_level"];
	if ($level==0 || ($numls>=0 && (($TEXT_DIRECTION=="rtl" && hasRtLText($parent[$numls])) || ($TEXT_DIRECTION=="ltr" && !hasRtLText($parent[$numls]))))) print $pgv_lang["top_level"];
	print "</a>";

	print_help_link("ppp_levels_help", "qm");
	
	//-- create a string to hold the variable links
	$linklevels="";
	for($j=0; $j<$level; $j++) {
		$linklevels .= "&amp;parent[$j]=".urlencode($parent[$j]);
	}
	$i=0;
	$ct1=count($placelist);
	
	// -- print the array
	foreach ($placelist as $key => $value) {
		if ($i==0) {
			print "\n\t<br /><br />\n\t<table class=\"list_table, $TEXT_DIRECTION\"";
			if ($TEXT_DIRECTION=="rtl") print " dir=\"rtl\"";
			print ">\n\t\t<tr>\n\t\t<td class=\"list_label\" ";
			if ($ct1 > 20) print "colspan=\"3\"";
			else if ($ct1 > 4) print "colspan=\"2\"";
			print ">&nbsp;";
			print "<img src=\"".$PGV_IMAGE_DIR."/".$PGV_IMAGES["place"]["small"]."\" border=\"0\" width=\"25\" title=\"".$pgv_lang["search_place"]."\" alt=\"".$pgv_lang["search_place"]."\" />&nbsp;&nbsp;";
			if ($level>0) {
				print " ".$pgv_lang["place_list_aft"]." ";
//				if (ord(substr($num_place,0,2))=="215" || ord(substr($num_place,strlen(trim($num_place))-2,2))=="215") print "&rlm;".$num_place."&rlm;";
				print PrintReady($num_place);
			}
			else print $pgv_lang["place_list"];
			
			print "&nbsp;";
			print_help_link("ppp_placelist_help", "qm");
			print "</td></tr><tr><td class=\"list_value\"><ul>\n\t\t\t";
		}
	
		print "<li type=\"square\">\n<a href=\"placelist.php?action=$action&amp;level=".($level+1).$linklevels;
		print "&amp;parent[$level]=".urlencode($value)."\" class=\"list_item\">";
		
		if (trim($value)=="") print $pgv_lang["unknown"];
//		else if (ord(substr($value,0,2))=="215" || ord(substr($value,strlen(trim($value))-2,2))=="215") print "&rlm;".$value."&rlm;";
		else print PrintReady($value);
		print "</a></li>\n";
		if ($ct1 > 20){
			if ($i == floor($ct1 / 3)) print "\n\t\t</ul></td>\n\t\t<td class=\"list_value\"><ul>";
			if ($i == floor(($ct1 / 3) * 2)) print "\n\t\t</ul></td>\n\t\t<td class=\"list_value\"><ul>";
		}
		else if ($ct1 > 4 && $i == floor($ct1 / 2)) print "\n\t\t</ul></td>\n\t\t<td class=\"list_value\"><ul>";
	    $i++;
	}
	if ($i>0){
//		print "\n\t\t</ul></td>\n\t\t</tr>\n\t</table>";
		print "\n\t\t</ul></td></tr>";
		if (($action!="show")&&($level>0)) {
//			print "<br />";
			print "<tr>\n\t\t<td class=\"list_label\" ";
			if ($ct1 > 20) print "colspan=\"3\"";
			else if ($ct1 > 4) print "colspan=\"2\"";
			print ">\n\t";
			print $pgv_lang["view_records_in_place"];
			print_help_link("ppp_view_records_help", "qm");
			print "</td></tr><tr><td class=\"list_value\" ";
			if ($ct1 > 20) print "colspan=\"3\"";
			else if ($ct1 > 4) print "colspan=\"2\"";
			print " style=\"text-align: center;\">";
			print "<a href=\"placelist.php?action=show&amp;level=$level";
			foreach($parent as $key=>$value) {
				print "&amp;parent[$key]=".urlencode(trim($value));
			}
			print "\"><span class=\"formField\">";
//			if (ord(substr($num_place,0,2))=="215" || ord(substr($num_place,strlen(trim($num_place))-2,2))=="215") print "&rlm;".$num_place."&rlm;</span></a> ";
			print PrintReady($num_place)."</span></a> ";
			print "</td></tr>";
		}
		print "</table>";
//		print "<br /><br />";
	}

}

if ($level > 0) {
	if ($action=="show") {
		// -- array of names
		$myindilist = array();
		$mysourcelist = array();
		$myfamlist = array();

		$positions = get_place_positions($parent, $level);
		for($i=0; $i<count($positions); $i++) {
			$gid = $positions[$i];
			$indirec=find_gedcom_record($gid);
			$ct = preg_match("/0 @(.*)@ (.*)/", $indirec, $match);
			if ($ct>0) {
				$type = trim($match[2]);
				if ($type == "INDI") {
					$myindilist["$gid"] = get_sortable_name($gid);
				}
				else if ($type == "FAM") {
					$myfamlist["$gid"] = get_family_descriptor($gid);
				}
				else if ($type == "SOUR") {
					$mysourcelist["$gid"] = get_source_descriptor($gid);
				}
			}
		}

		print "\n\t<br /><br /><table class=\"list_table, $TEXT_DIRECTION\">\n\t\t<tr>";
		$ci = count($myindilist);
		$cs = count($mysourcelist);
		$cf = count($myfamlist);
		if ($ci>0) print "<td class=\"list_label\"><img src=\"".$PGV_IMAGE_DIR."/".$PGV_IMAGES["indis"]["large"]."\" border=\"0\" width=\"25\" alt=\"\" /> ".$pgv_lang["individuals"]."</td>";
		if ($cs>0) print "<td class=\"list_label\"><img src=\"".$PGV_IMAGE_DIR."/".$PGV_IMAGES["source"]["large"]."\" border=\"0\" width=\"25\" alt=\"\" /> ".$pgv_lang["sources"]."</td>";
		if ($cf>0) print "<td class=\"list_label\"><img src=\"".$PGV_IMAGE_DIR."/".$PGV_IMAGES["sfamily"]["large"]."\" border=\"0\" width=\"25\" alt=\"\" /> ".$pgv_lang["families"]."</td>";
		$i=0;
//		$pass = FALSE;
		$indi_private=0;
		$indi_hide=0;
		$indisurnames = array();
		foreach($myindilist as $gid=>$indi) {
//			if (showLivingNameByID($gid)||displayDetailsByID($gid)) {
				$name = trim($indi);
				$names = preg_split("/,/", $name);
				$indi = check_NN($names);
				$indisurnames[$gid] = array();
				$indisurnames[$gid]["name"] = $indi;
				$indisurnames[$gid]["gid"] = $gid;
//			}
//			else {
//				$pass = TRUE;
//			}
		}
		print "</tr><tr>";
		if ($ci>0) {
			uasort($indisurnames, "itemsort");
			print "\n\t\t<td class=\"list_value_wrap\">";
			print "\n<ul>";
			foreach ($indisurnames as $value) {
	    		print_list_person($value["gid"], array($value["name"], $GEDCOM));
				$i++;
			}
			if ($indi_hide>0) {
				print "<li>".$pgv_lang["hidden"]." (".$indi_hide.")";
				print_help_link("privacy_error_help", "qm");
				print "</li>";
			}
			print "\n</ul>";
//			if ($pass == TRUE){
//				print "<div class=\"error\">".$pgv_lang["privacy_list_indi_error"]."</div>";
//				$pass = FALSE;
//			}
			print "\n\t\t</td>\n\t\t";
		}
		if ($cs>0) {
			print "<td class=\"list_value_wrap\">";
			print "\n<ul>";
			asort($mysourcelist);
			$i=0;
			foreach ($mysourcelist as $key => $value) {
			    print "\n\t\t\t<li type=\"circle\"><a href=\"source.php?sid=$key\"><span class=\"list_item\">$value</span></a></li>\n";
			    $i++;
			}
			print "\n</ul>";
			print "<br />\n\t\t</td>\n\t\t";
		}
		$surnames = array();
		foreach($myfamlist as $gid=>$fam) {
			// Added space to regexp after z to also remove prefixes
			$name = preg_replace(array("/ [jJsS][rR]\.?,/", "/ I+,/", "/^[a-z. ]*/"), array(",",",",""), $fam);
			$name = trim($name);
			$names = preg_split("/[,+]/", $name);
			$surname = $names[0];
			$firstname = "";
			if (isset($names[1])) $firstname = trim($names[1]);
			else $surname = "";
			$surname = str2upper(trim($surname));
			if (!isset($surnames[$surname.$firstname.$gid])) {
				$surnames[$surname.$firstname.$gid] = array();
				// Convert names again to include prefixes for displaying
				$name = preg_replace(array("/ [jJsS][rR]\.?,/", "/ I+,/"), array(",",","), $fam);
				$names = preg_split("/[,+]/", $name);
				$fam = check_NN($names);
				$surnames[$surname.$firstname.$gid]["name"] = $fam;
				$surnames[$surname.$firstname.$gid]["gid"] = $gid;
			}
		}
		$i=0;
		if (isset($surnames)) $ct=count($surnames);
		if ($ct>0) {
			uasort($surnames, "itemsort");
			reset($surnames);
			print "<td class=\"list_value_wrap\">";
			print "\n<ul>";
			foreach ($surnames as $value) {
				print_list_family($value["gid"], array($value["name"], $GEDCOM));
				$i++;
			}
			if ($fam_hide>0) {
				print "<li>".$pgv_lang["hidden"]." (".$fam_hide.")";
				print_help_link("privacy_error_help", "qm");
				print "</li>";
			}
			print "</ul></td>";
		}
//		if ($pass == TRUE){
//			print "<div class=\"error\">".$pgv_lang["privacy_list_fam_error"]."</div>";
//			$pass = FALSE;
//		}
		print "\n\t\t</tr><tr>";
		if ($ci>0) {
			print "<td>";
			$ci -= $indi_hide;
			if ($ci>0)print $pgv_lang["total_indis"]." ".$ci;
			if ($indi_private>0) print "&nbsp;(".$pgv_lang["private"]." ".$indi_private.")";
			if ($ci>0 && $indi_hide>0) print "&nbsp;--&nbsp;";
			if ($indi_hide>0) print $pgv_lang["hidden"]." ".$indi_hide;
			print "</td>\n";
		}
		if ($cs>0) {
			print "<td>";
			if ($cs>0) print $pgv_lang["total_sources"]." ".$cs;
//			if ($cs>0) $cs." ".print $pgv_lang["total_sources"];
			print "</td>\n";
		}
		if ($cf>0) {
			print "<td>";
			$cf -= $fam_hide;
			if ($cf>0)print $pgv_lang["total_fams"]." ".$cf;
			if ($fam_private>0) print "&nbsp;(".$pgv_lang["private"]." ".$fam_private.")";
			if ($cf>0 && $fam_hide>0) print "&nbsp;--&nbsp;";
			if ($fam_hide>0) print $pgv_lang["hidden"]." ".$fam_hide;
			print "</td>\n";
		}
		print "</tr>\n\t</table>";
		print_help_link("ppp_name_list_help", "qm");
		print "<br />";
	}
}

//-- list type display
if ($display=="list") {
	$placelist = array();
	
	find_place_list("");
	uasort($placelist, "itemsort");

	if (count($placelist)==0) {
		print "<b>".$pgv_lang["no_results"]."</b><br />";
	}
	else {
		print "\n\t<table class=\"list_table, $TEXT_DIRECTION\"";
		if ($TEXT_DIRECTION=="rtl") print " dir=\"rtl\"";
		print ">\n\t\t<tr>\n\t\t<td class=\"list_label\" ";
		$ct = count($placelist);
		print " colspan=\"".($ct>20?"3":"2")."\">&nbsp;";
		print "<img src=\"".$PGV_IMAGE_DIR."/".$PGV_IMAGES["place"]["small"]."\" border=\"0\" width=\"25\" title=\"".$pgv_lang["search_place"]."\" alt=\"".$pgv_lang["search_place"]."\" />&nbsp;&nbsp;";
		print $pgv_lang["place_list2"];
		print "&nbsp;";
		print_help_link("ppp_placelist_help2", "qm");
		print "</td></tr><tr><td class=\"list_value\"><ul>\n\t\t\t";
		$i=0;
		foreach($placelist as $revplace) {
			$linklevels = "";
			$levels = preg_split ("/,/", $revplace);		// -- split the place into comma seperated values
			$level=0;
			foreach($levels as $place) {
				$place = trim($place);
				$linklevels .= "&amp;parent[$level]=".urlencode($place);
				$level++;
			}
			$revplace = trim($revplace);
			$revplace = preg_replace("/ ,/", " ".$pgv_lang["unknown"].",", $revplace);
			if ($revplace{strlen($revplace)-1}==',') $revplace .= " ".$pgv_lang["unknown"];
			print "<li type=\"square\"><a href=\"placelist.php?action=show&amp;display=hierarchy&amp;level=$level$linklevels\">";
			print PrintReady($revplace)."</a></li>\n";
//			if (ord(substr($revplace,0,2))=="215" || ord(substr($revplace,strlen(trim($revplace))-2,2))=="215") print "&rlm;".$revplace."&rlm;</a></li>\n";
//			else print "&lrm;".$revplace."&lrm;</a></li>\n";
			$i++;
//			if ($i == floor($ct/2)) print "</ul></td><td class=\"list_value\"><ul>\n\t\t\t";
			if ($ct > 20){
				if ($i == floor($ct / 3)) print "\n\t\t</ul></td>\n\t\t<td class=\"list_value\"><ul>";
				if ($i == floor(($ct / 3) * 2)) print "\n\t\t</ul></td>\n\t\t<td class=\"list_value\"><ul>";
			}
			else if ($i == floor($ct/2)) print "</ul></td><td class=\"list_value\"><ul>\n\t\t\t";
		}
		print "\n\t\t</ul></td></tr>\n\t\t";
		if ($i>1) {
			print "<tr><td>";
			if ($i>0) print $pgv_lang["total_unic_places"]." ".$i;
			print "</td></tr>\n";
		}
		print "\n\t\t</table>";
	}
}

if ($display=="list") print "<br /><a href=\"placelist.php?display=hierarchy\">".$pgv_lang["show_place_hierarchy"]."</a><br /><br />\n";
else print "<br /><a href=\"placelist.php?display=list\">".$pgv_lang["show_place_list"]."</a>\n<br /><br />";
if ($hasplaceform) {
	$placeheader = substr($header, $hasplaceform);
	$ct = preg_match("/2 FORM (.*)/", $placeheader, $match);
	if ($ct>0) {
		print  $pgv_lang["form"].$match[1];
		print_help_link("ppp_match_one_help", "qm");
	}
}
else {
	print $pgv_lang["form"].$pgv_lang["default_form"]."  ".$pgv_lang["default_form_info"];
	print_help_link("ppp_default_form_help", "qm");
}

print "<br /><br /></div>";
print_footer();

?>
