<?php
/*************************************************************\
+-------------------------------------------------------------+
| 通灵佛教网在线烧香程序 System                                 |
| Copyright (c) 2003-2010 Alfred China                        |
| All rights reserved.                                        |
+-------------------------------------------------------------+
\*************************************************************/
require_once('chk.php');
require_once('netgou.php');

$listid = intval($listid);

if($REQUEST_METHOD == "POST"){
	
	if($listid != ''){
		
		if($name == ''){
			echo "<script language=\"JavaScript\">\r\n"; 
			echo " alert(\"分类名称为空，请输入分类名称\");\r\n"; 
			echo " history.go(-1);\r\n";echo "</script>"; 
			exit; 
		}
		$sub_lists = ng_query("SELECT listid FROM $sql_tbl[newslists] WHERE $sql_tbl[newslists].parentid = $listid");
		
		if($sub_lists == ""){
		
			$subids = "(".$listid.")";
			 
		}else{
			$subids = "(".$listid;
			foreach($sub_lists as $sl){
				$subids .= ",".$sl["listid"];
			}
			$subids .= ")";
		}
		
		db_query("UPDATE $sql_tbl[newslists] SET name = '$name', descr = '$descr', avail = '$avail', parentid = '$parentid', subids = '$subids' WHERE listid='$listid'");

		showmsg("分类修改成功","newscat.php");
		
	}elseif($mode == 'add'){
		
		if ($name == ''){
			echo "<script language=\"JavaScript\">\r\n"; 
			echo " alert(\"分类名称为空，请输入分类名称\");\r\n"; 
			echo " history.go(-1);\r\n";echo "</script>"; 
			exit; 
		}
		db_query("INSERT INTO $sql_tbl[newslists] (name,descr,avail,parentid) VALUES ('$name','$descr', '$avail', '$parentid')");
		$subids = mysql_insert_id();
		$strids = "(".$subids.")";
		db_query("UPDATE $sql_tbl[newslists] SET subids = '$strids' WHERE listid='$subids'");
		
		showmsg("分类添加成功","newscat.php");
	}
}

if($mode == "setavail"){
		
	db_query("UPDATE $sql_tbl[newslists] SET avail = '$avail' WHERE listid='$listid'");
	showmsg("状态修改成功","newscat.php");
	
}elseif($mode == "delcat"){
	
	$sub_lists = ng_query("SELECT listid FROM $sql_tbl[newslists] WHERE $sql_tbl[newslists].parentid = $listid");
	if($sub_lists != ''){
			echo "<script language=\"JavaScript\">\r\n"; 
			echo " alert(\"分类下有子分类不能删除，请先删除子分类\");\r\n"; 
			echo " history.go(-1);\r\n";echo "</script>"; 
			exit; 
		}
	db_query("DELETE FROM $sql_tbl[newslists] WHERE listid='$listid'");
	showmsg("分类删除成功","newscat.php");

}

if($listid){
	$list = ng_query_first("SELECT * FROM $sql_tbl[newslists] WHERE listid='$listid'");
	$txtsou = " where $sql_tbl[newslists].parentid = '$listid'";
	
}else{

	$txtsou = " where $sql_tbl[newslists].parentid = 0";
	
}

$lists = ng_query("SELECT * FROM $sql_tbl[newslists] $txtsou");

$ng_obstart && function_exists('ob_gzhandler') ? ob_start('ob_gzhandler') : ob_start();
$ng_obstart ? $ng_gzip="Gzip enabled":$ng_gzip="Gzip disabled";

require_once PrintTemp('newscat');
require_once "./footer.php";

$output=str_replace(array('<!--<!--<!---->','<!--<!---->','<!---->'),array('','',''),ob_get_contents());
ob_end_clean();
echo $output;
flush;
exit;
?>