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

<html>
<? $Conf->header("Confirming Paper Submission to $ConfShortName") ?>

<body>

<?
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 {
  //
  // All fields are here -- try to insert the paper
  //
  $title = addslashes(htmlspecialchars($title));
  $abstract = addslashes(htmlspecialchars($abstract));
  $authorInfo = addslashes(htmlspecialchars($authorInfo));

  if (!IsSet($uploadedFile)
      || $uploadedFile=="none"
      || !file_exists($uploadedFile)) {
    $Conf->infoMsg("You are submitting a paper without specifying a file to uploaded. "
		   . "A placeholder file will be provided for you, but you should "
		   . "insure that you eventually replace that with your final paper. ");
  }
  if($mockok != 1){
    $mockok = 0;
  }
  $query="INSERT into Paper SET "
    . "title='$title', "
    . "abstract='$abstract', "
    . "authorInformation='$authorInfo', "
    . "mockok=$mockok, "
    . "contactId='$Me->contactId' "
    ;
  $result = $Conf->qe($query);
  if ( DB::isError($result) ) {
    $Conf->errorMsg("There was a problem adding your paper to the database. "
		    . "The error message was " . $result->getMessage() . " "
		    . "Please press BACK and try again.");
  } else {
    $query2 = "SELECT LAST_INSERT_ID()";
    $result = $Conf->qe($query2);
    if ( DB::isError($result) ) {
      $Conf->errorMsg("There was a problem retrieving your paper ID from the database. "
		      . "The error message was " . $result->getMessage() . " "
		      . "Please press BACK and try again.");
    } else {
      $row = $result->fetchRow();
      $paperId = $row[0];

      $result = $Conf -> storePaper($uploadedFile,
				    $uploadedFile_type,
				    $paperId);
      if (DB::isError($result)) {
	$Conf->errorMsg("There was an error storing your paper."
			. "Please press BACK and try again.");
	if ( 1 ) {
	  $Conf->errorMsg("uploadedFile = $uploadedFile, mimetype=$uploadedFile_type, paperId=$paperId");
	  $Conf->errorMsg("error = " . $result->getMessage());
	}
      } else {
	//
	  // Ok - the paper is loaded. Now, update the author 
	  // information and the topic information
	  //
	
	$query = "INSERT into PaperAuthor SET "
	  . "paperId='$paperId', "
	  . "authorId='$Me->contactId' "
	  ;

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

	if (DB::isError($result3)) {
	  $Conf->errorMsg("There was a problem associating the "
			  . " paper with you (the contact author). "
			  . "The error message was " . $result3->getMessage() . " "
			  . "Please press BACK and try again.");

	  $Conf->qe("DELETE from Paper WHERE paperId='$paperId'");

	  $Conf->log("Problem creating PaperAuthor for $paperId", $Me);

	} else {
	    
	  $result3 = $Conf->qe("INSERT into PaperConflict SET "
			       . "paperId='$paperId', "
			       . "authorId='$Me->contactId' ");

	  if ( DB::isError($result3) ) {
	    $Conf->errorMsg("There was another problem associating the paper with you (the contact author). "
			    . "The error message was " . $result3->getMessage() . " "
			    . "Please press BACK and try again.");
	    $Conf->qe("DELETE from Paper WHERE paperId='$paperId'");
	    $Conf->qe("DELETE from PaperAuthor WHERE paperId='$paperId'");
	    $Conf->log("Problem creating PaperConflict for $paperId", $Me);
	  } else {
	    //
	      // Ok, now let's associate topics with the paper
	      //
	    for ($i = 0; $i < sizeof($topics); $i++) {
	      $thistopic=$topics[$i];
	      $query="INSERT into PaperTopic SET "
		. " topicId='$thistopic', "
		. " paperId='$paperId' "
		;
	      $result = $Conf->qe($query);
	      if (DB::isError($result)) {
		$Conf->errorMsg("I was unable to associate one of your topics "
				. "with your paper due to a database error. "
				. "The message was " . $result->getMessage()
				);
	      }
	    }

	    $Me -> updateContactRoleInfo($Conf);
	    $Conf->confirmMsg("It looks like your paper has been successfully submitted "
			    . "and uploaded to the server as paper #$paperId. <b></b> "
			      . "If you've uploaded your final paper and it is ready for review, "
                              . "you can <a href=\"FinalizePaper.php?paperId=$paperId\"> "
			      . "finalize</a> it. If you're not done yet, you can return "
                              . "to <a href=\"/nsdi04/\">this site</a> later to finish."
			      );

	    //
	    // Send them happy email
	    //
	    $Conf->sendPaperStartNotice($Me->email, $paperId, $title);

	    $Conf->log("Submit paper $paperId: $title", $Me);
	  }
	}
      }
    }
  }
}
?>

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