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

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

<body>

<?php 
if (!IsSet($_REQUEST[title]) || !IsSet($_REQUEST[abstract]) || !IsSet($_REQUEST[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
  //
  $_REQUEST[title] = addslashes(htmlspecialchars($_REQUEST[title]));
  $_REQUEST[abstract] = addslashes(htmlspecialchars($_REQUEST[abstract]));
  $_REQUEST[authorInfo] = addslashes(htmlspecialchars($_REQUEST[authorInfo]));

  if (!IsSet($_FILES[uploadedFile])
      || $_FILES[uploadedFile]=="none"
      || !file_exists($_FILES[uploadedFile][tmp_name])) {
    $Conf->infoMsg("You are submitting a paper without specifying a file to upload. "
		   . "A placeholder file will be provided for you, but you should "
		   . "insure that you eventually replace that with your final paper. ");
  }
  $query="INSERT into Paper SET "
    . "title='$_REQUEST[title]', "
    . "abstract='$_REQUEST[abstract]', "
    . "authorInformation='$_REQUEST[authorInfo]', "
    . "contactId='" . $_SESSION[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($_FILES[uploadedFile][tmp_name],
				    $_FILES[uploadedFile][type],
				    $paperId);
      if ($result == 0 || DB::isError($result)) {
	$Conf->errorMsg("There was an error storing your paper."
			. "Please press BACK and try again.");
	if ( 0 ) {
	  $Conf->errorMsg("uploadedFile = $_FILES[uploadedFile], mimetype=$_FILES[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='" . $_SESSION[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", $_SESSION[Me]);

	} else {
	    
	  $result3 = $Conf->qe("INSERT into PaperConflict SET "
			       . "paperId='$paperId', "
			       . "authorId='$_SESSION[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", $_SESSION[Me]);
	  } else {
	    //
	      // Ok, now let's associate topics with the paper
	      //
	    for ($i = 0; $i < sizeof($_REQUEST[topics]); $i++) {
	      $thistopic=$_REQUEST[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()
				);
	      }
	    }

	    $_SESSION[Me] -> updateContactRoleInfo($Conf);
	    $Conf->confirmMsg("It looks like your paper has been successfully submitted "
			    . "and uploaded to the server as paper #$paperId. <b> There's still one step! </b> "
			      . "You need to " .
			      $Conf->mkTextButton("finalize your paper",
						  "FinalizePaper.php",
						  "<input type=hidden NAME=paperId value=$paperId>") .
			      " Your paper will not be reviewed "
			      . "until it has been finalized."
			      );

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

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

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