<?php
define('EmpireCMSAdmin','1');
require("../../class/connect.php");
require("../../class/db_sql.php");
require("../../class/functions.php");
require "../".LoadLang("pub/fun.php");
$link=db_connect();
$empire=new mysqlquery();
$editor=1;
//驗證用戶
$lur=is_login();
$logininid=$lur['userid'];
$loginin=$lur['username'];
$loginrnd=$lur['rnd'];
$loginlevel=$lur['groupid'];
$loginadminstyleid=$lur['adminstyleid'];
//ehash
$ecms_hashur=hReturnEcmsHashStrAll();
//驗證權限
CheckLevel($logininid,$loginin,$classid,"viewgroup");

//返回會員組
function ReturnAddViewMemberGroup($membergroup){
	$count=count($membergroup);
	if($count==0)
	{
		return '';
	}
	$mg='';
	for($i=0;$i<$count;$i++)
	{
		$mg.=intval($membergroup[$i]).',';
	}
	if($mg)
	{
		$mg=','.$mg;
	}
	return $mg;
}

//增加會員訪問組
function AddViewGroup($add,$userid,$username){
	global $empire,$dbtbpre;
	if(empty($add['gname']))
	{
		printerror('EmptyViewGroup','history.go(-1)');
	}
	$gname=hRepPostStr($add['gname'],1);
	$gids=ReturnAddViewMemberGroup($add['membergroup']);
	$ingids=ReturnAddViewMemberGroup($add['ingroup']);
	$agids=ReturnAddViewMemberGroup($add['madmingroup']);
	$sql=$empire->query("insert into {$dbtbpre}enewsvg(gname,gids,ingids,agids,mlist) values('$gname','$gids','$ingids','$agids',0);");
	if($sql)
	{
		$vgid=$empire->lastid();
		insert_dolog("vgid=$vgid&gname=$gname");//操作日誌
		printerror("AddViewGroupSuccess","AddViewGroup.php?enews=AddViewGroup".hReturnEcmsHashStrHref2(0));
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//修改會員訪問組
function EditViewGroup($add,$userid,$username){
	global $empire,$dbtbpre;
	$vgid=intval($add['vgid']);
	if(empty($add['gname'])||!$vgid)
	{
		printerror('EmptyViewGroup','history.go(-1)');
	}
	$gname=hRepPostStr($add['gname'],1);
	$gids=ReturnAddViewMemberGroup($add['membergroup']);
	$ingids=ReturnAddViewMemberGroup($add['ingroup']);
	$agids=ReturnAddViewMemberGroup($add['madmingroup']);
	//會員白名單
	$membernum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsvglist where vgid='$vgid'");
	$mlist=$membernum?1:0;
	$sql=$empire->query("update {$dbtbpre}enewsvg set gname='$gname',gids='$gids',ingids='$ingids',agids='$agids',mlist='$mlist' where vgid='$vgid'");
	if($sql)
	{
		insert_dolog("vgid=$vgid&gname=$gname");//操作日誌
		printerror("EditViewGroupSuccess","ListViewGroup.php".hReturnEcmsHashStrHref2(1));
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//刪除會員訪問組
function DelViewGroup($add,$userid,$username){
	global $empire,$dbtbpre;
	$vgid=intval($add['vgid']);
	if(!$vgid)
	{
		printerror('EmptyViewGroupid','history.go(-1)');
	}
	$r=$empire->fetch1("select vgid,gname from {$dbtbpre}enewsvg where vgid='$vgid'");
	if(!$r['vgid'])
	{
		printerror('EmptyViewGroupid','history.go(-1)');
	}
	$sql=$empire->query("delete from {$dbtbpre}enewsvg where vgid='$vgid'");
	$msql=$empire->query("delete from {$dbtbpre}enewsvglist where vgid='$vgid'");
	if($sql)
	{
		insert_dolog("vgid=$vgid&gname=$r[gname]");//操作日誌
		printerror("DelViewGroupSuccess","ListViewGroup.php".hReturnEcmsHashStrHref2(1));
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

$enews=$_POST['enews'];
if(empty($enews))
{$enews=$_GET['enews'];}
if($enews)
{
	hCheckEcmsRHash();
}
if($enews=="AddViewGroup")
{
	AddViewGroup($_POST,$logininid,$loginin);
}
elseif($enews=="EditViewGroup")
{
	EditViewGroup($_POST,$logininid,$loginin);
}
elseif($enews=="DelViewGroup")
{
	DelViewGroup($_GET,$logininid,$loginin);
}


$search=$ecms_hashur['ehref'];
$page=(int)$_GET['page'];
$page=RepPIntvar($page);
$start=0;
$line=16;//每頁顯示條數
$page_line=25;//每頁顯示鏈接數
$offset=$page*$line;//總偏移量
$query="select * from {$dbtbpre}enewsvg";
$totalquery="select count(*) as total from {$dbtbpre}enewsvg";
$num=$empire->gettotal($totalquery);//取得總條數
$query=$query." order by vgid desc limit $offset,$line";
$sql=$empire->query($query);
$returnpage=page2($num,$line,$page_line,$start,$page,$search);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<link href="../adminstyle/<?=$loginadminstyleid?>/adminstyle.css" rel="stylesheet" type="text/css">
<title>會員訪問組</title>
</head>

<body>
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1">
  <tr> 
    <td width="50%" height="25">位置：<a href="ListViewGroup.php<?=$ecms_hashur['whehref']?>">管理會員訪問組</a></td>
    <td><div align="right" class="emenubutton">
        <input type="button" name="Submit5" value="增加會員訪問組" onclick="self.location.href='AddViewGroup.php?enews=AddViewGroup<?=$ecms_hashur['ehref']?>';">
      </div></td>
  </tr>
</table>

<br>
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">
  <tr class="header"> 
    <td width="6%" height="25"> <div align="center">ID</div></td>
    <td width="39%" height="25"> <div align="center">組名稱</div></td>
    <td width="38%"><div align="center">允許會員白名單</div></td>
    <td width="17%" height="25"> <div align="center">操作</div></td>
  </tr>
  <?php
  while($r=$empire->fetch($sql))
  {
  	$color="#ffffff";
	$movejs=' onmouseout="this.style.backgroundColor=\'#ffffff\'" onmouseover="this.style.backgroundColor=\'#C3EFFF\'"';
	$membernum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsvglist where vgid='$r[vgid]'");
  ?>
  <tr bgcolor="<?=$color?>"<?=$movejs?>> 
    <td height="25"> <div align="center"> 
        <?=$r['vgid']?>
      </div></td>
    <td height="25"> <div align="center"> 
        <?=$r['gname']?>
      </div></td>
    <td><div align="center"><a href="ListVgMember.php?vgid=<?=$r['vgid']?><?=$ecms_hashur['ehref']?>" target="_blank">管理會員白名單(數量：<strong><?=$membernum?></strong>)</a></div></td>
    <td height="25"> <div align="center"> [<a href="AddViewGroup.php?enews=EditViewGroup&vgid=<?=$r['vgid']?><?=$ecms_hashur['ehref']?>">修改</a>]&nbsp;[<a href="ListViewGroup.php?enews=DelViewGroup&vgid=<?=$r['vgid']?><?=$ecms_hashur['href']?>" onclick="return confirm('確認要刪除？');">刪除</a>]</div></td>
  </tr>
  <?
  }
  ?>
  <tr bgcolor="#FFFFFF"> 
    <td height="25" colspan="4">&nbsp;&nbsp;&nbsp; 
      <?=$returnpage?>    </td>
  </tr>
</table>
</body>
</html>
<?
db_close();
$empire=null;
?>
