<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Msg extends CI_Controller {

	function __construct(){
		parent::__construct();
        	$this->load->helper('url');
        	$this->load->helper('form');
                $this->load->library('session'); //加载session
		$this->load->model('CsdjDB');
		$this->load->model('CsdjSkins');
		$this->load->model('CsdjUser');
		$this->CsdjUser->User_Login();
	}

	public function index()
	{
                $data='';
                $op = $this->input->get('op', TRUE);   //op，默认为空为全部，xt为系统消息，wd为未读消息
                $page = intval($this->input->get('page', TRUE));   //page
                if($page==0) $page=1;


                $this->load->get_user_templates();
		$template=$this->load->view('msg-list.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","消息 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[msg:op]",$op,$Mark_Text); //CID

		//预先除了分页
		$pagenum=$this->CsdjSkins->GetPageNum($Mark_Text);
		preg_match_all('/{cscms:msg(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/cscms:msg}/',$Mark_Text,$page_arr);//判断是否有分页标识
		if(!empty($page_arr) && !empty($page_arr[2])){

                        $sqlstr="select * from ".CS_SqlPrefix."xiaoxi where CS_Usera='".$this->session->userdata('cs_name')."'";
                        if($op=='xt'){
                            $sqlstr.=" and cs_userb='管理员'";
                        }elseif($op=='wd'){
                             $sqlstr.=" and cs_did=0";
                        }

                        $sqlstr.=" order by CS_AddTime desc";
                        $data_content='';
			$Arr=$this->CsdjSkins->SpanUsersPage($sqlstr,$page_arr[2][0],$pagenum,'user/msg?op='.$op,$page);//sql,每页显示条数
			$result=$this->CsdjDB->db->query($Arr[2]);
			$recount=$result->num_rows();
			if($recount==0){
				$data_content="<div align=center><strong><font style=font-size:12px;>暂无数据！</font></strong></div>";
			}else{
				if($result){
					$sorti=1;
					foreach ($result->result() as $row2) {
						$datatmp=$this->CsdjSkins->DataMsg($page_arr[0][0],$page_arr[3][0],$row2,$sorti);
						$sorti=$sorti+1;
						$data_content.=$datatmp;
					}
				}
			}// end if recount
			$Mark_Text=$this->CsdjSkins->Page_Mark($Mark_Text,$Arr);	
			$Mark_Text=str_replace($page_arr[0][0],$data_content,$Mark_Text);
			unset($Arr);
                }
		unset($page_arr);
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
	        $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
                $this->CsdjSkins->skins($Mark_Text,$data);
        }

        //消息内容
	public function show()
	{
            $data='';$data_content='';
            $id = intval($this->uri->segment(5));   //ID
                
            if($id==0){
                exit($this->CsdjSkins->Msg_url('出错了，ID不能为空！','javascript:history.back();'));
            }

		$rowm=$this->CsdjDB->get_select ('xiaoxi','CS_ID','*',''.$id.'');
		if(!$rowm){
                	exit($this->CsdjSkins->Msg_url('抱歉，该消息记录不存在！','javascript:history.back();'));
		}
                if($rowm[0]->CS_DID==0){ //变更为已读
                     $this->db->query("update ".CS_SqlPrefix."xiaoxi set cs_did=1 where cs_id='".$id."'");
                }

                $this->load->get_user_templates();
		$template=$this->load->view('msg-show.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","阅读消息 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
		$Mark_Text=$this->CsdjSkins->DataMsg($Mark_Text,$Mark_Text,$rowm[0],$this->session->userdata('cs_name')); //全部解析
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
	        $rowu=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$rowu);
		$this->CsdjSkins->skins($Mark_Text,$data);
	}

	public function add()
	{
                $data='';
                $user = $this->CsdjSkins->str_checkhtml($this->input->get('user', TRUE)); //接收会员
                $this->load->get_user_templates();
		$template=$this->load->view('msg-add.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","消息 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[user:msgaddsave]",site_url('user/msg/add_save'),$Mark_Text); //发表连接
		$Mark_Text=str_replace("[msg:usera]",$user,$Mark_Text); //接收会员

        $token=md5(uniqid(rand(), true));
		$this->session->set_userdata('token',$token);
		$Mark_Text=str_replace("[user:token]",$token,$Mark_Text);

                if(empty($user)){
		     $Mark_Text=str_replace("[msg:name]","",$Mark_Text); //回复标题
                }else{
		     $Mark_Text=str_replace("[msg:name]","回复：".$user,$Mark_Text); //回复标题
                }

		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
                $this->CsdjSkins->skins($Mark_Text,$data);
        }

	public function add_save()
	{
                $msg['cs_name']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_name', TRUE));   //标题
                $msg['cs_usera']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_usera', TRUE));   //接收会员
                $msg['cs_neir']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_neir', TRUE));   //消息内容

				//token check
                $token=$this->input->post('token', TRUE);
				if(!$this->session->userdata('token') || $token!=$this->session->userdata('token'))  $this->CsdjSkins->Msg_url('非法提交数据!','javascript:history.back();'); 

                if(empty($msg['cs_name']))  $this->CsdjSkins->Msg_url('消息标题不能为空!','javascript:history.back();'); 
                if(empty($msg['cs_usera']))  $this->CsdjSkins->Msg_url('接收会员不能为空!','javascript:history.back();'); 
                if(empty($msg['cs_neir']))  $this->CsdjSkins->Msg_url('内容不能为空!','javascript:history.back();'); 

                //判断接收会员是否存在
	        $row=$this->CsdjDB->get_select('user','CS_Name','CS_ID',''.$msg['cs_usera'].'');
	        if(!$row){
                      $this->CsdjSkins->Msg_url('接收会员不存在，请认真填写!','javascript:history.back();'); 
                }

                $msg['CS_Userb']=$this->session->userdata('cs_name');
                $msg['CS_AddTime']=date('Y-m-d H:i:s');
                $res=$this->CsdjDB->get_insert('xiaoxi',$msg);
                if($res>0){
                       $this->session->unset_userdata('token');
                       $this->CsdjSkins->Msg_url('恭喜您，消息发送成功!',site_url('user/msg')); 
                }else{
                       $this->CsdjSkins->Msg_url('抱歉，消息发送失败!','javascript:history.back();'); 
                }
        }

	public function del()
	{
                $op=$this->input->get('op', TRUE);   //op all为全部，yue为一个月以前
                $id=intval($this->input->get('id', TRUE));   //id
                if($id==0 && empty($op)) $this->CsdjSkins->Msg_url('抱歉，参数错误!','javascript:history.back();');  

                if($op=='all'){ //删除全部
                     $this->db->query("delete from ".CS_SqlPrefix."xiaoxi where cs_usera='".$this->session->userdata('cs_name')."'");
                }elseif($op=='yue'){ //删除一个月以前的记录
                     $this->db->query("delete from ".CS_SqlPrefix."xiaoxi where cs_usera='".$this->session->userdata('cs_name')."' and cs_addtime<date_sub(date(now()),interval 1 month)");
                }elseif($id>0){  //按ID
                     $this->db->query("delete from ".CS_SqlPrefix."xiaoxi where cs_usera='".$this->session->userdata('cs_name')."' and cs_id='$id'");
                }
                $this->CsdjSkins->Msg_url('恭喜您，删除成功!',@$_SERVER['HTTP_REFERER']);  
        }
}


