<?php
//////////////////////////CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC//////////////////////////////////////
(!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 == 'cnodescommon'){//ism表示交叉节点
	$ism = empty($ism) ? 0 : 1;
	$ismstr = !$ism ? '' : '&ism=1';
	$page = !empty($page) ? max(1, intval($page)) : 1;
	submitcheck('bfilter') && $page = 1;
	$viewdetail = empty($viewdetail) ? 0 : $viewdetail;
	$caid = !isset($caid)? '0' : $caid;
	$mainline = !isset($mainline)? '0' : $mainline;
	$cnlevel = !isset($cnlevel) ? '0' : $cnlevel;
	$inconfig = !isset($inconfig)? '-1' : $inconfig;
	$wheresql = "sid=$sid";
	$wheresql .= $ism ? ' AND cnlevel>1' : ' AND cnlevel=1';
	$mainline && $wheresql .= " AND mainline='$mainline'";
	$cnlevel && $wheresql .= " AND cnlevel='$cnlevel'";
	if(!empty($caid)){
		$caids = array($caid);
		$tempids = array();
		$tempids = son_ids($catalogs,$caid,$tempids);
		$caids = array_merge($caids,$tempids);
		$wheresql .= " AND caid ".multi_str($caids);
	}
	$inconfig != '-1' && $wheresql .= " AND inconfig='$inconfig'";
	$filterstr = '';
	foreach(array('viewdetail','caid','mainline','cnlevel','inconfig') as $k){
		$filterstr .= "&$k=".urlencode($$k);
	}

	foreach($cotypes as $coid => $cotype){
		if($cotype['sortable']){
			${"ccid$coid"} = isset(${"ccid$coid"}) ? ${"ccid$coid"} : 0;
			if(!empty(${"ccid$coid"})){
				$filterstr .= "&ccid$coid=".${"ccid$coid"};
				$wheresql .= " AND ename REGEXP 'ccid$coid=".${"ccid$coid"}."(&|$)'";
			}
		}
	}

	$wheresql = $wheresql ? ("WHERE ".$wheresql) : "";

	if(!submitcheck('bcnodescommon')){
		$pagetmp = $page;

		do{
			$query = $db->query("SELECT * FROM {$tblprefix}cnodes $wheresql ORDER BY cnid ASC LIMIT ".(($pagetmp - 1) * $atpp).",$atpp");
			$pagetmp--;
		} while(!$db->num_rows($query) && $pagetmp);
		$itemcnode = '';
		while($cnode = $db->fetch_array($query)) {
			$cnode['catalog'] = empty($cnode['caid']) ? '-' : $catalogs[$cnode['caid']]['title'];
			$cnode['inconfig'] = $cnode['inconfig'] ? '-' : lang('outconfig');
			view_cnurl($cnode['ename'],$cnode);
			$cnode['cname'] = cnode_cname($cnode['ename']);
			$itemcnode .= "<tr class=\"txt\"><td class=\"txtC\"><input class=\"checkbox\" type=\"checkbox\" name=\"selectid[$cnode[cnid]]\" value=\"$cnode[cnid]\">\n".
				"<td class=\"txtL\">$cnode[cname]</td>\n".
				"<td class=\"txtC\"><a href=\"$cnode[indexurl]\" target=\"_blank\">".lang('index')."</a>&nbsp; <a href=\"$cnode[listurl]\" target=\"_blank\">".lang('list_page0')."</a>&nbsp; <a href=\"$cnode[bkurl]\" target=\"_blank\">".lang('bklist')."</a></td>\n".
				"<td class=\"txtC\">".(!empty($mtpls[$cnode['indextpl']]) ? $mtpls[$cnode['indextpl']]['cname'] : $cnode['indextpl'])."</td>\n".
				"<td class=\"txtC\">".(!empty($mtpls[$cnode['listtpl']]) ? $mtpls[$cnode['listtpl']]['cname'] : $cnode['listtpl'])."</td>\n".
				"<td class=\"txtC\">".(!empty($mtpls[$cnode['bktpl']]) ? $mtpls[$cnode['bktpl']]['cname'] : $cnode['bktpl'])."</td>\n".
				"</tr>\n";
		}
		$cnodecount = $db->result_one("SELECT count(*) FROM {$tblprefix}cnodes $wheresql");
		$multi = multi($cnodecount, $atpp, $page, "?entry=cnodes&action=cnodescommon$ismstr$param_suffix$filterstr");

		tabheader(lang('catas_cnode_list'),'cnodescommon',"?entry=cnodes&action=cnodescommon$ismstr$param_suffix&page=$page",12);
		trcategory(array("<input class=\"checkbox\" type=\"checkbox\" name=\"chkall\" onclick=\"checkall(this.form, 'selectid', 'chkall')\">",lang('cnode_name'),lang('look'),lang('index_tpl'),lang('list_tpl'),lang('bklist_tpl')));
		echo $itemcnode;
		tabfooter();
		echo $multi;

		tabheader(lang('operate_item'));
		trbasic("<input class=\"checkbox\" type=\"checkbox\" name=\"cndeal[indextpl]\" value=\"1\">&nbsp;".lang('cnode_index_tpl'),'cnindextpl',makeoption(array('' => lang('noset')) + mtplsarr('cindex')),'select');
		trbasic("<input class=\"checkbox\" type=\"checkbox\" name=\"cndeal[listtpl]\" value=\"1\">&nbsp;".lang('cnode_list_tpl'),'cnlisttpl',makeoption(array('' => lang('noset')) + mtplsarr('list')),'select');
		trbasic("<input class=\"checkbox\" type=\"checkbox\" name=\"cndeal[bktpl]\" value=\"1\">&nbsp;".lang('cnode_bklist_tpl'),'cnbktpl',makeoption(array('' => lang('noset')) + mtplsarr('list')),'select');
		tabfooter('bcnodescommon');
		a_guide('cnodesedit');
	}
	else{
		if(empty($cndeal) && empty($dealstr)){
			amessage('selectoperateitem',"?entry=cnodes&action=cnodescommon$ismstr$param_suffix&page=$page$filterstr");
		}
		if(empty($selectid) && empty($select_all)){
			amessage('selectcnode',"?entry=cnodes&action=cnodescommon$ismstr$param_suffix&page=$page$filterstr");
		}
		if(!empty($select_all)){
			if(empty($dealstr)){//第一页通过表单传送
				$dealstr = implode(',',array_keys(array_filter($cndeal)));
			}else{//通过url传送的参数
				$cndeal = array();
				foreach(array_filter(explode(',',$dealstr)) as $k){
					$cndeal[$k] = 1;
				}
			}

			$parastr = "";
			foreach(array('cnindextpl','cnlisttpl','cnbktpl') as $k){
				$parastr .= "&$k=".$$k;
			}
			
			$selectid = $cnstrarr = array();
			$npage = empty($npage) ? 1 : $npage;
			if(empty($pages)){
				$archivecount = $db->result_one("SELECT count(*) FROM {$tblprefix}cnodes $wheresql");
				$pages = @ceil($archivecount / $atpp);
			}
			if($npage <= $pages){
				$fromstr = empty($fromid) ? "" : "cnid>$fromid";
				$nwheresql = !$wheresql ? ($fromstr ? "WHERE $fromstr" : "") : ($wheresql.($fromstr ? " AND " : "").$fromstr);
				$query = $db->query("SELECT cnid,ename FROM {$tblprefix}cnodes $nwheresql ORDER BY cnid ASC LIMIT 0,$atpp");
				while($item = $db->fetch_array($query)) $selectid[] = $item['cnid'];
			}
			if(empty($selectid)) amessage('selectcnode',"?entry=cnodes&action=cnodescommon$ismstr$param_suffix&page=$page$filterstr");

		}

		if(!empty($cndeal['delete'])){
			$query = $db->query("SELECT ename FROM {$tblprefix}cnodes WHERE cnid ".multi_str($selectid));
			while($item = $db->fetch_array($query)) $cnstrarr[] = $item['ename'];
			foreach($cnstrarr as $cnstr){
				$dirstr = cn_htmldir($cnstr,$sid);
				@unlink(M_ROOT.$dirstr.$homedefault);
				m_unlink(M_ROOT.$dirstr.'{$page}.html');
				m_unlink(M_ROOT.$dirstr.'bk_{$page}.html');
				@rmdir(M_ROOT.$dirstr);//内部不为空则不能删除
				del_cnode($cnstr,$sid);
			}
			$db->query("DELETE FROM {$tblprefix}cnodes WHERE cnid ".multi_str($selectid), 'UNBUFFERED');
		}else{
			if(!empty($cndeal['indextpl'])) $db->query("UPDATE {$tblprefix}cnodes SET indextpl='".$cnindextpl."' WHERE cnid ".multi_str($selectid));
			if(!empty($cndeal['listtpl'])) $db->query("UPDATE {$tblprefix}cnodes SET listtpl='".$cnlisttpl."' WHERE cnid ".multi_str($selectid));
			if(!empty($cndeal['bktpl'])) $db->query("UPDATE {$tblprefix}cnodes SET bktpl='".$cnbktpl."' WHERE cnid ".multi_str($selectid));
			foreach($selectid as $id) updatecache('cnode',$id,$sid);
		}
		updatecache('cnodes','',$sid);
		if(!empty($select_all)){
			$npage ++;
			if($npage <= $pages){
				$fromid = max($selectid);
				$transtr = '';
				$transtr .= "&select_all=1";
				$transtr .= "&pages=$pages";
				$transtr .= "&npage=$npage";
				$transtr .= "&bcnodescommon=1";
				$transtr .= "&fromid=$fromid";
				amessage('operating',"?entry=cnodes&action=cnodescommon$ismstr$param_suffix&page=$page$filterstr$transtr$parastr&dealstr=$dealstr",$pages,$npage,"<a href=\"?entry=cnodes&action=cnodescommon$ismstr$param_suffix&page=$page$filterstr\">",'</a>');
			}
		}
		adminlog(lang('cnode_admin_operate'),lang('cnode_list_admin'));
		amessage('cnodeoperatefinish',"?entry=cnodes&action=cnodescommon$ismstr&page=$page$param_suffix$filterstr");
	
	}
}elseif($action = 'cnodedetail' && $cnid){
	$forward = empty($forward) ? M_REFERER : $forward;
	$cnode = $db->fetch_one("SELECT * FROM {$tblprefix}cnodes WHERE cnid=$cnid");
	if(!submitcheck('bcnodedetail')){
		tabheader(lang('cnode_detail_set'),'cnodedetail',"?entry=cnodes&action=cnodedetail$param_suffix&cnid=$cnid&forward=".urlencode($forward));
		trbasic(lang('cnode_name'),'',cnode_cname($cnode['ename']),'');
		trbasic(lang('cnode_alias'),'cnodenew[alias]',$cnode['alias']);
		trbasic(lang('cnode_url'),'cnodenew[appurl]',$cnode['appurl'],'btext',lang('agappurl'));
		trbasic(lang('index_tpl'),'cnodenew[indextpl]',makeoption(array('' => lang('noset')) + mtplsarr('cindex'),$cnode['indextpl']),'select');
		trbasic(lang('list_tpl'),'cnodenew[listtpl]',makeoption(array('' => lang('noset')) + mtplsarr('list'),$cnode['listtpl']),'select');
		trbasic(lang('bklist_tpl'),'cnodenew[bktpl]',makeoption(array('' => lang('noset')) + mtplsarr('list'),$cnode['bktpl']),'select');
		tabfooter('bcnodedetail');
		a_guide('cnodedetail');
	}
	else{
		$cnodenew['alias'] = trim($cnodenew['alias']);
		$cnodenew['appurl'] = trim($cnodenew['appurl']);
		$db->query("UPDATE {$tblprefix}cnodes SET
			alias='$cnodenew[alias]',
			appurl='$cnodenew[appurl]',
			indextpl='$cnodenew[indextpl]', 
			listtpl='$cnodenew[listtpl]',
			bktpl='$cnodenew[bktpl]'
			WHERE cnid='$cnid'");
		adminlog(lang('detail_catas_cnode'));
		updatecache('cnodes','',$sid);
		amessage('cnodesetfinish',axaction(6,$forward));
	}
}

function save_cnode($cnstr,$cncid){
	global $sid,$timestamp,$db,$tblprefix;
	parse_str($cnstr,$idsarr);
	$cnode = array('caid' => 0,'cnlevel' => count($idsarr),'mainline' => '');
	$i = 0;
	foreach($idsarr as $k => $v){
		!$i && $cnode['mainline'] = $k == 'caid' ? 'ca' : str_replace('ccid','',$k);
		$k == 'caid' && $cnode['caid'] = $v;
		$i ++;
	}
	unset($idsarr);
	if($cnid = $db->result_one("SELECT cnid FROM {$tblprefix}cnodes WHERE sid=$sid AND ename='$cnstr'")){
		$db->query("UPDATE {$tblprefix}cnodes SET inconfig='1' WHERE cnid='$cnid' AND sid='$sid'");
		return;
	}
	$db->query("INSERT INTO {$tblprefix}cnodes SET 
		ename='$cnstr', 
		sid='$sid', 
		inconfig='1',
		cncids='|$cncid|',
		mainline='$cnode[mainline]',
		caid='$cnode[caid]',
		cnlevel='$cnode[cnlevel]',
		ineedstatic='$timestamp',
		lneedstatic='$timestamp',
		bkneedstatic='$timestamp'
		");
	if($cnid = $db->insert_id()){
		cn_blank($cnstr,'i,l,bk',$sid,1);//静态模式下生成静态链接
		//直接写入单个节点的缓存
		$cnode['sid'] = $sid;
		$cnode['alias'] = $cnode['appurl'] = $cnode['indexurl'] = $cnode['listurl'] = $cnode['bkurl'] = $cnode['indextpl'] = $cnode['listtpl'] = $cnode['bktpl'] = '';
		$cnode['ename'] = $cnstr;
		$cnode['ineedstatic'] = $cnode['lneedstatic'] = $cnode['bkneedstatic'] = $timestamp;
		cnode2file($cnode,$cnstr,$sid);//根据数组写入单个节点缓存
	}
	unset($cnode);
	return;
}


?>
