<?php

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

beginSession();
$uid = $_SESSION[OCC_SESSION_VAR_NAME]['acreviewerid'];

$OC_displayTop = '
<a href="' . $_SERVER['PHP_SELF'] . '?module=oc_discussion&action=list_threads.php&access=2">' . oc_('Discussion Threads') . '</a> &#187; 
';

printHeader(oc_('Discussion Options'), 2);

// submission?
if (isset($_POST['ocaction']) && ($_POST['ocaction'] == 'Save Options')) {
	if (isset($_POST['oc_discussion_notification']) && preg_match("/^[01]$/", $_POST['oc_discussion_notification'])) {
		// delete current setting if any
		$q = "DELETE FROM `" . OCC_TABLE_MODULE_OC_DISCUSSION_NOTIFICATION . "` WHERE `accountid`='" . safeSQLstr($uid) . "'";
		$r = ocsql_query($q) or err('Unable to update notification option');
		// add setting if no email to be received
		if ($_POST['oc_discussion_notification'] == 0) {
			$q = "INSERT INTO `" . OCC_TABLE_MODULE_OC_DISCUSSION_NOTIFICATION . "` SET `accountid`='" . safeSQLstr($uid) . "', `notification`=0";
			$r = ocsql_query($q) or err('Unable to update notification option (2)');
		}
		print '<p style="text-align: center" class="note2">' . oc_('Options saved') . '</p>';
	}
}

// Get notification status if ! chair
$notification = 1;
if ($uid != 0) {
	$q = "SELECT `notification` FROM `" . OCC_TABLE_MODULE_OC_DISCUSSION_NOTIFICATION . "` WHERE `accountid`='" . safeSQLstr($uid) . "'";
	$r = ocsql_query($q) or err('Unable to retrieve notification status');
	if (ocsql_num_rows($r) == 1) {
		$l = ocsql_fetch_assoc($r);
		if ($l['notification'] == 0) {
			$notification = 0;
		}
	}
}

$notificationOptions = '<label><input type="radio" name="oc_discussion_notification" value="1" /> ' . oc_('On') . '</label> &nbsp; &nbsp; <label><input type="radio" name="oc_discussion_notification" value="0" /> ' . oc_('Off') . '</label>';

print '
<form method="post" action="' . OCC_SELF . '">
<input type="hidden" name="ocaction" value="Save Options" />
<p><strong>' . oc_('Email Notification:') . '</strong> ' . preg_replace("/(value=\"" . $notification . "\")/", "$1 checked", $notificationOptions) . '<br />
<span class="note">' . oc_('Receive email notification whenever a post is made for a submission you are assigned to') . '</span>
</p>

<p><input type="submit" name="oc_discussion_post" value="' . oc_('Save Options') . '" /></p>
</form>
';

printFooter();