<?php

// +----------------------------------------------------------------------+
// | OpenConf                                                             |
// +----------------------------------------------------------------------+
// | Copyright (c) 2002-2015 Zakon Group LLC.  All Rights Reserved.       |
// +----------------------------------------------------------------------+
// | This source file is subject to the OpenConf License, available on    |
// | the OpenConf web site: www.OpenConf.com                              |
// +----------------------------------------------------------------------+

require_once "../include.php";
 
oc_sendNoCacheHeaders();

// Set number of author fields to display
if (isset($_POST['authornum']) && ctype_digit($_POST['authornum'])) {
	$oc_authorNum = $_POST['authornum'];
} else {
	$oc_authorNum = $OC_configAR['OC_authorsMinDisplay'];
}

printHeader(oc_('Submission'), 3);

// Check whether cfp still open
if (! $OC_statusAR['OC_submissions_open'] || ((defined('OCC_LICENSE_EXPIRES')) && (strtotime(OCC_LICENSE_EXPIRES) < time()))) {
	print '<p class="warn">' . oc_('Submissions are closed.') . '</p>';
	printFooter();
	exit;
}

// File upload settings
$uploadDir = $OC_configAR['OC_paperDir'];
$extAR = $OC_configAR['OC_extar'];
$fileNotice = ( isset($OC_configAR['OC_paperFldNote']) ? $OC_configAR['OC_paperFldNote'] : '' );
$formatDBFldName = 'format';
$fileFldName = 'File';

if (oc_hookSet('author-submit-preprocess')) {
	foreach ($GLOBALS['OC_hooksAR']['author-submit-preprocess'] as $hook) {
		require_once $hook;
	}
}

require_once OCC_FORM_INC_FILE;
require_once OCC_SUBMISSION_INC_FILE;

// Check whether this is a submission
if (isset($_POST['submit'])) {
	$err = '';
	$errInc = '';
	$qfields = array();	// fields to insert into submission table
	$afields = array(); // fields to insert into authors table
	$tfields = array(); // fields to insert into topics table
	$fileUploaded = false;
	
	require_once 'submission-validate.inc';
	
	// errors?
	if (!empty($err)) {
        print '<p><span class="err">' . oc_('Please check the following:') . '<ul>' . $err . $errInc . '</ul></span><p /><hr /><p />';
		// remove uploaded file?
		if (isset($_FILES['file']['tmp_name']) && is_file($_FILES['file']['tmp_name'])) {
			unlink($_FILES['file']['tmp_name']);
		}
	} else {
		// Check that paper hasn't been submitted yet; if it has notify author and bail
		$q = "SELECT `" . OCC_TABLE_PAPER . "`.`paperid` FROM `" . OCC_TABLE_PAPER . "`, `" . OCC_TABLE_AUTHOR . "` WHERE `" . OCC_TABLE_PAPER . "`.`title`='" . safeSQLstr($_POST['title']) . "' AND `" . OCC_TABLE_AUTHOR . "`.`paperid`=`" . OCC_TABLE_PAPER . "`.`paperid` AND `" . OCC_TABLE_AUTHOR . "`.`position`=`" . OCC_TABLE_PAPER . "`.`contactid` AND `" . OCC_TABLE_AUTHOR . "`.`name_last`='" . safeSQLstr($_POST['name_last'.$_POST['contactid']]) . "'";
		$r = ocsql_query($q) or err(oc_('Unable to verify whether submission has already been made'));
		if (ocsql_num_rows($r) > 0) {
			print '<p><span class="err">' . oc_('This submission appears to already have been made; please check your email for a confirmation. ');
			if ($OC_statusAR['OC_edit_open']) {
				print sprintf(oc_('You may review or edit the submission <a href="%s">here</a>.'), 'edit.php') . '  ';
			}
			print oc_('Please contact the Chair with any questions.') . '</span></p>';
			printFooter();
			exit;
		}
		
		$backupMsg = '';

		// add paper
		$q = "INSERT INTO `" . OCC_TABLE_PAPER . "` SET `submissiondate`='" . safeSQLstr(date("Y-m-d")) . "', `lastupdate`='" . safeSQLstr(date("Y-m-d")) . "'";
		foreach ($qfields as $qid => $qval) {
			$q .= ", `" . $qid . "`=" . $qval;
		}
		$r = ocsql_query($q) or err(oc_('unable to process submission'));

        $backupMsg .= "$q\n\n";

		// get paper ID
		$pid = ocsql_insert_id() or err(oc_('unable to get submission ID'));
	
	    // add authors
		foreach ($afields as $qid => $qar) {
			$q = "INSERT INTO `" . OCC_TABLE_AUTHOR . "` SET `paperid`=" . $pid . ", `position`=" . $qid;
			foreach ($qar as $qqid => $qqval) {
				$q .= ", `" . $qqid . "`=" . $qqval;
			}
			$r = ocsql_query($q) or err(oc_('unable to add one or more authors, but submission added.  Please edit submission.'));
			$backupMsg .= "$q\n\n";
		}
	
	    // add topic(s)
		if (!empty($tfields)) {
			$q = "INSERT INTO `" . OCC_TABLE_PAPERTOPIC . "` (`paperid`,`topicid`) VALUES";
			foreach ($tfields as $t) {
				$q .= " ($pid,$t),";
			}
			$r = ocsql_query(rtrim($q, ',')) or err(oc_('unable to add submission topic, but submission and authors added'));
			$backupMsg .= "$q\n\n";
		}

		if (!empty($OC_configAR['OC_subBackupEmail'])) {
			sendEmail($OC_configAR['OC_subBackupEmail'], "Submission ID $pid SQL", $backupMsg);
		}

		$formFields = oc_('Submission ID') . ": " . $pid . "\n\n" . oc_genFieldMessage($OC_submissionFieldSetAR, $OC_submissionFieldAR, $_POST);

		// confirm it
		$confirmmsg = '';
		
		// File Uploaded?
		if ($fileUploaded) {
			$fileName = $uploadDir . $pid . '.' . $_POST['format'];
			if (oc_saveFile($_FILES['file']['tmp_name'], $fileName, $_POST['format'])) {
				$formFields .= "\n\n" . oc_('File') . ': ' . oc_('uploaded') . "\n"; // confirm to user
				// update format
				$fq = "UPDATE `" . OCC_TABLE_PAPER . "` SET `" . $formatDBFldName . "`='" . safeSQLstr($_POST['format']) . "' WHERE `paperid`='" . $pid . "' LIMIT 1";
				ocsql_query($fq);  // note no error check
			} else {
				$formFields .= "\n\n" . oc_('File') . ':' . oc_('NOT uploaded') . "\n"; // confirm to user
				$confirmmsg .= '<p class="warn">' . sprintf(oc_('Your file failed to load properly.  Please try <a href="%s">uploading just the file</a> or contact the Chair.'), 'upload.php') . '</p>';
			}
		}

		if (isset($OC_configAR['OC_subConfirmNotice'])) {
			if (preg_match("/<\w+>/", $OC_configAR['OC_subConfirmNotice'])) { // HTML?
				$confirmmsg .= oc_(preg_replace("/[\r\n]/", "", $OC_configAR['OC_subConfirmNotice']));
			} else {
				$confirmmsg .= nl2br(oc_($OC_configAR['OC_subConfirmNotice']));
			}
		} else {
			//T: [:code:] should be left untranslated
			$confirmmsg .= oc_('<p><strong>Thank you for your submission. Your submission ID number is [:sid:]. Please write this number down and include it in any communications with us.</strong></p><p><strong>Below is the information submitted. We have also emailed a copy to the submission contact. If you notice any problems or do <em>not</em> receive the email within 24 hours, please contact us.</strong></p><p>[:formfields:]</p>');
		}
		
	    $mailbody = sprintf(oc_('Thank you for your submission to %s.  Below is a copy of the information submitted for your records.'), $OC_configAR['OC_confName']) . "\n\n" . $formFields;
		
		$mailsubject = sprintf(oc_('Submission ID %s'), $pid);

		if (oc_hookSet('author-submit-save')) {
			foreach ($GLOBALS['OC_hooksAR']['author-submit-save'] as $hook) {
				require_once $hook;
			}
		}
		
		$confirmmsg = preg_replace("/\[:sid:\]/", $pid, $confirmmsg);
		$confirmmsg = preg_replace("/\[:formfields:\]/", '<br />' . nl2br(safeHTMLstr($formFields)), $confirmmsg);
		
		print $confirmmsg;
	
		sendEmail($contactemail, $mailsubject, $mailbody, $OC_configAR['OC_notifyAuthorSubmit']);
	
		printFooter();

		// log
		ocsql_query("INSERT INTO `" . OCC_TABLE_LOG . "` SET `datetime`='" . safeSQLstr(gmdate('Y-m-d H:i:s')) . "', `type`='submission', `entry`='" . safeSQLstr('Submission ID ' . $pid . ' made.  Title: ' . $_POST['title']) . "'");

		exit;
	} // else no errors
}

if (!empty($OC_configAR['OC_paperSubNote'])) {
	print '<div>' . (preg_match("/\<(?:p|br) ?\/?\>/", $OC_configAR['OC_paperSubNote']) ? $OC_configAR['OC_paperSubNote'] : nl2br($OC_configAR['OC_paperSubNote'])) . '</div><p><hr /></p>';
}

print '
<form method="post" enctype="multipart/form-data" action="' . $_SERVER['PHP_SELF'] . '" class="ocform" id="makesub">
<input type="hidden" name="authornum" id="authornum" value="' . $oc_authorNum . '" />
';

oc_displayFieldSet($OC_submissionFieldSetAR, $OC_submissionFieldAR, $_POST);

if (oc_hookSet('author-submit-fields')) {
	foreach ($GLOBALS['OC_hooksAR']['author-submit-fields'] as $hook) {
		require_once $hook;
	}
}

print '
<div class="note">' . oc_('Please check over your entries, making sure everything is filled out.  When ready, click on the Make Submission button below once.') . '</div>
<p><input type="submit" name="submit" id="submit" value="' . oc_('Make Submission') . '" class="submit" /></p>
<span id="processing" style="position: relative; visibility: hidden;">' . oc_('Processing...') . '</span>
</form>
<script type="text/javascript">
oc_setupProcessingForm("makesub");
</script>
';


printFooter();

?>
