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

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

<body>

<?
if (!IsSet($submittedFor) || !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. ");
  }
  $query="INSERT into Paper SET "
    . "title='$title', "
    . "abstract='$abstract', "
    . "authorInformation='$authorInfo', "
    . "contactId='$submittedFor' "
    ;
  $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("SQL 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='$submittedFor' "
	  ;

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

	  $Conf->deletePaper($paperId, 0);

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

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

	  if (DB::isError($result) ) {
	    $Conf->errorMsg("There was another problem associating the paper with you (the contact author). "
			    . "The error message was " . $result->getMessage() . " "
			    . "Please press BACK and try again.");
	    $Conf->deletePaper($paperId, 0);
	    $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.");

	    //
	    // Send them happy email
	    //

	    $result = $Conf->qe("SELECT email FROM ContactInfo WHERE contactId='$submittedFor'");
	    if ( !DB::isError($result) ) {
	      $row = $result->fetchRow();
	      $Conf->sendPaperStartNotice($row[0], $paperId, $title);
	      $Conf->sendPaperStartNotice($Conf->contactEmail, $paperId, $title);
	      $Conf->log("Submit paper $paperId on behalf of $row[0]: $title", $Me);
	    }
	  }
	}
      }
    }
  }
}
?>

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