<?php
/**
 * @version $Id: cate.func.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

/**
 * 删除分类
 * 
 * @param Int $cid 分类ID
 */
function delCate($cid)
{
	global $DB, $db_prefix, $categories, $a_message, $Cache;
	$cid = (int)$cid;
	$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}categories WHERE cid = $cid");
	if (!$rs['num'])
	{
		//cate not exist
		return $a_message['cate_not_exist'];
	}
	$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}categories WHERE cup = $cid");
	if ($rs['num'])
	{
		//if exists sub cate
		return $a_message['sub_cate_exist'];
	}
	//del cate
	require_once (PBDIGG_ROOT.'include/attachment.inc.php');
	delAttachment("cid = $cid");
	if ($categories[$cid]['icon'])
	{
		PDel(PBDIGG_ROOT.'images/cate/'.$categories[$cid]['icon']);
	}
	$DB->db_query("DELETE FROM {$db_prefix}categories WHERE cid = $cid");
	$DB->db_query("DELETE FROM {$db_prefix}threads WHERE cid = $cid");
	$DB->db_query("DELETE FROM {$db_prefix}comments WHERE cid = $cid");
	$DB->db_query("UPDATE {$db_prefix}sitestat SET catenum = catenum - 1 WHERE id = 1");
	PDel(PBDIGG_ROOT.'data/cache/cache_singlecate_'.$cid.'.php');
	$Cache->categories();
}

/**
 * 合并分类
 * 
 * @param Int $scid 源分类ID
 * @param Int $dcid 目标分类ID
 */
function mergeCate($scid, $dcid)
{
	global $DB, $db_prefix, $Cache;
	$rs = $DB->db_fetch_one_array("SELECT icon, tnum, cnum FROM {$db_prefix}categories WHERE cid = $scid");
	$tnum = $rs['tnum'];
	$cnum = $rs['cnum'];
	if ($rs['icon'])
	{
		PDel(PBDIGG_ROOT.'images/cate/'.$rs['icon']);
	}
	$DB->db_query("UPDATE {$db_prefix}threads SET cid = $dcid WHERE cid = $scid");
	$DB->db_query("UPDATE {$db_prefix}comments SET cid = $dcid WHERE cid = $scid");
	$DB->db_query("UPDATE {$db_prefix}attachments SET cid = $dcid WHERE cid = $scid");
	$DB->db_query("UPDATE {$db_prefix}categories SET tnum = tnum + $tnum, cnum = cnum + $cnum WHERE cid = $dcid");
	$DB->db_query("DELETE FROM {$db_prefix}categories WHERE cid = $scid");
	PDel(PBDIGG_ROOT.'data/cache/cache_singlecate_'.$scid.'.php');
	$Cache->categories();
	$Cache->singlecate($dcid);
}
?>
