<?

/*
	The following code was written for the MIT Laboratory for Computer Science
	by Patrick Nichols <pnichols@mit.edu>, Machinatio, Inc., on 1-8-2001.
	
	This file (viewpubs.php) displays the publications of a given type based upon
	entries in the lcs_categories tables, drawing from the publications table.

	The code was updated by Tony Scelfo <tonys@mit.edu> on 1-11-2002
*/

require("pubutil.php");
dbconnect();

// global variables
$debug = false;

$sort = sanitize_xss($_GET['sort']);
$cat = sanitize_xss($_GET['cat']);
$count = sanitize_xss($_GET['count']);
$currentpage = sanitize_xss($_GET['currentpage']);

if(!isset($sort) || $sort =='') {
	$sort = "docnum";
}

// check to see if any kind of sorting operation is requested
	// sorting has been requested by a given variable
	if ($sort == "title") {
	  $query = sprintf("SELECT * FROM publications WHERE publication_type = '%s' ORDER BY publication_title", mysql_real_escape_string($cat));
	  //$query = "SELECT * FROM publications WHERE publication_type = \"$cat\" ORDER BY publication_title";
		$result = mysql_query($query) or die ("database selection failed");
		if(!$result)
		{
			die('Invalid query: ' . mysql_error());
		}
	}
	if ($sort == "docnum") {
	  $query = sprintf("SELECT * FROM publications WHERE publication_type = '%s' ORDER BY substring_index(lcs_doc_number,'-', -1)+1 desc", mysql_real_escape_string($cat));
	  //$query = "SELECT * FROM publications WHERE publication_type = \"$cat\" ORDER BY substring_index(lcs_doc_number,'-', -1)+1 desc";
		$result = mysql_query($query) or die ("database selection failed");
		if(!$result)
		{
			die('Invalid query: ' . mysql_error());
		}
	}
	if ($sort == "author") {
	  $query = sprintf("SELECT * FROM publications WHERE publication_type = '%s' ORDER BY author_lastname, author_firstname", mysql_real_escape_string($cat));
		// $query = "SELECT * FROM publications WHERE publication_type = \"$cat\" ORDER BY author_lastname, author_firstname";
		$result = mysql_query($query) or die ("database selection failed");
		if(!$result)
		{
			die('Invalid query: ' . mysql_error());
		}
	}
	if ($sort == "group") {
	  $query = sprintf("SELECT * FROM publications WHERE publication_type ='%s'  ORDER BY lcs_group", mysql_real_escape_string($cat));
		// $query = "SELECT * FROM publications WHERE publication_type = \"$cat\" ORDER BY lcs_group";
		$result = mysql_query($query) or die ("database selection failed");
	}
	if ($sort == "date") {
	  $query = sprintf("SELECT * FROM publications WHERE publication_type = '%s' ORDER BY publication_year, publication_month, publication_day", mysql_real_escape_string($cat));
		// $query = "SELECT * FROM publications WHERE publication_type = \"$cat\" ORDER BY publication_year, publication_month, publication_day";
		$result = mysql_query($query) or die ("database selection failed");
		die("ran query: " . $query);
		if(!$result)
		{
			die('Invalid query: ' . mysql_error());
		}
	}
	if ($sort == "url") {
	  //$query = "SELECT * FROM publications WHERE publication_type = \"$cat\" ORDER BY url_additional";
		$query = "SELECT * FROM publications";
		$result = mysql_query($query) or die ("database selection failed");
		if(!$result)
		{
			die('Invalid query: ' . mysql_error());
		}
	}

$catresult = mysql_query(sprintf("SELECT * FROM lcs_categories WHERE pub_category='%s'", mysql_real_escape_string($cat)));
if(!$catresult)
{
	die('Invalid query: ' . mysql_error());
}

$catarray = mysql_fetch_array($catresult);
$cat_description = $catarray["cat_description"];

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
   <TITLE>LCS Publications: <? echo $cat_description ?></TITLE>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <LINK REL="stylesheet" TYPE="text/css" HREF="includes/pubs.css">
   <SCRIPT language="JavaScript" type="text/javascript">
	<!-- (hide)
	function window_pop(URL) {
	eval("window.open(URL,'LCS','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,left=10,top=10,width=400,height=500');");
	}
	// End -->
   </SCRIPT>

   <STYLE TYPE="text/css">
	
	<!--
		.table-heading {
			background-color:#000033;
			color:#ffffff;
			font-family:Verdana,Arial,Helvetica,Sans-Serif;
			font-size:12px;
			font-weight:bold;
			text-align:center;
		}
		.table-data-white {
			background-color: #ffffff;
			font-family:Verdana,Arial,Helvetica,Sans-Serif;
			font-size:10px;
		}
		.table-data-grey {
			background-color:#cccccc;
			font-family:Verdana,Arial,Helvetica,Sans-Serif;
			font-size:10px;
		}
	-->
   </STYLE>

</HEAD>

<BODY TEXT="#000033" LINK="#CC0000" VLINK="#6666FF" ALINK="#FF0000">

<table width="100%" border="0" cellspacing="5" cellpadding="5">
  <tr>
    <td>

    <? virtual("/includes/header-nav.html"); ?>

</td>
  </tr>
</table>
<TABLE WIDTH="860" BORDER="0" CELLPADDING="0" CELLSPACING="0" class="table-data-white">
  <TR>
      
    <TD VALIGN="top" WIDTH="150">&nbsp;</TD>
    <TD WIDTH="40" ALIGN="left" VALIGN="top">&nbsp; </TD>
    <TD WIDTH="670" VALIGN="top" class="table-data-white"> 
      <!--MAIN SECTION START-->
      <TABLE WIDTH="670" BORDER="0" CELLPADDING="0" CELLSPACING="0">
         <TR>
            <TD WIDTH="670" ALIGN="left" VALIGN="top" CLASS="text">
               <BR><IMG SRC="/images/spacer.gif" WIDTH="10" HEIGHT="1" alt=""><BR>
               <P><SPAN CLASS="textsectionheader1">LCS 
              <?
	if ($cat == "tr") echo "Technical Reports";
	if ($cat == "tm") echo "Technical Memos";
?>
              </SPAN><BR>
              <BR>
               <IMG SRC="/images/spacer.gif" WIDTH="10" HEIGHT="1" alt=""></P>
               
               
            <P>Click on the title of the document for more information. Click 
              the column headers to sort by different fields.</P>
            <P><img src="/includes/dotorange.gif" width="100%" height="1"></P>
               
               </TD>
         </TR>
         
         <TR>
            
          <TD CLASS="text"> 
            <!-- Here is where it figure out what range to display -->
            <?
//for ($i = 0; $i < mysql_num_rows($result); $i++) {

//Check the number of listings to be made.  If this is bigger than $count we are going to display the results on multiple pages

//If there is no value set for the current page, set it at page 1

if(!isset($currentpage)) {
	$currentpage = 1;
}

//If there is no value set for the number of items to display on a page, set it to 50

if(!isset($count)) {
	$count = 50;
}

if(!isset($sort)) {
	$sort = "docnumber";
}

//Display the table that shows links to next and previous pages as well as page count options

echo("<TABLE WIDTH=\"670\" CELLPADDING=\"0\" CELLSPACING=\"1\" BORDER=\"0\"><tr>");
if(mysql_num_rows($result) > $count) {
	echo("<td class=table-data-white align=\"left\">");
	echo("Page ".$currentpage." of ".ceil(mysql_num_rows($result) / $count));
	echo(" | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=".$count."\">First Page</a>");
	if($currentpage > 1) {
		echo(" | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=".$count."&amp;currentpage=".($currentpage - 1)."\">Previous Page</a>");
	}
	else {
		echo(" | Previous Page");
	}
	if($currentpage < ceil(mysql_num_rows($result) / $count)) {
		echo(" | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=".$count."&amp;currentpage=".($currentpage + 1)."\">Next Page</a>");
	}
	else {
		echo(" | Next Page");
	}
	echo(" | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=".$count."&amp;currentpage=".ceil(mysql_num_rows($result) / $count)."\">Last Page</a>");
	echo("</td><td class=table-data-white align=\"right\">");
	if($count==10) {
		echo("Show 10 | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=25\">25</a> | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=50\">50</a> | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=100\">100</a> per page");
	}
	if($count==25) {
		echo("Show <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=10\">10</a> | 25 | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=50\">50</a> | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=100\">100</a> per page");
	}
	if($count==50) {
		echo("Show <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=10\">10</a> | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=25\">25</a> | 50 | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=100\">100</a> per page");
	}
	if($count==100) {
		echo("Show <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=10\">10</a> | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=25\">25</a>  | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=50\">50</a> | 100 per page");
	}
	echo("</td></tr></table>");
}

//If the current page is not the first page, move the array foward so that the next item
//in the array will be the one that is to be displayed first

if($currentpage > 1) {
	for($i = 0; $i < (($currentpage - 1) * $count); $i++) {
 		mysql_fetch_array($result);
	}
}
?>
            <!-- Display the range -->
            <!-- Outer table for formatting magic -->
             
            <TABLE WIDTH="670" CELLPADDING="0" CELLSPACING="1" BORDER="0">
              <TR>
                     
                <TD  class="table-data-grey"> 
                  <!-- Inner table contains actually data -->
                  <TABLE WIDTH="668" BORDER="0" CELLPADDING="4" CELLSPACING="1" class="table-data-grey">
                    <TR class="table-data-white"> 
                      <TD WIDTH="168" class="table-data-grey"> <A HREF="viewpubs.php?cat=<? echo $cat ?>&amp;sort=title&amp;count=<? echo $count?>" CLASS="table-data-grey">Publication 
                        Title</A> </TD>
                              
                      <TD WIDTH="150" class="table-data-grey"> <A HREF="viewpubs.php?cat=<? echo $cat ?>&amp;sort=docnum&amp;count=<? echo $count?>" CLASS="table-data-grey">LCS 
                        Document Number</A> </TD>
                              
                      <TD WIDTH="150" class="table-data-grey"> <A HREF="viewpubs.php?cat=<? echo $cat ?>&amp;sort=author&amp;count=<? echo $count?>" CLASS="table-data-grey">Publication 
                        Author</A> </TD>
                              
                      <TD WIDTH="100" class="table-data-grey"> <A HREF="viewpubs.php?cat=<? echo $cat ?>&amp;sort=group&amp;count=<? echo $count?>" CLASS="table-data-grey">LCS 
                        Group</A> </TD>
                              
                      <TD WIDTH="100" class="table-data-grey"> <A HREF="viewpubs.php?cat=<? echo $cat ?>&amp;sort=date&amp;count=<? echo $count?>" CLASS="table-data-grey">Publication 
                        Date</A> </TD>
                           </TR>

<?

//If the current page is the last page, check to see if there are enough remaining
//items to be displayed at a full $count.  If they are not, set $stopafter to be
//however many items are left, otherwise set $stopafter to be equal to $count

if($currentpage == (ceil(mysql_num_rows($result) / $count))) {
	$stopafter = ((mysql_num_rows($result)) - (($currentpage-1) * $count));
//	$stopafter = (($currentpage * $count) - (mysql_num_rows($result)));
}
elseif((mysql_num_rows($result)) < $count){
	$stopafter = mysql_num_rows($result);
}
else{
	$stopafter = $count;
}

//Display the items on the current page.  Stop after $stopafter items.

for ($i = 0; $i < $stopafter; $i++) {
	// grab the next row in the incoming message array
	$array = mysql_fetch_array($result);

	// localize array variables (useful for clarity, not really needed)
	$publication_id = $array["publication_id"]; 
	$lcs_doc_number = $array["lcs_doc_number"];
	$lcs_group = $array["lcs_group"];
	$publication_title = $array["publication_title"];
	$author_firstname = $array["author_firstname"];
	$author_lastname = $array["author_lastname"];
	$author_additional = $array["author_additional"];
	$publication_day = $array["publication_day"];
	$publication_month = $array["publication_month"];
	$publication_year = $array["publication_year"];
	$file_key = $array["file_key"];
	$url_additional = $array["url_additional"];
	$publication_abstract = $array["publication_abstract"];

	// grab lcs_group_name from lcs_groups table
	$array = mysql_fetch_array( mysql_query("SELECT * FROM lcs_groups WHERE lcs_group = \"$lcs_group\""));
	$lcs_group_name = $array["group_description"];
	
	if ($i % 2) {
	       $rowcolor = "table-data-grey";
	} else {
	       $rowcolor = "table-data-white";
	}
	echo "<TR CLASS=\"$rowcolor\">";
	echo "<TD CLASS='text' WIDTH='168'><A HREF=\"specpub.php?id=$publication_id\" onClick=\"javascript:window_pop('specpub.php?id=$publication_id') ;return false\">$publication_title</A></TD>"; 
	
	echo "<TD CLASS='text' WIDTH='150'>$lcs_doc_number</TD>";
	echo "<TD CLASS='text' WIDTH='150'>";
	if ($author_lastname == "") {
		echo "No Author Available";
	} else {
		echo "$author_lastname, $author_firstname";
	}
	echo "</TD>";
	echo "<TD CLASS='text' WIDTH='100'>$lcs_group_name</TD>";
	echo "<TD CLASS='text' WIDTH='100'>".$publication_month."-".$publication_day."-".$publication_year."</TD>";
	echo "</TR>";
	
	if ($debug) {
		// echo database information for debug reasons
		echo "<LI><B>Publication Id:</B> $publication_id";
		echo "<BR><B>LCS Document Number:</B> $lcs_doc_number";
		echo "<BR><B>LCS Group:</B> $lcs_group_name";
		echo "<BR><B>Publication Title:</B> $publication_title";
		echo "<BR><B>Publication Author:</B> $author_lastname, $author_firstname";
		echo "<BR><B>Publication Date:</B> " . $publication_month . "-" . $publication_day . "-" . $publication_year;
		echo "<BR><B>Additional URL:</B><A HREF=\"$url_additional\">$url_additional</A>";
		echo "<BR><B>Publication Abstract:</B> $publication_abstract";
	}
}
?>

                        </TABLE>
                        
                     </TD>
                  </TR>
				  <img src="/includes/dotorange.gif" width="100%" height="1">
               </TABLE>

            <img src="/includes/dotorange.gif" width="100%" height="1"> 
            <?

echo("<TABLE WIDTH=\"670\" CELLPADDING=\"0\" CELLSPACING=\"1\" BORDER=\"0\"><tr>");
if(mysql_num_rows($result) > $count) {
	echo("<td class=table-data-white align=\"left\">");
	echo("Page ".$currentpage." of ".ceil(mysql_num_rows($result) / $count));
	echo(" | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=".$count."\">First Page</a>");
	if($currentpage > 1) {
		echo(" | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=".$count."&amp;currentpage=".($currentpage - 1)."\">Previous Page</a>");
	}
	else {
		echo(" | Previous Page");
	}
	if($currentpage < ceil(mysql_num_rows($result) / $count)) {
		echo(" | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=".$count."&amp;currentpage=".($currentpage + 1)."\">Next Page</a>");
	}
	else {
		echo(" | Next Page");
	}
	echo(" | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=".$count."&amp;currentpage=".ceil(mysql_num_rows($result) / $count)."\">Last Page</a>");
	echo("</td><td class=table-data-white align=\"right\">");
	if($count==10) {
		echo("Show 10 | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=25\">25</a> | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=50\">50</a> | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=100\">100</a> per page");
	}
	if($count==25) {
		echo("Show <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=10\">10</a> | 25 | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=50\">50</a> | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=100\">100</a> per page");
	}
	if($count==50) {
		echo("Show <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=10\">10</a> | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=25\">25</a> | 50 | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=100\">100</a> per page");
	}
	if($count==100) {
		echo("Show <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=10\">10</a> | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=25\">25</a>  | <a href=\"viewpubs.php?cat=".$cat."&amp;sort=".$sort."&amp;count=50\">50</a> | 100 per page");
	}
	echo("</td></tr></table>");
}
?>
            <!--MAIN SECTION END-->
            
            </TR>
         </TABLE>

      <!--FOOTER SECTION START-->
      <?
	$curDir = dirname( getenv( "SCRIPT_FILENAME" ) ); 
	virtual("/includes/footer.html");
	chdir( $curDir );
?>
      <!--FOOTER SECTION END-->
    </TD>
   </TR>
</TABLE>

</BODY>
</HTML>
