<?
include('auth.php');
include('common.php');

//propagate the data we need
if ($_GET['setting_id'])	$sid= $_GET['setting_id']; else 	$sid = $_POST['setting_id'];
if ($_GET['t'])$dbt= $_GET['t']; else $dbt = $_POST['t'];

if ($dbt==3)$colname="skill"; else if ($dbt==4)$colname="resource"; else $colname="name";
	
$do_close=0;

if (count($_POST) > 0)
{
	$order = secure_user_input($_POST['order']);
	$thename = secure_user_input($_POST['thename']);
	$show = secure_user_input($_POST['show']);
	
	if (!$sid)
	{
	
		$SQL="INSERT INTO ".$_SETTINGTABLES[$dbt]." 
			(
			`".$colname."`, 
			`listorder`, 
			`show` 
			)
			VALUES (
			'".$thename."',
			'".$order."',
			'".$show."' 			
			)";
	}
	else
	{
		$SQL="UPDATE ".$_SETTINGTABLES[$dbt]." 
			SET
				`".$colname."`= '".$thename."',
				`listorder`=  '".$order."',
				`show`=  '".$show."' 
				WHERE id=".$sid;
	}
	$results = llnb_query($SQL);
	$do_close=1;
}
else if ($sid)
{

	//fetch the learning goal and populate
	$SQL="SELECT * FROM ".$_SETTINGTABLES[$dbt]." 
		WHERE id=".$sid;
	$results = llnb_query($SQL);
	$row = llnb_fetch_assoc($results);
	
	if ($row['show'] == 1)
		$showchecked="checked";
	else
		$showchecked="";
	
}
else //default case for blanks
{
	//fetch the learning goal and populate
	$SQL="SELECT * FROM ".$_SETTINGTABLES[$dbt]." 
		ORDER BY listorder DESC";
	$results = llnb_query($SQL);
	$row = llnb_fetch_assoc($results);
	$row['listorder']=$row['listorder']+1;
	unset($row[$colname]);

}

?>

<head>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/llnb-style.css">
</head>

<style type="text/css">
body { background-color: #fff; background-image:none; padding: 20px;}

div { font-family: sans-serif; color: #222; }

.btn	{ -moz-border-radius: 7px; -webkit-border-radius: 7px; -khtml-border-radius: 7px; border-radius: 7px; border: 1px #222 solid; background-image: url(img/btn-bg-nor.jpg) ; line-height: 25px; padding: 3px 10px 0; text-decoration: none; color: #222; }
.btn:hover	{ border-color: #33C; color: #33c;}

.btnhidden {border-color: #CCC; background-image: none; font-size: .8em;}

.btncnt	{ width: 160px; text-align:center; display: block; margin-bottom: 10px;}

.btnlrg	{width: 300px; padding-left: 20px;}
.btnsml	{ padding: 1px 10px; font-weight: bold;}
.btnpg	{ padding: 2px 4px 1px; font-size: .75em;margin-right: 4px;}

.lfr	{ float: right; margin-right: 10px;}
.lfl	{ float: left; margin-right: 10px;}
form label { display:block; margin-top: 10px; font-size: .8em; color: #555; }
.session { margin-bottom: 15px; padding-bottom: 10px;}
.talrg	{ width: 460px; height: 60px;}

input.ssminp[type="text"] {width: 40px;}
input.sminp[type="text"] {width: 80px;}
input.btn { height: 30px;}


</style>

<body onLoad="document.forms.setting.order.focus()">
<? 
if ($do_close) 
	echo "<script>parent.location.href='setting_list.php?t=".$dbt."';</script>"; 

?>

<div style="width: 600px;">
<h2>Add or Edit Setting for:<br />
<?=$_SETTINGTABLES_USR_DISPLAY[$dbt];?></h2>
<form name="setting" action="setting_list_m.php" method="post">
<div class="input">

        <div class="input">
          <label>Order:</label>
               <input name="order" type="text"  class="ssminp" value="<?= ($_POST['order']) ? ($_POST['order']) : $row['listorder']; ?>">
        </div>
         
        <div class="input">
          <label>Name of setting item:</label>
               <input name="thename" type="text"   class="lrginp" value="<?= ($_POST['thename']) ? ($_POST['thename']) : $row[$colname]; ?>">
        </div>
         
        <div class="input">
          <label>Show:</label>
               <input name="show" type="checkbox" <?=$showchecked;?>  value="1">
        </div>
         
		<div style="clear:both;"></div>


</div>

<div style="clear:both;"></div>

<p class="clearfix">
<input type="submit" class="btnlfr_m" value="Save &amp; Close"> 
<a href="#" onclick="parent.SqueezeBox.close();"class="btn lfr">Cancel</a></p>


<input type="hidden" name="setting_id" value="<?=$sid;?>">
<input type="hidden" name="t" value="<?=$dbt;?>">

</form>
</div>