<?php

// +----------------------------------------------------------------------+
// | OpenConf                                                             |
// +----------------------------------------------------------------------+
// | Copyright (c) 2002-2008 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";
require_once "../include-submissions.inc";

beginChairSession();

printHeader("Submissions",1);

$verbAR = array(
	'Delete'	=> 'deleted',
	'Withdraw'	=> 'withdrawn',
	'Restore'	=> 'restored'
);

if (isset($_POST['action']) && preg_match("/^(Delete|Withdraw|Restore) Submissions$/", $_POST['action'], $matches) && !empty($_POST['papers'])) {
	$action = $matches[1];

	// Check for valid submission
	if (!validToken('chair')) {
		warn('Invalid submission');
	}

	foreach ($_POST['papers'] as $paperid) {
		if (preg_match("/^\d+$/",$paperid)) {
			// restore paper?
			if ($action == 'Restore') {
				print "restoring id $paperid ...<p />\n";
				restorePaper($paperid);
			} else {
				// withdraw paper?
				if ($action == 'Withdraw') {
					print "withdrawing id $paperid ...<p />\n";
					if (! withdrawPaper($paperid, 'Chair')) {
						print " &nbsp; &nbsp; SUBMISSION NOT FOUND<p />\n";
					}
				} else {
					print "deleting id $paperid ...<p>\n";
				}
				// delete paper
				deletePaper($paperid);
			}
		} else {
			print "Unable to process submission id " . safeHTMLstr($paperid) . ".<p>\n";
		}
	}
	print '
	Submission(s) have been ' .  $verbAR[$action] . '.<p>
	<a href="list_papers.php">Return to Submission Listings</a><p>
	';
	printFooter();
	exit;
}

if (!isset($_GET['s']) || ($_GET['s'] == "id")) {
	$sortby = "paperid";
	$rsortstr = 'ID<br />' . $OC_sortImg;
	$tsortstr = '<a href="'.$_SERVER['PHP_SELF'].'?s=title">Title</a>';
	$nsortstr = '<a href="'.$_SERVER['PHP_SELF'].'?s=name">Contact Author</a>';
	$ssortstr = '<a href="'.$_SERVER['PHP_SELF'].'?s=student">Stud.</a>';
} elseif ($_GET['s'] == "title") {
	$sortby = "title";
	$tsortstr = 'Title<br />' . $OC_sortImg;
	$rsortstr = '<a href="'.$_SERVER['PHP_SELF'].'?s=id">ID</a>';
	$nsortstr = '<a href="'.$_SERVER['PHP_SELF'].'?s=name">Contact Author</a>';
	$ssortstr = '<a href="'.$_SERVER['PHP_SELF'].'?s=student">Stud.</a>';
} elseif ($_GET['s'] == "student") {
	$sortby = "student, paperid";
	$ssortstr = 'Stud.<br />' . $OC_sortImg;
	$tsortstr = '<a href="'.$_SERVER['PHP_SELF'].'?s=title">Title</a>';
	$rsortstr = '<a href="'.$_SERVER['PHP_SELF'].'?s=id">ID</a>';
	$nsortstr = '<a href="'.$_SERVER['PHP_SELF'].'?s=name">Contact Author</a>';
} else {	// name sort
	$sortby = "name_last, name_first";
	$nsortstr = 'Contact Author<br />' . $OC_sortImg;
	$rsortstr = '<a href="'.$_SERVER['PHP_SELF'].'?s=id">ID</a>';
	$tsortstr = '<a href="'.$_SERVER['PHP_SELF'].'?s=title">Title</a>';
	$ssortstr = '<a href="'.$_SERVER['PHP_SELF'].'?s=student">Stud.</a>';
}

$q = "SELECT " . OCC_TABLE_PAPER . ".paperid, CONCAT_WS(' ',name_first,name_last) AS name, title, format, student FROM " . OCC_TABLE_PAPER . " LEFT JOIN " . OCC_TABLE_AUTHOR . " ON " . OCC_TABLE_AUTHOR . ".paperid=" . OCC_TABLE_PAPER . ".paperid AND " . OCC_TABLE_AUTHOR . ".position=" . OCC_TABLE_PAPER . ".contactid ORDER BY $sortby";
$r = ocsql_query($q) or err("Unable to get submissions");
if (mysql_num_rows($r) == 0) {
	print '<span class="warn">No submissions have been made yet.</span><p>';
} else {
	print "Count: ".mysql_num_rows($r)."<p>\n";

	print '
	<span class="note"><b>Note:</b> When deleting a submission, all records associated with the submission is permanently removed.  When withdrawing a submission, all reviews and assignments, uploaded files, etc, are permanently removed; the submission (paper) and author table data however may be restored.</span><p>
	<form method="post" action="' . $_SERVER['PHP_SELF'] . '">
	<input type="hidden" name="token" value="' . $_SESSION[OCC_SESSION_VAR_NAME]['chairtoken'] . '" />

	<p align="left"><table border=0 cellpadding=5 cellspacing=0 bgcolor="#ccccff"><tr><td><input type="submit" name="action" value="Delete Submissions"> &nbsp; <input type="submit" name="action" value="Withdraw Submissions"></td></tr></table></p>

	<table border="0" cellspacing="1" cellpadding="4" cols="4">
	<tr class="rowheader"><th class="del" title="Delete / Withdraw">D/W</th><th>' . $rsortstr . '</th><th>' . $tsortstr . '</th><th>' . $nsortstr . '</th>';
	if ($OC_configAR['OC_trackStudent']) { print '<th>' . $ssortstr . '</th>'; }
	print '<th>File</th></tr>
';

	$row = 1;
	while ($l = mysql_fetch_array($r)) {
		print '<tr class="row' . $row . '"><td class="del" width="25">&nbsp;<input type="checkbox" name="papers[]" value="' . $l['paperid'] . '"></td><td align="right" width="40">' . $l['paperid'] . '</td><td><a href="show_paper.php?pid=' . $l['paperid'] . '">' . safeHTMLstr($l['title']) . '</a></td><td>' . safeHTMLstr($l['name']) . '</td>';
		if ($OC_configAR['OC_trackStudent']) { 
			print '<th>';
			if ($l['student'] == 'T') {
				print '<span style="color:#090;">S</span>';
			} else { print '&nbsp;'; }
			print '</th>';
		}
		print '<th>';
		if (isset($l['format']) && !empty($l['format']) && is_file($OC_configAR['OC_paperDir'] . ($file = $l['paperid'].'.'.$l['format']))) {
			print '<a href="../review/paper.php?c=1&p=' . $file . '"><img src="../images/document-sm.gif" border="0" alt="view paper" title="view paper" width="13" height="16" /></a>';
		} else {
			print '&nbsp;';
		}
		print '</th></tr>';
		if ($row==1) { $row=2; } else { $row=1; }
	}
	
	print '
	</table>
	<div align="left"><table border=0 cellpadding=5 cellspacing=0 bgcolor="#ccccff"><tr><td><input type="submit" name="action" value="Delete Submissions"> &nbsp; <input type="submit" name="action" value="Withdraw Submissions"></td></tr></table></div>
	</form>
	';
}

$q = "SELECT `paperid`, `title`, `contact_author`, `contact_email`, `withdraw_date`, `withdrawn_by` FROM `" . OCC_TABLE_WITHDRAWN . "` ORDER BY `paperid`";
$r = ocsql_query($q) or err('Unable to check for withdrawn submissions');
if (mysql_num_rows($r) > 0) {
	print '
<a name="withdrawn"></a>
<p><hr /></p>

<p style="text-align: center; font-weight: bold; font-size: 1.1em">Withdrawn Submissions</p>

<p class="note"><b>Note:</b> Restoring a withdrawn submission will not restore all the submission data, only the information stored in the submission (paper) and authors table.</p>

<form method="post" action="' . $_SERVER['PHP_SELF'] . '">
	<input type="hidden" name="token" value="' . $_SESSION[OCC_SESSION_VAR_NAME]['chairtoken'] . '" />
<p align="left"><table border=0 cellpadding=5 cellspacing=0 bgcolor="#ccccff"><tr><td><input type="submit" name="action" value="Restore Submissions"></td></tr></table></p>
<table border="0" cellspacing="1" cellpadding="4" cols="4">
<tr class="rowheader"><th class="del" title="Restore">&nbsp;</th><th>ID</th><th>Title</th><th>Contact Author</th><th>Withdrawn By / On</th></tr>
';

	$row = 1;
	while ($l = mysql_fetch_assoc($r)) {
		print '<tr class="row' . $row . '"><td class="del" width="25"><input type="checkbox" name="papers[]" value="' . $l['paperid'] . '"></td><td align="right" width="40">' . $l['paperid'] . '</td><td>' . safeHTMLstr($l['title']) . '</td><td><a href="mailto:' . safeHTMLstr($l['contact_email']) . '">' . safeHTMLstr($l['contact_author']) . '</a></td><td>' . safeHTMLstr($l['withdrawn_by']) . ' / ' . safeHTMLstr($l['withdraw_date']) . '</tr>';
		if ($row==1) { $row=2; } else { $row=1; }
	}

print '
</table>
<div align="left"><table border=0 cellpadding=5 cellspacing=0 bgcolor="#ccccff"><tr><td><input type="submit" name="action" value="Restore Submissions"></td></tr></table></div>
</form>
';
}

printFooter();

?>
