Location: PHPKode > projects > PhpFamilyTree > phpfamilytree/functions.php
<?
# functions.php - Contains all project functions.
# $Id: functions.php,v 1.1.1.1 2004/01/01 05:51:24 rocket_169 Exp $
#
# Copyright (c) 2003 The phpFamilyTree Project Team
# Licensed under the GNU GPL. For full terms see the file COPYING.
#
# http://www.phpfamilytree.org
#


# Define globals
#
$maxcol=0;
$maxlevel=0;
$matrix=array(0);

# Connect to database
#
$db = @mysql_pconnect ($db_host,$db_user, $db_pass) or die(mysql_error());
mysql_selectdb($db_name, $db) or die(mysql_error());

function balancematrix() {

	global $matrix;
	global $maxlevel;
        global $maxcol;

	# ASSIGN WEIGHT TO EACH PARENT
	for ($level=$maxlevel;$level>=1;$level--) {
		for ($column=1;$column<=$maxcol;$column++) {
			if ($level==$maxlevel) {
				$matrix[$level][$column]["weight"]=1;
			} else {
				$weight=0;
				for ($col=1;$col<=$maxcol;$col++) {
					if ($matrix[$level+1][$col]["parent"]==$matrix[$level][$column]["id"]) {
						$weight=$weight+$matrix[$level+1][$col]["weight"];
					}
				}
				$matrix[$level][$column]["children"]=$weight;
				if ($weight==0) { $weight=1; }
				$matrix[$level][$column]["weight"]=$weight;
			}
		}
	}


	# DEFINE X COORDINATES
	$matrix[1][1]["x"]=.5;
	$matrix[1][1]["width"]=1;
	for ($level=2;$level<=$maxlevel;$level++) {
		for ($column=1;$column<=$maxcol;$column++) {
			if ($matrix[$level][$column]["id"]) {
				$parentweight=1; $parentwidth=1; $parentx=1;
				for ($col=1;$col<=$maxcol;$col++) {
					if ($matrix[$level-1][$col]["id"]==$matrix[$level][$column]["parent"]) {
						$parentweight=$matrix[$level-1][$col]["weight"];
						$parentwidth=$matrix[$level-1][$col]["width"];
						$parentx=$matrix[$level-1][$col]["x"];
					}
				}
				$mywidth=($matrix[$level][$column]["weight"] / $parentweight) * $parentwidth;
				# IF I AM NOT THE FIRST CHILD, CALCULATE LEFT EDGE
				if ($matrix[$level][$column-1]["parent"]!=$matrix[$level][$column]["parent"]) {
					$myleftedge = $parentx - ($parentwidth / 2);
				} else {
					$myleftedge = $matrix[$level][$column-1]["x"] +
					($matrix[$level][$column-1]["width"]/2);
				}
				$myx = $myleftedge + ($mywidth / 2);
				$matrix[$level][$column]["width"]=$mywidth;
				$matrix[$level][$column]["x"]= $myx;
			} else {
				$column=9999;
			}
		}
	}
}


function displaymatrix() {
	global $matrix;
	global $maxcol;
	global $maxlevel;
	global $graphwidth;
	global $boxwidth;
	global $boxheight;
	global $topmargin;
	global $leftmargin;
        $maxweight=$matrix[1][1]["weight"];
        $unit=$graphwidth/$maxweight;

	#print "<div style='position:absolute; top:$topmargin\px; left:$leftmargin\px; padding:0px; background-color:#dcbe7a; height:".(40 + ($boxheight * $maxlevel))."px; width:$graphwidth;'>&nbsp;</div>";

	for ($level=1;$level<=$maxlevel;$level++) {
		for ($column=1;$column<=$maxcol;$column++) {
			# DRAW BOXES
			if ($matrix[$level][$column]["id"]) {
				$x=($matrix[$level][$column]["x"] * $graphwidth) - ($boxwidth / 2 ) + $leftmargin;
				$y=($level * $boxheight)- $boxheight + $topmargin + 20;
				print "<div align='center' style='position:absolute; top:$y\px; left:$x\px;
				padding:0px; background-color:#fcffd3; border:1px solid #000;
				height:".($boxheight - 20)."px; 		width:$boxwidth;'><div id=\"member\">" .
				"<a href=\"ft_chart.php?id={$matrix[$level][$column]["id"]}\">".
				$matrix[$level][$column]["name"]."</a></div>";

				# PRINT SPOUSE
				print spouselist ($matrix[$level][$column]["id"]);

				print "</div>";

				# DRAW CONNECTING LINES
				if ($matrix[$level][$column]["parent"] == $matrix[$level][$column-1]["parent"]) {
					if ($level > 1) {
						# HORIZONTAL LINE
						$prevx=($matrix[$level][$column-1]["x"] *
						$graphwidth) + $leftmargin;
						$y2=$y-10;
						$width = $x - $prevx + ($boxwidth / 2);
						print "<div style='position:absolute; top:$y2\px; 							left:$prevx\px; border-top:1px solid #000; width:$width\px ; 						height:0px'>&nbsp;</div>";
					}
				}

				# VERTICAL LINE (TOP)
				if ($level > 1) {
					$x=($matrix[$level][$column]["x"] * $graphwidth) + $leftmargin;
					$y2=$y-10;
					print "<div style='position:absolute; top:$y2\px; left:$x\px;
					border-left:1px solid #000; width:0px;height:10px'>&nbsp;</div>";
				}
				# VERTICAL LINE (BOTTOM)
				if ($level < $maxlevel && $matrix[$level][$column]["children"]) {
					$x=($matrix[$level][$column]["x"] * $graphwidth) + $leftmargin;
					$y2=$y+$boxheight-20+1;
					print "<div style='position:absolute; top:$y2\px; left:$x\px;
					border-left:1px solid #000; width:0px;height:10px'>&nbsp;</div>";
				}

				# "REDRAW" ICON
				if ($level == 1) {
					$rs=mysql_query("SELECT father_id FROM ft_individual WHERE id=".
					$matrix[$level][$column]["id"]) or die(mysql_error());
					$member = mysql_fetch_array($rs);
					$root = $member["father_id"];
				} else {
					$root=$matrix[$level][$column]["id"];
				}
				if ($root) {
					$x=($matrix[$level][$column]["x"] * $graphwidth) -3 + $leftmargin;
					print "<div onclick=\"document.location='ft_chart.php?id=$root'\"
					style='position:absolute; top:".($y-6)."\px; left:$x\px;
					border:1px solid #000; cursor:pointer; background-color:#fcffd3;
					width:6px ; height:6px ;'>&nbsp;</div>\n";
				}
			}

		}
	}
}


function addtomatrix($id,$level,$parent) {
	global $matrix;
	global $maxcol;
	global $maxlevel;

	# GET POSITION IN MATRIX
	$matrix[$level][0]++;
	$column=$matrix[$level][0];

	# SET MAXCOL AND MAXLEVEL
	if ($column>$maxcol) { $maxcol=$column; }
	if ($level>$maxlevel) { $maxlevel=$level; }

	# RECURSIVITY AIRBAG
	if ($level>100) { return; }

	$rs=mysql_query("SELECT id, first, last, father_id, mother_id FROM ft_individual WHERE id=$id")
	or die(mysql_error());
	if ( $member = mysql_fetch_array($rs) ) {

		# ADD TO MATRIX
		$name=$member["first"]." ".$member["last"];
		$matrix[$level][$column]["id"]=$id;
		$matrix[$level][$column]["name"]=$name;
		$matrix[$level][$column]["parent"]=$parent;
		#$spouse=$member["spouse"];
		#$matrix[$level][$column]["spouse"]=$spouse;

		for ($e=0;$e<$level;$e++) { print "&nbsp;&nbsp;&nbsp;"; }

		# GET CHILDREN
		$rs=mysql_query("SELECT id FROM ft_individual WHERE mother_id=$id or father_id=$id")
		or die(mysql_error());
		while ( $member = mysql_fetch_array($rs) ) {
			addtomatrix($member["id"],$level+1, $id);
		}
	}
}

function printmember($id,$level) {

	# RECURSIVITY AIRBAG
	if ($level>100) { return; }

	$rs=mysql_query("SELECT * FROM ft_individual WHERE id=$id")
	or die(mysql_error());
	if ( $member = mysql_fetch_array($rs) ) {

		# PRINT MEMBER
		$name=$member["first"]." ".$member["last"];
		$spouse=$member["spouse"];
		$family=$member["family_id"];

		print "<tr><td>";
		for ($e=0;$e<$level;$e++) { print "&nbsp;&nbsp;&nbsp;"; }
		print "$name</td></td";
		print "</td>";
		if (logged_in()) {
			print "<td><a href=\"ft_edit_members.php?action=edit&id=$family&memberid=$id\">Edit</a></td>
			<td><a href=\"ft_edit_members.php?action=del&id=$family&memberid=$id\">Delete</a></td>";
		}
		print "<td><a href=\"ft_edit_marriages.php?id=$id\">Spouse</a></td>";
		print "<td><a href=\"ft_chart.php?id=$id\">Graph</a></td>";
		print "</tr>";

		# GET CHILDREN
		$rs=mysql_query("SELECT id FROM ft_individual WHERE father_id=$id or mother_id=$id")
		or die(mysql_error());
		while ( $member = mysql_fetch_array($rs) ) {
			printmember($member["id"],$level+1);
		}
	}
}


function parentcombo($current, $family, $myid, $gender) {

	if ($gender=="F") { $parent="mother_id"; } else { $parent="father_id"; }

	print "<select name=\"$parent\">";
	print "<option value=0 ";
	if ($current == 0) { print "selected"; }
	print ">Undefined</option>";

	# PRINT FAMILY MEMBERS
	$rs=mysql_query("SELECT id, first,last FROM ft_individual WHERE id!=$myid AND gender=\"$gender\"") or die(mysql_error());
	while ( $member = mysql_fetch_array($rs) ) {
		print "<option value={$member["id"]} ";
		if ($current == $member["id"]) { print "selected"; }
		print ">{$member["first"]} {$member["last"]}</option>";
	}

	print "</select>";
}


function gendercombo ($current) {
 	print "<select name=\"gender\">";
	print "<option value=''>Undefined</option>";

	print "<option value='M'";
	if ($current=="M") { print " selected";}
	print ">Male</option>";

	print "<option value='F'";
	if ($current=="F") { print " selected";}
	print ">Female</option>";

 	print "</select>";
}

function spousecombo($current, $family, $memberid, $gender) {

	print "<select name=\"spouse\">";
	print "<option value=0 ";
	if ($current == 0) { print "selected"; }
	print ">Undefined</option>";

	# PRINT OTHER FAMILIES MEMBERS
	$rs=mysql_query("SELECT id, first,last FROM ft_individual WHERE gender!=\"$gender\"") or die(mysql_error());

	while ( $member = mysql_fetch_array($rs) ) {
		print "<option value={$member["id"]} ";
		if ($current == $member["id"]) { print "selected"; }
		print ">{$member["first"]} {$member["last"]}</option>";
	}

	print "</select>";
}


function divorcedcombo($current) {

	print "<select name=\"divorced\">";
	print "<option value=0 ";
	if ($current == 0) { print "selected"; }
	print ">No</option>";
	print "<option value=1 ";
	if ($current == 1) { print "selected"; }
	print ">Yes</option>";
	print "</select>";
}


function db_connect($host,$user,$pass,$db) {
	$mysql_link = mysql_connect("$host", "$user", "$pass")
		or die("Could not connect: " . mysql_error());

	mysql_select_db("$db", $mysql_link)
		or die("Error: " . mysql_error());
	return $mysql_link;
}

function safe_query($query) {
	$result_set = mysql_query($query)
		or die("Invalid query: " . mysql_error());
	return $result_set;
}

function auth($login='',$passwd='') {
	session_start();
	$check =! empty($login);
	if(is_array($_SESSION["authdata"])) {
		return true;
	} elseif($check) {
		$query = "SELECT username, admin FROM ft_users WHERE username = '$login' AND password = MD5('$passwd')";
		$result = safe_query($query);
		$rows = mysql_num_rows($result);
		$user = mysql_fetch_array($result);
		if($rows == 1) {
			$_SESSION["authdata"] = array("login"=>$login);
			$_SESSION["authdata"]["username"]=$user["username"];
			$_SESSION["authdata"]["admin"]=$user["admin"];
			return true;
		}
		unset($_SESSION["authdata"]);
		return false;
	} else {
		return false;
	}
}

function login_form($error = 'false') {

	print "<table width=\"50%\" border=\"0\"><tr>";
	print "<td valign=\"top\" id=\"leftpanel\">";
	print "<h3>Login</h3>";

	if($error) {
		print("Please enter your login information<br /><br />\n");
	}
	print("<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">\n");
	print("<table><tr><td>Username</td><td><input type=\"text\" id=\"username\" name=\"username\" /></td></tr>\n");
	print("<tr><td>Password</label></td><td><input type=\"password\" id=\"password\" name=\"password\" /></td></tr>\n");
	print("<tr><td></td><td><input type=\"submit\" name=\"submit\" value=\"Submit\">\n");
	print "</td></tr></table>";

}

function logout() {
	session_unset();
	session_destroy();
}

function logged_in() {
	return is_array($_SESSION["authdata"]);
}

function username() {
 return $_SESSION["authdata"]["username"];
}

function isadmin() {
 return $_SESSION["authdata"]["admin"];
}

function getname($id) {
	$rs=mysql_query("SELECT first, last FROM ft_individual where id=$id") or die(mysql_error());
	$member = mysql_fetch_array($rs);
	return  $member["first"]." ".$member["last"];
}

function getgender($id) {
	$rs=mysql_query("SELECT gender FROM ft_individual where id=$id") or die(mysql_error());
	$member = mysql_fetch_array($rs);
	return  $member["gender"];
}

function spouselist ($id) {

	# RETRIEVE MEMBER NAME
	$membername = getname($id);
	$gender = getgender($id);

	# SPOUSE LIST
	$rs=mysql_query("SELECT * FROM ft_marriages WHERE husband_id=$id or wife_id=$id") or die(mysql_error());
	while ($marriage = mysql_fetch_array($rs)) {
		if ($gender=="F") {
			$spouseid=$marriage["husband_id"];
		} else {
			$spouseid=$marriage["wife_id"];
		}
		print "<div id=\"member\"><a href=\"ft_chart.php?id=$spouseid\">".
		getname($spouseid)."</a></div>";
	}
}

?>
Return current item: PhpFamilyTree