<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
  <TITLE>Department of Mathematics Graduate Students Alumni Guestbook</TITLE>
</HEAD>
<BODY text="#000000" bgcolor="#CCDDA0" link="#5959AB" vlink="#6F4242" alink="#FF3300"
      background="backbd2.jpg">
<P>
<A name="Top"></A>

<?php
//
// Graduate guestbook display code
//
// The choice of output is handled very simply.  If $order is present,
// then it is assumed that it is a general browse, and not a search
// (searches will have default orderings based on type of search).
//
// Searches can be by lastname, firstname, or gradyear.  lastname and
// gradyear will order by lastname, firstname will order by firstname.
//

require_once('DB.php');
require_once('dbtools.inc.php');
require_once('htmltools.inc.php');
require_once('guestbook.inc.php');

$dbname = "gradguestbook";
$dbtype = "pgsql";

set_magic_quotes_runtime(0);

// Handle GET variables
$lastname  = $_GET["lastname"];
$firstname = $_GET["firstname"];
$gradyear  = $_GET["gradyear"];
$offset    = $_GET["offset"];
$entries   = $_GET["entries"];
$order     = $_GET["order"];

if($offset == "") $offset = 0;
if($entries == "") $entries = 10;

echo "<UL><UL><CENTER>\n";
echo "<H1>Graduate Guestbook</H1>\n";
echo "<P><TABLE><TR><TD>";
if($offset == 0) echo "</TD>";
else
{
    $newoffset = $offset - $entries;
    if($newoffset < 0) $newoffset = 0;

    if($order != "")
    {
	echo "<A href=\"".$PHP_SELF."?order=".$order."&offset=".$newoffset."&entries=".$entries."\">"
	    ."Previous ".$entries." Entries</TD>";
    }
    else
    {
	echo "<A href=\"".$PHP_SELF."?lastname=".$lastname."&firstname="
	    .$firstname."&gradyear=".$gradyear."&offset=".$newoffset
	    ."&entries=".$entries."\">Previous ".$entries." Entries</TD>";
    }
}
echo "<TD></TD>";
echo "<TD>Entries ".$offset." to ".($offset+$entries)."</TD>";
echo "<TD></TD>";
echo "<TD>";

$newoffset = $offset + $entries;
if($order != "")
{
    echo "<A href=\"".$PHP_SELF."?order=".$order."&offset=".$newoffset."&entries=".$entries."\">"
	."Next ".$entries." Entries</TD>";
}
else
{
    echo "<A href=\"".$PHP_SELF."?lastname=".$lastname."&firstname="
	.$firstname."&gradyear=".$gradyear."&offset=".$newoffset
	."&entries=".$entries."\">Next ".$entries." Entries</TD>";
}
echo "</TR></TABLE>\n";
echo "</CENTER></UL></UL>\n";

$db = DB::connect($dbtype."://www-data@unix+localhost/".$dbname);

if(DB::isError($db))
{
    echo "<UL><UL>\n";
    echo "<H1>Whoops, I couldn't find the guestbook database!</H1>\n";
    echo "<P>Please report the above error and the following data to";
    echo " <A href=\"mailto:merlin@math.lsu.edu\">merlin@math.lsu.edu</A>:</P>\n";
    echo "<P>Failed connecting to ".$dbtype." database ".$dbname."</H1>\n";
    echo "<P>Error: ".DB::errorMessage($db)."</P>\n";
    echo "<P>Please press the Back button on your browser to return to the guestbook\n";
    echo "</UL></UL>\n";
    exit;
}

if($order != "")  // General browse
{
    $selectstring = "SELECT * FROM guestbook ORDER BY ".$order;
    if($order == gradyear) $selectstring .= " DESC";

    $result = $db->query($selectstring);

    if(DB::isError($result))
    {
	gberror("database failure while browsing guestbook","",$result);
	exithtml();
    }

    if($result->numRows() == 0)
    {
	echo "<UL><UL>\n";
	echo "<H2>No entries found!</H2>\n";
	echo "<P>You may press the back button on your browser to return to"
	    ." the guestbook, or <A href=/grad/>click here</A> to return to"
	    ." the main graduate studies page.</P>\n";
	echo "</UL></UL>\n";
	exithtml();
    }

//    echo "<UL><UL><H2>Browsing by: ".$order."</H2></UL></UL>\n";

    $position = 0;
    while($data = $result->fetchrow(DB_FETCHMODE_ASSOC))
    {
	$position = showentry($db, $data, $position, $offset, $entries);
    }
} // if($order != "")  // General browse
else
{
    $querystring = "SELECT * FROM guestbook";

    $firstsearchterm = true;
    $order1 = "lastname";
    $order2 = "firstname";

    if($lastname != "")
    {
	$querystring .= " WHERE lastname ~* '".$lastname."'";
	$firstsearchterm = false;
    }
    if($firstname != "")
    {
	if($firstsearchterm)
	{
	    $querystring .= " WHERE firstname ~* '".$firstname."'";
	    $firstsearchterm = false;
	    $order1 = "firstname";
	    $order2 = "lastname";
	}
	else
	{
	    $querystring .= " AND firstname ~* '".$firstname."'";
	}
    }
    if($gradyear != "")
    {
	if($firstsearchterm)
	{
	    $querystring .= " WHERE gradyear = ".$gradyear;
	}
	else
	{
	    $querystring .= " AND gradyear = ".$gradyear;
	}
    }
    $querystring .= " ORDER BY ".$order1.",".$order2.",gradyear DESC;\n";

    $result = $db->query($querystring);

    if(DB::isError($result))
    {
	gberror("database failure while searching guestbook","",$result);
	exithtml();
    }

    if($result->numRows() == 0)
    {
	echo "<UL><UL>";
	echo "<H2>No entries found!</H2>\n";
	echo "<P>You may press the back button on your browser to return to"
	    ." the guestbook, or <A href=/grad/>click here</A> to return to"
	    ." the main graduate studies page.</P>\n";
	echo "</UL></UL>";
	exithtml();
    }

    $position = 0;
    while($data = $result->fetchrow(DB_FETCHMODE_ASSOC))
    {
	$position = showentry($db, $data, $position, $offset, $entries);
    }
}

function showentry($db, $data, $position, $offset, $entries)
{
    if( ($position >= $offset) && ($position < ($offset+$entries)) )
    {
	$ms = sqltobool($data["ms"]);
	$phd = sqltobool($data["phd"]);
	echo "<UL><UL>\n";
	echo "<P><B>".$data["firstname"]." ".$data["lastname"]." (";
	if($ms)
	{
	    echo "M.S., ";
	}
	if($phd)
	{
	    echo "Ph.D., ";
	}
	echo $data["gradyear"].")</B></P>\n";
	echo "<UL>\n";
	if($data["advisor"] != "") echo "<P>Advisor: ".$data["advisor"]."</P>\n";
	if($data["email"] != "") echo "<P>E-mail Address: ".$data["email"]."</P>\n";
	if($data["publicinfo"] != "") echo "<P>".$data["publicinfo"]."</P>\n";
	if($data["pictures"] != "")
	{
	    // In theory, pictures could contain an array of md5
	    // hashes.  This needs to be accounted for eventually.
	    
	    $picresult = $db->query("SELECT * FROM pictures WHERE md5 = '".$data["pictures"]."'");
	    if(DB::isError($picresult))
	    {
		gberror("database failure while finding picture with md5 hash: ".$data["pictures"],
			"",$picresult);
		exithtml();
	    }
	    if($picresult -> numRows() > 1)
	    {
		gberror("more than one picture found on what should have been a unique search!",
			"");
		exithtml();
	    }
	    if($picresult -> numRows() == 0)
	    {
		gberror("a picture referred to in the entry doesn't exist in the database",
			"MD5 hash: ".$data["pictures"]);
		exithtml();
	    }
	    $picdata = $picresult->fetchrow(DB_FETCHMODE_ASSOC);
	    echo "<P><A href=\"getimage.php?md5=".$data["pictures"]."\">"
		.$picdata["description"]."</A></P>\n";
	} // if($data["pictures"] != "")
	echo "</UL>\n";
	echo "<HR>\n";
	echo "</UL></UL>\n";
    }
    return $position + 1;
}
?>
