<script language=javascript src="./engine/js/forum.js"></script>
<?php
if(!defined('IN_GATEWEI')) {	exit('Access Denied');}
if($ipost=='savecategory'){
	
	$new_forumcategory=trim($new_forumcategory);
	if(empty($new_forumcategory) || strlen($new_forumcategory)>30){
		gadminmessage("分类标题不能为空且不能超过30个字符");
	}

	$db->query("INSERT INTO g_forumcategory ( forid , pid , upstatus , threads , posts , todayposts , forumtitle , ord , access , accesspwd , accessgroup , forumurl , lastpost , pagerecords , dateformat , hlightway) VALUES (NULL , 0 , $timestamp , 0 , 0 , 0 , '$new_forumcategory', 0 , 0 , '' , 4 , '' , '' , 0 , 'Y-n-j' , 0)");
	w2forumcategory();
	writelog($userid."增加论坛分类标题为".$new_forumcategory."成功");
	gadminmessage("论坛分类添加成功","siteadmin.php?iframe=forumcategory",1);
}

if($ipost=='saveforum'){
	
	$forumtitle=trim($forumtitle);
	if(empty($forumtitle) || strlen($forumtitle)>30){
		gadminmessage("论坛版块标题不能为空且不能超过30个字符");
	}
	$db->query("INSERT INTO g_forumcategory ( forid , pid , upstatus , threads , posts , todayposts , forumtitle , ord , access , accesspwd , accessgroup , forumurl , lastpost , pagerecords , dateformat , hlightway) VALUES (NULL , $pid , $timestamp , 0 , 0 , 0 ,'$forumtitle', 0 , 0 , '' , 4 , '' , '' , 20 , 'Y-n-j' , 0)");
	w2forumcategory();
	writelog($userid."增加论坛版块标题为".$forumtitle."成功");
	gadminmessage("论坛版块添加成功","siteadmin.php?iframe=forumcategory",1);
}


if($ipost=='update')
{
	$forumtitle=isset($forumtitle)?trim($forumtitle):'';
	if(empty($forumtitle) || strlen($forumtitle)>30){
		gadminmessage("分类标题不能为空且不能超过30个字符");
	}
	if($action=='forum'){
	   $pagerecords=intval($pagerecords);
	   $accesspwd=($access==1)?md5($accesspwd):'';
	   $accessgroup=($access==2)?$accessgroup:0;
	   $db->query("UPDATE g_forumcategory SET forumtitle = '$forumtitle', pid = $pid, access = $access, accesspwd = '$accesspwd',accessgroup=$accessgroup ,forumurl='$forumurl' , dateformat='$dateformat' ,pagerecords=$pagerecords , hlightway=$hlightway WHERE forid =$forid LIMIT 1");
	}elseif($action=='category'){
		$db->query("UPDATE g_forumcategory SET forumtitle = '$forumtitle' WHERE forid =$forid LIMIT 1");
	}
	w2forumcategory();
	require('./clientdata/cache/cache_navigation.php');
	foreach($nav as $key => $val){
		if($val['type']=='forum' && $val['id']==$forid){
			$nav[$key]['title']=$forumtitle;
			break;
		}
	}
	ksort($nav);
	w2navigation($nav,'update');
	writelog($userid."修改论坛版块或分类标题为".$forumtitle."成功");
	gadminmessage("修改成功","siteadmin.php?iframe=forumcategory",1);
}





if($ipost=='delete')
{
	$forid=$cid;
	$c=$db->result($db->query("SELECT count(*) FROM g_forumcategory WHERE pid = $forid"),0);
	if($c && !$pid){
		gadminmessage("该分类存在子分类，请先删除子分类");
	}
	if($db->result($db->query("SELECT count(*) FROM g_threads WHERE forid = $forid"),0)){
		gadminmessage("有论坛主题记录的分类将不能删除，请通过论坛管理手动删除论坛主题记录或合并到其他分类中，再删除该分类");
	}
	$db->query("DELETE FROM g_forumcategory WHERE forid = $forid LIMIT 1");
	w2forumcategory();
	writelog($userid."修改论坛版块或分类(".$forumcategory[$forid]['forumtitle'].")成功");

	gadminmessage("成功删除分类信息","siteadmin.php?iframe=forumcategory",1);
}

//这些地方还没有开始做
if($ipost=='merge')
{
	list($spid,$sforid)=explode(',',$sourcecategory);
	list($dpid,$dforid)=explode(',',$destinationcategory);

	if(($spid == 0 && $sforid==0) || ($dpid == 0 && $dforid==0))
	{
		gadminmessage("不能合并根分类");
	}

	$db->query("UPDATE g_threads SET forid = $dforid WHERE forid = $sforid");
	w2forumcategory();
	writelog($userid."合并分类(源:".$sforid."目的:".$dforid.")");
	gadminmessage("成功合并分类中的内容","siteadmin.php?iframe=forumcategory",1);
}

if($ipost=='order'){
	foreach($forumord as $key => $val){
	$val=intval($val);
	$db->query("UPDATE g_forumcategory SET ord = $val WHERE forid = $key");
	}
	w2forumcategory();
	writelog($userid."排序论坛版块成功");
	gadminmessage("设置成功","siteadmin.php?iframe=forumcategory",1);
}

if($ipost=='modify')
{
	$q=$db->query("SELECT * FROM g_forumcategory WHERE forid = $forid");
	$category=$db->fetch_array($q);
	if($category['pid']==0){
?>
<table>
	<caption>论坛版块修改</caption>
	<form method="post" action="siteadmin.php?iframe=forumcategory&ipost=update&action=category">
		<tr>
			<th colspan="2"><span>论坛分类修改</span><input type="hidden" name="forid" value="<?php echo $forid?>"></th>
		</tr>
		<tr>
		   <td><span>论坛版块名称</span></td>
           <td><input type="text" name="forumtitle" class="formtext" value="<?php echo $category['forumtitle']?>"></td>
       </tr>
	    <tr>
			<td  colspan="2"> <div id="submitdiv"><input name="submit" type="submit" value="提&nbsp;&nbsp;交" ></div></td>
	   </tr>
</table>
</form>
<?php }else{?>
<form method="post" action="siteadmin.php?iframe=forumcategory&ipost=update&action=forum">
 <table>
	<caption>论坛版块修改</caption>
		<tr>
			<th colspan="2"><span>论坛版块修改</span><input type="hidden" name="forid" value="<?php echo $forid?>"></th>
		</tr>
  <tr>
    <td><span>论坛版块名称</span></td>
    <td ><input type="text" name="forumtitle" class="formtext" value="<?php echo $category['forumtitle']?>"></td>
  </tr>
  <tr>
    <td><span>所属论坛分类</span></td>
    <td >
	  <select name="pid" class="formselect">
		<option value="0" <?php echo $category['pid']==0?'selected':''?>>请选择论坛分类</option >
		<?php
		$q=$db->query("SELECT * FROM g_forumcategory WHERE pid = 0 ORDER BY ord");
		while($ptitle=$db->fetch_array($q))
		{ ?>
		<option value="<?php echo $ptitle['forid'] ?>" <?php echo $ptitle['forid']==$category['pid']?'selected':''?> ><?php echo $ptitle['forumtitle']?></option >
		<?php } ?>
      </select>
	</td>
  </tr>
    <tr>
    <td><span>访问控制</span></td>
    <td >
	 <select id="access" name="access" class="formselect" onchange="showaccessway()">
	    <option value="0" <?php echo $category['access']==0?'selected':''?>>任何人可直接访问</option >
		<option value="1" <?php echo $category['access']==1?'selected':''?>>密码访问</option >
		<option value="2" <?php echo $category['access']==2?'selected':''?>>指定用户组访问</option >
	 </select>
	</td>
	</tr>
       <tbody id="accesspwd" style="display:none">
	    <tr>
			<td><span>访问密码:</span></td>
			<td ><input type="text" name="accesspwd" class="formtext" value="<?php echo $category['accesspwd']?>"></td>
		</tr>
		</tbody>
		<tbody id="accessgroup" style="display:none">
		<tr>
			<td><span>授权用户组</span></td>
			 <td >
			 <select name="accessgroup" class="formselect" >
			 <option value="0" >请选择授权用户组</option >
			 <?php foreach($groupnames as $key => $val){?>
				<option value="<?php echo $key?>" <?php echo $category['accessgroup']==$key?'selected':''?>><?php echo $val[0]?></option >
			 <?php }?>
			</select>
			</td>
		</tr>
		</tbody>

    <tr>
    <td><span>版块转向 URL:</span></td>
    <td ><input type="text" name="forumurl" class="formtext" value="<?php echo $category['forumurl']?>"></td>
  </tr>
   <tr>
    <td><span>列表每页显示记录数</span></td>
    <td >
	 <input type="text" name="pagerecords" class="formtext" value="<?php echo $category['pagerecords']?>">
	</td>
  </tr>
      <tr>
    <td><span>发布时间格式化</span></td>
    <td >
		 <select name="dateformat" class="formselect">

<?php foreach($dateformats as $val){
				$selected=$category['dateformat']==$val?'selected':'';
				echo '<option value="'.$val.'" '.$selected.'>'.$val.'</option >';
			}
?>
		 </select>
	</td>
  </tr>
  <tr>
    <td><span>高亮显示样式</span></td>
    <td >
	  <select name="hlightway" class="formselect">
	  <?php 
	   
       foreach($hlightways as $key => $val){
		   $hlightselected=$category['hlightway']==$key?'selected':'';
			echo '<option value="'.$key.'"  style="'.$val[1].'" '.$hlightselected.'>'.$val[0].'</option>';
	 }
	 ?>
	  </select>


	</td>
  </tr>
  		<tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
			 
	      </td>
   </tr>
       <tr>
    <td  colspan="2"> <div id="submitdiv"><input name="" type="submit" value="提&nbsp;&nbsp;交" ></div></td>
    </tr>
	</form>
	</table>
	
	<SCRIPT LANGUAGE="JavaScript">
	showaccessway();
	</SCRIPT>

<?php  }
	exit();			  
}?>





 <table>
	<caption>论坛版块设置</caption>
	<form method="post" action="siteadmin.php?iframe=forumcategory&ipost=savecategory">
		<tr>
			<th colspan="2"><span>添加论坛分类</span></th>
		</tr>
		<tr>
			<td width="40%"><span>论坛分类名称</span></td>
		    <td > <input type="text" name="new_forumcategory" class="smalltext"> </td>
	   </tr>
  	   <tr>
           <td colspan="2">
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
			</td>
       </tr>
       <tr>
             <td  colspan="2"> <div id="submitdiv"><input type="submit" value="提&nbsp;&nbsp;交" ></div></td>
       </tr>
	</form>

	<form method="post" action="siteadmin.php?iframe=forumcategory&ipost=saveforum">
		<tr>
			<th colspan="2"><span>添加论坛版块</span></th>
		</tr>
		<tr>
			<td width="40%"><span>论坛分类名称</span></td>
			<td ><input type="text" name="forumtitle" class="smalltext"></td>
		</tr>
	   <tr>
			<td><span>所属父分类</span></td>
	    <td >
			 <select name="pid" class="smallselect">
				<option value="0" selected>请选择论坛分类</option >
				<?php
						$q=$db->query("SELECT * FROM g_forumcategory WHERE pid = 0");
						while($forumcategory=$db->fetch_array($q))
						{ ?>
						<option value="<?php echo $forumcategory['forid'] ?>"><?php echo $forumcategory['forumtitle']?></option >
						<?php } ?>
			   </select>
	    </td>
     </tr>
  	 <tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
    </tr>
        <tr>
          <td  colspan="2"> <div id="submitdiv"><input name="" type="submit" value="提&nbsp;&nbsp;交" ></div></td>
     </tr>
</form>


<!-- 排序表单 -->
<form id="orderarticlecategory" method="post" action="siteadmin.php?iframe=forumcategory&ipost=order">
   <tr>
       <th colspan="2"><span>论坛版块管理</span></th>
   </tr>
   <tr>
     <td colspan="2">
       <ul>
       <?php 
	$q=$db->query("SELECT * FROM g_forumcategory WHERE pid = 0 ORDER BY ord");

while($categorys=$db->fetch_array($q))
{
	$pid=$categorys['forid'];
	$c=$db->result($db->query("SELECT count(*) FROM g_forumcategory WHERE pid = $pid"),0);

	echo '<li>'.$categorys['forumtitle'].' - 显示顺序:<input type="text" name="forumord['.$categorys['forid'].']" size="2"> '.' [<a href="siteadmin.php?iframe=forumcategory&ipost=modify&forid='.$categorys['forid'].' ">编辑</a>]  [<a href="javascript:void(0)" onclick="delcategory(2,'.$categorys['pid'].','.$categorys['forid'].','.$c.')">删除</a>]';
	if($c){
		echo '<ul>';
	}
	$subq=$db->query("SELECT * FROM g_forumcategory WHERE pid = $pid ORDER BY ord");
	while($subcategorys=$db->fetch_array($subq)){
			echo '<li>'.$subcategorys['forumtitle'].' - 显示顺序:<input type="text" name="forumord['.$subcategorys['forid'].']" size="2"> '.'  [<a href="siteadmin.php?iframe=forumcategory&ipost=modify&forid='.$subcategorys['forid'].' ">编辑</a>] 
			[<a href="javascript:void(0)" onclick="delcategory(2,'.$subcategorys['pid'].','.$subcategorys['forid'].',0)">删除</a>] </li>';
		}
	if($c){
		echo '</ul>';
	}
	echo '</li>';
}
//delcategory(参数1为int (article,product,forum) , pid 上级分类ID  , cid 当前分类id , c分类中的内容数量);
?>
	</ul>
  </td>
  </tr>
         		<tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
			 
	      </td>
   </tr>
    <tr>
    <td  colspan="2"> <div id="submitdiv"><input name="g_forum_category_submit" type="submit" value="提&nbsp;&nbsp;交" ></div></td>
    </tr>
	</form>

<!-- 合并表单 -->

   <form id="mergeforumcategory" method="post">
   <tr>
    <th colspan="2"><span>论坛合并</span></th>
  </tr>
  <tr>
    <td><span>源论坛名称(源论坛不会删除)</span></td>
    <td >
       <select name="sourcecategory" id="sourcecategory" class="smallselect">
	   <option value="0,0" selected >请选择源论坛</option >
	<?php
$qc=$db->query("SELECT * FROM g_forumcategory WHERE pid = 0");
while($category=$db->fetch_array($qc)){ ?>
<optgroup label="<?php echo $category['forumtitle']?>">
	<?php echo $category['forumtitle']?>
</option >
<?php 
$pid=$category['forid'];
$subqc=$db->query("SELECT * FROM g_forumcategory WHERE pid = $pid");
while($subcategory=$db->fetch_array($subqc)){
				echo '<option value="'.$subcategory['pid'].','.$subcategory['forid'].'">'.$subcategory['forumtitle'].'</option>';
			}
		  echo '</optgroup>';
		}
?>
      </select>
</td>
  </tr>
  <tr>
    <td><span>目标论坛名称(源分类所有论坛会归属到目标分类中)</span></td>
    <td >
	  <select name="destinationcategory" id="destinationcategory"  class="smallselect">
	  <option value="0,0" selected >目标论坛</option >
<?php
	$qc=$db->query("SELECT * FROM g_forumcategory WHERE pid = 0");
	while($category=$db->fetch_array($qc)){ ?>
	<optgroup label="<?php echo $category['forumtitle']?>">
	<?php 
	$pid=$category['forid'];
	$subqc=$db->query("SELECT * FROM g_forumcategory WHERE pid = $pid");
	while($subcategory=$db->fetch_array($subqc)){
					echo '<option value="'.$subcategory['pid'].','.$subcategory['forid'].'">'.$subcategory['forumtitle'].'</option>';
				}
			 echo '</optgroup>';
			}
?>
      </select>
	</td>
  </tr>
  		<tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
			 
	      </td>
   </tr>
       <tr>
    <td  colspan="2"> <div id="submitdiv"><input name="g_article_category_submit" type="button" value="合&nbsp;&nbsp;并" onclick="mergecategory('forum')"></div></td>
    </tr>
</table>
</form>


