<?php
/*******************************************************************
 * 版权协议：2009-2019
 * 版权所有: PHP-北京金南曦网络技术有限公司旗下PHPWMS，PHP商城系统技术支持中心http://www.php188.net，并保留所有权利。 
* PHP商城系统网址: http://www.php188.com
 * PHPWMS系统网址：http://www.phpwms.com
 * 销售电话：010-87875088
 * 销售电话：010-87875188 
*------------------------------------------------------------------
 * 这不是一个完全免费软件！您只能在不用于商业目的的前提下对程序代码进
 * 行修改和使用；不允许对程序代码以任何形式任何目的的再发布。
 *------------------------------------------------------------------
 * 文件名称：admin_channel.php
 * 摘　　要：频道
 * 适用范围：PHPWMS 1.0
 * 版　　本：1.0
 *------------------------------------------------------------------
 * Script Written By phpwms项目组
 * 最后更新： 2009-09-24 phpwms 由PHP金南曦网络技术有限公司整理发布
 *******************************************************************/
if(!defined('IN_SYS'))exit('access die');
$do			  = trim($_REQUEST['do']);
$action		  = trim($_REQUEST['action']);
$channel_type = to_int($_REQUEST['channel_type']);
$tpl -> assign("do",$do);
$tpl -> assign("action",$action);
if ($channel_type==1) {
   if (!getAdminPri('3101')) { //判断是否有权限2008-6-27
   		exit(NO_AUTH_LOGIN);
      }
} else if ($channel_type==2) {
   if (!getAdminPri('3201')) {
	  exit(NO_AUTH_LOGIN);
   }
} else if ($channel_type==3) {
   if (!getAdminPri('3301')) {
	  exit(NO_AUTH_LOGIN);
   }
}else if ($channel_type==4) {
   if (!getAdminPri('4401')) {
	   exit(NO_AUTH_LOGIN);
   }
}
$model_list = get_models();
$channel_type = to_int($channel_type);
if(empty($channel_type))$channel_type = '1';
$tpl -> assign("channel_type",empty($channel_type)?'':$channel_type);
if (!getAdminPri('3333'))exit(NO_AUTH_LOGIN);
require SYS_ROOT.'pub/phpwms.kineditor.php';
if(is_post()){
	$task = $_POST['task'];
	$_SESSION['post_se_data'] = $_POST;
	switch ($task){
		case 'add_channel':
		case 'edit_channel':
			$channel_name = $_POST['data']['channel_name'];
			if(empty($channel_name))exit($sp->bgmsg('请输入频道名称'));
			$channel_type = to_int($_POST['data']['channel_type']);
			if(empty($channel_type))exit($sp->bgmsg('请选择所属模型'));
			$user_group = to_int($_POST['data']['g_id']);
			$channel_id = to_int($_POST['channel_id']);
			$display_num = to_int($_POST['display_num']);
			$display_num = empty($display_num)?10:$display_num;
			$insert_data= array(
				'channel_name'=>$channel_name,
				'channel_model'=>$channel_type,
			    'user_group'=>empty($user_group)?'1':$user_group,
				'opt_time'=>time(),
				'menu_show'=>empty($_POST['data']['menu_show'])?0:'1',
				'is_hidden'=>empty($_POST['data']['is_hidden'])?0:'1',
				'out_link'=>$_POST['data']['out_link'],
				'order_num'=>to_int($_POST['data']['order_num']),
				'channel_desc'=>$_POST['read_me_content'],
				'display_num'=>$display_num
			);
			$tpl_name = $_POST['channel_tpl_name'];
			$insert_data['channel_tpl_name'] = !empty($tpl_name)?$tpl_name:'';
			if($task=='edit_channel'){
				$connet = " AND `channel_id` <> '$channel_id'";
			}
			$check_sql = "SELECT * FROM ##__channel WHERE `channel_name` = '$channel_name' AND `channel_model` ='$channel_type' $connet ;";
			if($task=='add_channel'){
				$check = $db->fetch($check_sql);
				if(!empty($check))exit($sp->bgmsg('已存在相同的频道名称'));
				$db->insert($insert_data,'channel');
				$msg = '添加频道成功';
			}else{
				$channel_id = to_int($channel_id);
				$check = $db->fetch($check_sql);
				if(!empty($check))exit($sp->bgmsg('已存在相同的频道名称,请重新填写'));
				if(empty($channel_id))exit('参数错误');
				//检查
				$db->update($insert_data,'channel',array('channel_id'=>$channel_id));
				$msg = '修改频道成功';
			}
			$_SESSION['post_se_data'] = '';
			php188StaticCache::getInstance()->remove('phpwms_site_channels');
			$sp->bgmsg($msg,'admincp.php?action=channel&channel_type='.$channel_type);
		break;
	}
	exit();
}
$tpl->assign('user_group_list',get_user_group());
foreach ($model_list as $list){
	if(trim($list['id']) == trim($channel_type)){
		$channel_type_name = $list['cn_name'];
		break;
	}else{
		$channel_type_name  = '';
	}
}
$tpl -> assign("channel_type_name",$channel_type_name);
$n = !empty($channel_type_name)?'['.$channel_type_name.']':'';
$tpl->assign('model_list',$model_list);
$tpl->assign('se_data',$_SESSION['post_se_data']);
switch ($do){
	case 'add':
		$pn  = '添加 '.$n.' 频道';
		$tpl -> assign("admin_nav",shownav($pn));
		$tpl -> assign("channel_type",$channel_type);
		$nav = '<a href="?action='.$action.'&channel_type='.$channel_type.'">频道管理</a>&nbsp;&raquo;&nbsp;添加频道';
		$editor = new editor();
		$editor->instanceName = 'read_me_content';
		$editor->model = 'basic';
		$editor->height = '200';
		$description = $editor->create();
		$tpl -> assign("description",$description);
		$tpl->assign('task','add_channel');
		break;
	case 'modify':
		$tpl->assign('task','edit_channel');
		$channel_id = to_int($_REQUEST['channel_id']);
		$tpl -> assign("channel_id",$channel_id);
		$sql = "SELECT * FROM ##__channel WHERE channel_id = ".$channel_id;
		$arr = $db ->fetch($sql,TRUE);
		if(intval(count($arr)) < 1){
			exit($sp -> bgmsg("您要编辑的频道不存在！","admincp.php?action=".$action."&channel_type=".$channel_type));
		} else {
			$tpl -> assign("data",$arr);
			$editor = new editor();
			$editor->instanceName = 'read_me_content';
			$editor->value = $arr[0]['read_me'];
			$editor->model = 'basic';
			$editor->height = '200';
			if(!empty($_SESSION['post_se_data']['read_me_content'])){
				$editor->value = $_SESSION['post_se_data']['read_me_content'];
			}else{
				$editor->value = $arr['channel_desc'];
			}
			$description = $editor->create();
			$tpl -> assign("description",$description);
		}
		$pn  = '修改['.$arr['channel_name'].']频道';
		$nav = '<a href="?action='.$action.'&channel_type='.$channel_type.'">频道管理</a>&nbsp;&raquo;&nbsp;修改－[<font color=blue>'.$arr['channel_name'].'</font>]－频道';
		$tpl -> assign("admin_nav",shownav($nav));
		break;
	case 'delete':
		$channel_id = to_int($_GET['channel_id']);
		if(empty($channel_id))exit($sp->bgmsg('参数错误','admincp.php?action=channel&channel_type=1'));
		$check = $db->fetch("SELECT * FROM ##__category WHERE `channel_id` ='$channel_id';");
		if(!empty($check))exit($sp->bgmsg('该频道下还有分类请先删除分类再删除该频道数据.'));
		$db->query("DELETE FROM ##__channel WHERE `channel_id` = '$channel_id'");
		php188StaticCache::getInstance()->remove('phpwms_site_channels');
		exit($sp->bgmsg('删除成功'));
		break;
	default://列表
	$tpl->assign('task','channel_list');
	$pn  = '频道管理';
	$tpl -> assign("admin_nav",shownav('<a href="?action='.$action.'&channel_type='.$channel_type.'">'.$pn.'</a>&nbsp;&raquo;&nbsp;频道列表'));
	$condition = !empty($channel_type)?" WHERE C.`channel_model` = '$channel_type' ":'';
	$sql = "SELECT C.*,M.cn_name,M.en_name,M.id,M.id AS channel_type FROM ##__channel AS C LEFT JOIN ##__model AS M ON C.`channel_model`= M.`id` $condition ORDER BY C.`order_num` ASC ;";
	$arr = $db->fetch($sql);

	$tpl -> assign("data",$arr);
}
$sp -> outPut('channel.tpl',$pn);
?>