<?php
(!defined('M_COM') || !defined('M_ADMIN')) && exit('No Permission');
aheader();
load_cache('cotypes,channels,grouptypes,permissions,vcps,rprojects,cafields,amconfigs');
include_once M_ROOT."./include/upload.cls.php";
include_once M_ROOT."./include/fields.fun.php";
include_once M_ROOT."./include/fields.cls.php";

if($action == 'catalogsedit'){
	if(!submitcheck('bcatalogedit')){
		tabheader('栏目管理','catalogsedit',"?entry=catalogs_cp&action=catalogsedit$param_suffix",'10');
		trcategory(array('序号',array('栏目名称','txtL'),array('静态目录','txtL'),'排序','小栏目','详情','删除'));
		$i = 0;
		foreach($catalogs as $k => $v){
			$v = read_cache('catalog',$k,'',$sid);
			$spacestr = $v['level'] ? '&nbsp; &nbsp; &nbsp; ' : '';
			if(empty($v['not_list'])){
				$i ++;
				echo "<tr class=\"txt\">".
					"<td class=\"txtC w40\">$i</td>\n".
					"<td class=\"txtL\">$spacestr<input type=\"text\" size=\"15\" maxlength=\"30\" name=\"catalogsnew[$k][title]\" value=\"$v[title]\"></td>\n".
					"<td class=\"txtL\">$v[dirname]</td>\n".
					"<td class=\"txtC w40\"><input type=\"text\" size=\"4\" maxlength=\"4\" name=\"catalogsnew[$k][vieworder]\" value=\"$v[vieworder]\"></td>\n".
					"<td class=\"txtC w60\">".(empty($v['level']) ? "<a href=\"?entry=catalogs_cp&action=catalogadd&pid=$k$param_suffix\" onclick=\"return floatwin('open_catalogsedit',this)\">添加</a>" : '-')."</td>\n".
					"<td class=\"txtC w40\"><a href=\"?entry=catalogs_cp&action=catalogdetail&caid=$k$param_suffix\" onclick=\"return floatwin('open_catalogsedit',this)\">编辑</a></td>\n".
					"<td class=\"txtC w40\">".(!$v['level'] ? '-' : "<a href=\"?entry=catalogs_cp&action=catalogdelete&caid=$k$param_suffix\">删除</a>")."</td>\n".
					"</tr>\n";
			}
		}
		tabfooter('bcatalogedit',lang('modify'));
	}else{
		if(isset($catalogsnew)){
			foreach($catalogsnew as $caid => $catalognew){
				$catalognew['title'] = trim(strip_tags($catalognew['title']));
				$catalognew['title'] = $catalognew['title'] ? $catalognew['title'] : $catalogs[$caid]['title'];
				$catalognew['vieworder'] = max(0,intval($catalognew['vieworder']));
				if(($catalognew['title'] != $catalogs[$caid]['title']) || ($catalognew['vieworder'] != $catalogs[$caid]['vieworder'])){
					$db->query("UPDATE {$tblprefix}catalogs SET 
								title='$catalognew[title]', 
								vieworder='$catalognew[vieworder]' 
								WHERE caid='$caid'
								");
				}
			}
		}
		updatecache('catalogs','',$sid);
		adminlog(lang('edit_catalog_mlist'));
		amessage('catalogeditfinish', "?entry=catalogs_cp&action=catalogsedit$param_suffix");
	}
}elseif($action == 'catalogadd'){
	if(!submitcheck('bcatalogadd')){
		$pid = empty($pid) ? 0 : $pid;
		if($pid) $pmsg = read_cache('catalog',$pid,'',$sid);
		if(empty($pmsg)) amessage('请指定大栏目');
		$submitstr = '';
		tabheader(lang('add_catalog').'-'.lang('base_setting'),'catalogadd',"?entry=catalogs_cp&action=catalogadd$param_suffix",2,1,1);
		trbasic(lang('parent_catalog'),'',$pmsg['title'],'');
		trhidden('catalognew[pid]',$pid);
		trbasic(lang('catalog_name'),'catalognew[title]','','text');
		trbasic(lang('html_dirname'),'catalognew[dirname]','','text');
		tabfooter('bcatalogadd');
		$submitstr .= makesubmitstr('catalognew[title]',1,0,0,30);
		$submitstr .= makesubmitstr('catalognew[dirname]',1,'tagtype',0,30);
		check_submit_func($submitstr);
		a_guide('catalogadd');

	} else {
		$catalognew['pid'] = empty($catalognew['pid']) ? 0 : $catalognew['pid'];
		if($catalognew['pid']) $pmsg = read_cache('catalog',$catalognew['pid'],'',$sid);
		if(empty($pmsg)) amessage('请指定大栏目',M_REFERER);

		$catalognew['title'] = trim(strip_tags($catalognew['title']));
		if(!$catalognew['title'] || !$catalognew['dirname']){
			amessage('catalogdatamiss',M_REFERER);
		}
		if(preg_match("/[^a-zA-Z_0-9]+/",$catalognew['dirname'])){
			amessage('catalogenameillegal',M_REFERER);
		}
		$catalognew['dirname'] = strtolower($catalognew['dirname']);
		$enamearr = array('archives','freeinfos',);
		foreach($catalogs as $caid => $catalog) $enamearr[] = $catalog['dirname'];
		foreach($cotypes as $coid => $cotype){
			$coclasses = read_cache('coclasses',$coid);
			foreach($coclasses as $ccid => $coclass){
				$enamearr[] = $coclass['dirname'];
			}
		}
		unset($coclasses);
		if(in_array($catalognew['dirname'], $enamearr)){
			amessage('catalogenamerepeat',M_REFERER);
		}
		$catalognew['level'] = $pmsg['level'] + 1;
		$catalognew['chids'] = $pmsg['chids'];
		$catalognew['customurl'] = $pmsg['customurl'];
		$catalognew['indextpl'] = $pmsg['indextpl'];
		$catalognew['listtpl'] = $pmsg['listtpl'];
		$catalognew['bktpl'] = $pmsg['bktpl'];
		
		$db->query("INSERT INTO {$tblprefix}catalogs SET 
					title='$catalognew[title]', 
					dirname='$catalognew[dirname]', 
					sid='$sid', 
					level='$catalognew[level]', 
					chids='$catalognew[chids]', 
					indextpl='$catalognew[indextpl]',
					listtpl='$catalognew[listtpl]',
					bktpl='$catalognew[bktpl]',
					customurl='$catalognew[customurl]',
					pid='$catalognew[pid]'
					");
		if($caid = $db->insert_id()){
			//自动继承父栏目的管理后台配置
			foreach($amconfigs as $k => $v){
				if(!empty($v['anodes'][$catalognew['pid']])){
					$v['anodes'][$caid] = $v['anodes'][$catalognew['pid']];
					$anodes = addslashes(serialize($v['anodes']));
					$db->query("UPDATE {$tblprefix}amconfigs SET anodes='$anodes' WHERE amcid='$k'");
				}
			}
			updatecache('amconfigs');
			//自动生成节点
			$db->query("INSERT INTO {$tblprefix}cnodes SET 
				ename='caid=$caid', 
				sid='$sid', 
				inconfig='1',
				cncids='',
				mainline='ca',
				caid='$caid',
				cnlevel='1',
				indextpl='$catalognew[indextpl]',
				listtpl='$catalognew[listtpl]',
				bktpl='$catalognew[bktpl]',
				ineedstatic='$timestamp',
				lneedstatic='$timestamp',
				bkneedstatic='$timestamp'
				");
			if($cnid = $db->insert_id()){
				updatecache('cnodes',1,$sid);
				updatecache('cnode',$cnid,$sid);
			}
		}
		
		adminlog(lang('add_catalog'));
		updatecache('catalogs','',$sid);
		amessage('添加栏目后，需要刷新页面，新加的栏目才会在内容管理中显示！', axaction(6,"?entry=catalogs_cp&action=catalogedit$param_suffix"));
	}
}elseif($action == 'catalogdelete' && $caid){
	if(!isset($confirm) || $confirm != 'ok') {
		$message = lang('del_alert')."<br><br>";
		$message .= lang('confirmclick').">><a href=?entry=catalogs_cp&action=catalogdelete&caid=".$caid."&confirm=ok$param_suffix>".lang('delete')."</a><br>";
		$message .= lang('giveupclick').">><a href=?entry=catalogs_cp&action=catalogsedit$param_suffix>".lang('goback')."</a>";
		amessage($message);
	}
	if($db->result_one("SELECT COUNT(*) FROM {$tblprefix}catalogs WHERE pid='$caid'")) {
		amessage('catalognosoncandel', "?entry=catalogs_cp&action=catalogsedit$param_suffix");
	}
	if($db->result_one("SELECT COUNT(*) FROM {$tblprefix}archives WHERE caid='$caid'")) {
		amessage('catalognoarccandel', "?entry=catalogs_cp&action=catalogsedit$param_suffix");
	}

	//删除相关的节点
	$query = $db->query("SELECT ename FROM {$tblprefix}cnodes WHERE caid='$caid' ORDER BY cnid");
	while($row = $db->fetch_array($query)) del_cnode($row['ename'],$sid);
	$db->query("DELETE FROM {$tblprefix}cnodes WHERE caid='$caid'");
	updatecache('cnodes',1,$sid);

	$db->query("DELETE FROM {$tblprefix}catalogs WHERE caid='$caid'");
	del_cache('catalog',$caid,'',$sid);
	adminlog(lang('del_catalog'));
	updatecache('catalogs','',$sid);
	amessage('catalogdelfinish', "?entry=catalogs_cp&action=catalogsedit$param_suffix");
}elseif($action =='catalogdetail' && $caid){
	$catalog = read_cache('catalog',$caid,'',$sid);
	if(!submitcheck('bcatalogdetail')){
		$submitstr = '';
		tabheader('栏目参数设置'."&nbsp;&nbsp;[$catalog[title]]",'catalogdetail',"?entry=catalogs_cp&action=catalogdetail&caid=$caid$param_suffix",2,1,1);
		trbasic(lang('html_dirname'),'',$catalog['dirname'],'');
		trbasic('栏目名称','catalognew[title]',$catalog['title']);
		$submitstr .= makesubmitstr('catalognew[title]',1,0,0,30);
		$a_field = new cls_field;
		foreach($cafields as $k => $field){
			if(in_array($k,array('list_keywords','list_abstract'))){
				$a_field->init();
				$a_field->field = $field;
				$a_field->oldvalue = isset($catalog[$field['ename']]) ? $catalog[$field['ename']] : '';
				$a_field->trfield('catalognew');
				$submitstr .= $a_field->submitstr;
			}
		}
		tabfooter('bcatalogdetail');
		check_submit_func($submitstr);
		a_guide('catalogdetail');
	}else{
		$catalognew['title'] = trim($catalognew['title']);

		$c_upload = new cls_upload;	
		$cafields = fields_order($cafields);
		$a_field = new cls_field;
		$sqlstr = "";
		foreach($cafields as $k => $field){
			if(in_array($k,array('list_keywords','list_abstract'))){
				$a_field->init();
				$a_field->field = $field;
				$a_field->oldvalue = isset($catalog[$field['ename']]) ? $catalog[$field['ename']] : '';
				$a_field->deal('catalognew');
				if(!empty($a_field->error)){
					$c_upload->rollback();
					amessage($a_field->error,"?entry=catalogs_cp&action=catalogdetail&caid=$caid$param_suffix");
				}
				$sqlstr .= ','.$field['ename']."='".$a_field->newvalue."'";
			}
		}
		$c_upload->closure(1, $caid, 'catalogs');
		$c_upload->saveuptotal(1);
		unset($a_field,$c_upload);
		$db->query("UPDATE {$tblprefix}catalogs SET
			title='$catalognew[title]'
			$sqlstr
			WHERE caid='$caid'");
		adminlog(lang('detail_modify_catalog'));
		updatecache('catalogs','',$sid);
		amessage('catalogsetfinish', axaction(6,"?entry=catalogs_cp&action=catalogedit$param_suffix"));
	}

}
?>