<?php
if(!defined('MOD'))  die("access die");
if($admin_info['groupid'] > 2 ) qbase::purviewCheckDie();
if($_POST['insert']==1){
	$param = array();
	$param['channelid']    = intval($_POST['channelid']);
	$param['channelname']  = clearSpecialChars(trim($_POST['channelname']));
	$param['channeltitle'] = clearSpecialChars(trim($_POST['channeltitle']));
	$param['channelitem']  = clearSpecialChars(trim($_POST['channelitem']));
	$param['channeldir']   = clearSpecialChars(trim($_POST['channeldir']));
	$param['channelselect']= intval($_POST['channelselect']);
	$param['system']       = 0;
	$param['show']         = intval($_POST['show']);
	$param['close_index']         = intval($_POST['close_index']);
	$param['link']=intval($_POST['link']);
	$param['url']=trim($_POST['url']);
	$param['is_html']=intval($_POST['is_html']);
	$param['is_html_content']=intval($_POST['is_html_content']);
	$param['is_auto']=intval($_POST['is_auto']);
	$param['html_date_path']=intval($_POST['html_date_path']);
	$param['upload_date_path']=intval($_POST['upload_date_path']);
	$param['html_type']=trim($_POST['html_type']);
	$param['html_pre']=trim($_POST['html_pre']);
	$param['html_size']=intval($_POST['html_size']);
	$param['CidCacheTime']=intval($_POST['CidCacheTime']);
	$param['ShowCacheTime']=intval($_POST['ShowCacheTime']);
	$param['allow_upload']=intval($_POST['allow_upload']);
	$param['upload_size']=intval($_POST['upload_size']);
	$param['upload_type']=trim($_POST['upload_type']);
	$param['coment_pass']=intval($_POST['coment_pass']);
	$param['pic_height']=intval($_POST['pic_height']);
	$param['pic_width']=intval($_POST['pic_width']);
	$param['pic_cut']=intval($_POST['pic_cut']);
	$param['water']=intval($_POST['water']);
	$param['water_position']=intval($_POST['water_position']);
	$param['water_word']=trim($_POST['water_word']);
	$param['water_size']=intval($_POST['water_size']);
	$param['water_color']=trim($_POST['water_color']);
	$param['target']	=	trim($_POST['target']);
	$param['description']	=	clearSpecialChars(trim($_POST['description']));
	$param['skin']	=	clearSpecialChars(trim($_POST['skin']));
	$param['index_template']	=	trim($_POST['index_template']);
	$param['displayorder'] = intval($_POST['displayorder']);
	$param['domain'] = trim($_POST['domain']);

	$param['show_special']=intval($_POST['show_special']);
	$param['show_vote']=intval($_POST['show_vote']);
	$param['show_money']=intval($_POST['show_money']);
	$param['show_related']=intval($_POST['show_related']);

	$is_channelid=qdb::QueryAlone("select `channelid` from `".TABLEPRE."channel` where `channelid`={$param['channelid']}");
	$is_channeliname=qdb::QueryAlone("select `channelname` from `".TABLEPRE."channel` where `channelname`='".$param['channelname']."'");
	$is_channeldir=qdb::QueryAlone("select `channeldir` from `".TABLEPRE."channel` where `channeldir`='".$param['channeldir']."'");

	if($is_channelid || $is_channeliname || $is_channeldir || $param['channelid'] < 1) {
		qbase::showmsg('与现有频道的名称或者目录或者ID重合');
	}else{
		if(!$_POST['url']) {	
			$dir = R_P . DIRECTORY_SEPARATOR . $param['channeldir'];
			qbase::createdir($dir);
			clearstatcache();
			if(!is_dir($dir)) qbase::showmsg("出错了，创建频道目录{$param['channeldir']}失败，请更改服务器目录权限");
			$data = qdb::QueryFirst("select is_morechannel, is_hidden from ".TABLEPRE."module where id='{$param['channelselect']}' limit 1");
			if($data['is_hidden'] > 0) qbase::showmsg("error request");
			if( $data['is_morechannel']==0){
				$num = qdb::QueryAlone("select count(*) from ".TABLEPRE."channel where channelselect={$param['channelselect']}");
				if( $num>0){
					qbase::showmsg("此模型不允许设置多频道");
				}
			}
			if($param['index_tempalte'] && !is_file(R_P . '/templates/' . $param['index_tempalte'])) qbase::showmsg("出错了，频道首页模板不存在");
			$sql = qbase::createSql(TABLEPRE.'channel', $param, 'insert');
			qdb::Query($sql);
		}else{
			$item = array();
			$field = array('channelid','channelname','displayorder','show','link','url','target');
			foreach($field as $val){
				$item[$val] = $param[$val];
			}
			$item['link'] = 1;
			$sql = qbase::createSql(TABLEPRE.'channel', $param, 'insert');
			qdb::Query($sql);
		}
		createChannelTable($param['channelselect'], $param['channelid']);
		commonCache();
		$wait = 3;
		unset( $_POST['insert']);
		qbase::showmsg("频道添加成功<script type='text/javascript'>parent.document.getElementById('menu').src  = \"index.php?action=left&d=\" + (new Date()).toString();</script>",'default',"system.php?sys=channel&action=modify");
	}
}else{
	unset($_MODULE);
	TemplateSelect();
	$_MODULE = qdb::fetchAll("select * from ".TABLEPRE."module where is_hidden = 0 and is_morechannel = 1");
}
?>