<?
include('../Code/confHeader.inc');
$Me -> goIfInvalid("../index.php");
$Me -> goIfNotChair('../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;
}
//
// 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 {

    if (!IsSet($finalized)) {
      $finalized = 0;
    }

    if (!IsSet($withdrawn)) {
      $withdrawn = 0;
    }

    $title = addslashes($title);
    $abstract = addslashes($abstract);
    $authorInfo = addslashes($authorInfo);
    $authorsResponse = addslashes($authorsResponse);
    $query="UPDATE Paper SET "
      . "contactId='$contactId', "
      . "title='$title', "
      . "abstract='$abstract', "
      . "acknowledged='$finalized', "
      . "withdrawn='$withdrawn', "
      . "authorsResponse='$authorsResponse', "
      . "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);

      if ( ! IsSet($uploadedFile) || $uploadedFile == "none" ) {
	$Conf->errorMsg("Did NOT change the paper itself");
      } else {
	$Conf->errorMsg("Paper #$paperId being updated");
	$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->q("DELETE FROM PaperStorage "
			     . " WHERE ( PaperStorage.paperId='$paperId' )");

	  if (! $result) {
	    $Conf->errorMsg("Could not delete the paper, may be necessary for replacing.");
	  }
	  else {

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

	    if (DB::isError($result)) {
	      $Conf->errorMsg("There was an error opening the file to store your paper."
			      . "Please press BACK and try again.");
	      $Conf->errorMsg("msg is " . $result->getMessage());
	    } else {
	      $Conf->infoMsg("Looks like paper #$paperId was updated, but confirm it");

	      $Conf->log("Replace paper $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' "
	  ;
	$resultTopic=$Conf->qe($query);
	if (DB::isError($resultTopic)) {
	  $Conf->errorMsg("I was unable to associate one of your topics "
			  . "with your paper due to a database error. "
			  . "The message was " . $resultTopic->getMessage()
			  );
	}
      }
    }
  }
}


  $query = "SELECT title, abstract, authorInformation, "
  . " acknowledged, withdrawn, contactId, authorsResponse "
  . " 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];
$contactId=$row[5];
$authorsResponse=$row[6];
?>

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

<table>
<tr>
<td>
<?
$Conf->textButton("Click here to go back to viewing the paper",
		  "../PC/PCAllAnonReviewsForPaper.php",
		  $Conf->mkHiddenVar('paperId', $paperId)
		  );
print "</td><td>";
$Conf->textButton("Click here to go to the paper list",
		  "../Assistant/AssistantListPapers.php");
?>
</td>
</tr>
</table>

<form method="POST" action="<?echo $PHP_SELF ?>"
ENCTYPE="multipart/form-data" >
<center> <input type="submit" value="Update" name="submit"> </center>
<input type="hidden" value="<? echo $paperId?>" name="paperId">

<table border="0" width="100%" bgcolor="<?echo $Conf->bgOne ?>">

<tr>
<th> Contact Email </th>
<td>
<SELECT name=contactId SINGLE>
  <?
  $q2 = "SELECT ContactInfo.contactId, ContactInfo.firstName, ContactInfo.lastName, ContactInfo.email "
. " FROM ContactInfo ORDER BY ContactInfo.email";
 $r2 = $Conf->qe($q2);
 print "<OPTION VALUE=\"-1\"> Remember to select a contact!</OPTION>";
 if ($r2) {
   while($row2=$r2->fetchRow()) {
     print "<OPTION VALUE=\"$row2[0]\" ";
     if ( $row2[0] == $contactId) {
       print " SELECTED ";
     }
     print "> $row2[1] $row2[2] ($row2[3]) </OPTION>";
   }
 }
?>
</SELECT>
</td>
</tr>

<tr>
  <th>Title</th>
  <td> <input type="text" name="title" size="75"
	value="<?echo stripslashes($title)?>"
	MAXLENGTH=200></td>
</tr>

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

<tr>
<th> Finalized? </th>
<td>
<INPUT type=checkbox name=finalized value=1
<? if ($finalized) echo "CHECKED"?> Yes
</td>
</tr>

<tr>
<th> Withdrawn? </th>
<td>
<INPUT type=checkbox name=withdrawn value=1
<? if ($withdrawn) echo "CHECKED"?> Yes
</td>
</tr>

<tr>
  <th>Author Information<br>
      (list all authors in order of appearance)</th>
  <td> <textarea rows=10 name="authorInfo" cols=75><?echo stripslashes($authorInfo)?></textarea></td>
</tr>

<tr>
  <th>Author Review Response</th>
  <td> <textarea rows=10 name="authorsResponse" cols=75><?echo stripslashes($authorsResponse)?></textarea></td>
</tr>

<tr>
   <th>Location of PDF or PostScript File</th>
   <td> <input type="file" name="uploadedFile" ACCEPT="text/pdf" size="69"></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" name="submit"> </p>
</center> </div>
</form>

<? $Conf->footer() ?>

</body>

</html>
