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

<html>
<? $Conf->header("Modify Paper #$paperId for $Conf->shortName") ?>

<body>
<?
if (!IsSet($paperId) ) {
  $Conf -> errorMsg("You didn't specify a paper to modify.");
  exit;
}

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


$query = "SELECT acknowledged, withdrawn  FROM Paper " 
  . " WHERE Paper.paperId=$paperId";

$result = $Conf->qe($query);
$row = $result->fetchRow();
$finalized=$row[0];
$withdrawn=$row[1];

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;
}

//
// Process any updates
//
if (IsSet($submit)) {
  if (!IsSet($title) || !IsSet($abstract) || !IsSet($authorInfo) ) {
    $Conf->errorMsg("One or more of the required fields is not set. "
		    . "Please press BACK and correct this. ");
  } else {

    $title = addslashes($title);
    $abstract = addslashes($abstract);
    $authorInfo = addslashes($authorInfo);
    if($mockok != 1){
      $mockok = 0;
    }
    $query="UPDATE Paper SET "
      . "title='$title', "
      . "abstract='$abstract', "
      . "mockok=$mockok, "
      . "authorInformation='$authorInfo' WHERE paperId=$paperId "
      ;

    $result = $Conf->q($query);
    if ( DB::isError($result) ) {
      $Conf->errorMsg("Update failed: " . $result->getMessage());
      exit;
    } else {
      $Conf->infoMsg("Updated information for #$paperId");

      $Conf->log("Updated paper information for $paperId", $Me);

      $query="DELETE FROM PaperTopic WHERE paperId=$paperId";
      $Conf->qe($query);
      //
      // Now, update the paper topics..
      //
      for ($i = 0; $i < sizeof($topics); $i++) {
	$thistopic=$topics[$i];
	$query="INSERT into PaperTopic SET "
	  . " topicId='$thistopic', "
	  . " paperId='$paperId' "
	  ;
	$result2 = $Conf->qe($query);
	if ( DB::isError($result2) ) {
	  $Conf->errorMsg("I was unable to associate one of your topics "
			  . "with your paper due to a database error. "
			  . "The message was " . $result2->getMessage()
			  );
	}
      }
    }
  }
}


  $query = "SELECT title, abstract, authorInformation, "
  . " acknowledged, withdrawn, mockok "
  . " FROM Paper " 
  . " WHERE Paper.paperId=$paperId";

  $result = $Conf->q($query);
if ( DB::isError($result) ) {
  $Conf->errorMsg("Unable to read information about paper: "
		  . $result->getMessage());
  exit;
} 

$row = $result->fetchRow();
$title=$row[0];
$abstract=$row[1];
$authorInfo=$row[2];
$finalized=$row[3];
$withdrawn=$row[4];
$mockok=$row[5];
?>

<p>
Modify any of the following information, and then hit "Update Paper"
at the top or bottom of the page.
</p>

<table align=center>
<tr>
<td>
<?
$Conf->textButton("Click here to go to the paper list",
		  "../Author/PaperActions.php");
?>
</td>
</tr>
</table>

&nbsp;<br>
&nbsp;<br>

<form method="POST" action="<?echo $PHP_SELF ?>"
ENCTYPE="multipart/form-data" >
<center> <input type="submit" value="Update Fields" name="submit"> </center>
<p>
<input type="hidden" value="<? echo $paperId?>" name="paperId">
<table border="0" width="100%" bgcolor="<?echo $Conf->bgOne ?>">
<tr>
  <td>Title:</td>
  <td> <input type="text" name="title" size="75"
	value="<?echo stripslashes($title)?>"
	MAXLENGTH=200></td>
</tr>

<tr>
<td>Abstract:</td>
<td><textarea rows="30" name="abstract" cols="75"><?echo stripslashes($abstract)?></textarea> </td>
</tr>

<tr>
  <td>Author Names and institutions, one per line:</td>
  <td> <textarea rows=10 name="authorInfo" cols=75><?echo stripslashes($authorInfo)?></textarea></td>
</tr>

<tr>
<td>
Would you be willing to let your paper be
reviewed by students in shadow program
committees? See
<a href="/nsdi04/shadow.htm">this page</a> for details.
</td>
<td>
<? if($mockok == 1) { $chch = "checked"; } else { $chch = ""; } ?>
<p><input type=checkbox name=mockok value=1 "<?echo $chch ?>"><p>
</td>
</tr>

<?
    //
    // Get the topics for this paper - I couldn't figure
    // out how to do this as a left join
    //

$query="SELECT PaperTopic.topicId FROM PaperTopic "
. " WHERE PaperTopic.paperId=$paperId ";
$result = $Conf->q($query);
$topics=array();
if ( DB::isError($result) ) {
  $Conf->errorMsg("Error in query " . $result->getMessage());
} else if ($result->numRows() > 0) {
  while($row=$result->fetchRow()) {
    $topic=$row[0];
    $topics[$topic] = 1;
  }
}

$query="SELECT TopicArea.topicAreaId, TopicArea.topicName FROM TopicArea ";
$result = $Conf->q($query);
if ( DB::isError($result) ) {
  $Conf->errorMsg("Error in query " . $result->getMessage());
} else if ($result->numRows() > 0) {
?>
 <tr>
    <td width="16%" height="19">Topic (select any that apply)</td>
    <td width="84%" height="19">
<?
  while ($row = $result->fetchRow()) {
    $id = $row[0];
    $topic = $row[1];
    if (IsSet($topics[$id])) {
      $checked = " CHECKED ";
    } else {
      $checked ="";
    }
    print "<INPUT type=checkbox name=topics[] value=$id $checked> $topic <br>";
  }
?>
</td>
</tr>
<?
	      }
?>
</table>
<div align="center"> <center>
<p> <input type="submit" value="Update Fields" name="submit"> </p>
</center> </div>
</form>

<? $Conf->footer() ?>

</body>

</html>
