<?php
//iflook 0=new,1=look,2=del,3=replay
//contype 1=pro,2=info,3=compamy
//is_save 是否保存
//msgtype 1是发出去的给别人的，２．是记录自己发送的放入收件箱，３．是系统管理员发过来的
	
class msg
{
	var $db;
	var $tpl;
	var $page;
	function msg()
	{
		global $db;
		global $config;
		global $tpl;		
		$this -> db     = & $db;
		$this -> tpl    = & $tpl;
	}
	
	//取得用户的邮件总数,返回二维数组，$re[0][0]为未读邮件，$re[1][0]为已经读邮件
	function getMailNum()
	{
		global $db,$buid;
		$sql="SELECT count(*) as num,iflook  FROM ".FEEDBACK." where touserid='$buid' and (msgtype='1' or msgtype='3') group by iflook order by iflook desc";
		$db->query($sql);
		$re=$db->getRows();
		foreach($re as $v)
		{
			if(!$v['iflook'])
				$type='new';
			if($v['iflook']=='1')
				$type='read';
			if($v['iflook']=='2')
				$type='del';
			if($v['iflook']=='3')
				$type='del';
			$ms[$type]=$v['num'];
		}
		if(empty($ms['new']))
			$ms['new']=0;
		if(empty($ms['read']))
			$ms['read']=0;
		return $ms;
	}

	function mail_list($type='inbox')
	{
		global $buid;;
		if($type=='inbox')
			$sql="SELECT * FROM ".FEEDBACK." WHERE touserid='$buid' and (msgtype=1 or msgtype=3) and (iflook is null or iflook='1' or iflook='3') ORDER BY date DESC";
		if($type=='outbox')
			$sql="SELECT * FROM ".FEEDBACK." WHERE fromuserid='$buid' and msgtype=2 and (iflook is null or iflook='1') ORDER BY date DESC";
		if($type=='savebox')
			$sql="SELECT * FROM ".FEEDBACK." WHERE touserid='$buid' and is_save='1' ORDER BY date DESC";
		if($type=='delbox')
			$sql="SELECT * FROM ".FEEDBACK." WHERE (touserid='$buid' or fromuserid='$buid') and iflook='2' ORDER BY date DESC";
		//=====================
	  	$page = new Page;
		$page->listRows=20;
		if (!$page->__get('totalRows')){
			$this->db->query($sql);
			$page->totalRows = $this->db->num_rows();
		}
        $sql .= "  limit ".$page->firstRow.",20";
		//=====================
		$this->db->query($sql);
		$list=$this->db->getRows();
		foreach($list as $key=>$v)
		{    
		   if($v["fromuserid"]==$buid&&!empty($v['touserid']))
		   {	
			   $sql="select name,user from ".ALLUSER." where userid=".$v['touserid'];
			   $this->db->query($sql);
			   $fc=$this->db->fetchRow();
			   $v["fromname"]=$fc['user']."[$fc[name]]";
		   }
		   if($v["fromuserid"]!=$buid&&!empty($v['fromuserid']))
		   {
			   $sql="select name,user from ".ALLUSER." where userid=".$v['fromuserid'];
			   $this->db->query($sql);
			   $fc=$this->db->fetchRow();
			   $v["fromname"]=$fc['user']."[$fc[name]]";
		   }
		   $list[$key]=$v;
		}
		$re["page"]=$page->prompt();
		$re["list"]=$list;
		return $re;
	}
	//撤低删除邮件
	function remove_mail($id=NULL)
	{	
		global $buid,$admin;
		if(isset($_POST["deid"])&&!empty($_POST['remove']))
		{
			for($i=0;$i<count($_POST["deid"]);$i++)
			{
				$id=$_POST["deid"][$i];
				$sql="delete from ".FEEDBACK." where id='$id' and (touserid='$buid' or fromuserid='$buid')";
				$this->db->query($sql);
			}
		}
		if(!empty($id))
		{
			$sql="delete from ".FEEDBACK." where id='$id' and (touserid='$buid' or fromuserid='$buid')";
			$this->db->query($sql);
			$admin->msg("main.php?m=message&s=admin_message_list_delbox");
		}
	}
	//恢复邮件
	function recover_mail($id=NULL)
	{
		global $admin;
		
		if(!empty($id))
		{
			$sql="update  ".FEEDBACK." set iflook=1 where id=$id";
			$this->db->query($sql);
			$admin->msg("main.php?m=message&s=admin_message_list_inbox");
		}
		else
		{	
			for($i=0;$i<count($_POST["deid"]);$i++)
			{
				$id=$_POST["deid"][$i];
				$sql="update  ".FEEDBACK." set iflook=1 where id=$id";
				$this->db->query($sql);
				unset($sql);
			}
		}

	}
	//删除邮件功能
	function del_mail($id=NULL)
	{
		if(empty($id))
		{
			for($i=0;$i<count($_POST["deid"]);$i++)
			{
				$id=$_POST["deid"][$i];
				$sql="update  ".FEEDBACK." set iflook=2 where id=$id";
				$this->db->query($sql);
				unset($sql);
			}
		}
		else
		{
			$sql="update  ".FEEDBACK." set iflook=2 where id=$id";
			$this->db->query($sql);
		}
	}
	
	function del_freom_save_mail()
	{
		if(empty($id))
		{
			for($i=0;$i<count($_POST["deid"]);$i++)
			{
				$id=$_POST["deid"][$i];
				$sql="update  ".FEEDBACK." set is_save=0 where id=$id";
				$this->db->query($sql);
				unset($sql);
			}
		}
		else
		{
			$sql="update  ".FEEDBACK." set is_save=0 where id=$id";
			$this->db->query($sql);
		}
	}
	//=======================================保存邮件功能
	function save_mail($save_id=NULL)
	{
		global $admin;
		
		if(!empty($save_id))
		{
			$sql="update  ".FEEDBACK." set is_save=1 where id='$save_id'";
			$this->db->query($sql);
			$admin->msg('main.php?m=message&s=admin_message_list_savebox');
		}
		else
		{
			for($i=0;$i<count($_POST["deid"]);$i++)
			{
				$id=$_POST["deid"][$i];
				$sql="update  ".FEEDBACK." set is_save=1 where id='$id'";
				$this->db->query($sql);
				unset($sql);
			}
			$admin->msg('main.php?m=message&s=admin_message_list_savebox');
		}

	}
	//========================================邮件详情
	function mail_det($id)
	{
		global $buid;
		$sql="select *,NULL as about from ".FEEDBACK." where id='$id'";
		$this->db->query($sql);
		$re=$this->db->fetchRow();
		if($re['iflook']<1)
		{
			$sql="update ".FEEDBACK." SET iflook=1  WHERE id='$id'";
			$this->db->query($sql);
		}
		
		if($re["fromuserid"]&&$re['msgtype']==1)
		{//收件箱
			$sql="select * from ".ALLUSER." where userid='".$re['fromuserid']."'";
			$this->db->query($sql);
			$re["fromInfo"]=$this->db->fetchRow();
		}
		if($re["touserid"]&&$re['msgtype']==2)
		{//发件箱
			$sql="select * from ".ALLUSER." where userid='".$re['touserid']."'";
			$this->db->query($sql);
			$re["fromInfo"]=$this->db->fetchRow();
		}
				
		$re['edit_con']='<br><br><br><br><br>//======================================================='.$re['con'];
		
		return $re;
	}
	
	//商铺中留言邮件发送功能
	function send_email()
	{
		if(!empty($_POST["submit"]))
		{	
			//===========================
			global $config,$buid;$fromInfo=NULL;$date=date("Y-m-d H:i");
			$touserid=is_array($_POST['toid'])?$_POST['toid']:array('0'=>$_POST['toid']);
			$rt=$rb=$attachments=NULL;
			if(empty($buid))
			{
				$fromInfo='';
				if(!empty($_POST['name']))
					$fromInfo.="Name:$_POST[name]<br>";
				if(!empty($_POST['company']))
					$fromInfo.="Company:$_POST[company]<br>";
				if(!empty($_POST['country']))
					$fromInfo.="Country:$_POST[country]<br>";
				if(!empty($_POST['province']))
					$fromInfo.="Province:$_POST[province]<br>";
				if(!empty($_POST['city']))
					$fromInfo.="City:$_POST[city]<br>";
				if(!empty($_POST['addr']))
					$fromInfo.="Address:$_POST[addr]<br>";
				if(!empty($_POST['post']))
					$fromInfo.="Post:$_POST[post]<br>";
				if(!empty($_POST['fax']))
					$fromInfo.="FAX:$_POST[fax]<br>";
				if(!empty($_POST['tell']))
					$fromInfo.="Tel:$_POST[tell]<br>";
				if(!empty($_POST['mobile']))
					$fromInfo.="Mobile:$_POST[mobile]<br>";
				if(!empty($_POST['email']))
					$fromInfo.="Email:$_POST[email]<br>";
				if(!empty($_POST['url']))
					$fromInfo.="Website:$_POST[url]<br>";
				$buid=0;
			}
			if(!empty($_POST['receive_type']))
				$rt=implode("<br>",$_POST['receive_type']);
			if(!empty($_POST['ask_reply']))
				$rb=strtotime($_POST['Date_Year'].'-'.$_POST['Date_Month'].'-'.$_POST['Date_Day']);
			else
				$rb=time()+3600*7;
			foreach($touserid as $key=>$revid)
			{
				if(is_array($_POST['inquire_con']))
					$inquire_con=$_POST['inquire_con'][$key];
				$sql="insert into ".FEEDBACK."
					(touserid,fromuserid,fromInfo,sub,con,date,inquire_con,msgtype,receive_type,reply_by,attachments)
					VALUES
					('$revid','$buid','$fromInfo','$_POST[sub]','$_POST[con]','$date','$inquire_con','1','$rt','$rb','$attachments')";
				$res=$this->db->query($sql);
				
				if($buid)
				{
					$sql="insert into ".FEEDBACK."
						(touserid,fromuserid,fromInfo,sub,con,date,msgtype)
						VALUES
						('$revid','$buid','$fromInfo','$_POST[sub]','$_POST[con]','$date','2')";
					$res=$this->db->query($sql);
				}
				$this->send_email_for_inquery($ar=array('toid'=>$revid,'sub'=>$_POST['sub'],'con'=>$_POST['con']));
			}
			//============================
			return $res;
		}
	}
	
	//发送电子邮件给客户
	function send_email_for_inquery($ar=array())
	{	
		global $config;
		if(!empty($ar))
		{
			$sql="select user,email from ".ALLUSER." where userid='$ar[toid]'";
			$this->db->query($sql);
			$user=$this->db->fetchRow();
			if(!empty($user['email']))
			{
				$mail_temp=get_mail_template('sendmsg');
				$con=$mail_temp['message'];
				$subject=$mail_temp['subject'];
				$con=str_replace("[username]",$user["user"],$con);
				$con=str_replace("[content]",$ar["con"],$con);
				send_mail($user["email"],$user["user"],$ar["sub"],$con);
				unset($con);
			}
		}
	}
	//-------------会员中心发邮件---------------------------
	function friend_msg_batch_send()
	{	
		global $buid,$admin;
		if(!empty($_POST['senduser'])&&!empty($_POST['msgcon']))
		{
			$date=date("Y-m-d H:i:s");
			$sear=explode(';',$_POST['senduser']);
			if(count($sear)>1)
			{
				$sear1=array_unique($sear);
				$suser="'0'";
				foreach($sear1 as $v)
				{
					$suser.=",'$v'";
				}
				$sql="select user,email,userid from ".ALLUSER." where user in ($suser)";
			}
			else
				$sql="select user,email,userid from ".ALLUSER." where user ='$_POST[senduser]'";
			$this->db->query($sql);
			$re=$this->db->getRows();
			foreach($re as $v)
			{
				$sql="insert into ".FEEDBACK." (touserid,fromuserid,fromInfo,sub,con,date,msgtype) VALUES 
				('$v[userid]','$buid','Business Friends Message','$_POST[msgtitle]','$_POST[msgcon]','$date','1')";
				$this->db->query($sql);
						
				$sql="insert into ".FEEDBACK." (touserid,fromuserid,fromInfo,sub,con,date,msgtype) VALUES 
				('$v[userid]','$buid','Business Friends Message','$_POST[msgtitle]','$_POST[msgcon]','$date','2')";
				$this->db->query($sql);
				//-----------------如果是回复邮件标记为已回复
				$this->db->query("UPDATE ".FEEDBACK." set iflook='3' where id='$_GET[id]'");
				
				if(!empty($_POST['semail'])&&$v["email"])
				{
					send_mail($v["email"],$v["name"],$_POST['msgtitle'],$_POST['msgcon']);
				}
			}
			$admin->msg("main.php?m=message&s=admin_message_list_inbox");//发送成功
		}
		else
			$admin->msg("main.php?m=message&s=admin_message_sed&msgsend=error");
	}

}
?>
