<?php
// $Id: admin_forums.php,v 1.1 2004/07/11 10:28:22 Farsus Exp $
// -------------------------------------------------------------------------//
//			                 RUNCMS                                         //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// Based on:
// phpBB: http://www.phpBB.com
// License type: GPL
// ------------------------------------------------------------------------- //

include_once("./admin_header.php");

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function forums_config() {
global $myts, $newbbConfig, $HTTP_POST_VARS;

if (!$HTTP_POST_VARS['submit']) {

if ($newbbConfig['disc_show'] == 1) {
	$sel1 = " selected='selected'";
	} else if ($newbbConfig['disc_show'] == 2) {
		$sel2 = " selected='selected'";
		} else if ($newbbConfig['disc_show'] == 3) {
			$sel3 = " selected='selected'";
		}

$img_set = XoopsLists::getDirListAsArray("../images/");
?>
<table width="100%" border="0" cellspacing="0" cellpadding="5"><tr>
<form method="post" action="admin_forums.php">
<td colspan="2" class="bg2"><?php echo _MD_A_CONFIGFORUM;?></td>
</tr><tr>
<td><?php echo _MD_A_IMG_SET;?>:</td>
<td><select class="select" name="image_set">
<?php
foreach ($img_set as $key) {
	echo "<option value='$key'";
	if ($newbbConfig['image_set'] == $key) {
		echo " selected='selected'";
	}
	echo ">$key</option>";
}
?>
</select></td>
</tr><tr>
<td colspan="2"><hr size="1" noshade="noshade" /></td>
</tr><tr>
<td><?php echo _MD_A_SHOW_DIS;?>:</td>
<td><select class="select" name="disc_show">
<option value="0"><?php echo _NONE;?></option>
<option value="1"<?php echo $sel1;?>><?php echo _POST;?></option>
<option value="2"<?php echo $sel2;?>><?php echo _REPLY;?></option>
<option value="3"<?php echo $sel3;?>><?php echo _MD_A_BOTH;?></option>
</select></td>
</tr><tr>
<td valign="top"><?php echo _MD_A_DISCLAIMER;?>:</td>
<td>
<?php
$disclaimer = join('', file("../cache/disclaimer.php"));
$disclaimer = trim($disclaimer);
$desc       = new XoopsFormDhtmlTextArea('', 'disclaimer', $disclaimer, 10, 50);
echo $desc->render();
echo "<br /><br />";
echo _ALLOWEDHTML."<br />";
echo get_allowed_html();
?>
</td>
</tr><tr>
<td>&nbsp;</td>
<td>
<input type="hidden" name="mode" value="config">
<input type="submit" class="button" name="submit" value="<?php echo _SAVE;?>">
</td>
</tr></form></table>

<?php
} else {
$content  = "<?php\n";
$content .= "\$newbbConfig['disc_show'] = ".intval($HTTP_POST_VARS['disc_show']).";\n";
$content .= "\$newbbConfig['image_set'] = '".$myts->makeTboxData4Save($HTTP_POST_VARS['image_set'])."';\n";
$content .= "?>";

$filename = "../cache/config.php";
if ( $file = fopen($filename, "w") ) {
	fwrite($file, $content);
	fclose($file);
	} else {
		redirect_header("index.php", 1, _NOTUPDATED);
		exit();
	}

$filename = "../cache/disclaimer.php";
if ( $file = fopen($filename, "wb") ) {
	$disclaimer = $myts->oopsStripSlashesGPC($HTTP_POST_VARS['disclaimer']);
	fwrite($file, $disclaimer);
	fclose($file);
	} else {
		redirect_header("index.php", 1, _NOTUPDATED);
		exit();
}

redirect_header("index.php", 1, _UPDATED);
exit();
}
}


//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function forums_editforum() {
global $db, $myts, $bbUrl, $HTTP_POST_VARS, $HTTP_GET_VARS;

$forum = !empty($HTTP_POST_VARS['forum']) ? $HTTP_POST_VARS['forum'] : $HTTP_GET_VARS['forum'];

// Show forum select box
if ( empty($HTTP_POST_VARS['save']) && !empty($forum) ) {
	$sql   = "SELECT * FROM ".$db->prefix("bb_forums")." WHERE forum_id=$forum";
	if (!$result = $db->query($sql)) {
		CloseTable();
		xoops_cp_footer();
		exit();
	}
	if (!$myrow = $db->fetch_array($result)) {
		echo _MD_A_NOSUCHFORUM;
	}
	$name = $myts->makeTboxData4Edit($myrow['forum_name']);
	$desc = $myts->makeTboxData4Edit($myrow['forum_desc']);
?>

<form action="./admin_forums.php" method="post">
<table border="0" cellpadding="1" cellspacing="0" align="center" valign="top" width="95%"><tr>
<td class="bg2">
<table border="0" cellpadding="1" cellspacing="1" width="100%"><tr class="bg3">
<td align="center" colspan="2"><b><?php echo _MD_A_EDITTHISFORUM;?></b></td>
</tr><tr class="bg1">
<td colspan="2"><input type="checkbox" class="checkbox" name="delete" value="1"><?php echo _MD_A_DTFTWARAPITF;?></td>
</tr><tr class="bg1">
<td><?php echo _MD_A_FORUMNAME;?></td>
<td><input type="text" class="text" name="name" size="40" maxlength="150" value="<?php echo $name?>"></td>
</tr><tr class="bg1">
<td valign="top"><?php echo _MD_A_FORUMDESCRIPTION;?></td>
<td nowrap>
<?php
$desc = new XoopsFormDhtmlTextArea('', 'desc', $desc, 10, 50);
echo $desc->render();
?>
</td>
</tr><tr class="bg1">
<td valign="top"><?php echo _MD_A_MODERATOR;?></td>
<td><b>Current:</b><br />

<?php
$sql = "
	SELECT
	u.uname,
	u.uid
	FROM
	".$db->prefix("users")." u,
	".$db->prefix("bb_forum_mods")." f
	WHERE
	f.forum_id=$forum
	AND
	u.uid=f.user_id";

if (!$r = $db->query($sql)) {
	CloseTable();
	xoops_cp_footer();
	exit();
}

if ($row = $db->fetch_array($r)) {
	do {
		echo $row['uname']." (<input type='checkbox' class='checkbox' name='rem_mods[]' value='".$row['uid']."'> "._MD_A_REMOVE.")<br />";
		$current_mods[] = $row['uid'];
		} while ($row = $db->fetch_array($r));
	echo "<br />";
	} else {
		echo _MD_A_NOMODERATORASSIGNED."<br /><br />";
	}
?>

<b>Add:</b><br />

<select class="select" name="mods[]" size="5" multiple>
<?php

$sql = "SELECT uid, uname FROM ".$db->prefix("users")." WHERE uid != 0 AND level > 0 ";

while (list($null, $currMod) = each($current_mods)) {
$sql .= "AND uid != $currMod ";
}

$sql .= "ORDER BY uname";

if (!$r = $db->query($sql)) {
	CloseTable();
	xoops_cp_footer();
	exit();
}

while (list($uid, $uname) = $db->fetch_row($r)) {
	echo "<option value='".$uid."'>".$uname."</option>";
}
?>

</select></td>
</tr><tr class="bg1">
<td><?php echo _MD_A_CATEGORY;?></td>
<td><select class="select" name="cat">

<?php
$sql = "SELECT * FROM ".$db->prefix("bb_categories")."";
if (!$r = $db->query($sql)) {
	CloseTable();
	xoops_cp_footer();
	exit();
}

if ($row = $db->fetch_array($r)) {
	do {
		$s = "";
		if ($row['cat_id'] == $myrow['cat_id']) {
			$s = "selected";
		}
			echo "<option value='".$row['cat_id']."' $s>".$row['cat_title']."</option>";
	} while ($row = $db->fetch_array($r));
	} else {
		echo "<option value='0'>"._NONE."</option>";
	}
?>

</select></td>

<?php
if ($myrow['forum_access'] == 1) {
	$access1 = "selected";
}

if ($myrow['forum_access'] == 2) {
	$access2 = "selected";
}

if ($myrow['forum_access'] == 3) {
	$access3 = "selected";
}
?>

</tr><tr class="bg1">
<td><?php echo _MD_A_ACCESSLEVEL;?></td>
<td><select class="select" name="forum_access">
<option value="2" <?php echo $access2?>><?php echo _MD_A_ANONYMOUSPOST;?></option>
<option value="1" <?php echo $access1?>><?php echo _MD_A_REGISTERUSERONLY;?></option>
<option value="3" <?php echo $access3?>><?php echo _MD_A_MODERATORANDADMINONLY;?></option>
</select>
</td>
</tr><tr class="bg1">
<td><?php echo _TYPE;?>:</td>
<td><select class="select" name="type">

<?php
if ($myrow['forum_type'] == 1) {
	$priv = "selected";
	} else {
		$pub = "selected";
	}
?>

<option value="0" <?php echo $pub?>><?php echo _MD_A_PUBLIC;?></option>
<option value="1" <?php echo $priv?>><?php echo _MD_A_PRIVATE;?></option>
</select>
</td>
</tr>

<?php
$html_yes = $html_no = $sig_yes = $sig_no = "";
if ( $myrow['allow_html'] == 1 ) {
	$html_yes = "checked='checked'";
	} else {
		$html_no = "checked='checked'";
	}

if ( $myrow['allow_sig'] == 1) {
	$sig_yes = "checked='checked'";
	} else {
		$sig_no = "checked='checked'";
	}
?>

<tr class="bg1">
<td><?php echo _MD_A_ALLOWHTML;?></td>
<td><input type="radio" class="radio" name="html" value="1" <?php echo $html_yes;?> /><?php echo _YES;?><input type="radio" name="html" value="0" <?php echo $html_no;?> /><?php echo _NO;?></td>
</tr><tr class="bg1">
<td><?php echo _MD_A_ALLOWSIGNATURES;?></td>
<td><input type="radio" class="radio" name="sig" value="1" <?php echo $sig_yes;?> /><?php echo _YES;?><input type="radio" name="sig" value="0" <?php echo $sig_no;?> /><?php echo _NO;?></td>
</tr><tr class="bg1">
<td><?php echo _MD_A_HOTTOPICTHRESHOLD;?></td>
<td><input type="text" class="text" name="hot" size="3" maxlength="3" value="<?php echo $myrow["hot_threshold"];?>" /></td>
</tr><tr class="bg1">
<td><?php echo _MD_A_POSTPERPAGE;?><br /><i><?php echo _MD_A_TITNOPPTTWBDPPOT;?></i></td>
<td><input type="text" class="text" name="ppp" size="3" maxlength="3" value="<?php echo $myrow["posts_per_page"];?>" /></td>
</tr><tr class="bg1">
<td><?php echo _MD_A_TOPICPERFORUM;?><br /><i><?php echo _MD_A_TITNOTPFTWBDPPOAF;?></i></td>
<td><input type="text" class="text" name="tpp" size="3" maxlength="3" value="<?php echo $myrow["topics_per_page"];?>" /></td>
</tr><tr class="bg3">
<td align="center" colspan="2">
<input type="hidden" name="mode" value="editforum" />
<input type="hidden" name="forum" value="<?php echo $forum;?>" />
<input type="submit" class="button" name="save" value="<?php echo _SAVE;?>" />
<input type="reset" class="button" value="<?php echo _MD_A_CLEAR;?>" />
</td>
</tr></tr></table></td></tr></table>

<?php
// Save forum data
} elseif ( !empty($HTTP_POST_VARS['save']) && !empty($forum) ) {

if ( empty($HTTP_POST_VARS['delete']) ) {
	$name         = $myts->makeTboxData4Save($HTTP_POST_VARS['name']);
	$desc         = $myts->makeTboxData4Save($HTTP_POST_VARS['desc']);
	$type         = intval($HTTP_POST_VARS['type']);
	$cat          = intval($HTTP_POST_VARS['cat']);
	$forum_access = intval($HTTP_POST_VARS['forum_access']);
	$html         = intval($HTTP_POST_VARS['html']);
	$sig          = intval($HTTP_POST_VARS['sig']);
	$ppp          = intval($HTTP_POST_VARS['ppp']);
	$hot          = intval($HTTP_POST_VARS['hot']);
	$tpp          = intval($HTTP_POST_VARS['tpp']);

	$sql = "
		UPDATE
		".$db->prefix("bb_forums")."
		SET
		forum_name='$name',
		forum_desc='$desc',
		forum_type='$type',
		cat_id=$cat,
		forum_access=$forum_access,
		allow_html='$html',
		allow_sig='$sig',
		posts_per_page='$ppp',
		hot_threshold='$hot',
		topics_per_page='$tpp'
		WHERE
		forum_id=$forum";

	if (!$r = $db->query($sql)) {
		CloseTable();
		xoops_cp_footer();
		exit();
	}

$count = 0;
if ( isset($HTTP_POST_VARS["mods"]) ) {
	while (list($null, $mod) = each($HTTP_POST_VARS["mods"])) {
		$mod_data = new XoopsUser($mod);
		if ($mod_data->isActive()) {
			$mod_query = "INSERT INTO ".$db->prefix("bb_forum_mods")." SET forum_id=$forum, user_id=$mod";
			if (!$db->query($mod_query)) {
				CloseTable();
				xoops_cp_footer();
				exit();
			}
		}
	}
}

if ( !isset($HTTP_POST_VARS["mods"]) ) {
	$current_mods = "SELECT count(*) AS total FROM ".$db->prefix("bb_forum_mods")." WHERE forum_id=$forum";
	$r = $db->query($current_mods);
	list($total) = $db->fetch_row($r);
	} else {
		$total = count($HTTP_POST_VARS["mods"]);
	}

if ( isset($HTTP_POST_VARS["rem_mods"]) && ($total != count($HTTP_POST_VARS["rem_mods"])) ) {
	while (list($null, $mod) = each($HTTP_POST_VARS["rem_mods"])) {
		$rem_query = "DELETE FROM ".$db->prefix("bb_forum_mods")." WHERE forum_id=$forum AND user_id=$mod";
		if (!$db->query($rem_query)) {
			CloseTable();
			xoops_cp_footer();
			exit();
		}
	}
	} else {
		if ( isset($HTTP_POST_VARS["rem_mods"]) ) {
			$mod_not_removed = 1;
			}
}

echo "
<table width='95%' border='0' cellspacing='0' cellpadding='0' align='center'><tr>
<td align='center' width='100%' class='bg3'><b>"._UPDATED."</b></td>";

if ($mod_not_removed) {
	echo "</tr><tr><td align='center' width='100%'><br /><b>"._MD_A_HTSMHNBRBITHBTWNLBAMOTF."</b></td>";
}

echo "
</tr><tr>
<td>
<table width='100%' cellspacing='0' cellpadding='0'><tr>
<td align='center' width='100%' class='bg1'><p><br />&nbsp;&nbsp;<a href='".$bbUrl['admin']."/index.php'>"._MD_A_RETURNTOADMINPANEL."</a><p><a href='".$bbUrl['root']."/index.php'>"._MD_A_RETURNTOFORUMINDEX."</a></td>
</tr></table></td></tr></table>";

} else {
	$sql = "DELETE FROM ".$db->prefix("bb_posts")." WHERE forum_id=$forum";
	if (!$r = $db->query($sql)) {
		CloseTable();
		xoops_cp_footer();
		exit();
	}
	$sql = "DELETE FROM ".$db->prefix("bb_topics")." WHERE forum_id=$forum";
	if (!$r = $db->query($sql)) {
		CloseTable();
		xoops_cp_footer();
		exit();
	}
	$sql = "DELETE FROM ".$db->prefix("bb_forums")." WHERE forum_id=$forum";
	if (!$r = $db->query($sql)) {
		CloseTable();
		xoops_cp_footer();
		exit();
	}
	$sql = "DELETE FROM ".$db->prefix("bb_forum_mods")." WHERE forum_id=$forum";
	if (!$r = $db->query($sql)) {
		CloseTable();
		xoops_cp_footer();
		exit();
	}
		echo "
		<table width='95%' border='0' cellspacing='0' cellpadding='0' align='center'><tr>
		<td align='center' width='100%' class='bg3'><b>"._MD_A_FORUMREMOVED."</b></td>
		</tr><tr><td>
		<table width='100%' cellspacing='0' cellpadding='0'><tr>
		<td align='center' width='100%' class='bg1'><p><br />&nbsp;&nbsp;"._MD_A_FRFDAWAIP."<p><a href='".$bbUrl['admin']."/index.php'>"._MD_A_RETURNTOADMINPANEL."</a></p><p><a href='".$bbUrl['root']."/index.php'>"._MD_A_RETURNTOFORUMINDEX."</a></p></td>
		</tr></table></td></tr></table>";
}

} else {
// Show forum select form
?>
<form action="./admin_forums.php" method="post">
<table border="0" cellpadding="1" cellspacing="0" align="center" valign="top" width="95%"><tr>
<td class="bg2">
<table border="0" cellpadding="1" cellspacing="1" width="100%"><tr class="bg3" align="left">
<td align="center" colspan="2"><b><?php echo _MD_A_SELECTFORUMEDIT;?></b></td>
</tr><tr class="bg1" align="left">
<td align="center" colspan="2"><select class="select" name="forum" size="0">
<?php

$sql = "SELECT forum_name, forum_id FROM ".$db->prefix("bb_forums")." ORDER BY forum_id";
if ($result = $db->query($sql)) {
	if ($myrow = $db->fetch_array($result)) {
		do {
			$name = $myts->makeTboxData4Show($myrow['forum_name']);
			echo "<option value='".$myrow['forum_id']."'>$name</option>";
		} while ($myrow = $db->fetch_array($result));
		} else {
			echo "<option value='-1'>"._MD_A_NOFORUMINDATABASE."</option>";
		}
	} else {
		echo "<option value='-1'>"._MD_A_DATABASEERROR."</option>";
	}

?>
</select></td>
</tr><tr class="bg3" align="left">
<td align="center" colspan="2">
<input type="hidden" name="mode" value="editforum">
<input type="submit" class="button" name="submit" value="<?php echo _EDIT;?>">
</td>
</tr></table></td></tr></table>
<?php
}

}


//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function forums_editcat() {
global $HTTP_POST_VARS, $bbUrl, $db, $myts;

$cat_id = $HTTP_POST_VARS['cat_id'];

if (isset($HTTP_POST_VARS['submit']) && isset($HTTP_POST_VARS['save'])) {
	$new_title = $myts->makeTboxData4Save($HTTP_POST_VARS['new_title']);
	$sql = "UPDATE ".$db->prefix("bb_categories")." SET cat_title='$new_title' WHERE cat_id=$cat_id";
	if (!$result = $db->query($sql)) {
		CloseTable();
		xoops_cp_footer();
		exit();
		} else {
			echo "
			<table width='95%' border='1' cellspacing='0' cellpadding='0' align='center'><tr>
			<td align='center' width='100%' class='bg3'><b>"._UPDATED."</b></td>
			</tr><tr><td>
			<table width='100%' cellspacing='0' cellpadding='0'><tr>
			<td align='center' width='100%' class='bg'><p><a href='".$bbUrl['admin']."/index.php'>"._MD_A_RETURNTOADMINPANEL."</a></p><p><a href='".$bbUrl['root']."/index.php'>"._MD_A_RETURNTOFORUMINDEX."</p></td>
			</tr></table></td></tr></table>";
			}
	} elseif (isset($HTTP_POST_VARS['submit']) && $HTTP_POST_VARS['submit'] != "") {
		$sql = "SELECT cat_title FROM ".$db->prefix("bb_categories")." WHERE cat_id='$cat_id'";
		if (!$result = $db->query($sql)) {
			CloseTable();
			xoops_cp_footer();
			exit();
		}
		$cat_data = $db->fetch_array($result);
		$cat_title = $myts->makeTboxData4Edit($cat_data["cat_title"]);
		?>
		<form action="./admin_forums.php" method="post">
		<table border="0" cellpadding="1" cellspacing="0" align="center" valign="TOP" width="95%"><tr><td class="bg2">
		<table border="0" cellpadding="1" cellspacing="1" width="100%">
		<tr class="bg3" align="left">
		<td align="center" colspan="2"><b><?php echo _MD_A_EDITCATEGORY;?> <?php echo $cat_title ?></b></td>
		</tr><tr class="bg1" align="left">
		<td><?php echo _MD_A_CATEGORYTITLE;?></td>
		<td><input type="text" class="text" name="new_title" value="<?php echo $cat_title ?>" size="45" maxlength="100"></td>
		</tr><tr class="bg3" align="left">
		<td align="center" colspan="2">
		<input type="hidden" name="mode" value="editcat" />
		<input type="hidden" name="save" value="TRUE" />
		<input type="hidden" name="cat_id" value="<?php echo $cat_id?>" />
		<input type="submit" class="button" name="submit" value="<?php echo _SAVE;?>" /></td>
		</tr></tr></table></td></tr></table></form>
		<?php
		} else {
			$sql = "SELECT cat_id, cat_title FROM ".$db->prefix("bb_categories")." ORDER BY cat_order";
			if (!$result = $db->query($sql)) {
				CloseTable();
				xoops_cp_footer();
				exit();
			}
			?>
			<form action="./admin_forums.php" method="post">
			<table border="0" cellpadding="1" cellspacing="0" align="center" valign="top" width="95%"><tr><td class="bg2">
			<table border="0" cellpadding="1" cellspacing="1" width="100%"><tr class="bg3" align="left">
			<td align="center" colspan="2"><b><?php echo _MD_A_SELECTACATEGORYEDIT;?></b></td>
			</tr><tr class="bg1" align="left">
			<td align="center" colspan="2">
			<select class="select" name="cat_id" SIZE="0">
			<?php
			while ($cat_data = $db->fetch_array($result)) {
				echo "<option value='".$cat_data["cat_id"]."'>".$myts->makeTboxData4Show($cat_data["cat_title"])."</option>";
			}
			?>
			</select></td>
			<tr class="bg3" align="left">
			<td align="center" colspan="2">
			<input type="hidden" name="mode" value="editcat">
			<input type="submit" class="button" name="submit" value="<?php echo _EDIT;?>"></td>
			</tr></tr></table></td></tr></table></form>
			<?php
		}
}


//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function forums_remcat() {
global $HTTP_POST_VARS, $bbUrl, $db, $myts;

$cat = $HTTP_POST_VARS['cat'];

if (isset($HTTP_POST_VARS['submit']) && $HTTP_POST_VARS['submit'] != "") {
	$sql = "DELETE FROM ".$db->prefix("bb_categories")." WHERE cat_id=$cat";
	if (!$r = $db->query($sql)) {
		CloseTable();
		xoops_cp_footer();
		exit();
	}
	echo "
	<table width='95%' border='1' cellspacing='0' cellpadding='0' align='center'><tr>
	<td align='center' width='100%' class='bg3'><b>"._MD_A_CATEGORYCREATED."</b></td>
	</tr><tr>
	<td>
	<table width='100%' cellspacing='0' cellpadding='0'><tr>
	<td align='center' width='100%' class='bg1'><p><a href='".$bbUrl['admin']."/index.php'>"._MD_A_RETURNTOADMINPANEL."</a></p><p><a href='".$bbUrl['root']."/index.php'>"._MD_A_RETURNTOFORUMINDEX."</a></p></td>
	</tr></table></td>
	</tr></table>";
	} else {
		?>
		<form action="./admin_forums.php" method="post">
		<table border="0" cellpadding="1" cellspacing="0" align="center" valign="top" width="95%"><tr><td class='bg2'>
		<table border="0" cellpadding="1" cellspacing="1" width="100%">
		<tr class="bg3" align="left">
		<td align="center" colspan="2"><b><?php echo _MD_A_RMVACAT;?></b></td>
		</tr><tr class="bg3" align="left">
		<td align="center" colspan="2"><i><?php echo _MD_A_NTWNRTFUTCYMDTVTEFS;?></i></td>
		</tr><tr class="bg1">
		<td align="center" colspan="2">
		<select class="select" name="cat">
		<?php
		$sql = "SELECT * FROM ".$db->prefix("bb_categories")." ORDER BY cat_title";
		if (!$r = $db->query($sql)) {
			CloseTable();
			xoops_cp_footer();
			exit();
		}
	while ($m = $db->fetch_array($r)) {
		echo "<option value='".$m['cat_id']."'>".$myts->makeTboxData4Show($m['cat_title'])."</option>";
	}
	?>
	</select>
	<input type="hidden" name="mode" value="remcat" /></td>
	</tr><tr class="bg3">
	<td align="center" colspan="2">
	<input type="submit" class="button" name="submit" value="<?php echo _MD_A_REMOVECATEGORY;?>" /></td>
	</tr></table></tr></table></form>
	<?php
	}
}


//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function forums_addcat() {
global $HTTP_POST_VARS, $bbUrl, $db, $myts;

if (isset($HTTP_POST_VARS['submit']) && $HTTP_POST_VARS['submit'] != "") {
	$nextid = $db->genId($db->prefix("bb_categories")."_cat_id_seq");
	$sql    = "SELECT max(cat_order) AS highest FROM ".$db->prefix("bb_categories")."";
	if (!$r = $db->query($sql)) {
		CloseTable();
		xoops_cp_footer();
		exit();
	}

	list($highest) = $db->fetch_row($r);
	$highest++;
	$title = $myts->makeTboxData4Save($HTTP_POST_VARS['title']);
	$sql   = "INSERT INTO ".$db->prefix("bb_categories")." SET cat_id='$nextid', cat_title='$title', cat_order='$highest'";
	if (!$result = $db->query($sql)) {
		CloseTable();
		xoops_cp_footer();
		exit();
	}
	echo "
	<table width='95%' border='0' cellspacing='0' cellpadding='0' align='center'><tr>
	<td align='center' width='100%' class='bg3'><b>"._MD_A_CATEGORYCREATED."</b></td>
	</tr><tr><td>
	<table width='100%' cellspacing='0' cellpadding='0'><tr>
	<td align='center' width='100%' class='bg1'><p><a href='".$bbUrl['admin']."/index.php'>"._MD_A_RETURNTOADMINPANEL."</a></p><p><a href='".$bbUrl['root']."/index.php'>"._MD_A_RETURNTOFORUMINDEX."</a></p></td>
	</tr></table></td></tr></table>";
	} else {
	?>
	<form action="./admin_forums.php" method="post">
	<table border="0" cellpadding="1" cellspacing="0" align="center" valign="top" width="95%"><tr><td class="bg2">
	<table border="0" cellpadding="1" cellspacing="1" width="100%"><tr class="bg3" align="left">
	<td align="center" colspan="2"><b><?php echo _MD_A_CREATENEWCATEGORY;?></b></td>
	</tr><tr class="bg1" align="left">
	<td><?php echo _MD_A_CATEGORYTITLE;?></td>
	<td><input type="text" class="text" name="title" size="40" maxlength="100"></td>
	</tr><tr class="bg3" align="left">
	<td align="center" colspan="2">
	<input type="hidden" name="mode" value="addcat" />
	<input type="submit" class="button" name="submit" value="<?php echo _MD_A_CREATENEWCATEGORY;?>" />&nbsp;&nbsp;
	<input type="reset" class="button" value="<?php echo _MD_A_CLEAR;?>" /></td>
	</tr></tr></table></td></tr></table></form>
	<?php
	}
}


//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function forums_addforum() {
global $HTTP_POST_VARS, $bbUrl, $db, $myts;

if (isset($HTTP_POST_VARS['submit']) && $HTTP_POST_VARS['submit'] != "") {
	if ( empty($HTTP_POST_VARS['name']) || empty($HTTP_POST_VARS['desc']) || !is_array($HTTP_POST_VARS['mods'])) {
		echo _MD_A_YDNFOATPOTFDYAA;
		CloseTable();
		xoops_cp_footer();
		exit();
	}
	$name         = $myts->makeTboxData4Save($HTTP_POST_VARS['name']);
	$desc         = $myts->makeTboxData4Save($HTTP_POST_VARS['desc']);
	$forum_access = intval($HTTP_POST_VARS['forum_access']);
	$cat          = intval($HTTP_POST_VARS['cat']);
	$type         = intval($HTTP_POST_VARS['type']);
	$html         = intval($HTTP_POST_VARS['html']);
	$sig          = intval($HTTP_POST_VARS['sig']);
	$ppp          = intval($HTTP_POST_VARS['ppp']);
	$hot          = intval($HTTP_POST_VARS['hot']);
	$tpp          = intval($HTTP_POST_VARS['tpp']);

	$nextid = $db->genId($db->prefix("bb_forums")."_forum_id_seq");
	$sql    = "
		INSERT INTO ".$db->prefix("bb_forums")." SET
		forum_id=$nextid,
		forum_name='$name',
		forum_desc='$desc',
		forum_access=$forum_access,
		cat_id=$cat,
		forum_type=$type,
		allow_html='$html',
		allow_sig='$sig',
		posts_per_page=$ppp,
		hot_threshold=$hot,
		topics_per_page=$tpp";

	if (!$result = $db->query($sql)) {
		CloseTable();
		xoops_cp_footer();
		exit();
	}
	if ($nextid == 0) {
		$nextid = $db->insert_id();
	}
	$count = 0;
	while (list($mod_number, $mod) = each($HTTP_POST_VARS["mods"])) {
		$mod_data = new XoopsUser($mod);

		if ($mod_data->isActive() && $mod_data->level() < 2) {
			if (!isset($user_query)) {
				$user_query = "UPDATE ".$db->prefix("users")." SET level=2 WHERE ";
			}
			if ($count > 0) {
				$user_query .= " OR";
				}
				$user_query .= " uid=$mod";
				$count++;
		}
		$mod_query = "INSERT INTO ".$db->prefix("bb_forum_mods")." SET forum_id=$nextid, user_id=$mod";
		if (!$db->query($mod_query)) {
			CloseTable();
			xoops_cp_footer();
			exit();
		}
	}

if (isset($user_query)) {
	if (!$db->query($user_query)) {
		CloseTable();
		xoops_cp_footer();
		exit();
	}
}

echo "
<table width='95%' border='1' cellspacing='0' cellpadding='0' align='center'><tr>
<td align='center' width='100%' class='bg3'><b>"._MD_A_FORUMCREATED."</b></td>
</tr><tr>
<td>
<table width='100%' cellspacing='0' cellpadding='0'><tr>
<td align='center' width='100%' class='bg1'><p><a href='".$bbUrl['admin']."/index.php'>"._MD_A_RETURNTOADMINPANEL."</a></p><p><a href='".$bbUrl['root']."viewforum.php?forum=$nextid'>"._MD_A_VTFYJC."</a></p></td>
</tr></table></td></tr></table>";

} else {
	$sql = "SELECT count(*) AS total FROM ".$db->prefix("bb_categories")."";
	if (!$r = $db->query($sql)) {
		CloseTable();
		xoops_cp_footer();
		exit();
	}
	list($total) = $db->fetch_row($r);
	if ($total < 1 || !isset($total)) {
		echo _MD_A_EYMAACBYAF;
		CloseTable();
		xoops_cp_footer();
		exit();
	}
?>

<form action="./admin_forums.php" method="post">
<table border="0" cellpadding="1" cellspacing="0" align="center" valign="top" width="95%"><tr>
<td class="bg2">
<table border="0" cellpadding="1" cellspacing="1" width="100%"><tr class="bg3">
<td align="center" colspan="2"><b><?php echo _MD_A_CREATENEWFORUM;?></b></td>
</tr><tr class="bg1">
<td><?php echo _MD_A_FORUMNAME;?></td>
<td><input type="text" class="text" name="name" size="40" maxlength="150"></td>
</tr><tr class="bg1">
<td valign="top"><?php echo _MD_A_FORUMDESCRIPTION;?></td>
<td nowrap>
<?php
$desc = new XoopsFormDhtmlTextArea('', 'desc', '', 10, 50);
echo $desc->render();
?>
</td>
</tr><tr class="bg1">
<td valign="top"><?php echo _MD_A_MODERATOR;?></td>
<td><select class="select" name="mods[]" size="5" multiple="multiple">

<?php
$sql = "SELECT uid, uname FROM ".$db->prefix("users")." WHERE uid != 0 AND level > 0 ORDER BY uname";
if (!$result = $db->query($sql)) {
		CloseTable();
		xoops_cp_footer();
		exit();
	}

if ($myrow = $db->fetch_array($result)) {
	do {
		echo "<option value='".$myrow['uid']."'>".$myrow['uname']."</option>";
	} while ($myrow = $db->fetch_array($result));
	} else {
		echo "<option value='0'>"._NONE."</option>";
	}
?>

</select></td>
</tr><tr class="bg1">
<td><?php echo _MD_A_CATEGORY;?></td>
<td><select class="select" name="cat">

<?php
$sql = "SELECT * FROM ".$db->prefix("bb_categories")."";
if (!$result = $db->query($sql)) {
	CloseTable();
	xoops_cp_footer();
	exit();
}

if ($myrow = $db->fetch_array($result)) {
	do {
		echo "<option value='".$myrow['cat_id']."'>".$myrow['cat_title']."</option>";
	} while ($myrow = $db->fetch_array($result));
	} else {
		echo "<option value='0'>"._NONE."</option>";
	}
?>

</select></td>
</tr><tr class="bg1">
<td><?php echo _MD_A_ACCESSLEVEL;?></td>

<td>
<select class="select" name="forum_access">
<option value="2"><?php echo _MD_A_ANONYMOUSPOST;?></option>
<option value="1"><?php echo _MD_A_REGISTERUSERONLY;?></option>
<option value="3"><?php echo _MD_A_MODERATORANDADMINONLY;?></option>
</select>
</td>

</tr><tr class="bg1">
<td><?php echo _TYPE;?>:</td>

<td>
<select class="select" name="type">
<option value="0"><?php echo _MD_A_PUBLIC;?></option>
<option value="1"><?php echo _MD_A_PRIVATE;?></option>
</select>
</td>

</tr><tr class="bg1">
<td><?php echo _MD_A_ALLOWHTML;?></td>
<td><input type="radio" class="radio" name="html" value="1" /><?php echo _YES;?><input type="radio" name="html" value="0" checked="checked"  /><?php echo _NO;?></td>
</tr><tr class="bg1">
<td><?php echo _MD_A_ALLOWSIGNATURES;?></td>
<td><input type="radio" class="radio" name="sig" value="1" checked="checked" /><?php echo _YES;?><input type="radio" name="sig" value="0" /><?php echo _NO;?></td>
</tr><tr class="bg1">
<td><?php echo _MD_A_HOTTOPICTHRESHOLD;?></td><td><input type="text" class="text" name="hot" size="3" maxlength="3" value="10" /></td>
</tr><tr class="bg1">
<td><?php echo _MD_A_POSTPERPAGE;?><br /><i><?php echo _MD_A_TITNOPPTTWBDPPOT;?></i></td>
<td><input type="text" class="text" name="ppp" size="3" maxlength="3" value="10" /></td></tr>
<tr class="bg1"><td><?php echo _MD_A_TOPICPERFORUM;?><br /><i><?php echo _MD_A_TITNOTPFTWBDPPOAF;?></i></td><td><input type="text" class="text" name="tpp" size="3" maxlength="3" value="20" /></td>
</tr><tr class="bg3">
<td align="center" colspan="2">
<input type="hidden" name="mode" value="addforum" />
<input type="submit" class="button" name="submit" value="<?php echo _MD_A_CREATENEWFORUM;?>" />
<input type="reset" class="button" value="<?php echo _MD_A_CLEAR;?>" />
</td>
</tr></table></td></tr></table>
<?php
	}
}


//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function forums_catorder() {
global $db, $myts, $HTTP_POST_VARS;

$current_order = $HTTP_POST_VARS['current_order'];
$up            = $HTTP_POST_VARS['up'];
$down          = $HTTP_POST_VARS['down'];
$last_id       = $HTTP_POST_VARS['last_id'];
$cat_id        = $HTTP_POST_VARS['cat_id'];

// update catagories set cat_order = cat_order + 1 WHERE cat_order >= 2; update catagories set cat_order = cat_order - 2 where cat_id = 3;
if (isset($up) && $up != "") {
	if ($current_order > 1) {
		$order = $current_order - 1;
		$sql1 = "UPDATE ".$db->prefix("bb_categories")." SET cat_order=$order WHERE cat_id=$cat_id";
		if (!$r = $db->query($sql1)) {
			CloseTable();
			xoops_cp_footer();
			exit();
		}
		$sql2 = "UPDATE ".$db->prefix("bb_categories")." SET cat_order=$current_order WHERE cat_id=$last_id";
		if (!$r = $db->query($sql2)) {
			CloseTable();
			xoops_cp_footer();
			exit();
		}
		echo "<div align='center'>"._MD_A_CATEGORYMOVEUP."</div><br />";
		} else {
			echo "<div align='center'>"._MD_A_TCIATHU."</div><br />";
		}
	} elseif (isset($down) && $down != "") {
		$sql = "SELECT cat_order FROM ".$db->prefix("bb_categories")." ORDER BY cat_order DESC";
		if (!$r  = $db->query($sql,1,0)) {
			CloseTable();
			xoops_cp_footer();
			exit();
		}
		list($last_number) = $db->fetch_row($r);
		if ($last_number != $current_order) {
			$order = $current_order + 1;
			$sql = "UPDATE ".$db->prefix("bb_categories")." SET cat_order = $current_order WHERE cat_order=$order";
			if (!$r  = $db->query($sql)) {
				CloseTable();
				xoops_cp_footer();
				exit();
			}
			$sql = "UPDATE ".$db->prefix("bb_categories")." SET cat_order = $order where cat_id=$cat_id";
			if (!$r  = $db->query($sql)) {
				CloseTable();
				xoops_cp_footer();
				exit();
				}
			echo "<div align='center'>"._MD_A_CATEGORYMOVEDOWN."</div><br />";
			} else {
				echo "<div align='center'>"._MD_A_TCIATLD."</div><br />";
			}
		}
		?>

<form action="./admin_forums.php" method="post">
<table border="0" cellpadding="1" cellspacing="0" align="center" valign="top" width="95%"><tr>
<td class="bg2">
<table border="0" cellpadding="1" cellspacing="1" width="100%"><tr class="bg3" align="left">
<td align="center" colspan="3"><b><?php echo _MD_A_SETCATEGORYORDER;?></b><br />
<?php echo _MD_A_TODHITOTCWDOTIP;?><br />
<?php echo _MD_A_ECWMTCPUODITO;?></td>
</tr><tr class="bg3" align="center">
<td><?php echo _MD_A_CATEGORY1;?></td>
<td><?php echo _MD_A_MOVEUP;?></td>
<td><?php echo _MD_A_MOVEDOWN;?></td>
</tr>

<?php
$sql = "SELECT * FROM ".$db->prefix("bb_categories")." ORDER BY cat_order";
if (!$r = $db->query($sql)) {
	exit();
}

while ($m = $db->fetch_array($r)) {
	echo "
	<form action='./admin_forums.php' method='post'>
	<tr class='bg1' align='center'>
	<td>".$myts->makeTboxData4Show($m['cat_title'])."</td>
	<td>
	<input type='hidden' name='mode' value='catorder'>
	<input type='hidden' name='cat_id' value='".$m['cat_id']."'>
	<input type='hidden' name='last_id' value='";
	if (isset($last_id)) {
		echo $last_id;
	}
	echo "
	'>
	<input type='hidden' name='current_order' value='".$m['cat_order']."'>
	<input type='submit' class='button' name='up' value='"._MD_A_MOVEUP."'></td>
	<td><input type='submit' class='button' name='down' value='"._MD_A_MOVEDOWN."'></td>
	</tr></form>";
	$last_id = $m['cat_id'];
}
?>

</table></table>

<?php
}


//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function forums_sync() {
global $HTTP_POST_VARS;

if ($HTTP_POST_VARS['submit']) {
	echo _MD_A_SYNCHING."<br />";
	flush();
	sync(NULL, "all forums");
	flush();
	sync(NULL, "all topics");
	echo "<br />";
	echo _DONE;
	} else {
		?>
		<table border="0" cellpadding="1" cellspacing="0" align="center" valign="top" width="95%"><tr>
		<td class="bg2">
		<table border="0" cellpadding="1" cellspacing="1" width="100%">
		<tr class="bg3" align="left">
		<td><?php echo _MD_A_CLICKBELOWSYNC;?></td>
		</tr><tr class="bg1" align="center">
		<td>
		<form action="./admin_forums.php" method="post">
		<input type="hidden" name="mode" value="sync">
		<input type="submit" class="button" name="submit" value="<?php echo _MD_A_SYNCFORUM;?>">
		</form>
		</td>
		</td></tr></table>
		</td></tr></table>
	<?php
	}
}


//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
$mode = !empty($HTTP_POST_VARS['mode']) ? $HTTP_POST_VARS['mode'] : $HTTP_GET_VARS['mode'];

switch($mode) {

case "config":
	forums_config();
	break;

case "editforum":
	forums_editforum();
	break;

case "editcat":
	forums_editcat();
	break;

case "remcat":
	forums_remcat();
	break;

case "addcat":
	forums_addcat();
	break;

case "addforum":
	forums_addforum();
	break;

case "catorder":
	forums_catorder();
	break;

case "sync":
	forums_sync();
	break;
}

CloseTable();
xoops_cp_footer();
?>
