<?php
define('THIS_SCRIPT', 'indivgroup');
include_once 'global.php';
$lang = load_lang($lang, THIS_SCRIPT);
checklogin();
include_once 'inc/board_functions.php';
header("content-Type: text/html; charset=".$charset);
if ( strpos($sysinfo['flag'],'52,') === false ){
	head();
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
switch ($_GET['action']) {
	case 'search':
		head();
		readme($lang['page_title'],$lang['page_readme'],$lang['nav']);
		group_search();
		footer();
		break;
	case 'batchmodify':
		head();
		readme($lang['page_title'],$lang['page_readme'],$lang['nav']);
		del_date();
		group_list();
		footer();
		break;
	case 'add':
		new_group();
		break;
	case 'addedit':
		edit_group();
		break;
	case 'savenew':
		save_new();
		break;
	case "del":
		head();
		readme($lang['page_title'],$lang['page_readme'],$lang['nav']);
		clear_date();
		group_list();
		footer();
		break;
	case "deluser":
		head();
		readme($lang['page_title'],$lang['page_readme'],$lang['nav']);
		clear_user_date();
		user_list();
		footer();
		break;
	case 'batchusermodify':
		head();
		readme($lang['page_title'],$lang['page_readme'],$lang['nav']);
		del_user_date();
		footer();
		break;
	case 'orders':
		head();
		readme($lang['page_title2'],$lang['page_readme2'],$lang['nav']);
		user_list();
		footer();
		break;
	case 'user_search':
		head();
		readme($lang['page_title2'],$lang['page_readme2'],$lang['nav']);
		user_search();
		footer();
		break;
	default:
		head();
		readme($lang['page_title'],$lang['page_readme'],$lang['nav']);
		group_list();
		footer();
}
function save_new(){
	global $db,$lang,$dv,$restriction,$page;
	$GroupName= trim($_POST['GroupName']);
	$AppUserName= trim($_POST['AppUserName']);
	$GroupInfo=$_POST['GroupInfo'];
	$LimitUser=intval($_POST['LimitUser']);
	$GroupStats=intval($_POST['GroupStats']);
	$Locked=intval($_POST['Locked']);
	$ViewFlag=intval($_POST['ViewFlag']);

	$editSql= '';

	if(isset($_GET["groupid"]))		//判断是否为编辑状态
	{
		$editid= trim($_GET["groupid"]);

		if($editid == "")
		{
			head();
			show_msg($lang['page_title'],$lang['error_edit_groupid'],1,'','','indivgroup.php?action=add');
			footer();
		}
		else
			$editSql.= " and id<>'".$editid."'";
	}

	if($GroupName == '')
	{
		head();
		show_msg($lang['page_title'],$lang['error_name'],1,'','','indivgroup.php?action=add');
		footer();
	}
	else if($AppUserName == '')
	{
		head();
		show_msg($lang['page_title'],$lang['error_app'],1,'','','indivgroup.php?action=add');
		footer();
	}

	$query=$db->query("select id from {$dv}group_name where groupname='".$GroupName."'".$editSql);
	if ($ts = $db->fetch_array($query)) {
		head();
		show_msg($lang['page_title'],$lang['error_rename'],0,'','','indivgroup.php?action=add');
		footer();
	}

	$query=$db->query("select id from {$dv}group_name where appusername='".$AppUserName."'".$editSql);
	if ($ts = $db->fetch_array($query)) {
		head();
		show_msg($lang['page_title'],$lang['new_reapp'],0,'','','indivgroup.php?action=add');
		footer();
	}

	$query=$db->query("select userid,usergroupid,usergroup From {$dv}user Where username='".$AppUserName."'");
	if ($ts = $db->fetch_array($query)) {
		if(($ts["usergroupid"] == '5') || ($ts["usergroupid"] == '6')){
			head();
			show_msg($lang['page_title'],$lang['error_unchecked'],0,'','','indivgroup.php?action=add');
			footer();
		}
		else
			$AppUserID= $ts["userid"];
	}
	else{
		head();
		show_msg($lang['page_title'],$lang['error_unreg'],0,'','','indivgroup.php');
		footer();
	}
	
	if(isset($_GET["groupid"])){
		$db->query("update {$dv}group_name set groupname='".$GroupName."',groupinfo='".$GroupInfo."',appuserid='".$AppUserID."',appusername='".$AppUserName."',stats='".$GroupStats."',limituser='".$LimitUser."',locked='".$Locked."',viewflag='".$ViewFlag."' where id='".$editid."'");

		$db->query("update {$dv}group_user set islock=2 where userid=".$AppUserID." and groupid='".$editid."'");
		
		head();
		show_msg($lang['page_title'],$lang['edit_suc'].'<br>',1,'','','indivgroup.php?groupid='.$editid.'&action=addedit');
		footer();
	}
	else{
		$db->query("insert into {$dv}group_name(groupname,groupinfo,appuserid,appusername,usernum,stats,limituser,appdate,passdate,locked,viewflag) values ('".$GroupName."','".$GroupInfo."',".$AppUserID.",'".$AppUserName."',1,".$GroupStats.",".$LimitUser.",now(),now(),".$Locked.",".$ViewFlag.")");

		$groupid= $db->insert_id();

		if($groupid){
			$db->query("Insert Into {$dv}group_user(groupid,userid,username,islock) values(".$groupid.",".$AppUserID.",'".$AppUserName."',2)");
			head();
			show_msg($lang['page_title'],$lang['new_suc'].'<br>',1,'','','indivgroup.php?action=add');
			footer();
		}

		head();
		show_msg($lang['page_title'],$lang['new_fai'].'<br>',1,'','','indivgroup.php?action=add');
		footer();
	}

}
function user_list(){
	global $dv,$db,$lang,$info_list,$page,$total,$page_size,$offset,$page_count;
	$info_list=array();
	$keyword=$_POST['keyword'];
	if($keyword==''){
		$keyword=$_GET['keyword'];
	}
	$groupword=$_POST['groupword'];
	if($groupword==''){
		$groupword=$_GET['groupword'];
	}
	if(($keyword!='') || ($groupword!='')){
		user_search();
		exit;
	}
	$stmt="select a.id,a.groupid,a.userid,a.username,a.islock,a.intro,b.groupname from {$dv}group_user a,{$dv}group_name b where a.groupid=b.id order by a.id desc";
	$total=intval($db->scalar("select count(1) from {$dv}group_user"));
	page_info(20);
	if($result=$db->query($stmt,array('absolutePage'=>$offset,'pageSize'=>$page_size))){
			while ($arr=$db->fetch_array($result)){
			
				$info_list[]=$arr;
			}
	}
	$db->free_result($result);
	require_once printout('indivgroup_user');
}
function user_search(){
	global $dv,$db,$lang,$info_list,$page,$total,$page_size,$offset,$page_count;
	$info_list=array();

	$keyword=$_POST['keyword'];
	if($keyword==''){
		$keyword=$_GET['keyword'];
	}
	$groupword=$_POST['groupword'];
	if($groupword==''){
		$groupword=$_GET['groupword'];
	}

	if($groupword=='')
		$groupSql="";
	else
		$groupSql=" and b.groupname='".$groupword."'";

	$stmt="select a.id,a.groupid,a.userid,a.username,a.islock,a.intro,b.groupname from {$dv}group_user a,{$dv}group_name b where a.groupid=b.id and username like '%$keyword%'".$groupSql." order by a.id desc";

	$total=intval($db->scalar("select count(1) from {$dv}group_user a,{$dv}group_name b where a.groupid=b.id and username like '%$keyword%'".$groupSql));

	page_info(20);

	if(($orders=='3') || ($orders=='4')){
		if($result=$db->query($stmt)){
				while ($arr=$db->fetch_array($result)){
					$info_list[]=$arr;
				}
		}
	}
	else{
		if($result=$db->query($stmt,array('absolutePage'=>$offset,'pageSize'=>$page_size))){
			while ($arr=$db->fetch_array($result)){
			
				$info_list[]=$arr;
			}
		}
	}
	$db->free_result($result);
	require_once printout('indivgroup_user');
}

function group_list(){
	global $dv,$db,$lang,$info_list,$page,$total,$page_size,$offset,$page_count;
	$info_list=array();
	$keyword=$_POST['keyword'];
	if($keyword==''){
		$keyword=$_GET['keyword'];
	}
	$orders=$_POST['orders'];
	if($orders==''){
		$orders=$_GET['orders'];
	}
	if(($keyword!='') || ($orders!='' && $orders!='0')){
		group_search();
		exit;
	}
	$stmt='select id,groupname,groupinfo,appuserid,appusername,usernum,postnum,topicnum,appdate,stats,limituser,locked,viewflag from '.$dv.'group_name order by id desc';
	$total=intval($db->scalar("select count(1) from {$dv}group_name"));
	page_info(20);
	if($result=$db->query($stmt,array('absolutePage'=>$offset,'pageSize'=>$page_size))){
			while ($arr=$db->fetch_array($result)){
			
				$info_list[]=$arr;
			}
	}
	$db->free_result($result);
	require_once printout('indivgroup_list');
}

function group_search(){
	global $dv,$db,$lang,$info_list,$page,$total,$page_size,$offset,$page_count;
	$info_list=array();

	$keyword=$_POST['keyword'];
	if($keyword==''){
		$keyword=$_GET['keyword'];
	}
	$orders=$_POST['orders'];
	if($orders==''){
		$orders=$_GET['orders'];
	}
	if($orders=='' || $orders=='0')
		$ordersSql= '';
	else if($orders=='1')
		$ordersSql= ' and stats=0';
	else if($orders=='2')
		$ordersSql= '  order by id desc';
	else if($orders=='3')
		$ordersSql= ' order by postnum desc limit 20';
	else if($orders=='4')
		$ordersSql= ' order by usernum desc limit 20';

	$stmt="select id,groupname,groupinfo,appuserid,appusername,usernum,postnum,topicnum,appdate,stats,limituser,locked,viewflag from {$dv}group_name where groupname like '%$keyword%'".$ordersSql;

	$total=intval($db->scalar("select count(1) from {$dv}group_name where groupname like '%$keyword%'"));
	page_info(20);

	if(($orders=='3') || ($orders=='4')){
		if($result=$db->query($stmt)){
				while ($arr=$db->fetch_array($result)){
					$info_list[]=$arr;
				}
		}
	}
	else{
		if($result=$db->query($stmt,array('absolutePage'=>$offset,'pageSize'=>$page_size))){
			while ($arr=$db->fetch_array($result)){
			
				$info_list[]=$arr;
			}
		}
	}
	$db->free_result($result);
	require_once printout('indivgroup_list');
}
function new_group(){
	global $db,$lang,$dv,$restriction,$editid;
	head();
	readme($lang['page_title3'],$lang['page_readme'],$lang['nav']);
	$query1=$db->query("select id,type from {$dv}fsettings where s_class=1 order by c_orders");
	require_once printout('indivgroup');
	footer();
}
function edit_group(){
	global $db,$lang,$dv,$restriction,$editid;
	$groupid= $_GET["groupid"];
	head();
	readme($lang['page_title'],$lang['page_readme'],$lang['nav']);
	$query1=$db->query('select id,groupname,groupinfo,appuserid,appusername,usernum,postnum,topicnum,appdate,stats,limituser,locked,viewflag from '.$dv.'group_name where id='.$groupid);
	
	if($row=$db->fetch_array($query1))
	{
		$GroupName = $row["groupname"];
		$GroupInfo = $row["groupinfo"];
		$AppUserName = $row["appusername"];
		$GroupStats  =$row["stats"];
		$LimitUser = $row["limituser"];
		$Locked = $row["locked"];
		$ViewFlag = $row["viewflag"];
	}
	else
	{
		$GroupName = "";
		$GroupInfo = "";
		$AppUserName = "";
		$GroupStats  = 1;
		$LimitUser = "";
		$Locked = 0;
		$ViewFlag = 1;
	}

	require_once printout('indivgroup');
	group_list();
	footer();
}
function clear_date(){//清理数据;
	global $lang,$dv,$db,$id,$board_type,$str1,$str2,$str3,$str4;
	$id=$_GET['groupid'];
	if($id==0 || $id==''){
		head();
		show_msg($lang['page_title'],$lang['del_2'],0,'','','indivgroup.php');
		footer();
		exit;
	}
	else{
		$total= intval($db->scalar("Select count(id) From {$dv}group_board where rootid=".$id));
		if ($total) {
			head();
			show_msg($lang['page_title'],$lang['del_1'],0,'','','indivgroup.php');
			footer();
		}

		$db->query("Delete From {$dv}group_user Where groupid=".$id);
		$db->query("Delete From {$dv}group_name Where id=".$id);

		head();
		show_msg($lang['page_title'],$lang['del_suc'].'<br>',1,'','','indivgroup.php');
		footer();
	}
}
function clear_user_date(){//清理数据;
	global $lang,$dv,$db,$id,$board_type,$str1,$str2,$str3,$str4;
	$groupid=$_GET['groupid'];
	$groupuserid=$_GET['groupuserid'];
	if($groupid==0 || $groupid=='' || $groupuserid==0 || $groupuserid==''){
		head();
		show_msg($lang['page_title'],$lang['del_4'],0,'','','indivgroup.php');
		footer();
		exit;
	}
	else{
		$db->query("Delete From {$dv}group_user Where groupid=".$groupid." and id=".$groupuserid);

		head();	show_msg($lang['page_title'],$lang['del_suc'].'<br>',1,'','','indivgroup.php?action=orders&page='.$_GET['page'].'&keyword='.$_GET['keyword'].'&groupword='.$_GET['groupword']);
		footer();
	}
}
function del_user_date(){//清理数据;
	global $lang,$dv,$db,$id,$board_type,$str1,$str2,$str3,$str4;
	$chkID=$_POST['chkID'];
	if($chkID==''){
		$chkID=$_GET['chkID'];
	}
	$l = count($chkID);
	if($l)
	 {
	
		if($l > 0)
		{
			$f= 0;
			$t= "";	
			for($i=0;$i<$l;$i++)
			{
				$statsval=$_POST['islock_'.$chkID[$i]];
				if($statsval==''){
					$statsval=$_GET['islock_'.$chkID[$i]];
				}
				if($_POST['Submit'] == $lang['sub_info7'])
					$db->query("delete from {$dv}group_user Where id=".$chkID[$i]);
				else
					$db->query("update {$dv}group_user set islock='".$statsval."' Where id=".$chkID[$i]);
			}
		}

		head();
		show_msg($lang['page_title'],$lang['batchform_suc'],1,'','','indivgroup.php?action=orders&page='.$_GET['page'].'&keyword='.$_GET['keyword'].'&groupword='.$_GET['groupword']);
		footer();
	 }
	 else{
			head();
			show_msg($lang['page_title'],$lang['del_3'],0,'','','indivgroup.php?action=orders&page='.$_GET['page'].'&keyword='.$_GET['keyword'].'&groupword='.$_GET['groupword']);
			footer();
	 }
}

function del_date(){//清理数据;
	global $lang,$dv,$db,$id,$board_type,$str1,$str2,$str3,$str4;
	$chkID=$_POST['chkID'];
	if($chkID==''){
		$chkID=$_GET['chkID'];
	}
	$l = count($chkID);
	if($l)
	 {
	
		if($l > 0)
		{
			$f= 0;
			$t= "";	
			for($i=0;$i<$l;$i++)
			{
				$statsval=$_POST['GroupStats_'.$chkID[$i]];
				if($statsval==''){
					$statsval=$_GET['GroupStats_'.$chkID[$i]];
				}

				$limitval=$_POST['limituser_'.$chkID[$i]];
				if($limitval==''){
					$limitval=$_GET['limituser_'.$chkID[$i]];
				}

				$db->query("Update {$dv}group_name Set limituser='".$limitval."',stats='".$statsval."' Where id=".$chkID[$i]);
			}
		}

		head();
		show_msg($lang['page_title'],$lang['batchform_suc'],1,'','','indivgroup.php');
		footer();
	 }
	 else{
			head();
			show_msg($lang['page_title'],$lang['del_3'],0,'','','indivgroup.php');
			footer();
	 }
}
?>