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

$stats = $lang['forum.info1'];
$gettitle = $stats;
head();
if (ChkPost() === false){
	showmsg($lang['err.info2']);
}
if ($announceid == ''){
	showmsg($lang['err.info3']);
}
$updateboardid = $groupboardid;

global $db,$dv,$userid,$membername;
head();
//检查发贴权限start
$groupid= intval($_GET["groupid"]);
$groupboardid= intval($_GET["groupboardid"]);
$groupmaster= false;							//是否群组管理员
if($userid <= 0)
	showmsg($lang['err.info1']);
if($fsetting['indivgroup_show'] == '0')
	showmsg($lang['err.info8']);
if (($groupid==0) || ($groupboardid == 0)){
	showmsg($lang['err.info3']);
}else{
	$groupinfo= array();
	if($query=$db->query("select id,groupname,groupinfo,appuserid,appusername,usernum,stats,postnum,topicnum,todaynum,yesterdaynum,limituser,appdate,passdate,visidate,locked,viewflag from {$dv}group_name where id={$groupid}")){
		if ($arr=$db->fetch_array($query)){
			$groupinfo=$arr;
		}
	}
	$db->free_result($query);

	if(count($groupinfo)==0)
		showmsg($lang['err.info9']);
	else{
		$groupmasterStr= $arr["appusername"];
		if(($groupmasterStr == $membername) && ($membername != ""))
			$groupmaster= true;

		$userresult=$db->query("select username from {$dv}group_user where islock=2 and groupid=".$groupid);	
		while ($userarr=$db->fetch_array($userresult)){
			if((trim($userarr["username"]) != "") && (trim($userarr["username"]) != $arr["appusername"])){
				$groupmasterStr.= "|".trim($userarr["username"]);
				if(($userarr["username"] == $membername) && ($membername != ""))
					$groupmaster= true;
			}
		}
		$db->free_result($userresult);
	}

	if(!$master)
		if($groupinfo["stats"] == '0')			//检查群组状态
			showmsg($lang['err.info10']);

	if(!($master || $groupmaster)){
		showmsg($lang['err.info14']);
	}
}
//检查发贴权限end

switch($action){
	case 'lock':
		foreach($announceid as $val){
			$db->query("update {$dv}group_topic Set locktopic=1 Where boardid={$groupboardid} And topicid={$val}");
		}
		$db->query("insert into {$dv}log (l_boardid,l_touser,l_username,l_content,l_ip,l_type) Values ('$groupboardid','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 ('$groupboardid','More','$membername','{$lang['forum.info5']}','$usertrueip',3)");
		showmsg($lang['suc.info3']);
		break;
	case 'isbest':
		foreach($announceid as $val){
			$db->query("Update {$dv}group_bbs1 Set isbest='1' Where rootid={$val} and parentid=0");
			$db->query("update {$dv}group_topic Set isbest='1' Where topicid={$val}");
		}
		$db->query("insert into {$dv}log (l_boardid,l_touser,l_username,l_content,l_ip,l_type) Values ('$groupboardid','More','$membername','{$lang['forum.info4']}','$usertrueip',3)");
		showmsg($lang['suc.info5']);
		break;
	default:
		showmsg($lang['err.info3']);
		break;
}

footer();

function topic_batch_delete(){
	global $dv,$db,$lang,$action,$announceid,$groupid,$groupboardid,$updateboardid,$board_user,$fsetting,$boardinfo;
	$TodayNum= 0;
	$PostNum= 0;
	$TopicNum= 0;
	$time1 = strtotime(date('Y-m-d 00:00:00'));
	$time2 = $time1 + (24 * 3600 - 1);

	foreach($announceid as $val){
		if (!is_numeric($val) OR $val <= 0) {
			continue;
		}
		$query= $db->query("Select dateandtime,parentid From {$dv}group_bbs1 Where groupid={$groupid} And boardid={$groupboardid} And rootid='{$val}'");;
		while($ts =& $db->fetch_array($query)) {
			$PostNum++;
			if(($ts['dateandtime']>=$time1) && ($ts['dateandtime']<=$time2))
				$TodayNum++;
			if($ts['parentid'] == '0')
				$TopicNum++;
		}
		$db->query("UPDATE {$dv}group_bbs1 SET boardid=(-1),locktopic='{$groupboardid}' WHERE rootid='{$val}'");
		$db->query("UPDATE {$dv}group_topic SET boardid=(-1),locktopic='{$groupboardid}',isbest='0' WHERE topicid='{$val}'");
	}
	$db->query("UPDATE {$dv}group_board SET postnum=(postnum-{$PostNum}),topicnum=(topicnum-{$TopicNum}),todaynum=(todaynum-{$TodayNum}) WHERE id={$groupboardid}");

	$db->query("UPDATE {$dv}group_name SET postnum=(postnum-{$PostNum}),topicnum=(topicnum-{$TopicNum}),todaynum=(todaynum-{$TodayNum}) WHERE id={$groupid}");

	require_once INC_PATH.'BoardCache.class.php';
	Create_index_forumnum_group();
}
//删除帖子后更新“最后发帖” Add By xiaoD
function lastpost() {
	global $db, $dv, $groupboardid;

	$query = $db->query("SELECT username, announceid, dateandtime, parentid, topic, postuserid, rootid FROM {$dv}group_bbs1 WHERE boardid<>-1 AND boardid='$groupboardid' 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}group_bbs1 WHERE announceid='$parentid'");
	}

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

	$db->query("UPDATE {$dv}group_board SET lastpost='$lastpost' WHERE id='$groupboardid'");
}

?>