<?php
require('top.php');
require(R_P.'require/page.php');
require(R_P.'require/f_channel.php');
require_once(R_P.'require/f_Dir.php');

InitGP(array('action','bc_id','bc_str','schannel','tschannel','channel','bc_type','path','path_pre','bc_pub',));
// 频道管理
/***************************************************************************************/


if ($action == 'channel') {
	//INNER JOIN  ".$db_tablepre."schannel  ON ".$db_tablepre."schannel.BC_id=".$db_tablepre."channel.BC_id
	$channel_str = '';// 输出值初始化
	$sql = "SELECT * FROM ".$db_tablepre."channel WHERE BC_schannel=1 ";
	$query = sql_return($sql);
	while ($List =  sql_return($query,'FetchRow')){
		$showChannel = showChannel($List['BC_id'],1);

		if ($row['BC_type']) $List['BC_pub']? $bc_pub = '<a href="?action=bc_pub&bc_id='.$List['BC_id'].'&pub=0">允许用户发布</a>' : $bc_pub = '<a href="?action=bc_pub&bc_id='.$List['BC_id'].'&pub=1">不允许用户发布</a>';

		$channel_str .= '<br> ◎ <b>'.$List['BC_channel']."</b>&nbsp;&nbsp;&nbsp;&nbsp;";
		if ($List['BC_type']) {
			$channel_str .= "<a href=\"?action=mod_cg&bc_id=".$List['BC_id']."\">分类管理</a>&nbsp;&nbsp;&nbsp;&nbsp;
			<a href=\"?action=add&schannel=2&tschannel=".$List['BC_id']."&path_pre=".$List['BC_path']."&bc_type=".$List['BC_type']."&bc_pub=".$List['BC_pub']."\" >增加</a>&nbsp;&nbsp;&nbsp;&nbsp;";
		}

		$channel_str .= "<a href=\"?action=del&bc_id=".$List['BC_id']."&path=".$List['path']."\" >
			删除
			</a>
			&nbsp;&nbsp;&nbsp;&nbsp;
			<a href=\"?action=mod&bc_id=".$List['BC_id']."&channel=".$List['BC_channel']."&path=".$List['BC_path']."\">
			修改
			</a>
			<form action=\"?action=channel_order&bc_id=".$List['BC_id']."\" method=\"POST\" enctype=\"multipart/form-data\">
 	频道排序:<input type=\"text\"   name=\"bc_order\" size=\"3\" >
 	<input type=\"submit\" value=\"修改\" >
		</form>
		".$bc_pub."
		<br>
		".$showChannel;
	}
}

elseif ($action == 'channel_ins'){
	if (!$bc_type) {
		$sql = "INSERT INTO ".$db_tablepre."channel (BC_channel,BC_path,BC_schannel,BC_Tschannel,BC_type,bc_pub)  VALUES ('".$channel."','".$path."', ".$schannel.", ".$tschannel.", 0,0)";
	}else {
		if ($path_pre)	$path = $path_pre.'/'.$path;
		$sql = "INSERT INTO ".$db_tablepre."channel (BC_channel,BC_path,BC_schannel,BC_Tschannel,BC_type,bc_pub)  VALUES ('".$channel."','".$path."', ".$schannel.", ".$tschannel.", '".$bc_type."',".$bc_pub.")";
	}
	delChannelCache();
	sql_return($sql)  && Showmsg('post_Success');
}

elseif ($action == 'channel_up'){
	delChannelCache();
	$sql = "UPDATE ".$db_tablepre."channel   SET BC_channel='".$channel."' WHERE BC_id=".$bc_id;
	sql_return($sql) && Showmsg('post_Success');
}

elseif ($action == 'del'){
	$sql = "SELECT * FROM ".$db_tablepre."channel WHERE BC_Tschannel=".$bc_id;
	$query = sql_return($sql,2) && Showmsg('channel_err_1');
	delChannelCache();

	$sql = "DELETE FROM ".$db_tablepre.$query['BC_type']." WHERE ".$query['BC_type']."_channel=".$bc_id;
	$query['BC_type'] && sql_return($sql);

	$sql = "DELETE FROM  ".$db_tablepre."channel   WHERE BC_id=".$bc_id;
	sql_return($sql) && Showmsg('post_Success');
}


elseif ($action == 'mod_cg'){
	$sql = "SELECT BC_categories FROM ".$db_tablepre."channel WHERE BC_id=".$bc_id;
	$query = sql_return($sql,2) ;
	if ($query['BC_categories']) {
		$cg = explode('|',$query['BC_categories']);
		array_pop($cg);
	}
}

elseif ($action == 'mod_cg_up'){
	InitGP(array('cg','key',));
	$sql = "SELECT BC_categories FROM ".$db_tablepre."channel WHERE BC_id=".$bc_id;
	$query = sql_return($sql,2) ;
	$cgbak = explode('|',$query['BC_categories']);
	$cgbak[$key] = $cg;
	$cg = join('|',$cgbak);

	$sql = "UPDATE ".$db_tablepre."channel   SET BC_categories='".$cg."' WHERE BC_id=".$bc_id;
	sql_return($sql) && Showmsg('post_Success');
}


elseif ($action == 'add_cg_up'){
	InitGP(array('cg',));
	$sql = "SELECT BC_categories FROM ".$db_tablepre."channel WHERE BC_id=".$bc_id;
	$query = sql_return($sql,2) ;
	$cg = $query['BC_categories'].$cg.'|';

	$sql = "UPDATE ".$db_tablepre."channel SET BC_categories='".$cg."' WHERE BC_id=".$bc_id;
	sql_return($sql) && Showmsg('post_Success');
}

elseif ($action == 'bc_pub'){
	InitGP(array('pub',));
	$sql = "UPDATE ".$db_tablepre."channel   SET BC_pub='".$pub."' WHERE BC_id=".$bc_id;
	sql_return($sql) && Showmsg('post_Success');
}


elseif ($action == 'channel_order'){
	InitGP(array('bc_order',));
	$sql = "UPDATE ".$db_tablepre."channel   SET BC_order='".$bc_order."' WHERE BC_id=".$bc_id;
	sql_return($sql) && Showmsg('post_Success');
}

else {
}

require PrintEot($self_template,'A');
echo footer('N');
?>