<?php

function channel_KeyWords($id){
	global $db_tablepre,$header_Keywords;
	if (!$id) $id = 0;
	$sql = "SELECT BC_channel  FROM ".$db_tablepre."channel  WHERE BC_id=".$id;
	$str = sql_return($sql,2);
	return $str['BC_channel'].','.$header_Keywords;
}

function showChannel($parent,$level=1,$num=2){
	global $db_tablepre;
	//$str_channel = ''; // 输出值初始化
	$sql = "SELECT * FROM ".$db_tablepre."channel WHERE BC_schannel=".$num." AND BC_Tschannel=".$parent.' order by BC_order asc ';
	$result = sql_return($sql);
	while ($row = sql_return($result,'FetchRow')){
		$str_channel .= str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$level).' |- '.$row['BC_channel']."   &nbsp;&nbsp;&nbsp;&nbsp;
			<a href=\"?action=add&schannel=".($num+1)."&tschannel=".$row['BC_id']."&path_pre=".$row['BC_path']."&bc_type=".$row['BC_type']."&bc_pub=".$row['BC_pub']."\" >
			增加
			</a> 
			&nbsp;&nbsp;&nbsp;&nbsp;
			<a href=\"?action=del&bc_id=".$row['BC_id']."&path=".$row['BC_path']."\" >
			删除
			</a>
			&nbsp;&nbsp;&nbsp;&nbsp;
			<a href=\"?action=mod&bc_id=".$row['BC_id']."&channel=".$row['BC_channel']."\">
			修改
			</a>
			<br>";
		$str_channel .=	showChannel($row['BC_id'],$level+1,$num+1);
	}
	return $str_channel;
}



function page_Channel($Channel){
	global $web_domin,$db_tablepre;

	if(!$Channel) $Channel = 0;
	$sql = "SELECT * FROM ".$db_tablepre."channel WHERE BC_Tschannel=".$Channel;
	$r = sql_return($sql,3);
	if(!$r) {
		$sql = "SELECT BC_Tschannel FROM ".$db_tablepre."channel WHERE BC_id=".$Channel;
		$r = sql_return($sql,2);
		$Channel = $r['BC_Tschannel'];
	}

	$channel_cache_dir = R_P.'data/cache/channel';
	$channel_cache_path = $channel_cache_dir.'/channel_'.$Channel.'.php';

	if (file_exists($channel_cache_path)) return $channel_cache_path;
	else {
		$channel_config = '<li><a href="/">首页</a></li>';
		$sql = "SELECT BC_id,BC_type,BC_path,BC_channel  FROM ".$db_tablepre."channel WHERE BC_Tschannel=".$Channel.' order by BC_order asc ';
		$r_channel = sql_return($sql);
		while ($row = sql_return($r_channel,'FetchRow')) {
			$channel_config .= '<li calss="channel"><a href="/';
			if ($row['BC_type']) $channel_config .= $row['BC_type'].'/index.php?id='.$row['BC_id'];
			else $channel_config .= $row['BC_path'];
			$channel_config .= '">'.$row['BC_channel'].'</a></li>';
		}

		!file_exists($channel_cache_dir) &&  (mkdir($channel_cache_dir)  || Showmsg('mkdir_err'));

		writeover($channel_cache_path, $channel_config);

		return $channel_cache_path;

	}

}


function userShowChannel($parent,$level=1){
	global $db_tablepre;
	$str_channel = ''; // 输出值初始化
	$sql = "SELECT * FROM ".$db_tablepre."channel WHERE  BC_Tschannel=".$parent.' order by BC_order asc ';
	$result = sql_return($sql);
	while ($row = sql_return($result,'FetchRow')){
		$str_channel .= '<option value="'.$row['BC_id'].'" >'.str_repeat('&nbsp;&nbsp;',$level).' |- '.$row['BC_channel'].'</option>';
		$str_channel .=	 userShowChannel($row['BC_id'],$level+1);
	}
	return $str_channel;
}


function channel_site($id,$type=false){
	global $web_domin,$db_tablepre;
	$channel_site = '';
	if ($type){
		switch ($type) {
			case 'c':
				$type = 'content';
				break;
			case 'd':
				$type = 'discuss';
				break;
			default:
				$type = 'content';
				break;
		}
		$channel_site_title = '';
		$sql = "SELECT ".$type."_channel,".$type."_title,".$type."_id FROM ".$db_tablepre.$type."  WHERE ".$type."_id=".$id;
		$row = sql_return($sql,2);
		$channel_site_title = ' >>> <a href="/'.$type.'/'.$type.'.php?id='.$row[$type.'_id'].'&channel='.$row[$type.'_channel'].'">'.$row[$type.'_title'];
		if ($type == 'discuss') $channel_site_title .= '吧</a> >>> 主题列表' ;
		else $channel_site_title .= '</a> >>> 内容 ';
		$id = $row[$type.'_channel'];
	}
	$sql = "SELECT BC_id,BC_type,BC_channel,BC_schannel,BC_Tschannel  FROM ".$db_tablepre."channel  WHERE BC_id=".$id;
	$row = sql_return($sql,2);
	$channel_site = '<a href="/'.$row['BC_type'] .'/index.php';
	$channel_site .= '?id='.$row['BC_id'];
	$channel_site .= '">'.$row['BC_channel'].'</a>';
	if ($row['BC_Tschannel']) $channel_site = channel_site($row['BC_Tschannel']).' >>> '.$channel_site.$channel_site_title;

	return $channel_site;
}

function channel_site_title($id,$type=false){
	global $web_name,$db_tablepre;
	if ($type){
		switch ($type) {
			case 'c':
				$type = 'content';
				break;
			case 'd':
				$type = 'discuss';
				break;
			case 'd_m':
				$type = 'discuss_main ';
				break;
			default:
				$type = 'content';
				break;
		}
		$sql = "SELECT ".$type."_title FROM ".$db_tablepre.$type."  WHERE ".$type."_id=".$id;
		$row = sql_return($sql,2);
		$channel_site_title = $row[$type.'_title'];
		if (strpos($type,'isc')) $channel_site_title .= '吧';
	}else {
		$sql = "SELECT BC_channel FROM ".$db_tablepre."channel  WHERE BC_id=".$id;
		$row = sql_return($sql,2);
		$channel_site_title = $row['BC_channel'];
	}


	return $channel_site_title.' - '.$web_name;
}


function channel_discuss_site($id,$type=false){
	global $db_tablepre,$web_name;
	$sql = "SELECT d.discuss_title,d.discuss_id,d.discuss_channel  FROM ".$db_tablepre."discuss_index i  LEFT JOIN ".$db_tablepre."discuss d ON i.d_i_discuss_id=d.discuss_id  WHERE d_i_id=".$id;
	$row = sql_return($sql,2);
	$channel_site_title = $row['discuss_title'].'吧 - '.$web_name;
	$channel_discuss_site = $web_name.' >>> <a href="/discuss/discuss.php?id='.$row['discuss_id'].'&channel='.$row['discuss_channel'].'">'.$row['discuss_title'].'吧</a>  >>> 浏览贴子';

	return $channel_site_title.'|'.$channel_discuss_site.'|'.$row['discuss_title'].'|'.$row['discuss_id'];
}


function showGatherChannel($selected_chk = false,$parent=0,$level=1,$num=1){
	global $db_tablepre;
	$str_channel = ''; // 输出值初始化
	$sql = "SELECT BC_id,BC_channel FROM ".$db_tablepre."channel WHERE BC_schannel=".$num." AND BC_Tschannel=".$parent;
	$result = sql_return($sql);
	while ($row = sql_return($result,'FetchRow')){
		$str_channel .= '<option  value="'.$row['BC_id'].'" ';
		if ($selected_chk && $selected_chk == $row['BC_id']) $str_channel .= ' selected ';
		$str_channel .= ' >'.str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$level).' |- '.$row['BC_channel']."</option>";
		$str_channel .=	showGatherChannel($selected_chk, $row['BC_id'],$level+1,$num+1);
	}
	return $str_channel;
}


function channelCategoriesCache($arr){
	global $db_tablepre;
	$categories_cache_dir = R_P.'data/cache/channel';
	$categoriescache_cache_path = $categories_cache_dir.'/categories_cache.php';
	if (file_exists($categoriescache_cache_path)){
		require($categoriescache_cache_path);
		return $cg_arr[$arr];
	}
	else {
		$sql = "SELECT BC_categories,BC_id FROM ".$db_tablepre."channel WHERE BC_schannel=1";
		$result = sql_return($sql);
		while ($row = sql_return($result,'FetchRow')){
			$str_1 = explode('|',$row['BC_categories']);
			array_pop($str_1);
			$str_2[$row['BC_id']] = $str_1;
		}
		!file_exists($categories_cache_dir) &&  (mkdir($categories_cache_dir)  || Showmsg('mkdir_err'));
		CacheFile($str_2,$categoriescache_path,'cg');
		return $str_2[$arr];
	}
}


function channelChk($id){
	global $db_tablepre;
	$sql = "SELECT * FROM ".$db_tablepre."channel WHERE BC_Tschannel=".$id;
	sql_return($sql,3) && Showmsg('channl_pub_err_1');;
}

function delChannelCache($path){
	!$path && $path = R_P.'data/cache/channel';
	delDir($path);
}



?>