<?php
define('THIS_SCRIPT', 'admin_batch');
require_once 'global.php';
$lang = load_lang($lang, 'admin_batch' );

$stats = $lang['forum.info1'];
$gettitle = $stats;
head();
if ($userid == 0){
	showmsg($lang['err.info1']);
}
if (ChkPost() === false){
	showmsg($lang['err.info2']);
}
if ($announceid == ''){
	showmsg($lang['err.info3']);
}
if ($boardinfo['parentid'] == ''){
	$updateboardid = 0;
}else{
	if ($boardinfo['parentid'] == 0){
		$updateboardid = $boardid;
	}else{
		$updateboardid = $boardinfo['parentstr']. ',' .$boardid;
	}
}
$canbatchtopic = false;
if (($master == true || $boardmaster == true || $superboardmaster == true) && $groupsetting[45] == 1){
	$canbatchtopic = true;
}
if ($usergroupid > 3 && $groupsetting[45] == 1){
	$canbatchtopic = true;
}
if ($founduserper == true && $groupsetting[45] == 1){
	$canbatchtopic = true;
}elseif ($founduserper == true && $groupsetting[45] == 0){
	$canbatchtopic = false;
}
if ($canbatchtopic == false){
	showmsg($lang['err.info4']);
}
switch($action){
	case 'lock':
		foreach($announceid as $val){
			$db->query("update {$dv}topic Set locktopic=1 Where boardid={$boardid} And topicid={$val}");
			//删除缓存
			del_static_board($delboardid);
		}
		$db->query("insert into {$dv}log (l_boardid,l_touser,l_username,l_content,l_ip,l_type) Values ('$boardid','More','$membername','{$lang['forum.info2']}','$usertrueip',3)");
		showmsg($lang['suc.info1']);
		break;
	case 'dele':
		topic_batch_delete();
		lastpost();
		$db->query("insert into {$dv}log (l_boardid,l_touser,l_username,l_content,l_ip,l_type) Values ('$boardid','More','$membername','{$lang['forum.info5']}','$usertrueip',3)");
		showmsg($lang['suc.info3']);
		break;
	case 'move':
		topic_batch_move();
		$db->query("insert into {$dv}log (l_boardid,l_touser,l_username,l_content,l_ip,l_type) Values ('$boardid','More','$membername','{$lang['forum.info6']}','$usertrueip',3)");
		showmsg($lang['suc.info4']);
		break;
	case 'isbest':
		foreach($announceid as $val){
			$query=$db->query("Select posttable from {$dv}topic Where boardid={$boardid} And topicid={$val}");
			if ($ts =& $db->fetch_array($query)) {
				$TotalUseTable = $ts['posttable'];
			}
			if ($TotalUseTable != ''){
				$query=$db->query("Select announceid,boardid,topic,rootid,username,postuserid,dateandtime,expression,isbest From {$TotalUseTable} Where rootid={$val} Order By announceid Limit 1");
				if ($ps =& $db->fetch_array($query)){
					if (intval($ps['isbest']) == 0){
						$db->query("insert into {$dv}besttopic (title,boardid,announceid,rootid,postusername,postuserid,dateandtime,expression) Values ('$ps[topic]','$ps[boardid]','$ps[announceid]','$ps[rootid]','$ps[username]','$ps[postuserid]','$ps[dateandtime]','$ps[expression]')");
						$db->query("Update {$TotalUseTable} Set isbest='1' Where announceid={$ps[announceid]}");
						$db->query("update {$dv}topic Set isbest='1' Where topicid={$val}");

						//删除缓存
						del_static_board($ps['boardid'],$topicid);
					}
				}
			}
		}
		$db->query("insert into {$dv}log (l_boardid,l_touser,l_username,l_content,l_ip,l_type) Values ('$boardid','More','$membername','{$lang['forum.info4']}','$usertrueip',3)");
		showmsg($lang['suc.info5']);
		break;
	case 'topicmode':
		if ($mode == ''){
			showmsg($lang['err.info5']);
		}
		foreach($announceid as $val){
			$db->query("Update {$dv}topic Set Mode='{$mode}' Where boardid={$boardid} And topicid={$val}");
		}
		$db->query("insert into {$dv}log (l_boardid,l_touser,l_username,l_content,l_ip,l_type) Values ('$boardid','More','$membername','{$lang['forum.info3']}','$usertrueip',3)");

		//删除缓存
		del_static_board($boardid,$topicid);
		showmsg($lang['suc.info2']);
		break;
	case 'ask_batch':
		del_ask_batch();
		break;
	default:
		showmsg($lang['err.info3']);
		break;
}

footer();

function topic_batch_delete(){
	global $dv,$db,$lang,$action,$announceid,$boardid,$updateboardid,$board_user,$fsetting,$boardinfo;
	$childnum = 0;
	$ii = 0;
	$nowtime = TIME_NOW;//gmdate('Y-m-d H:i:s');
	$deltoday = 0;
	$AllTopNum = $fsetting['forum_alltopnum'];
	$Board_TopNum = $boardinfo['boardtopstr'];
	foreach($announceid as $val){
		if (!is_numeric($val) OR $val <= 0) {
			continue;
		}
		$query=$db->query("Select posttable,child,istop,dateandtime from {$dv}topic Where boardid={$boardid} And topicid={$val}");
		if ($ts =& $db->fetch_array($query)) {
			$childnum += $ts['child'];
			$istop = $ts['istop'];
			$query_1=$db->query("Select postuserid From {$ts[posttable]} Where boardid={$boardid} And rootid={$val}");
			while($ps =& $db->fetch_array($query_1)){
				$db->query("Update {$dv}user Set userwealth=userwealth-'{$board_user[3]}',usercp=usercp-'{$board_user[13]}',userep=userep-'{$board_user[8]}' Where userid='{$ps['postuserid']}'");
			}
			$db->query("Update {$ts[posttable]} Set locktopic=boardid,boardid=444,isbest='0' Where boardid={$boardid} And rootid={$val} and forumtype=0");
			$db->query("update {$dv}topic Set locktopic=boardid,boardid=444,isbest='0' Where boardid={$boardid} And topicid={$val}");
			$db->query("update {$dv}upfile Set f_flag=4 Where f_boardid={$boardid} And f_announceid LIKE '{$val}|%'");
			$db->query("delete from {$dv}besttopic where rootid={$val}");

			//删除缓存
			del_static_topic($boardid,$val);
			$ii++;
			if (dateDiff('D',$posts['dateandtime'],$nowtime) == 0){
				$todaynum = $ts['child'];
			}else{
				$todaynum = 0;
			}
			$deltoday += $todaynum;
		}
		if ($istop == 3){
			$Forum_AllTopNum = explode(',',$fsetting['forum_alltopnum']);
			$topnum = '';
			for ($i=0; $i<count($Forum_AllTopNum); $i++){
				if ($Forum_AllTopNum[$i] != $val){
					if ($topnum == ''){
						$topnum = $Forum_AllTopNum[$i];
					}else{
						$topnum .= ',' .$Forum_AllTopNum[$i];
					}
				}
			}
			$AllTopNum = $topnum;
		}elseif($istop == 2){
			$query=$db->query("Select boardid,boardtopstr From {$dv}board Where boardtopstr Like '%{$val}%'");
			while($ts =& $db->fetch_array($query)){
				$ts['boardtopstr'] = trim($ts['boardtopstr']);
				if (empty($ts['boardtopstr'])) {
					$iboardtopstr = '';
				}else{
					if (strpos(',' .$ts['boardtopstr']. ',', ','.$val.',') !== false){
						$iboardtopstr .= substr(str_replace(",{$val},", ',', ',' .$ts['boardtopstr']. ','), 0, -1);
						print '$iboardtopstr:'.$iboardtopstr.'<br />';
					}else{
						$iboardtopstr = $ts['boardtopstr'];
					}
				}
				$db->query("Update {$dv}board Set boardtopstr='{$iboardtopstr}' Where boardid={$ts[boardid]}");
				//删除缓存
				del_static_board($ts['boardid']);
				$boardtopstr = '';
				$iboardtopstr = '';
			}
		}elseif($istop == 1){
			if (false !== strpos(",{$boardinfo['boardtopstr']},", ",{$val},")) {
				$Board_TopNum .= substr(str_replace(",{$val},", ',', ",{$boardinfo['boardtopstr']},"), 0, -1);
				//if ($Board_TopNum != $boardinfo['boardtopstr']){
				$db->query("Update {$dv}board Set boardtopstr='{$Board_TopNum}' Where boardid={$boardid}");
				//}
			}
		}
	}
	//删除缓存
	del_static_board($boardid);
	Del_Math_Forum_Count($childnum,$ii);
	Del_Math_Forum_Today($boardid,$deltoday);
	LastCount($boardid);
	require_once INC_PATH.'BoardCache.class.php';//20070523
	Create_index_forumnum();
	if ($AllTopNum != $fsetting['forum_alltopnum']){
		$db->query("Update {$dv}setup Set forum_alltopnum='{$AllTopNum}'");
		$mycache =& new BoardCache($db);
		$mycache->createForumSetting();
	}
}
function topic_batch_move(){
	global $dv,$db,$lang,$action,$announceid,$boardid,$updateboardid,$board_user,$fsetting,$boardinfo;
	global $newboard;
	if ($newboard == '' || is_null($newboard) || !is_numeric($newboard)){
		showmsg($lang['err.info6']);
	}elseif($newboard == $boardid){
		showmsg($lang['err.info7']);
	}
	$childnum = 0;
	$ii = 0;
	$nowtime = TIME_NOW;//gmdate('Y-m-d H:i:s');
	$deltoday = 0;
	foreach($announceid as $val){
		$query=$db->query("Select posttable,isbest,child,istop from {$dv}topic Where boardid={$boardid} And topicid={$val}");
		if ($ts =& $db->fetch_array($query)) {
			$ii++;
			$childnum = $ts['child']+1;
			$db->query("UPDATE {$ts[posttable]} SET boardid={$newboard} WHERE boardid={$boardid} AND rootid={$val} and forumtype=0");
			$db->query("UPDATE {$dv}topic SET boardid = {$newboard},mode=0 WHERE boardid = {$boardid} AND topicid = {$val}");
			if (intval($ts['isbest']) == 1){
				$db->query("UPDATE {$dv}besttopic Set boardid={$newboard} WHERE boardid = {$boardid} AND rootid = {$val}");
			}
			if ($ts['istop'] > 0){
				$query_1=$db->query("SELECT boardtopstr From {$dv}board Where boardid = {$boardid}");
				$bs =& $db->fetch_array($query_1);
				$TopstrinfoO = $bs['boardtopstr'];
				$query_1=$db->query("SELECT boardtopstr From {$dv}board Where boardid = {$newboard}");
				$bs =& $db->fetch_array($query_1);
				$TopstrinfoN = $bs['boardtopstr'];
				$TopstrinfoO = str_replace($val.',','',$TopstrinfoO);
				$TopstrinfoO = str_replace(','.$val,'',$TopstrinfoO);
				$TopstrinfoO = str_replace($val,'',$TopstrinfoO);
				if ($TopstrinfoN == '' || is_null($TopstrinfoN)){
					$TopstrinfoN = $val;
				}elseif ($TopstrinfoN == $val || strpos($TopstrinfoN,','.$val) !== false){
					$TopstrinfoN = $TopstrinfoN;
				}else{
					$TopstrinfoN .= ',' .$val;
				}
				$db->query("UPDATE {$dv}board SET boardtopstr = '{$TopstrinfoO}' WHERE boardid = {$boardid}");
				$db->query("UPDATE {$dv}board SET boardtopstr = '{$TopstrinfoN}' WHERE boardid = {$newboard}");
			}
			$F_announceID = $val .'|';
			$db->query("UPDATE {$dv}upfile SET f_boardid = '{$newboard}' WHERE f_boardid = {$boardid} AND f_announceid LIKE '{$F_announceID}%'");
			if (dateDiff('D',$posts['dateandtime'],$nowtime) == 0){
				$todaynum = $ts['child'];
			}else{
				$todaynum = 0;
			}
			$deltoday += $todaynum;

			//删除缓存
			del_static_topic($boardid,$val);
		}
	}
	//删除缓存
	del_static_board($boardid);
	del_static_board($newboard);

	Del_Math_Forum_Count($childnum,$ii);
	Del_Math_Forum_Today($boardid,$deltoday);
	LastCount($boardid);
	$query=$db->query("Select parentstr from {$dv}board Where boardid={$newboard}");
	$bs =& $db->fetch_array($query);
	$updateboardid = $bs['parentstr']. ',' .$newboard;
	Add_Math_Forum_Count($childnum,$ii);
	Add_Math_Forum_Today($newboard,$deltoday);
	LastCount($newboard);
}

function Del_Math_Forum_Count($TopicNum,$BBSNUM){
	global $dv,$db,$updateboardid;
	$str=substr($updateboardid,0,1);
	if($str==','){
		$updateboardid=substr($updateboardid,1);
	}
	else{
		$updateboardid=$updateboardid;
	}
	$db->query("Update {$dv}setup Set forum_postnum=forum_postnum-{$TopicNum},forum_topicnum=forum_topicnum-{$BBSNUM}");
	$db->query("update {$dv}board Set postnum=postnum-{$TopicNum},topicnum=topicnum-{$BBSNUM} Where boardid In ({$updateboardid})");

	//删除缓存
	del_static_boards($updateboardid);
}
function Add_Math_Forum_Count($TopicNum,$BBSNUM){
	global $dv,$db,$updateboardid;
	$str=substr($updateboardid,0,1);
	if($str==','){
		$updateboardid=substr($updateboardid,1);
	}
	else{
		$updateboardid=$updateboardid;
	}
	$db->query("Update {$dv}setup Set forum_postnum=forum_postnum+{$TopicNum},forum_topicnum=forum_topicnum+{$BBSNUM}");
	$db->query("update {$dv}board Set postnum=postnum+{$TopicNum},topicnum=topicnum+{$BBSNUM} Where boardid In ({$updateboardid})");
	//删除缓存
	del_static_boards($updateboardid);
}
function Del_Math_Forum_Today($iboardid,$deltoday){
	global $dv,$db,$fsetting;
	$db->query("Update {$dv}board Set todaynum=(CASE WHEN todaynum>{$deltoday} THEN todaynum-{$deltoday} ELSE 0 END) Where boardid={$iboardid}");//edit 2007-04-20
	$db->query("Update {$dv}setup Set forum_todaynum=(CASE WHEN forum_todaynum>{$deltoday} THEN forum_todaynum-{$deltoday} ELSE 0 END)");

	//删除缓存
	del_static_board($iboardid);
}
function Add_Math_Forum_Today($iboardid,$deltoday){
	global $dv,$db,$fsetting;
	$db->query("Update {$dv}board Set todaynum=(CASE WHEN todaynum>{$deltoday} THEN todaynum-{$deltoday} ELSE 0 END) Where boardid={$iboardid}");//edit 2007-04-20
	$db->query("Update {$dv}setup Set forum_todaynum=(CASE WHEN forum_todaynum>{$deltoday} THEN forum_todaynum-{$deltoday} ELSE 0 END)");
	//删除缓存
	del_static_board($iboardid);
}
function LastCount($mboardid){
	global $dv,$db,$fsetting,$updateboardid;
	$query=$db->query("select T.title,b.announceid,b.dateandtime,b.username,b.postuserid,b.rootid from {$fsetting[nowtablename]} b inner join {$dv}topic T on b.rootid=T.topicid where b.boardid={$mboardid} order by b.announceid desc Limit 1");
	if ($ps =& $db->fetch_array($query)){
		$body = $ps['title'];
		$body = str_replace('$','',$body);
		$lastrootid = $ps['announceid'];
		$lastposttime = $ps['dateandtime'];
		$lastpostuser = str_replace('$','',$ps['username']);
		$lasttopic = substr($body,0,20);
		$lastpostuserid = $ps['postuserid'];
		$lastid = $ps['rootid'];
	}else{
		$lasttopic = 'none';
		$lastrootid = 0;
		$lastposttime = TIME_NOW;//gmdate('Y-m-d H:i:s');
		$lastpostuser = 'none';
		$lastpostuserid = 0;
		$lastid = 0;
	}
	$lastpost = $lastpostuser. '$' .$lastrootid. '$' .$lastposttime. '$' .$lasttopic. '$' .$lastpostinfo[4]. '$' .$lastpostuserid. '$' .$lastid. '$' .$mboardid;
	$splitboardid = explode(',',$updateboardid);
	for ($i=0; $i<count($splitboardid); $i++){
		$query=$db->query("select lastpost from {$dv}board where boardid={$splitboardid[$i]}");
		if ($bs =& $db->fetch_array($query)){
			$splitlastpost = explode('$',$bs['lastpost']);
			if (count($splitlastpost) == 8 && $lastrootid != $splitlastpost[1]){
				$db->query("update {$dv}board set lastpost='$lastpost' where boardid={$splitboardid[$i]}");
				//删除缓存
				del_static_board($iboardid);
			}
		}
	}
}

//删除帖子后更新“最后发帖” Add By xiaoD
function lastpost() {
	global $db, $dv, $boardid;

	$query = $db->query("SELECT username, announceid, dateandtime, parentid, topic, postuserid, rootid FROM {$dv}bbs1 WHERE boardid NOT IN (444,777) AND boardid='$boardid' ORDER BY dateandtime DESC");

	$rs = $db->fetch_array($query);

	$username = $rs['username'];
	$announceid = $rs['announceid'];
	$dateandtime = $rs['dateandtime'];
	$topic = $rs['topic'];
	$postuserid = $rs['postuserid'];
	$topicid = $rs['rootid'];

	$parentid = $rs['parentid'];

	if($parentid) {
		$topic = $db->scalar("SELECT topic FROM {$dv}bbs1 WHERE announceid='$parentid'");
	}

	$lastpost = $username.'$'.$announceid.'$'.$dateandtime.'$'.$topic.'$$'.$postuserid.'$'.$topicid.'$'.$boardid;

	$db->query("UPDATE {$dv}board SET lastpost='$lastpost' WHERE boardid='$boardid'");
	//删除缓存
	del_static_board($boardid);
}


function del_ask_batch(){
	global $lang,$db,$dv,$askid,$boardid,$announceid;
	foreach ($announceid as $val){
		$ask_posttable=$db->scalar("select posttable from {$dv}savvy_topic where topicid={$val}");
		if(!empty($ask_posttable)){
			$db->query("delete from {$ask_posttable} where rootid={$val} and forumtype=1");		
		}
		$db->query("delete from {$dv}savvy_topic where topicid={$val}");
	}
	showmsg($lang['suc.info3']);
}
?>