<?php
/***************************************************************************
                          admin_forums.php  -  description
                             -------------------
    begin                : Wed July 19 2000
    copyright            : (C) 2001 The phpBB Group
    email                : support@phpbb.com

    $Id: admin_forums.php,v 1.41 2001/06/16 06:05:50 thefinn Exp $
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/


include("admin_header.php");
include('../functions.php');
include('../config.php');
require('../auth.php');

switch($mode) {
 case 'editforum':
    $myts =& MyTextSanitizer::getInstance();
   if(isset($HTTP_POST_VARS['save']) && $HTTP_POST_VARS['save'] != "") {
      if(!$HTTP_POST_VARS['delete']) {
	 		$name = $myts->makeTboxData4Save($HTTP_POST_VARS['name']);
	 		$desc = $myts->makeTareaData4Save($HTTP_POST_VARS['desc']);

	 $sql = "UPDATE ".$xoopsDB->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 = $xoopsDB->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 ".$xoopsDB->prefix("bb_forum_mods")." (forum_id, user_id) VALUES ('$forum', '$mod')";
	       		if(!$xoopsDB->query($mod_query)){
				CloseTable();
				xoops_cp_footer();
				exit();
			}
		}
	    }
	 }

	 if(!isset($mods)) {
	    $current_mods = "SELECT count(*) AS total FROM ".$xoopsDB->prefix("bb_forum_mods")." WHERE forum_id = $forum";
	    $r = $xoopsDB->query($current_mods);
	    list($total) = $xoopsDB->fetchRow($r);
	 }
	 else
	   $total = count($mods) + 1;

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

	 echo "<table width=\"95%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" >";
	 echo "<tr><td align='center' width='100%' class='bg3'><span class='fg2'><b>"._MD_A_FORUMUPDATED."</b></span></td>";
	 if($mod_not_removed)
	   echo "<tr><td align=\"center\" width=\"100%\" class='bg3'><span class='fg2'><b>"._MD_A_HTSMHNBRBITHBTWNLBAMOTF."</b></span></td>";

	 echo "</tr><tr><td><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
	 echo "<td align='center' width='100%' class='bg1'><span class='fg2'><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></span><p><br /><p></td>";
	 echo "</tr></table></td></tr></TABLE>";
      }
      else {
      	$sql = "SELECT post_id FROM ".$xoopsDB->prefix("bb_posts")." WHERE forum_id = $forum";
    		if(!$r = $xoopsDB->query($sql)){
			CloseTable();
			xoops_cp_footer();
			exit();
		}
		if($xoopsDB->getRowsNum($r) > 0){
	 		$sql = "DELETE FROM ".$xoopsDB->prefix("bb_posts_text")." WHERE ";
	 		$looped = FALSE;
	 		while($ids = $xoopsDB->fetchArray($r))
	 		{
	 			if($looped == TRUE)
	 			{
	 				$sql .= " OR ";
	 			}
	 			$sql .= "post_id = ".$ids["post_id"]." ";
	 			$looped = TRUE;
	 		}
			if(!$r = $xoopsDB->query($sql)){
				CloseTable();
				xoops_cp_footer();
				exit();
			}

	 		$sql = "DELETE FROM ".$xoopsDB->prefix("bb_posts")." WHERE forum_id = '$forum'";
	 		if(!$r = $xoopsDB->query($sql)){
				CloseTable();
				xoops_cp_footer();
				exit();
			}
		}
	 		$sql = "DELETE FROM ".$xoopsDB->prefix("bb_topics")." WHERE forum_id = '$forum'";
	 		if(!$r = $xoopsDB->query($sql)){
				CloseTable();
				xoops_cp_footer();
				exit();
			}
			 $sql = "DELETE FROM ".$xoopsDB->prefix("bb_forums")." WHERE forum_id = '$forum'";
	 		if(!$r = $xoopsDB->query($sql)){
				CloseTable();
				xoops_cp_footer();
				exit();
			}

	 		$sql = "DELETE FROM ".$xoopsDB->prefix("bb_forum_mods")." WHERE forum_id = '$forum'";
	 		if(!$r = $xoopsDB->query($sql)){
				CloseTable();
				xoops_cp_footer();
				exit();
			}
	 		echo "<table width=\"95%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">";
	 		echo "<tr><td align='center' width='100%' class='bg3'><span class='fg2'><b>"._MD_A_FORUMREMOVED."</b></span></td>";
	 		echo "</tr><tr><td><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
	 		echo "<td align='center' width='100%' class='bg1'><span class='fg2'><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></span></p></td>";
	 		echo "</tr></table></td></tr></table>";
      }
   }
   if(isset($HTTP_POST_VARS['submit']) && !isset($HTTP_POST_VARS['save'])) {
      $sql = "SELECT * FROM ".$xoopsDB->prefix("bb_forums")." WHERE forum_id = $forum";
      if(!$result = $xoopsDB->query($sql)){
		CloseTable();
		xoops_cp_footer();
		exit();
	}
      if(!$myrow = $xoopsDB->fetchArray($result)) {
	 echo _MD_A_NOSUCHFORUM;
      }
      $name = $myts->makeTboxData4Edit($myrow['forum_name']);
      $desc = $myts->makeTareaData4Edit($myrow['forum_desc']);
      ?>
<form action="<?php echo $PHP_SELF?>" 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"><span class='fg2'><b><?php echo _MD_A_EDITTHISFORUM;?></b></span></td>
</tr>
<tr class='bg1' align='left'>
        <td colspan=2><input type="CHECKBOX" name='delete' value="1"><span class='fg2'> <?php echo _MD_A_DTFTWARAPITF;?></span></td>
</tr>
<tr class='bg1' align='left'>
        <td><span class='fg2'><?php echo _MD_A_FORUMNAME;?></span></td>
        <td><input type='text' name='name' SIZE="40" MAXLENGTH="150" value="<?php echo $name?>"></td>
</tr>
<tr class='bg1' align='left'>
        <td><span class='fg2'><?php echo _MD_A_FORUMDESCRIPTION;?></span></td>
        <td><TEXTAREA name='desc' ROWS="15" COLS="45" WRAP="VIRTUAL"><?php echo $desc?></TEXTAREA></td>
</tr>
<tr class='bg1' align='left'>
        <td valign="top"><span class='fg2'><?php echo _MD_A_MODERATOR;?></span></td>
        <td><b>Current:</b><br />
<?php
	$sql = "SELECT u.uname, u.uid FROM ".$xoopsDB->prefix("users")." u, ".$xoopsDB->prefix("bb_forum_mods")." f WHERE f.forum_id = $forum AND u.uid = f.user_id";
      if(!$r = $xoopsDB->query($sql)){
		CloseTable();
		xoops_cp_footer();
		exit();
	}
      if($row = $xoopsDB->fetchArray($r)) {
	 do {
	    echo $row['uname']." (<input type=\"checkbox\" name=\"rem_mods[]\" value=\"".$row['uid']."\"> "._MD_A_REMOVE.")<br />";
	    $current_mods[] = $row['uid'];
	 } while($row = $xoopsDB->fetchArray($r));
	 echo "<br />";
      }
      else {
	 echo _MD_A_NOMODERATORASSIGNED."<br /><br />\n";
      }
?>
	<b>Add:</b><br />
	<select name='mods[]' size="5" multiple>
<?php
	$sql = "SELECT uid, uname FROM ".$xoopsDB->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 = $xoopsDB->query($sql)){
		CloseTable();
		xoops_cp_footer();
		exit();
	}
      if($row = $xoopsDB->fetchArray($r)) {
	 do {
	    $s = "";
	    if($row['uid'] == $myrow['forum_moderator'])
	      $s = "SELECTED";
	    echo "<option value=\"".$row['uid']."\" $s>".$row['uname']."</option>\n";
	 } while($row = $xoopsDB->fetchArray($r));
      }
      else {
	 echo "<option value=\"0\">"._MD_A_NONE."</option>\n";
      }
?>
        </select></td>
</tr>
<tr class='bg1' align='left'>
        <td><span class='fg2'><?php echo _MD_A_CATEGORY;?></span></td>
        <td><select name='cat'>
<?php
	$sql = "SELECT * FROM ".$xoopsDB->prefix("bb_categories")."";
      if(!$r = $xoopsDB->query($sql)){
		CloseTable();
		xoops_cp_footer();
		exit();
	}
      if($row = $xoopsDB->fetchArray($r)) {
	 do {
	    $s = "";
	    if($row['cat_id'] == $myrow['cat_id'])
						$s = "SELECTED";
	    echo "<option value=\"".$row['cat_id']."\" $s>".$row['cat_title']."</option>\n";
	 } while($row = $xoopsDB->fetchArray($r));
      }
      else {
	 echo "<option value=\"0\">"._MD_A_NONE."</option>\n";
      }
?>
        </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' align='left'>
         <td><span class='fg2'>Access Level:</span></td>
	 <td><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' align='left'>
	<td><span class='fg2'><?php echo _MD_A_TYPE;?></span></td>
	<td><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'";
			}
			echo "<tr class='bg1' align='left'><td><span class='fg2'>". _MD_A_ALLOWHTML ."</span></td>			<td><input type='radio' name='html' value='1'".$html_yes." />"._MD_A_YES."<input type='radio' name='html' value='0'".$html_no." />"._MD_A_NO ."</td></tr>
			<tr class='bg1' align='left'><td><span class='fg2'>". _MD_A_ALLOWSIGNATURES ."</span></td>
			<td><input type='radio' name='sig' value='1'$sig_yes />". _MD_A_YES ."<input type='radio' name='sig' value='0'$sig_no />". _MD_A_NO ."</td></tr>
			<tr class='bg1' align='left'><td><span class='fg2'>". _MD_A_HOTTOPICTHRESHOLD ."</span></td><td><input type='text' name='hot' size='3' maxlength='3' value='". $myrow['hot_threshold']."' /></td></tr>
			<tr class='bg1' align='left'><td><span class='fg2'>". _MD_A_POSTPERPAGE ."</span><br /><span class='fg2'><i>". _MD_A_TITNOPPTTWBDPPOT ."</i></span></td>
			<td><input type='text' name='ppp' size='3' maxlength='3' value='".$myrow['posts_per_page']."' /></td></tr>
			<tr class='bg1' align='left'><td><span class='fg2'>". _MD_A_TOPICPERFORUM ."</span><br><span class='fg2'><i>". _MD_A_TITNOTPFTWBDPPOAF ."</i></span></td><td><input type='text' name='tpp' size='3' maxlength='3' value='". $myrow['topics_per_page'] ."' /></td></tr>";
echo "<tr class='bg3' align='left'><td align='center' colspan='2'><input type='hidden' name='mode' value='editforum' /><input type='hidden' name='forum' value='$forum' /><input type='submit' name='save' value='". _MD_A_SAVECHANGES ."' />&nbsp;&nbsp;<input type='reset' value='". _MD_A_CLEAR ."' /></td></tr>";

			echo "</tr></table></td></tr></table>";





		}
		if(!isset($HTTP_POST_VARS['submit']) && !isset($HTTP_POST_VARS['save'])){
?>
<form action="<?php echo $PHP_SELF?>" 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"><span class='fg2'><b><?php echo _MD_A_SELECTFORUMEDIT;?></b><span class='fg2'></td>
</tr>
<tr class='bg1' align='left'>
	<td align='center' colspan="2"><select name='forum' SIZE="0">
	<?php

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

	?>
	</select></td>
</tr>
<tr class='bg3' align='left'>
	<td align='center' colspan="2">
		<input type="hidden" name='mode' value="editforum">
		<input type='submit' name='submit' value="<?php echo _MD_A_EDIT;?>">&nbsp;&nbsp;
	</td>
</tr>
</tr>
</TABLE></td></tr></TABLE>
<?php
		}
   break;
   case 'editcat':
    $myts =& MyTextSanitizer::getInstance();
   	if(isset($HTTP_POST_VARS['submit']) && isset($HTTP_POST_VARS['save']))
   	{
			$new_title = $myts->makeTboxData4Save($HTTP_POST_VARS['new_title']);
			$sql = "UPDATE ".$xoopsDB->prefix("bb_categories")." SET cat_title = '$new_title' WHERE cat_id = $cat_id";
			if(!$result = $xoopsDB->query($sql))
   		{
			CloseTable();
			xoops_cp_footer();
			exit();
   		}
   		else
   		{
   			echo "<table width='95%' border='1' cellspacing='0' cellpadding='0' align='center'>";
	 			echo "<tr><td align='center' width='100%' class='bg3'><span class='fg2'><b>"._MD_A_CATEGORYUPDATED."</b></span></td>";
	 			echo "</tr><tr><td><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
	 			echo "<td align=\"center\" width=\"100%\" class='bg'><span class='fg2'><p><a href=\"".$bbUrl['admin']."/index.php\">"._MD_A_RETURNTOADMINPANEL."</a></p><p><a href=\"".$bbUrl['root']."/index.php\">"._MD_A_RETURNTOFORUMINDEX."</span></p></td>";
	 			echo "</tr></table></td></tr></table>";
	 		}

   	}
   	else if(isset($HTTP_POST_VARS['submit']) && $HTTP_POST_VARS['submit'] != "")
   	{
   		$sql = "SELECT cat_title FROM ".$xoopsDB->prefix("bb_categories")." WHERE cat_id = '$cat'";
   		if(!$result = $xoopsDB->query($sql))
   		{
			CloseTable();
			xoops_cp_footer();
			exit();
   		}
   		$cat_data = $xoopsDB->fetchArray($result);
   		$cat_title = $myts->makeTboxData4Edit($cat_data["cat_title"]);
?>
<form action="<?php echo $PHP_SELF?>" 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"><span class='fg2'><b><?php echo _MD_A_EDITCATEGORY;?> <?php echo $cat_title ?></b><span class='fg2'></td>
</tr>
<tr class='bg1' align='left'>
	<td><?php echo _MD_A_CATEGORYTITLE;?></td>
	<td><input type="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?>" />
		<input type='submit' name='submit' value="<?php echo _MD_A_SAVECHANGES;?>" />
	</td>
</tr>
</tr>
</table></td></tr></table>
<?php
   	}
   	else {
   		$sql = "SELECT cat_id, cat_title FROM ".$xoopsDB->prefix("bb_categories")." ORDER BY cat_order";
   		if(!$result = $xoopsDB->query($sql))
   		{
			CloseTable();
			xoops_cp_footer();
			exit();
   		}
?>
<form action="<?php echo $PHP_SELF?>" 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"><span class='fg2'><b><?php echo _MD_A_SELECTACATEGORYEDIT;?></b><span class='fg2'></td>
</tr>
<tr class='bg1' align='left'>
	<td align='center' colspan="2"><select name='cat' SIZE="0">
<?php
			while($cat_data = $xoopsDB->fetchArray($result))
			{
				echo "<option value=\"".$cat_data["cat_id"]."\">".$myts->makeTboxData4Show($cat_data["cat_title"])."</option>\n";
			}
?>
</select></td>
<tr class='bg3' align='left'>
	<td align='center' colspan="2">
		<input type="hidden" name='mode' value="editcat">
		<input type='submit' name='submit' value="<?php echo _MD_A_EDIT;?>">&nbsp;&nbsp;
	</td>
</tr>
</tr>
</TABLE></td></tr></TABLE>
<?php
   	}
   break;
 case 'remcat':
    $myts =& MyTextSanitizer::getInstance();
   if(isset($HTTP_POST_VARS['submit']) && $HTTP_POST_VARS['submit'] != "") {
      $sql = "DELETE FROM ".$xoopsDB->prefix("bb_categories")." WHERE cat_id = $cat";
      if(!$r = $xoopsDB->query($sql)){
		CloseTable();
		xoops_cp_footer();
		exit();
	}
      echo "<table width='95%' border='1' cellspacing='0' cellpadding='0' align='center'>";
      echo "<tr><td align='center' width='100%' class='bg3'><span class='fg2'><b>"._MD_A_CATEGORYCREATED."</b></span></td>";
      echo "</tr><tr><td><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
      echo "<td align='center' width='100%' class='bg1'><span class='fg2'><p><a href=\"".$bbUrl['admin']."/index.php\">"._MD_A_RETURNTOADMINPANEL."</a></p><p><a href=\"".$bbUrl['root']."/index.php\">"._MD_A_RETURNTOFORUMINDEX."</a></p></span></td>";
      echo "</tr></table></td></tr></table>";

   }
   else {
?>
	<form action="<?php echo $PHP_SELF?>" 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"><span class='fg2'><b><?php echo _MD_A_RMVACAT;?></b></span></td>
	</tr>
	<tr class='bg3' align='left'>
	<td align='center' colspan="2"><span class='fg2'><i><?php echo _MD_A_NTWNRTFUTCYMDTVTEFS;?></i></span></td>
	</tr>
	<tr class='bg1'>
	<td align='center' colspan="2"><span class='fg2'>
	<select name='cat'>

<?php
      $sql = "SELECT * FROM ".$xoopsDB->prefix("bb_categories")." ORDER BY cat_title";
      if(!$r = $xoopsDB->query($sql)){
		CloseTable();
		xoops_cp_footer();
		exit();
	}
      while($m = $xoopsDB->fetchArray($r)) {
	 echo "<option value=\"".$m['cat_id']."\">".$myts->makeTboxData4Show($m['cat_title'])."</option>\n";
      }
?>
	</select>
	<input type='hidden' name='mode' value='<?php echo $mode ?>' /></td>
	</tr>
	<tr class='bg3'>
	<td align='center' colspan="2"><span class='fg2'>
	<input type="submit" name="submit" value="<?php echo _MD_A_REMOVECATEGORY;?>" /></td></tr>
	</table></table></form>
<?php
   }
   break;
 case 'addcat':
    $myts =& MyTextSanitizer::getInstance();
   if(isset($HTTP_POST_VARS['submit']) && $HTTP_POST_VARS['submit'] != "") {
	$nextid = $xoopsDB->genId($xoopsDB->prefix("bb_categories")."_cat_id_seq");
      $sql = "SELECT max(cat_order) AS highest FROM ".$xoopsDB->prefix("bb_categories")."";
      if(!$r = $xoopsDB->query($sql)){
			CloseTable();
			xoops_cp_footer();
			exit();
	}
      list($highest) = $xoopsDB->fetchRow($r);
      $highest++;
      $title = $myts->makeTboxData4Save($title);
      $sql = "INSERT INTO ".$xoopsDB->prefix("bb_categories")." (cat_id, cat_title, cat_order) VALUES ($nextid, '$title', '$highest')";
      if(!$result = $xoopsDB->query($sql)){
			CloseTable();
			xoops_cp_footer();
			exit();
	}
      echo "<table width='95%' border='1' cellspacing='0' cellpadding='0' align='center'>";
      echo "<tr><td align=\"center\" width=\"100%\" class='bg3'><span class='fg2'><b>"._MD_A_CATEGORYCREATED."</b></span></td>";
      echo "</tr><tr><td><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
      echo "<td align=\"center\" width=\"100%\" class='bg1'><span class='fg2'><p><a href=\"".$bbUrl['admin']."/index.php\">"._MD_A_RETURNTOADMINPANEL."</a></p><p><a href=\"".$bbUrl['root']."/index.php\">"._MD_A_RETURNTOFORUMINDEX."</a></p></span></td>";
      echo "</tr></table></td></tr></table>";
   }
   else {
?>
<form action="<?php echo $PHP_SELF?>" 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"><span class='fg2'><b><?php echo _MD_A_CREATENEWCATEGORY;?></b></span></td>
</tr>
<tr class='bg1' align='left'>
	<td><span class='fg2'><?php echo _MD_A_CATEGORYTITLE;?></span></td>
	<td><input type="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" name="submit" value="<?php echo _MD_A_CREATENEWCATEGORY;?>" />&nbsp;&nbsp;
		<input type="reset" value="<?php echo _MD_A_CLEAR;?>" />
	</td>
</tr>
</tr>
</table></td></tr></table>
<?php
		}
   break;
 case 'addforum':
    $myts =& MyTextSanitizer::getInstance();
   if(isset($HTTP_POST_VARS['submit']) && $HTTP_POST_VARS['submit'] != "") {
      if($name == '' || $desc == '' || !is_array($mods)){
			echo _MD_A_YDNFOATPOTFDYAA;
			CloseTable();
			xoops_cp_footer();
			exit();
	}
      	$desc = $myts->makeTareaData4Save($HTTP_POST_VARS['desc']);
      	$name = $myts->makeTboxData4Save($HTTP_POST_VARS['name']);
	$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 = $xoopsDB->genId($xoopsDB->prefix("bb_forums")."_forum_id_seq");
	$sql = "INSERT INTO ".$xoopsDB->prefix("bb_forums")." (forum_id, forum_name, forum_desc, forum_access, cat_id, forum_type, allow_html, allow_sig,posts_per_page,hot_threshold,topics_per_page) VALUES ($nextid, '$name', '$desc', $forum_access, $cat, $type, '$html', '$sig', $ppp, $hot, $tpp)";
      if(!$result = $xoopsDB->query($sql)){
			CloseTable();
			xoops_cp_footer();
			exit();
	}
	if($nextid == 0){
		$nextid = $xoopsDB->getInsertId();
	}
      $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 ".$xoopsDB->prefix("users")." SET level = 2 WHERE ";
	    	if($count > 0)
	      	$user_query .= "OR ";
	    	$user_query .= "uid = $mod ";
	    	$count++;
	 		}
	 		$mod_query = "INSERT INTO ".$xoopsDB->prefix("bb_forum_mods")." (forum_id, user_id) VALUES ($nextid, $mod)";
	 		if(!$xoopsDB->query($mod_query)){
				CloseTable();
				xoops_cp_footer();
				exit();
			}
    	}

    if(isset($user_query)) {
	 	if(!$xoopsDB->query($user_query)){
			CloseTable();
			xoops_cp_footer();
			exit();
		}
    }
      echo "<table width='95%' border='1' cellspacing='0' cellpadding='0' align='center'>";
      echo "<tr><td align='center' width='100%' class='bg3'><span class='fg2'><b>"._MD_A_FORUMCREATED."</b></span></td>";
      echo "</tr><tr><td><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
      echo "<td align='center' width='100%' class='bg1'><span class='fg2'><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></span></td>";
      echo "</tr></table></td></tr></table>";
   }
   else {
      $sql = "SELECT count(*) AS total FROM ".$xoopsDB->prefix("bb_categories")."";
      if(!$r = $xoopsDB->query($sql)){
		CloseTable();
		xoops_cp_footer();
		exit();
	}
      list($total) = $xoopsDB->fetchRow($r);
      if($total < 1 || !isset($total)){
		echo _MD_A_EYMAACBYAF;
		CloseTable();
		xoops_cp_footer();
		exit();
	}
      ?>
<form action="<?php echo $PHP_SELF?>" 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"><span class='fg2'><b><?php echo _MD_A_CREATENEWFORUM;?></b></span></td>
</tr>
<tr class='bg1' align='left'>
	<td><span class='fg2'><?php echo _MD_A_FORUMNAME;?></span></td>
	<td><input type='text' name='name' SIZE="40" MAXLENGTH="150"></td>
</tr>
<tr class='bg1' align='left'>
	<td><span class='fg2'><?php echo _MD_A_FORUMDESCRIPTION;?></span></td>
	<td><TEXTAREA name='desc' ROWS="15" COLS="45" WRAP="VIRTUAL"></TEXTAREA></td>
</tr>
<tr class='bg1' align='left'>
	<td><span class='fg2'><?php echo _MD_A_MODERATOR;?></span></td>
	<td><select name='mods[]' size='5' multiple='multiple'>
<?php
	$sql = "SELECT uid, uname FROM ".$xoopsDB->prefix("users")." WHERE uid != 0 AND level > 0 ORDER BY uname";
      if(!$result = $xoopsDB->query($sql)){
		CloseTable();
		xoops_cp_footer();
		exit();
	}
      if($myrow = $xoopsDB->fetchArray($result)) {
	 do {
	    echo "<option value=\"".$myrow['uid']."\">".$myrow['uname']."</option>\n";
	 } while($myrow = $xoopsDB->fetchArray($result));
      }
      else {
	 echo "<option value=\"0\">"._MD_A_NONE."</option>\n";
      }
?>
	</select></td>
</tr>
<tr class='bg1' align='left'>
	<td><span class='fg2'><?php echo _MD_A_CATEGORY;?></span></td>
	<td><select name="cat">
<?php
			$sql = "SELECT * FROM ".$xoopsDB->prefix("bb_categories")."";
			if(!$result = $xoopsDB->query($sql)){
				CloseTable();
				xoops_cp_footer();
				exit();
			}
			if($myrow = $xoopsDB->fetchArray($result)) {
				do {
					echo "<option value=\"".$myrow['cat_id']."\">".$myrow['cat_title']."</option>\n";
				} while($myrow = $xoopsDB->fetchArray($result));
			}
			else {
				echo "<option value=\"0\">"._MD_A_NONE."</option>\n";
			}
?>
	</select></td>
</tr>
<tr class='bg1' align='left'>
	 <td><span class='fg2'><?php echo _MD_A_ACCESSLEVEL;?></span></td>
	 <td><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' align='left'>
        <td><span class='fg2'><?php echo _MD_A_TYPE;?></span></td>
        <td><select name="type">
        <option value="0"><?php echo _MD_A_PUBLIC;?></option>
        <option value="1"><?php echo _MD_A_PRIVATE;?></option>
        </select>
        </td>
</tr>
<?php
echo "<tr class='bg1' align='left'><td><span class='fg2'>". _MD_A_ALLOWHTML ."</span></td>			<td><input type='radio' name='html' value='1' />"._MD_A_YES."<input type='radio' name='html' value='0' checked='checked'  />"._MD_A_NO ."</td></tr>
			<tr class='bg1' align='left'><td><span class='fg2'>". _MD_A_ALLOWSIGNATURES ."</span></td>
			<td><input type='radio' name='sig' value='1' checked='checked' />". _MD_A_YES ."<input type='radio' name='sig' value='0' />". _MD_A_NO ."</td></tr>
			<tr class='bg1' align='left'><td><span class='fg2'>". _MD_A_HOTTOPICTHRESHOLD ."</span></td><td><input type='text' name='hot' size='3' maxlength='3' value='10' /></td></tr>
			<tr class='bg1' align='left'><td><span class='fg2'>". _MD_A_POSTPERPAGE ."</span><br /><span class='fg2'><i>". _MD_A_TITNOPPTTWBDPPOT ."</i></span></td>
			<td><input type='text' name='ppp' size='3' maxlength='3' value='10' /></td></tr>
			<tr class='bg1' align='left'><td><span class='fg2'>". _MD_A_TOPICPERFORUM ."</span><br><span class='fg2'><i>". _MD_A_TITNOTPFTWBDPPOAF ."</i></span></td><td><input type='text' name='tpp' size='3' maxlength='3' value='20' /></td></tr>";
?>
<tr class='bg3' align='left'>
	<td align='center' colspan="2">
		<input type="hidden" name="mode" value="addforum" />
		<input type="submit" name="submit" value="<?php echo _MD_A_CREATENEWFORUM;?>" />&nbsp;&nbsp;
		<input type="reset" value="<?php echo _MD_A_CLEAR;?>" />
	</td>
</tr>
</tr>

</table></td></tr></table>
<?php
		}
   break;
 case 'catorder':
    $myts =& MyTextSanitizer::getInstance();
//    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 ".$xoopsDB->prefix("bb_categories")." SET cat_order = $order WHERE cat_id = $cat_id";
	    if(!$r = $xoopsDB->query($sql1)){
			CloseTable();
			xoops_cp_footer();
			exit();
		}
	    $sql2 = "UPDATE ".$xoopsDB->prefix("bb_categories")." SET cat_order = $current_order WHERE cat_id = $last_id";
	    if(!$r = $xoopsDB->query($sql2)){
			CloseTable();
			xoops_cp_footer();
			exit();
		}
	    echo "<div align=\"center\"><span class='fg2'>"._MD_A_CATEGORYMOVEUP."</span></div><br />";
	 }
	 else
	   echo "<div align=\"center\"><span class='fg2'>"._MD_A_TCIATHU."</span></div><br>";

      }
      else if(isset($down) && $down != "") {
	 $sql = "SELECT cat_order FROM ".$xoopsDB->prefix("bb_categories")." ORDER BY cat_order DESC";
	 if(!$r  = $xoopsDB->query($sql,1,0)){
		CloseTable();
		xoops_cp_footer();
		exit();
	}
	 list($last_number) = $xoopsDB->fetchRow($r);
	 if($last_number != $current_order) {
	    $order = $current_order + 1;
	    $sql = "UPDATE ".$xoopsDB->prefix("bb_categories")." SET cat_order = $current_order WHERE cat_order = $order";
	    if(!$r  = $xoopsDB->query($sql)){
			CloseTable();
			xoops_cp_footer();
			exit();
		}
	    $sql = "UPDATE ".$xoopsDB->prefix("bb_categories")." SET cat_order = $order where cat_id = $cat_id";
	    if(!$r  = $xoopsDB->query($sql)){
			CloseTable();
			xoops_cp_footer();
			exit();
		}
	    echo "<div align=\"center\"><span class='fg2'>"._MD_A_CATEGORYMOVEDOWN."</span></div><br>";

	 }
	 else
	   echo "<div align=\"center\"><span class='fg2'>"._MD_A_TCIATLD."</span></div><br>";
      }

?>
     <form action="<?php echo $PHP_SELF?>" 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"><span class='fg2'><b><?php echo _MD_A_SETCATEGORYORDER;?></b></span><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 ".$xoopsDB->prefix("bb_categories")." ORDER BY cat_order";
   if(!$r = $xoopsDB->query($sql)) {
      exit();
   }
   while($m = $xoopsDB->fetchArray($r)) {
      echo "<!-- New Row -->\n";
      echo "<form action=\"$PHP_SELF\" METHOD=\"POST\">\n";
      echo "<tr class='bg1' align='center'>\n";
      echo "<td>".$myts->makeTboxData4Show($m['cat_title'])."</td>\n";
      echo "<td><input type=\"hidden\" name=\"mode\" value=\"$mode\">\n";
      echo "<input type=\"hidden\" name=\"cat_id\" value=\"".$m['cat_id']."\">\n";
      echo "<input type=\"hidden\" name=\"last_id\" value=\"";
      if(isset($last_id)){
	echo $last_id;
      }
      echo "\">\n";
      echo "<input type=\"hidden\" name=\"current_order\" value=\"".$m['cat_order']."\"><input type=\"submit\" name=\"up\" value=\""._MD_A_MOVEUP."\"></td>\n";
      echo "<td><input type=\"submit\" name=\"down\" value=\""._MD_A_MOVEDOWN."\"></td></tr></form>\n<!-- End of Row -->\n";
      $last_id = $m['cat_id'];
   }
?>
     </TABLE></TABLE>
<?php
   break;
	case 'sync':
		if($submit)
		{
			echo "<div>"._MD_A_SYNCHING."<br />";
			flush();
			sync(NULL, "all forums");
			flush();
			sync(NULL, "all topics");
			echo "<br />";
			echo _MD_A_DONESYNC."</div>";
		}
		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="<?php echo $PHP_SELF?>" method="post">
	    <input type="hidden" name="mode" value="<?php echo $mode?>"><input type="submit" name="submit" value="<?php echo _MD_A_SYNCFORUM;?>"></form></td>
	</td>
</tr>
</table>
</td></tr></table>
<?php
		}
	break;
}

CloseTable();
xoops_cp_footer();
?>