<?php
/**
 * @version $Id: check.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 Array $tid 文章ID数组
 */
function delArticle($tid)
{
	global $DB, $db_prefix, $pb_creditdb, $Cache, $categories;
	if (empty($tid) || !is_array($tid)) return FALSE;
	$commentuid = array();
	$stid = implode(',', $tid);
	$creditdb = explode("\t", $pb_creditdb);
	foreach ($tid as $value)
	{
		$rt = $DB->db_fetch_one_array("SELECT uid, cid FROM {$db_prefix}threads WHERE tid = $value");
		if (!$rt) continue;
		$uid = $rt['uid'];
		$cid = $rt['cid'];
		$DB->db_query("UPDATE {$db_prefix}memberdata SET postnum = postnum -1, currency = currency - ".$creditdb[2]." WHERE uid = $uid");
		$DB->db_query("SELECT uid FROM {$db_prefix}comments WHERE tid = $value");
		$cnum = $DB->db_num();
		while ($rt = $DB->db_fetch_array())
		{
			$commentuid[] = $rt['uid'];
		}
		if ($commentuid)
		{
			$commentcount = array_count_values($commentuid);
			foreach ($commentcount as $key => $value)
			{
				$DB->db_query("UPDATE {$db_prefix}memberdata SET commentnum = commentnum - $value, currency = currency - ".($value * $creditdb[3])." WHERE uid = $key");
			}
			unset($commentuid, $commentcount);
		}
		/**** update cate info ****/
		require_once(PBDIGG_ROOT.'data/cache/cache_singlecate_'.$cid.'.php');
//		$parCate = array();
//		parCate($categories, $parCate, $cid);
//		if ($parCate)
//		{
//			$cid .= ','.implode(',', $parCate);
//		}
//		if (substr($cid, -1) == ',')
//		{
//			$cid = substr($cid, 0, -1);
//		}
		$cid = $singlecate[$cid]['parcids'].$cid;
		$DB->db_query("UPDATE {$db_prefix}categories SET cnum = cnum - $cnum, tnum = tnum - 1 WHERE cid IN ($cid)");
		/**** update cate info ***/
	}
	$DB->db_query("DELETE FROM {$db_prefix}threads WHERE tid IN ($stid)");
	$tnum = $DB->db_affected();
	$DB->db_query("DELETE FROM {$db_prefix}tdata WHERE tid IN ($stid)");
	$DB->db_query("DELETE FROM {$db_prefix}comments WHERE tid IN ($stid)");
	$cnum = $DB->db_affected();
	$DB->db_query("DELETE FROM {$db_prefix}cdata WHERE tid IN ($stid)");
	$DB->db_query("SELECT tagid FROM {$db_prefix}tagcache WHERE tid IN ($stid)");
	if ($DB->db_num())
	{
		$tagid = '';
		while ($rs = $DB->db_fetch_array())
		{
			$tagid .= $rs['tagid'].',';
		}
		$DB->db_query("UPDATE {$db_prefix}tags SET usenum = usenum - 1 WHERE tagid IN (".substr($tagid, 0, -1).")");
		$DB->db_query("DELETE FROM {$db_prefix}tagcache WHERE tid IN ($stid)");
	}
	require_once (PBDIGG_ROOT.'include/attachment.inc.php');
	delAttachment("tid IN ($stid)");
	$DB->db_query("UPDATE {$db_prefix}sitestat SET artnum = artnum - $tnum, comnum = comnum - $cnum WHERE id = 1");
	$Cache->sitestat();
	$Cache->mktran();
}

/**
 * 删除评论
 * 
 * @param Array $rid 评论ID数组
 */
function delComment($rid)
{
	global $DB, $db_prefix, $_SITECONFIG, $Cache, $categories;
	if (empty($rid) || !is_array($rid)) return FALSE;
	$srid = implode(',', $rid);
	$creditdb = explode("\t", $_SITECONFIG['pb_creditdb']);
	foreach ($rid as $value)
	{
		$rt = $DB->db_fetch_one_array("SELECT uid, cid, tid FROM {$db_prefix}comments WHERE rid = $value");
		if (!$rt) continue;
		$uid = $rt['uid'];
		$cid = $rt['cid'];
		$tid = $rt['tid'];
		$DB->db_query("UPDATE {$db_prefix}memberdata SET commentnum = commentnum -1, currency = currency - ".$creditdb[3]." WHERE uid = $uid");
		$DB->db_query("UPDATE {$db_prefix}threads SET comments = comments -1 WHERE tid = $tid");
		/**** update cate info ****/
		require_once(PBDIGG_ROOT.'data/cache/cache_singlecate_'.$cid.'.php');
		$cid = $singlecate[$cid]['parcids'].$cid;
//		$parCate = array();
//		parCate($categories, $parCate, $cid);
//		if ($parCate)
//		{
//			$cid .= ','.implode(',', $parCate);
//		}
//		if (substr($cid, -1) == ',')
//		{
//			$cid = substr($cid, 0, -1);
//		}
		$DB->db_query("UPDATE {$db_prefix}categories SET cnum = cnum - 1 WHERE cid IN ($cid)");
		/**** update cate info ***/
	}
	$DB->db_query("DELETE FROM {$db_prefix}comments WHERE rid IN ($srid)");
	$cnum = $DB->db_affected();
	$DB->db_query("DELETE FROM {$db_prefix}cdata WHERE rid IN ($srid)");
	$DB->db_query("UPDATE {$db_prefix}sitestat SET comnum = comnum - $cnum WHERE id = 1");
	$Cache->sitestat();
	$Cache->mktran();
}
?>
