<?php

// +----------------------------------------------------------------------+
// | OpenConf                                                             |
// +----------------------------------------------------------------------+
// | Copyright (c) 2002-2011 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";

beginChairSession();

printHeader("Conflicts",1);

if (isset($_POST['submit']) && ($_POST{'submit'} == "Unset Conflicts (UC)") && (!empty($_POST['drop']))) {
	// Check for valid submission
	if (!validToken('chair')) {
		warn('Invalid submission');
	}

	foreach ($_POST['drop'] as $val) {
		if (preg_match("/^\d+,\d+$/",$val)) {
			list($pid,$rid) = split(",",$val);
			issueSQL("DELETE FROM " . OCC_TABLE_CONFLICT . " WHERE paperid='".$pid."' AND reviewerid='".$rid."'");
		}
		else { print "Unable to process $val.<p>\n"; }
	}
	print '<p class="note" align="center">Conflicts have been unset.</p>';
	print '<p align="center"><a href="' . $_SERVER['PHP_SELF'] . '">Return to Conflict Listings</a></p>';
	printFooter();
	exit;
}

$pq = "SELECT " . OCC_TABLE_CONFLICT . ".paperid, " . OCC_TABLE_CONFLICT . ".reviewerid, CONCAT_WS(' '," . OCC_TABLE_REVIEWER . ".name_first," . OCC_TABLE_REVIEWER . ".name_last) AS name, title FROM " . OCC_TABLE_PAPER . ", " . OCC_TABLE_REVIEWER . ", " . OCC_TABLE_CONFLICT . " WHERE " . OCC_TABLE_CONFLICT . ".paperid=" . OCC_TABLE_PAPER . ".paperid AND " . OCC_TABLE_CONFLICT . ".reviewerid=" . OCC_TABLE_REVIEWER . ".reviewerid";

if (empty($_GET['s']) || ($_GET['s'] == "pid")) { 
	$q = $pq . " ORDER BY " . OCC_TABLE_CONFLICT . ".paperid, " . OCC_TABLE_CONFLICT . ".reviewerid";
	$sortid = "paper";
	$pidsort="<nobr>P-ID</nobr><br />" . $OC_sortImg; 
	$psort='<a href="'.$_SERVER['PHP_SELF'].'?s=paper">Submission</a>'; 
	$rsort='<a href="'.$_SERVER['PHP_SELF'].'?s=reviewer">Reviewer</a>'; 
	$ridsort='<nobr><a href="'.$_SERVER['PHP_SELF'].'?s=rid">R-ID</a></nobr>'; 
} elseif ($_GET['s'] == "paper") {
	$q = $pq . " ORDER BY " . OCC_TABLE_PAPER . ".title, " . OCC_TABLE_CONFLICT . ".reviewerid";
	$sortid = "paper";
	$pidsort='<nobr><a href="'.$_SERVER['PHP_SELF'].'?s=pid">P-ID</a></nobr>';  
	$psort="Submission<br />" . $OC_sortImg;
	$rsort='<a href="'.$_SERVER['PHP_SELF'].'?s=reviewer">Reviewer</a>'; 
	$ridsort='<nobr><a href="'.$_SERVER['PHP_SELF'].'?s=rid">R-ID</a></nobr>'; 
} elseif ($_GET['s'] == "rid") {
	$q = $pq . " ORDER BY " . OCC_TABLE_CONFLICT . ".reviewerid, " . OCC_TABLE_CONFLICT . ".paperid";
	$sortid = "reviewer";
	$pidsort='<nobr><a href="'.$_SERVER['PHP_SELF'].'?s=pid">P-ID</a></nobr>'; 
	$psort='<a href="'.$_SERVER['PHP_SELF'].'?s=paper">Submission</a>'; 
	$rsort='<a href="'.$_SERVER['PHP_SELF'].'?s=reviewer">Reviewer</a>'; 
	$ridsort="<nobr>R-ID</nobr><br />" . $OC_sortImg; 
} elseif ($_GET['s'] == "reviewer") {
	$q = $pq . " ORDER BY " . OCC_TABLE_REVIEWER . ".name_last, " . OCC_TABLE_REVIEWER . ".name_first, " . OCC_TABLE_CONFLICT . ".paperid";
	$sortid = "reviewer";
	$pidsort='<nobr><a href="'.$_SERVER['PHP_SELF'].'?s=pid">P-ID</a></nobr>'; 
	$psort='<a href="'.$_SERVER['PHP_SELF'].'?s=paper">Submission</a>'; 
	$rsort="Reviewer<br />" . $OC_sortImg;
	$ridsort= '<nobr><a href="'.$_SERVER['PHP_SELF'].'?s=rid">R-ID</a></nobr>'; 
} else {
	err("Unknown sort source");
}

print '
<dl><dt><strong>Links:</strong></dt>
<dd><em>Reviewer</em> &#8211; Show reviewer info</dd>
<dd><em>Submission</em> &#8211; Show submission info</dd>
</dl>
<p><hr /></p>
<p><strong>Manually Set Conflicts:</strong></p>
';

$r = ocsql_query($q) or err("Unable to get information ".mysql_errno());
if (mysql_num_rows($r) == 0) {
        print '<p class="note"> &nbsp; &nbsp; &nbsp; No conflicts have been set.</p>';
} else {
	$s = substr($sortid,0,1);
	
	print '
<form method="post" action="' . $_SERVER['PHP_SELF'] . '">
<input type="hidden" name="token" value="' . $_SESSION[OCC_SESSION_VAR_NAME]['chairtoken'] . '" />
<input type="hidden" name="src" value="' . $s . '">
<table border="0" cellspacing="1" cellpadding="4" COLS=3>
<tr><td align="right" colspan="5"><span style="border: 6px solid #ccf;"><input type="submit" name="submit" value="Unset Conflicts (UC)" /></span></td></tr>
<tr class="rowheader"><th valign="top" style="width: 4em;">' . $pidsort . '</th><th valign="top">' . $psort . '</th><th valign="top" style="width: 4em;">' . $ridsort . '</th><th valign="top">' . $rsort . '</th><th bgcolor="#ccccff">UC</th></tr>
	';
	$currid = -1;
	$row = 1;
	while ($l = mysql_fetch_array($r)) {

		if ($sortid == "reviewer") {
			$ptags = '<td align="right">'.$l['paperid'].'</td><td><a href="show_paper.php?pid=' . $l['paperid'] . '">' . safeHTMLstr($l['title']) . '</a></td>';
			$rtags = '<td align="right">' . $l['reviewerid'] . '</td><td><a href="show_reviewer.php?rid=' . $l['reviewerid'] . '">' . safeHTMLstr($l['name']) . '</a></td>';
		} else {
			$ptags = '<td align="right">'.$l['paperid'].'</td><td><a href="show_paper.php?pid=' . $l['paperid'] . '">' . safeHTMLstr($l['title']) . '</a></td>';
			$rtags = '<td align="right">'.$l['reviewerid'].'</td><td><a href="show_reviewer.php?rid=' . $l['reviewerid'] . '">' . safeHTMLstr($l['name']) . '</a></td>';
		}
		
		$blanktags = '<td>&nbsp;</td><td>&nbsp;</td>';

		if ($currid != $l[$sortid.'id']) {
			if ($currid != -1) {
	        	if ($row==1) { $row=2; } else { $row=1; }
			}
			$currid = $l[$sortid.'id'];
		} else {
			if ($sortid == "reviewer") { $rtags = $blanktags; }
			else { $ptags = $blanktags; }
		}
		
		print '<tr class="row' . $row . '">' . $ptags . $rtags;
		print '<td align="center" bgcolor="#ccccff">';
		if (empty($l['reviewerid'])) { print '&nbsp;'; }
		else {
			print '<input type="checkbox" name="drop[]" value="'.$l['paperid'].','.$l['reviewerid'].'">';
		}
		print '</td>';

		print "</tr>\n";
	}
	print '
<tr><td align="right" colspan="5"><span style="border: 6px solid #ccf;"><input type="submit" name="submit" value="Unset Conflicts (UC)" /></span></td></tr>
</table></form>
';
}

// Show auto-detected conflicts
print '
<p><hr /></p>
<p><strong>Automatically Detected Conflicts:</strong> (email ' . ($OC_configAR['OC_allowOrgConflict'] ? '' : 'or organization ')  . 'matches)</p>
';
$q = "SELECT " . OCC_TABLE_AUTHOR . ".paperid, reviewerid, title, CONCAT_WS(' '," . OCC_TABLE_REVIEWER . ".name_first," . OCC_TABLE_REVIEWER . ".name_last) AS name FROM " . OCC_TABLE_AUTHOR . ", " . OCC_TABLE_PAPER . ", " . OCC_TABLE_REVIEWER . " WHERE ";
$q .= OCC_TABLE_PAPER . ".paperid=" . OCC_TABLE_AUTHOR . ".paperid AND ";
$q .= "(" . OCC_TABLE_AUTHOR . ".email=" . OCC_TABLE_REVIEWER . ".email";
$q .= " OR (" . OCC_TABLE_AUTHOR . ".organization<>'' AND " . OCC_TABLE_AUTHOR . ".organization=" . OCC_TABLE_REVIEWER . ".organization)";
$q .= ") GROUP BY paperid, reviewerid ORDER BY paperid, reviewerid";
$r = ocsql_query($q) or err("Unable to get auto paper/reviewer conflicts ".mysql_errno());
if (mysql_num_rows($r) < 1) {
	print '<p class="note"> &nbsp; &nbsp; &nbsp; No conflicts detected</p>';
} else {
	print '<table border="0" cellspacing="1" cellpadding="4"><tr class="rowheader"><th>Submission</th><th>Reviewer</th></tr>';
	$row = 1;
	while ($l=mysql_fetch_array($r)) {
		print '<tr class="row' . $row . '"><td>' . $l['paperid'] . '. <a href="show_paper.php?pid=' . $l['paperid'] . '">' . safeHTMLstr($l['title']) . '</a></td><td>' . $l['reviewerid'] .'. <a href="show_reviewer.php?rid=' . $l['reviewerid'] . '">' . safeHTMLstr($l['name']) . '</a></td>';
			$row = $rowAR[$row];
	}
	print '</table>';
}

// Check for addt'l (hook) conflict displays
if (isset($OC_hooksAR['list_conflicts-display']) && !empty($OC_hooksAR['list_conflicts-display'])) {
	foreach ($OC_hooksAR['list_conflicts-display'] as $hook) {
	print '<p><hr /></p>';
		require_once $hook;
	}
}

printFooter();

?>