<?php 
if(!defined('IN_GATEWEI')) {
	exit('access denied');
}
	if($ipost=="sendmsg" && $action=="broadcast"){
		$ismulticast=$isbroadcast=$sendok=0;
		if($recgid){
			$ismulticast=1;
		}else{
			$isbroadcast=1;
		}
		$msgtitle=trim($msgtitle);
		$g_editor_doc=trim($g_editor_doc);
		if(strlen($msgtitle)>100 || empty($msgtitle)){
			gadminmessage("标题字符数超出限制或为空");
		}

		if(strlen($g_editor_doc)>600  || empty($g_editor_doc)){
			gadminmessage("短消息内容字数超出限制或为空");
		}
		$sendok=$db->query("INSERT INTO g_msgs (msgid , title , content , ismulticast , isbroadcast , recgid , senderid , sender , sendergid , sendtime) VALUES (NULL , '$msgtitle', '$g_editor_doc', $ismulticast, $isbroadcast, $recgid, $uid,'$userid', $gid , $timestamp)");
		if($sendok){
		writelog($userid."群发消息成功");
		gadminmessage("短消息发送成功","siteadmin.php?iframe=sendmsg",1);	
		}else{
		writelog($userid."群发消息失败");
		gadminmessage("短消息未发送成功");
		}
	}


	if($ipost=="sendmsg" && $action=="unicast"){
		$sendok=$sendcount=$notsendcount=0;
		$q=$db->query("select uid,userid from g_members where uid in ($recmembers)");
		while($rec_userid=$db->fetch_array($q)){
			$useraccount[$rec_userid['uid']]=$rec_userid['userid'];
		}
		$msgtitle=trim($msgtitle);
		$g_editor_doc=trim($g_editor_doc);
		if(strlen($msgtitle)>100 || empty($msgtitle)){
			gadminmessage("标题字符数超出限制或为空");
		}

		if(strlen($g_editor_doc)>600  || empty($g_editor_doc)){
			gadminmessage("短消息内容字数超出限制或为空");
		}
		$recmember_array=explode(",",$recmembers);
		foreach($recmember_array as $val){
			$msgboxcount=$db->result($db->query("select count(*) from g_member_msg where uid=$val"),0);
			if($msgboxcount<20){
				$db->query("INSERT INTO g_member_msg (mmid , uid , userid , title , content , sendtime , readed , senderid , sender , sendergid) VALUES (NULL , $val , '$useraccount[$val]' , '$msgtitle' , '$g_editor_doc' , $timestamp , 0 , $uid , '$userid' , $gid)");
				$msgboxcount++;
				$db->query("UPDATE g_member_info SET msgboxcount = $msgboxcount  WHERE uid =$val LIMIT 1");
				$sendcount++;
			}else{
				$notsendcount++;
			}
		}
		if($notsendcount){
			gadminmessage("成功发送".$sendcount."条消息,".$notsendcount."条消息发送失败","siteadmin.php?iframe=sendmsg",1);
		}else{
			gadminmessage("成功发送".$sendcount."条消息","siteadmin.php?iframe=sendmsg",1);
		}
	}

    if($ipost=="delete"){
		if(is_array($msg_check)){
			$msg_check_str=implode(',',$msg_check);
			$db->query("delete from g_msgs where msgid in ($msg_check_str)");
			gadminmessage("短消息删除成功","siteadmin.php?iframe=sendmsg",1);
		}
	
	}


	if($ipost=="unicast"){
		$mid_str=implode(",",$mid);
		$q=$db->query("select userid from g_members where uid in ($mid_str)");
		while($userid=$db->fetch_array($q)){
			$useraccount[]=$userid['userid'];
		}
	$useraccount_str=implode(",",$useraccount);
	?>
<form method="post" action="siteadmin.php?iframe=sendmsg&ipost=sendmsg&action=unicast">
<table>
<caption>发送短消息</caption>
  <tr>
    <td><span>发送用户</span></td>
    <td ><?php echo $useraccount_str?>
	<input type="hidden" name="recmembers" value="<?php echo $mid_str?>">
	</td>
  </tr>
      <td width="20%"><span>短信息标题</span></td>
      <td width="80%"><input type="text" name="msgtitle" class="formtext" id="msgtitle" >
	  </td>
   </tr>
	<tr>
           <td width="20%" valign="top"><span>短信息内容</span></td>
	       <td width="80%" valign="top"><textarea style="width:500px;height:150px;" name="g_editor_doc"></textarea></td>
   </tr>

  		<tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>		 
	      </td>
   </tr>

   <tr>
    <td  colspan="2"> <div id="submitdiv"><input name="" type="submit" value="发&nbsp;&nbsp;送" ></div></td>
    </tr>
</table>
</form>

<?php }else{?>


<form method="post" action="siteadmin.php?iframe=sendmsg&ipost=sendmsg&action=broadcast">
<table>
<caption>发送短消息</caption>
  	<tr>
    <td><span>发送用户组</span></td>
    <td >
	  <select id="recgid" name="recgid" class="formselect">
		<option value="0" selected >所有会员</option >
		<?php foreach ($groupnames as $key => $value){
		echo "<option value=\"".$key."\" >".$value[0]."</option>";
		}
		?>
      </select>
	</td>
  </tr>
   <tr>
      <td width="20%"><span>短信息标题</span></td>
      <td width="80%"><input type="text" name="msgtitle" class="formtext" id="msgtitle" >
	  </td>
   </tr>
	<tr>
           <td width="20%" valign="top"><span>短信息内容</span></td>
	       <td width="80%" valign="top"><textarea style="width:500px;height:150px;" name="g_editor_doc"></textarea></td>
   </tr>

  		<tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>		 
	      </td>
   </tr>

   <tr>
    <td  colspan="2"> <div id="submitdiv"><input type="submit" value="发&nbsp;&nbsp;送" ></div></td>
    </tr>
</table>
</form>
<?php }?>


<?php 
$pagerecords=5;
$i=0;
$curpage=isset($page)?$page:1;
$startrecord=($curpage-1)*$pagerecords;
$totalrecord=$db->result($db->query("SELECT count(*) FROM g_msgs"),0);
$q=$db->query("select * from g_msgs order by sendtime desc limit $startrecord,$pagerecords");
$multipage=multi($totalrecord, $pagerecords , $curpage, "siteadmin.php?iframe=sendmsg");

?>
<table>
<caption>广播短消息</caption>
   <tr>
      <td width="5%">
	  <input type="checkbox" name="msg_check_all" id="msg_check_all" onclick="checkall_onclick('msg')">
	  </td>
      <th width="75%">短信标题</th>
	    <th width="20%">发送时间</th>
   </tr>
   <form method="post" id="msg_list_form">
   <?php while($msginfo=$db->fetch_array($q)){?>
	<tr>
           <td ><input type="checkbox" name="msg_check[]" id="msg_check_<?php echo $i?>" value="<?php echo $msginfo['msgid']?>"></td>
	       <td ><a href="javascript:showmsgbody(<?php echo $msginfo['msgid']?>)"><?php echo $msginfo['title']?></a></td>
		   <td ><?php echo gmdate('Y-m-j',$msginfo['sendtime']+3600*8)?></td>
   </tr>
   <tbody id="msgbody<?php echo $msginfo['msgid']?>" style="display:none">
   <tr>
   <td width="5%">&nbsp;</td>
           <td bgcolor="white"><span class="indent"><?php echo $msginfo['content']?></span></td>
		   <td>&nbsp;</td>
   </tr>
   </tbody>

<?php 
	$i++;
}
?>

  		<tr>
          <td colspan="3">
		  <?php echo $multipage?>&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>	
			 <input type="button" value="删除消息" onclick="msgdelete()">
			 <br />&nbsp;
	      </td>
   </tr>
</table>



<SCRIPT LANGUAGE="JavaScript">
<!--
	function showmsgbody(id){
		$('msgbody'+id).style.display=($('msgbody'+id).style.display=="none")?"":"none";
	}
	function msgdelete(){
			if(!is_checked('msg')){
					return alert("没有消息被选中");
			}
			var con=confirm("您确定删除被选中消息的所有信息吗");
			$("msg_list_form").action="siteadmin.php?iframe=sendmsg&ipost=delete";
			$("msg_list_form").submit();
	}
//-->
</SCRIPT>

