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

<html>

<? $Conf->header("Upload New Content For Paper #$paperId") ?>

<body>
<?
if (!IsSet($paperId) || $paperId == 0) {
  $Conf->errorMsg("No paper was selected for uploading?" );
  exit;
} 

if ( ! $Me -> amPaperAuthor($paperId, $Conf) ) {
  $Conf -> errorMsg("Only the submitting paper author can modify the "
		    . "paper information.");
  exit;
}

$query = "SELECT Paper.title, Paper.abstract, Paper.authorInformation, "
    . " Paper.withdrawn, Paper.acknowledged "
    . " FROM Paper WHERE "
    . " Paper.contactId='$Me->contactId' "
    . " AND Paper.paperId=$paperId "
//    . " AND PaperStorage.paperId=$paperId"
;

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

if ( DB::isError($result) ) {
    $Conf->errorMsg("SQL error - " . $result->getMessage());
    exit();
} 

if ($result -> numRows() != 1) {
  $Conf->errorMsg("There appears to be a problem retrieving your paper information.");
}

$row = $result->fetchRow(DB_FETCHMODE_ASSOC);

$title = $Conf->safeHtml($row['title']);
$abstract = $Conf->safeHtml($row['abstract']);
$authorInfo = $Conf->safeHtml($row['authorInformation']);
$withdrawn = $row['withdrawn'];
$finalized = $row['acknowledged'];

if ($withdrawn) {
  $Conf->infoMsg("This paper has been WITHDRAWN "
		 . " -- you can't do anything here");
  //      exit;
}

if ($finalized) {
  $Conf->infoMsg("This paper has been FINALIZED "
		 . " -- you can't do anything here");
  exit;
}

//
// Now check for a paper in paper storage - we do 
// this w/o a join for robustness in case somehow the paper
// went away.
//
$result = $Conf->qe("SELECT mimetype from PaperStorage "
		    . " WHERE paperId='$paperId'");

if ( DB::isError($result) ) {
    $Conf->errorMsg("SQL error - " . $result->getMessage());
    exit();
} 

if ($result -> numRows() != 1) {
  $mimetype="application/txt";
  $Conf->errorMsg("There appears to be a problem retrieving some of "
		  . "the information about your paper, but you should "
		  . "be able to upload a new copy anyway. ");
} else {
  $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
  $mimetype = $row['mimetype'];
}

    
//
// Now actually replace the paper
//
if (IsSet($submit)) {
  if ( ! IsSet($uploadedFile) || $uploadedFile == "none" ) {
    $Conf->errorMsg("Did NOT change the paper itself");
  } else {
    $fn = fopen($uploadedFile, "r");

    if ( ! $fn ) {
      $Conf->errorMsg("There was an error opening the file to store your paper."
		      . "Please press BACK and try again.");
    } else {

      $result = $Conf -> storePaper($uploadedFile,
				    $uploadedFile_type,
				    $paperId);

      if ($result == 0 || DB::isError($result)) {
	$Conf->errorMsg("There was an error when trying to update your paper. "
			. " Please try again.");
	if ( DB::isError($result) ) {
	  $Conf->errorMsg("msg is " . $result->getMessage());
	}
      } else {
	$Conf->infoMsg("Looks like paper #$paperId was updated, but "
		       . "you may want to confirm this by downloading "
		       . "your paper ");
	$Conf->log("Replace paper $paperId", $Me);
      }
    }
  }
}

print "<center>";
$Conf->textButton("Click here to go to the paper list",
		  "../Author/PaperActions.php");
print "</center>";

?>

<table border=1 bgcolor="<?echo $Conf->bgOne?>" width=100%>
<tr> <th colspan=2> Paper # <? echo $paperId ?> </th> <tr>
<tr> <th width=20%> Title: </th> <td> <? echo $title ?> </td> </tr>
<tr> <th width=20%> Author Information: </td> <td> <? echo $authorInfo ?> </td> </tr>
<tr> <th width=20%> Abstract: </th> <td> <? echo $abstract ?> </td> </tr>
<th width=20%> Indicated Topics </th>
<td>
<? $Conf->listTopicsForPaper($paperId) ?>
</td>
</table>

<br> <br> 
<? $Conf->infoMsg("If you wish to upload a new version of your paper, "
		  . " enter the file name here and click 'Update Paper Contents'. ");
?>

<form method="POST" action="<?echo $PHP_SELF ?>"
	ENCTYPE="multipart/form-data" >
Location of PDF or PostScript File
<br>
<input type="file" name="uploadedFile" ACCEPT="application/pdf" size="70">
<div align="left"> 
<input type="hidden" name=paperId value=<?echo $paperId?>>
<input type="submit" value="Update Paper Contents" name="submit">
</div>
</form>



<? $Conf->footer() ?>

</body>
</html>
