<?php
/*---------------------------------------------------+
| PHP-Fusion 6 Content Management System
+---------------------------------------------------------+
| Copyright (c) 2002-2006 Nick Jones
| http://www.php-fusion.co.uk/
+---------------------------------------------------------*/
require_once "../../maincore.php";
require_once BASEDIR."subheader.php";

$dsettings = dbarray(dbquery("SELECT * FROM ".$db_prefix."term_settings"));
$type = $dsettings['term_type'];
$usergroup = $dsettings['term_usergroup'];

if (!iMEMBER) fallback(BASEDIR."index.php");

if (iADMIN) { 
require_once ADMIN."navigation.php"; 
} ELSE {
require_once BASEDIR."side_left.php";
}

if (file_exists(INFUSIONS."terms/".$type."locale/".$settings['locale'].".php")) {
	include INFUSIONS."terms/".$type."locale/".$settings['locale'].".php";
} else { include INFUSIONS."terms/".$type."locale/English.php"; }

require_once INFUSIONS."terms/include/functions.php"; 

	if (isset($term_id) && !isNum($term_id)) fallback(FUSION_SELF);
	if (!isset($step)) $step = "";
	
	$result = dbquery("SELECT * FROM ".DB_PREFIX."term_cats");
	if (dbrows($result) != 0) {
		if ((iADMIN || checkgroup($usergroup)) AND $step == "delete") {
			$result = dbquery("DELETE FROM ".DB_PREFIX."terms WHERE term_id='$term_id'");
			redirect("terms.php?term_cat_id=$term_cat_id");
		}
		if ( isset($_POST['list']) )   {fallback(INFUSIONS."terms/terms.php") ; }
		if ( isset($_POST['save_term']) )   {
			$term_title = stripinput($_POST['term_title']);
			$term_descript = stripinput($_POST['term_descript']);
			if ($step == "edit") {
				$termsquery = "UPDATE ".DB_PREFIX."terms SET term_title='$term_title', term_descript='$term_descript', term_cat='$term_cat', term_sender='$term_sender', term_image='$term_image', term_rating='$term_rating', term_datestamp='".time()."', term_status='$term_status' WHERE term_id='$term_id'";
				$result = dbquery($termsquery);
				redirect("terms_admin.php");
			} else {
				$termsquery = "INSERT INTO ".DB_PREFIX."terms VALUES('', '$term_title', '$term_descript', '$term_cat', '$term_sender', '$term_image', '".time()."', '$term_rating', '$term_status')";
				$result = dbquery($termsquery);
				redirect("terms_admin.php");
			}
		}
		
		if ((iADMIN || checkgroup($usergroup)) AND $step == "edit") {
			$result = dbquery("SELECT * FROM ".DB_PREFIX."terms WHERE term_id='$term_id'");
			$data = dbarray($result);
			$term_title = $data['term_title'];
			$term_descript = $data['term_descript'];
			$term_sender = $data['term_sender'];
			$term_image = $data['term_image'];
			$term_status = $data['term_status'];
			$formaction = FUSION_SELF."?step=edit&term_cat_id=$term_cat_id&term_id=$term_id";
			opentable($locale['term101']." - ".$locale['term302']);
		} else {
			$term_title = "";
			$term_descript = "";
			$term_sender = $userdata['user_name'];
			$term_image = "";
			$term_status = "2";
			$formaction = FUSION_SELF;
			opentable($locale['term101']." - ".$locale['term303']);
		}
		$editlist    = ""; 
		$result2 = dbquery("SELECT * FROM ".DB_PREFIX."term_cats ORDER BY term_cat_name");
		if (dbrows($result2) != 0) {
			while ($data2 = dbarray($result2)) {
				if ($step == "edit") $sel = ($data['term_cat'] == $data2['term_cat_id'] ? " selected" : "");
				$editlist .= "<option value='".$data2['term_cat_id']."'$sel>".$data2['term_cat_name']."</option>\n";
			}
		}
		echo "<table border='0' cellpadding='0' cellspacing='0' width='100%'>\n";;
		echo "<tr><td width='100%' align='center'>";
if (iADMIN || checkgroup($usergroup)) {
		term_toolbar(); } else {
	echo "<table cellpadding='1' cellspacing='1' border='0'>";
	echo "<tr>\n";
	echo "<td valign='bottom' height='18' nowrap><b>".$locale['term213']."</b></td>\n";
	echo "</tr>\n";
	echo "<table>\n";
		}
		echo "</td></tr>\n";
		echo "</table>\n";
		tablebreak();
		echo "<form ENCTYPE='multipart/form-data' name='inputform' method='post' action='$formaction'>\n";
		echo "<table border='0' cellpadding='0' cellspacing='1' width='100%' align='center'><tr>";
		echo "<td width='100%'>";
		echo "<table cellspacing='1' cellpadding='3' width='100%' border='0'>";
		echo "<tr><td width='100%'>\n";
		echo "<table width='100%' border='0' cellpadding='0' cellspacing='1'>\n";
		echo "<tr><td width='200' class='tbl1' align='right'>".$locale['term207'].": </td>\n";
		echo "<td class='tbl1'><select name='term_cat' class='textbox' style='width:380px;'>$editlist</select></td></tr>\n"; 
/*
			if (iADMIN || checkgroup($usergroup)) {
				echo "<tr><td width='130' class='tbl1' align='right'>".$locale['term207'].": </td>\n";
				echo "<td class='tbl1'><select name='term_cat' class='textbox' style='width:380px;'>$editlist</select></td></tr>\n"; }
			else {
		echo "<tr><td colspan='2'><input type='hidden' name='term_cat' value='1'></td></tr>\n"; }
*/
		echo "<tr><td class='tbl1' align='right'>".$locale['term204'].": </td>\n";
		echo "<td class='tbl1'><input type='text' name='term_title' value='$term_title' class='textbox' style='width:380px'></td></tr>\n";
		echo "<tr><td class='tbl1' valign='top' align='right'>".$locale['term205'].": </td>\n";
		echo "<td class='tbl1'><textarea name='term_descript' rows='2' class='textbox' style='width:380px;height:100px;'>".$term_descript."</textarea></td>\n";
		echo "</tr>\n";
		echo "<tr><td class='tbl1'> </td><td class='tbl1' align='left'>\n";
		echo "<input type='button' value='b' class='button' style='font-weight:bold;width:25px;' onClick=\"addText('term_descript', '[b]', '[/b]');\">\n";
		echo "<input type='button' value='i' class='button' style='font-style:italic;width:25px;' onClick=\"addText('term_descript', '[i]', '[/i]');\">\n";
		echo "<input type='button' value='u' class='button' style='text-decoration:underline;width:25px;' onClick=\"addText('term_descript', '[u]', '[/u]');\">\n";
		echo "<input type='button' value='url' class='button' style='width:30px;' onClick=\"addText('term_descript', '[url]', '[/url]');\">\n";
		echo "<input type='button' value='center' class='button' style='width:45px;' onClick=\"addText('term_descript', '[center]', '[/center]');\">\n";
		echo "<input type='button' value='small' class='button' style='width:40px;' onClick=\"addText('term_descript', '[small]', '[/small]');\">\n";
		echo "<input type='button' value='code' class='button' style='width:40px;' onClick=\"addText('term_descript', '[code]', '