<?php
(!defined('M_COM') || !defined('M_ADMIN')) && exit('No Permission');
aheader();
backallow('cnode') || amessage('no_apermission');
load_cache('cotypes');
load_cache('catalogs,mtpls,cnconfigs,',$sid);
include_once M_ROOT."./include/cnode.fun.php";
include_once M_ROOT."./include/cparse.fun.php";
include_once M_ROOT."./include/parse/general.php";
if($action == 'catalogadd'){
	if(empty($caid) || empty($catalogs[$caid])) amessage('请指定正确的栏目',axaction(6,"?entry=catalogs&action=catalogedit$param_suffix"));
	if(!submitcheck('bsynch') && !submitcheck('bcnconfigadd')){
		tabheader('设置 ['.$catalogs[$caid]['title'].'] 与品牌及价格的关联','synch',"?entry=$entry&action=$action&caid=$caid$param_suffix",'10');
		trcategory(array('选择',array(lang('config_name'),'txtL'),array(lang('catas_configs'),'txtL'),array(lang('mlclass'),'txtL'),lang('detail')));
		$cnidsarr = array();
		foreach($cnconfigs as $k => $v){
			$configstr = '';
			$i = 0;
			$idsarr = cfgs2ids($v['configs'],$sid);
			if(count($idsarr) < 2) continue;
			foreach($v['configs'] as $k1 => $v1){
				$configstr .= ($configstr ? ' x ' : '').(!$k1 ? lang('catalog') : @$cotypes[$k1]['cname']).'('.count($idsarr[$k1]).')';
				!$i && $type = lang(!$k1 ? 'catalog' : @$cotypes[$k1]['cname']);
				!$i && $mlclassstr = fetch_mlclass($k1,$v['configs'][$k1]);
				$i ++;
			}
			$cnidsarr[$k] = $v['cname'].'('.$type.')';
			echo "<tr class=\"txt\">".
				"<td class=\"txtC w30\"><input class=\"radio\" type=\"radio\" name=\"selectid\" value=\"$k\"></td>\n".
				"<td class=\"txtL\"><input type=\"text\" size=\"25\" maxlength=\"30\" name=\"cnconfigsnew[$k][cname]\" value=\"$v[cname]\"></td>\n".
				"<td class=\"txtL\">$configstr</td>\n".
				"<td class=\"txtL\">$mlclassstr</td>\n".
				"<td class=\"txtC w30\"><a href=\"?entry=cnodes&action=cnconfigdetail&cncid=$k$param_suffix\" onclick=\"return floatwin('open_cnodes',this)\">".lang('detail')."</a></td>\n".
				"</tr>\n";
		
		}
		tabfooter('bsynch','同步','如果本栏目与品牌及价格的关联与上面的任一配置相同，选择该配置，点同步提交。');
		
		tabheader('为 ['.$catalogs[$caid]['title'].'] 增加新的关联配置','cnconfigadd',"?entry=$entry&action=$action&caid=$caid$param_suffix");
		trbasic('关联配置描述','cname','','btext');
		$modearr = array(0 => lang('allcoclass'),1 => lang('all_topic_catas'),2 => lang('all_1_catas'),3 => lang('all_2_catas'),4 => lang('all_3_catas'),-1 => lang('handpoint'));
		$nomodearr = array(0 => lang('nosetting'),1 => lang('handpoint'));
		$i = 1;
		foreach($cotypes as $k => $v){
			if(!in_array($k,array(1,2))) continue;
			$arr = read_cache('coclasses',$k);
			foreach($arr as $x => $y) $arr[$x] = $y['title'].'('.$y['level'].')';
			sourcemodule("$i.".lang('incbelow').$v['cname'].
				"<br><input class=\"checkbox\" type=\"checkbox\" name=\"configsnew[$k][son]\" value=\"1\"".(empty($v['son']) ? "" : " checked").">".lang('include_son'),
				"configsnew[$k][mode]",
				$modearr,
				empty($v['mode']) ? 0 : $v['mode'],
				-1,
				"configsnew[$k][ids][]",
				$arr,
				array(),
				'25%',1,'',1
			);
			sourcemodule("$i.".lang('nobelow').$v['cname'].
				"<br><input class=\"checkbox\" type=\"checkbox\" name=\"configsnew[$k][noson]\" value=\"1\"".(empty($v['noson']) ? "" : " checked").">".lang('include_son'),
				"configsnew[$k][nomode]",
				$nomodearr,
				empty($v['noids']) ? 0 : 1,
				1,
				"configsnew[$k][noids][]",
				$arr,
				array(),
				'25%',1,'',1
			);
			$i ++;
		}
		tabfooter('bcnconfigadd',lang('add'));
	}elseif(submitcheck('bsynch')){
		if(empty($selectid) || empty($cnconfigs[$selectid])) amessage('请指定需要同步的配置！',axaction(6,M_REFERER));
		$cncid = $selectid;
		modify_cnconfig($cnconfigs[$cncid],0,array($caid),1);
		updatecache('cnconfigs','',$sid);
		
		$cnconfigs = reload_cache('cnconfigs','','',$sid);
		$db->query("UPDATE {$tblprefix}cnodes SET cncids = REPLACE(cncids,'|$cncid|','') WHERE sid='$sid' AND cncids LIKE '%|$cncid|%'");
		if($cnconfig = $cnconfigs[$cncid]){
			cnodesfromcnc($cnconfig,$sid);
			$db->query("UPDATE {$tblprefix}cnodes SET inconfig=0 WHERE sid='$sid' AND inconfig='1' AND cncids=''");
			$db->query("UPDATE {$tblprefix}cnodes SET inconfig=1 WHERE sid='$sid' AND inconfig='0' AND cncids !=''");
			updatecache('cnodes',0,$sid);
		}
		amessage('栏目的关联已经同步到指定的配置中',axaction(6,"?entry=catalogs&action=catalogedit$param_suffix"));
	}elseif(submitcheck('bcnconfigadd')){
		if(!($cname = trim($cname)) || !$configsnew) amessage('datamissing',M_REFERER);
		$configs = array();
		$configs[0] = array('mode' => '-1','ids' => array($caid),'son' => '0','noids' => array(),'noson' => '0',);
		$configs[2] = $configsnew[2];
		$configs[1] = $configsnew[1];
		foreach($configs as $k => $v){
			$configs[$k]['ids'] = empty($v['ids']) ? '' : implode(',',$v['ids']);
			$configs[$k]['noids'] = empty($v['noids']) ? '' : implode(',',$v['noids']);
			unset($configs[$k]['nomode']);
		}
		$mainline = 0;
		$cnlevel = count($configs);
		$configs = addslashes(serialize($configs));
		$db->query("INSERT INTO {$tblprefix}cnconfigs SET cname='$cname',configs='$configs',level='$cnlevel',mainline='$mainline',sid='$sid'");
		$cncid = $db->insert_id();
		adminlog(lang('add_catas_configs'));
		updatecache('cnconfigs','',$sid);
		
		if($cncid){
			$cnconfigs = reload_cache('cnconfigs','','',$sid);
			$db->query("UPDATE {$tblprefix}cnodes SET cncids = REPLACE(cncids,'|$cncid|','') WHERE sid='$sid' AND cncids LIKE '%|$cncid|%'");
			if($cnconfig = $cnconfigs[$cncid]){
				cnodesfromcnc($cnconfig,$sid);
				$db->query("UPDATE {$tblprefix}cnodes SET inconfig=0 WHERE sid='$sid' AND inconfig='1' AND cncids=''");
				$db->query("UPDATE {$tblprefix}cnodes SET inconfig=1 WHERE sid='$sid' AND inconfig='0' AND cncids !=''");
				updatecache('cnodes',0,$sid);
			}
		}
		amessage('新配置及相应节点添加成功！',axaction(6,"?entry=catalogs&action=catalogedit$param_suffix"));
	}
	
}elseif($action == 'coclassadd1'){
	if(empty($ccid) || !($coclass = read_cache('coclass',1,$ccid))) amessage('请指定正确的品牌',axaction(6,"?entry=coclass&action=coclassedit&coid=1"));
	if(!submitcheck('bsynch')){
		tabheader('将 ['.$coclass['title'].'] 加入到已有的关联配置中','synch',"?entry=$entry&action=$action&ccid=$ccid$param_suffix",'10');
		trcategory(array('选择',array(lang('config_name'),'txtL'),array(lang('catas_configs'),'txtL'),array(lang('mlclass'),'txtL'),lang('detail')));
		$cnidsarr = array();
		foreach($cnconfigs as $k => $v){
			$configstr = '';
			$i = 0;
			$idsarr = cfgs2ids($v['configs'],$sid);
			if(count($idsarr) < 2) continue;
			foreach($v['configs'] as $k1 => $v1){
				$configstr .= ($configstr ? ' x ' : '').(!$k1 ? lang('catalog') : @$cotypes[$k1]['cname']).'('.count($idsarr[$k1]).')';
				!$i && $type = lang(!$k1 ? 'catalog' : @$cotypes[$k1]['cname']);
				!$i && $mlclassstr = fetch_mlclass($k1,$v['configs'][$k1]);
				$i ++;
			}
			$cnidsarr[$k] = $v['cname'].'('.$type.')';
			echo "<tr class=\"txt\">".
				"<td class=\"txtC w30\"><input class=\"checkbox\" type=\"checkbox\" name=\"selectid[]\" value=\"$k\"></td>\n".
				"<td class=\"txtL\"><input type=\"text\" size=\"25\" maxlength=\"30\" name=\"cnconfigsnew[$k][cname]\" value=\"$v[cname]\"></td>\n".
				"<td class=\"txtL\">$configstr</td>\n".
				"<td class=\"txtL\">$mlclassstr</td>\n".
				"<td class=\"txtC w30\"><a href=\"?entry=cnodes&action=cnconfigdetail&cncid=$k$param_suffix\" onclick=\"return floatwin('open_cnodes',this)\">".lang('detail')."</a></td>\n".
				"</tr>\n";
		
		}
		tabfooter('bsynch','加入','<br>选择新增品牌需要加入的配置,会执行相应节点更新，有可能要执行数分钟。');
	}else{
		if(empty($selectid)) amessage('请选择需要加入的配置！',axaction(6,M_REFERER));
		foreach($selectid as $cncid){
			modify_cnconfig($cnconfigs[$cncid],1,array($ccid),1);
			updatecache('cnconfigs','',$sid);
		}
		$cnconfigs = reload_cache('cnconfigs','','',$sid);
		foreach($selectid as $cncid){
			$db->query("UPDATE {$tblprefix}cnodes SET cncids = REPLACE(cncids,'|$cncid|','') WHERE sid='$sid' AND cncids LIKE '%|$cncid|%'");
			if($cnconfig = $cnconfigs[$cncid]){
				cnodesfromcnc($cnconfig,$sid);
				$db->query("UPDATE {$tblprefix}cnodes SET inconfig=0 WHERE sid='$sid' AND inconfig='1' AND cncids=''");
				$db->query("UPDATE {$tblprefix}cnodes SET inconfig=1 WHERE sid='$sid' AND inconfig='0' AND cncids !=''");
			}
		}
		updatecache('cnodes',0,$sid);
		amessage('选中的配置已经更新',axaction(6,"?entry=coclass&action=coclassedit&coid=1"));
		
	}
}elseif($action == 'coclassadd2'){
	if(empty($ccid) || !($coclass = read_cache('coclass',2,$ccid))) amessage('请指定正确的价格',axaction(6,"?entry=coclass&action=coclassedit&coid=2"));
	if(!submitcheck('bsynch')){
		tabheader('将 ['.$coclass['title'].'] 加入到已有的关联配置中','synch',"?entry=$entry&action=$action&ccid=$ccid$param_suffix",'10');
		trcategory(array('选择',array(lang('config_name'),'txtL'),array(lang('catas_configs'),'txtL'),array(lang('mlclass'),'txtL'),lang('detail')));
		$cnidsarr = array();
		foreach($cnconfigs as $k => $v){
			$configstr = '';
			$i = 0;
			$idsarr = cfgs2ids($v['configs'],$sid);
			if(count($idsarr) < 2) continue;
			foreach($v['configs'] as $k1 => $v1){
				$configstr .= ($configstr ? ' x ' : '').(!$k1 ? lang('catalog') : @$cotypes[$k1]['cname']).'('.count($idsarr[$k1]).')';
				!$i && $type = lang(!$k1 ? 'catalog' : @$cotypes[$k1]['cname']);
				!$i && $mlclassstr = fetch_mlclass($k1,$v['configs'][$k1]);
				$i ++;
			}
			$cnidsarr[$k] = $v['cname'].'('.$type.')';
			echo "<tr class=\"txt\">".
				"<td class=\"txtC w30\"><input class=\"checkbox\" type=\"checkbox\" name=\"selectid[]\" value=\"$k\"></td>\n".
				"<td class=\"txtL\"><input type=\"text\" size=\"25\" maxlength=\"30\" name=\"cnconfigsnew[$k][cname]\" value=\"$v[cname]\"></td>\n".
				"<td class=\"txtL\">$configstr</td>\n".
				"<td class=\"txtL\">$mlclassstr</td>\n".
				"<td class=\"txtC w30\"><a href=\"?entry=cnodes&action=cnconfigdetail&cncid=$k$param_suffix\" onclick=\"return floatwin('open_cnodes',this)\">".lang('detail')."</a></td>\n".
				"</tr>\n";
		
		}
		tabfooter('bsynch','加入','<br>选择新增价格需要加入的配置,会执行相应节点更新，有可能要执行数分钟。');
	}else{
		if(empty($selectid)) amessage('请选择需要加入的配置！',axaction(6,M_REFERER));
		foreach($selectid as $cncid){
			modify_cnconfig($cnconfigs[$cncid],2,array($ccid),1);
			updatecache('cnconfigs','',$sid);
		}
		$cnconfigs = reload_cache('cnconfigs','','',$sid);
		foreach($selectid as $cncid){
			$db->query("UPDATE {$tblprefix}cnodes SET cncids = REPLACE(cncids,'|$cncid|','') WHERE sid='$sid' AND cncids LIKE '%|$cncid|%'");
			if($cnconfig = $cnconfigs[$cncid]){
				cnodesfromcnc($cnconfig,$sid);
				$db->query("UPDATE {$tblprefix}cnodes SET inconfig=0 WHERE sid='$sid' AND inconfig='1' AND cncids=''");
				$db->query("UPDATE {$tblprefix}cnodes SET inconfig=1 WHERE sid='$sid' AND inconfig='0' AND cncids !=''");
			}
		}
		updatecache('cnodes',0,$sid);
		amessage('选中的配置已经更新',axaction(6,"?entry=coclass&action=coclassedit&coid=2"));
		
	}
}
?>
