<?
include('../Code/confHeader.inc');
$Me -> goIfInvalid("../index.php");
$Me -> goIfNotAssistant('../index.php');
$Conf -> connect();

function olink($key,$string)
{
  return "<a href=\"$PHP_SELF?orderBy=$key\"> $string </a>";
}

?>

<html>

<? $Conf->header("List All Submitted Papers") ?>

<body>
<?
if (!IsSet($ORDER)) {
  $ORDER="ORDER BY Paper.paperId";
}
if (IsSet($orderBy)) {
  $ORDER = "ORDER BY $orderBy";
}
?>
<h2> List of submitted papers and authors </h2>
<p>
This page shows you all the papers that have been entered into the database.
<br>
If you click on the paper title, a window will pop up with the paper
abstract and a link to download the paper.
<br>
If you click on the contact author email address, you can send them email.
<br>
If you click on the table headers, the table will be sorted using
that specified criteria.
</p>

<?
if ($Me->isChair &&
    IsSet($updateFinalized) && IsSet($markFinalized) && IsSet($allPapers)) {
  for ($i = 0; $i < sizeof($allPapers); $i++) {
    $p = $allPapers[$i];
    $mark=0;
    if ( IsSet($markFinalized[$p] ) ) {
      $mark = 1;
    } else {
      $mark = 0;
    }
    $query="UPDATE Paper SET acknowledged=$mark WHERE paperId=$p";
    $Conf->qe($query);
  }
}

$finalizedStr = "";
if ( $onlyFinalized ) {
  $finalizedStr =  " AND Paper.acknowledged!=0 ";
}

$withdrawnStr = "";
if ( $onlyWithdrawn ) {
  $withdrawnStr =  " AND Paper.withdrawn!=0 ";
}


  $query="SELECT Paper.paperId, Paper.title, "
  . " Paper.acknowledged, Paper.withdrawn, "
  . " Paper.authorInformation, Paper.contactId, "
  . " ContactInfo.firstName, ContactInfo.lastName, "
  . " ContactInfo.email, ContactInfo.affiliation, "
  . " LENGTH(PaperStorage.paper), PaperStorage.mimetype "
  . " FROM Paper, ContactInfo "
  . " LEFT JOIN PaperStorage ON (PaperStorage.paperId=Paper.paperId)"
  . " WHERE ContactInfo.contactId = Paper.ContactId "
  . $finalizedStr . $withdrawnStr
  . $ORDER;

$result=$Conf->q($query);

  if (DB::isError($result)) {
    $Conf->errorMsg("Error in retrieving paper list "
		    . $result -> getMessage());
  } else {

    $numpapers = $result->numRows($result);
?>

Found <? echo $numpapers ?> papers.

<FORM method="POST" action="<?echo $PHP_SELF?>">
<INPUT type=checkbox name=onlyWithdrawn value=1
   <? if ($onlyWithdrawn) {echo "checked";}?> > Only show withdrawn </br>

<INPUT type=checkbox name=onlyFinalized value=1
   <? if ($onlyFinalized) {echo "checked";}?> > Only show finalized </br>

<input type=hidden name="ORDER" value="<?echo $ORDER?>">

<input type="submit" value="Update View" name="submit">

<?
print "<table align=center>\n";
print "<tr> <td>\n";
$Conf->toggleButtonUpdate('SeeAuthorInfo');
$Conf->toggleButton('SeeAuthorInfo',
		    "Hide Author Information",
		    "See Author Information"
		    );
print "</td> <td>\n";
print "</td> </tr>\n";
print "</table>";


?>
</p>


<table border="1" width="100%" cellpadding=0 cellspacing=0>
<thead>
<tr>
<th width= 5%> <? echo olink("Paper.paperId", "Paper #") ?></th>
<th>
   <? echo olink("Paper.title", "Title") ?>  <br>
   <? echo olink("LENGTH(PaperStorage.paper)", "(size)") ?> 
</th>
   <? if ($SeeAuthorInfo) { ?>
<th width=30%>
<? echo olink("ContactInfo.lastName", "Author") ?>
<br>
<? echo olink("ContactInfo.email", "(email)") ?>
</th>
<th width=30%>
<? echo olink("ContactInfo.affiliation", "Affiliation") ?>
</th>
			    <?}?>
</tr>

<?
   $i = 0;
 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC) ){
     
     $paperId=$row['paperId'];
     $title=$row['title'];
     $author=nl2br($row['affiliation']);
     $authorInfo = " "
     . $row['firstName'] . " " . $row['lastName']
     . "<br> ( <a href=\"mailto:"
     . $row['email']
     . "\""
     . " TITLE=\"Concerning paper#$paperId\" "
     . ">"
     . $row['email'] . "</a> ) ";

     $finalized = "";

     if ( $row['acknowledged'] ) {
       $finalized = " <b> FINALIZED </b> ";
     }

     if ($row['mimetype'] == NULL) {
       $withdrawn = "<b> WITHDRAWN </b> \n";
     } else {
	$withdrawn = "<b> " . $row['LENGTH(PaperStorage.paper)']
	. " $bytes bytes "
	. $row['mimetype']
	. "</b>\n";
     }

  ?>
  <tr>
  <td> <? echo $paperId; ?></td>
  <td>
     <?
     if ($Me->isChair) {
       $link="../PC/PCAllAnonReviewsForPaper.php";
     } else {
       $link="AssistantViewSinglePaper.php";
     }
     ?>
  <a href="<?echo $link?>?paperId=<?echo $paperId?>" target=_blank>
  <? echo $title; echo "<br>"; echo $withdrawn ; echo $finalized ?>
  </a>
  </td>
						   <? if ($SeeAuthorInfo) { ?>
  <td> <? echo $authorInfo?> </td>
  <td> <? echo $author ?> </td>
     <? } ?>
  </tr>

  <?
}
}
?>
     </table>
</FORM>
</body>
<? $Conf->footer() ?>
</html>

