<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.7 www.exoops.info 2006/03 
// $Id: admin_forums.php,v 1.6 2003/02/28 06:02:47 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// 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, $_POST;
if (!$_POST['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 _CONFIGURATION;?></td></tr>
<tr><td width="100"><?php echo _MD_A_SORTDAYS;?></td>
<td><select class="select" name="sortdays">
<option value="1"<?php if($newbbConfig['sort_days']=="1") echo " selected";?>><?php echo _MD_LASTDAY;?></option>
<option value="2"<?php if($newbbConfig['sort_days']=="2") echo " selected";?>><?php printf(_FROMLASTDAYS,2);?></option>
<option value="5"<?php if($newbbConfig['sort_days']=="5") echo " selected";?>><?php printf(_FROMLASTDAYS,5);?></option>
<option value="10"<?php if($newbbConfig['sort_days']=="10") echo " selected";?>><?php printf(_FROMLASTDAYS,10);?></option>
<option value="20"<?php if($newbbConfig['sort_days']=="20") echo " selected";?>><?php printf(_FROMLASTDAYS,20);?></option>
<option value="30"<?php if($newbbConfig['sort_days']=="30") echo " selected";?>><?php printf(_FROMLASTDAYS,30);?></option>
<option value="45"<?php if($newbbConfig['sort_days']=="45") echo " selected";?>><?php printf(_FROMLASTDAYS,45);?></option>
<option value="60"<?php if($newbbConfig['sort_days']=="60") echo " selected";?>><?php printf(_FROMLASTDAYS,60);?></option>
<option value="75"<?php if($newbbConfig['sort_days']=="75") echo " selected";?>><?php printf(_FROMLASTDAYS,75);?></option>
<option value="100"<?php if($newbbConfig['sort_days']=="100") echo " selected";?>><?php printf(_FROMLASTDAYS,100);?></option>
<option value="365"<?php if($newbbConfig['sort_days']=="365") echo " selected";?>><?php echo _MD_THELASTYEAR;?></option>
<option value="1000"<?php if($newbbConfig['sort_days']=="10000") echo " selected";?>><?php echo _MD_BEGINNING;?></option>
</select></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.txt"));
$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($_POST['disc_show']).";\n";
$content .= "\$newbbConfig['image_set'] = '".$myts->makeTboxData4Save($_POST['image_set'])."';\n";
$content .= "\$newbbConfig['sort_days'] = ".intval($_POST['sort_days']).";\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.txt";
if ( $file = fopen($filename, "wb") ) {
	$disclaimer = $myts->oopsStripSlashesGPC($_POST['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, $_POST, $_GET;
$forum = !empty($_POST['forum']) ? $_POST['forum'] : $_GET['forum'];
// Show forum select box
if ( empty($_POST['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->fetchArray($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->fetchArray($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->fetchArray($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->fetchRow($r)) {
	echo "<option value='".$uid."'>".$uname."</option>";
}
?>
</select></td>
</tr><tr class="bg1">
<td><?php echo _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->fetchArray($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->fetchArray($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($_POST['save']) && !empty($forum) ) {
if ( empty($_POST['delete']) ) {
	$name         = $myts->makeTboxData4Save($_POST['name']);
	$desc         = $myts->makeTboxData4Save($_POST['desc']);
	$type         = intval($_POST['type']);
	$cat          = intval($_POST['cat']);
	$forum_access = intval($_POST['forum_access']);
	$html         = intval($_POST['html']);
	$sig          = intval($_POST['sig']);
	$ppp          = intval($_POST['ppp']);
	$hot          = intval($_POST['hot']);
	$tpp          = intval($_POST['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($_POST["mods"]) ) {
	while (list($null, $mod) = each($_POST["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($_POST["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->fetchRow($r);
	} else {
		$total = count($_POST["mods"]);
	}
if ( isset($_POST["rem_mods"]) && ($total != count($_POST["rem_mods"])) ) {
	while (list($null, $mod) = each($_POST["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($_POST["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->fetchArray($result)) {
		do {
			$name = $myts->makeTboxData4Show($myrow['forum_name']);
			echo "<option value='".$myrow['forum_id']."'>$name</option>";
		} while ($myrow = $db->fetchArray($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 $_POST, $bbUrl, $db, $myts;
$cat_id = $_POST['cat_id'];
if (isset($_POST['submit']) && isset($_POST['save'])) {
	$new_title = $myts->makeTboxData4Save($_POST['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($_POST['submit']) && $_POST['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->fetchArray($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->fetchArray($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 $_POST, $bbUrl, $db, $myts;
$cat = $_POST['cat'];
if (isset($_POST['submit']) && $_POST['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->fetchArray($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 $_POST, $bbUrl, $db, $myts;
if (isset($_POST['submit']) && $_POST['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->fetchRow($r);
	$highest++;
	$title = $myts->makeTboxData4Save($_POST['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 $_POST, $bbUrl, $db, $myts;
if (isset($_POST['submit']) && $_POST['submit'] != "") {
	if ( empty($_POST['name']) || empty($_POST['desc']) || !is_array($_POST['mods'])) {
		echo _MD_A_YDNFOATPOTFDYAA;
		CloseTable();
		xoops_cp_footer();
		exit();
	}
	$name         = $myts->makeTboxData4Save($_POST['name']);
	$desc         = $myts->makeTboxData4Save($_POST['desc']);
	$forum_access = intval($_POST['forum_access']);
	$cat          = intval($_POST['cat']);
	$type         = intval($_POST['type']);
	$html         = intval($_POST['html']);
	$sig          = intval($_POST['sig']);
	$ppp          = intval($_POST['ppp']);
	$hot          = intval($_POST['hot']);
	$tpp          = intval($_POST['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->getInsertId();
	}
	$count = 0;
	while (list($mod_number, $mod) = each($_POST["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->fetchRow($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->fetchArray($result)) {
	do {
		echo "<option value='".$myrow['uid']."'>".$myrow['uname']."</option>";
	} while ($myrow = $db->fetchArray($result));
	} else {
		echo "<option value='0'>"._NONE."</option>";
	}
?>
</select></td>
</tr><tr class="bg1">
<td><?php echo _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->fetchArray($result)) {
	do {
		echo "<option value='".$myrow['cat_id']."'>".$myrow['cat_title']."</option>";
	} while ($myrow = $db->fetchArray($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, $_POST;
$current_order = $_POST['current_order'];
$up            = $_POST['up'];
$down          = $_POST['down'];
$last_id       = $_POST['last_id'];
$cat_id        = $_POST['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->fetchRow($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 _CATEGORY;?></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->fetchArray($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 $_POST;
if ($_POST['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($_POST['mode']) ? $_POST['mode'] : $_GET['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();
?>
