<?php
if (!defined('MOD')) die("access die");
$cms = qbase :: get_object('qmecmsAcategory', $channelid);
if ($_POST['title']) {
	$cid = intval($_POST['cid']);
	$param['cupid'] = intval($_POST['cupid']);
	$param['title'] = trim($_POST['title']);
	$param['description'] = qbase :: substrs(qbase :: trimn($_POST['description']), 250);
	$param['keyword'] = qbase :: substrs(qbase :: trimn($_POST['keyword']), 250);
	$param['dir'] = qbase :: trimn($_POST['dir']) ? createCategoryDir(qbase :: trimn($_POST['dir'])) : createCategoryDir($param['title']);
	$param['displayorder'] = intval($_POST['displayorder']);
	$param['showphp'] = trim($_POST['showphp']);
	$param['pic'] = trim($_POST['pic']);
	$param['showmessage'] = trim($_POST['showmessage']);
	$param['pur_browse'] = intval($_POST['pur_browse']);
	$param['pur_usergroup'] = empty($_POST['pur_usergroup']) ? '' : implode(',', $_POST['pur_usergroup']);
	if ($param['pur_browse'] == 2 && empty($param['pur_usergroup'])) qbase :: showmsg("如果选择了会员栏目，必须选择相应会员组");
	else if ($param['pur_browse'] != 2) $param['pur_usergroup'] = '';
	$param['num'] = intval($_POST['num']);
	$param['c_allow'] = intval($_POST['c_allow']);
	$param['c_check'] = intval($_POST['c_check']);
	$param['c_post'] = intval($_POST['c_post']);
	$param['html'] = intval($_POST['html']);
	$param['category_html'] = intval($_POST['category_html']);
	$param['cfilename'] = formatFileName($_POST['cfilename']);

	if ($param['title'] == "") qbase :: showmsg($channeltitle . "分类名不能为空");
	if ($param['dir'] == "") qbase :: showmsg($channeltitle . "分类目录没有添加");

	if ($cid < 1) qbase :: showmsg($channeltitle . "分类不存在", 'default', "system.php?sys=acategory&channelid=" . $channelid . "&action=modify");
	if ($param['cupid'] == $cid) qbase :: showmsg("怎么能这样呢", 'default', "system.php?sys=acategory&channelid=" . $channelid . "&action=edit&cid=" . $cid);

	$pid = $param['cupid'];
	while ($pid > 0) {
		$pid = qdb :: queryAlone("select cupid from " . TABLEPRE . "acategory where cid = {$pid} ");
		$pid = intval($pid);
		if ($pid == $cid) {
			qbase :: showmsg("怎么能这样呢", 'default', "system.php?sys=acategory&channelid=" . $channelid . "&action=edit&cid=" . $cid);
		} 
	} 
	/**
	* 修改基本信息至数据库*****
	*/
	$sql = qbase :: createSql(TABLEPRE . 'acategory', $param , 'update') . " WHERE `cid` = '{$cid}' and `channelid`='{$channelid}'" ;
	qdb :: Query($sql);
	$cms->extend_update($cid);
	qbase :: get_object('category', $channelid) -> update();
	commonCache();
	cache :: delete('cid_' . $cid);
	adminLog("修改栏目分类{$param['title']}");
	qbase :: showmsg($channeltitle . "分类修改完成", 'default', "system.php?sys=acategory&channelid=" . $channelid . "&action=modify");
} else {
	$pcid = intval($_GET['cid']);
	$ppcid = qdb :: QueryAlone("SELECT `cupid` FROM `" . TABLEPRE . "acategory` where `channelid`=" . $channelid . "  and cid='{$pcid}' ");
	$strall = str_replace("<option value=\"{$ppcid}-", "<option selected value=\"{$ppcid}-", $menuTree);
	$category = qdb :: QueryFirst("SELECT * FROM `" . TABLEPRE . "acategory` WHERE `channelid`=" . $channelid . "  and  `cid` = '{$pcid}' LIMIT 1");
	$category['description'] = stripslashes($category['description']);
	$category['keyword'] = stripslashes($category['keyword']);

	if ($category['is_show']) {
		$is_show[1] = ' checked';
		$is_show[0] = '';
	} else {
		$is_show[1] = '';
		$is_show[0] = ' checked';
	} 

	if ($category['allowpost']) {
		$allowpost[1] = ' checked';
		$allowpost[0] = '';
	} else {
		$allowpost[1] = '';
		$allowpost[0] = ' checked';
	} 

	if($category['extdata']){
		$temp = @unserialize($category['extdata']);
		$category['extdata'] = array();
		if(is_array($temp)){
			foreach($temp as $key=>$val){
				$category['extdata'][] = array(
					'key'=>substr($key, 5),
					'value'=>$val
				);
			}
		}
	}else{
		$category['extdata'] = array();
	}
} 

?>
