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

class Gbook 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='';
                $page = intval($this->input->get('page', TRUE));   //page
                if($page==0) $page=1;


                $this->load->get_user_templates();
		$template=$this->load->view('gbook-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);

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

                        $sqlstr="select * from ".CS_SqlPrefix."gbook where CS_Usera='".$this->session->userdata('cs_name')."' order by CS_AddTime desc";
                        $data_content='';
			$Arr=$this->CsdjSkins->SpanUsersPage($sqlstr,$page_arr[2][0],$pagenum,'user/gbook',$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->DataGbook($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 add() //回复留言
	{
                $gbook['cs_usera'] = $this->CsdjSkins->str_checkhtml($this->input->get_post('cs_usera', TRUE)); //回复会员
                $gbook['cs_fid'] = intval($this->input->get_post('cs_fid', TRUE)); //回复ID
                $gbook['cs_hfneir']=$this->CsdjSkins->str_checkhtml($this->security->xss_clean($this->input->get_post('cs_hfneir', TRUE)));   //消息内容

                if($gbook['cs_fid']==0)  $this->CsdjSkins->Msg_url('参数错误，回复ID不能为空!','javascript:history.back();'); 
                if(empty($gbook['cs_usera']))  $this->CsdjSkins->Msg_url('参数错误，回复会员不能为空!','javascript:history.back();'); 
                if(empty($gbook['cs_hfneir']))  $this->CsdjSkins->Msg_url('内容不能为空!','javascript:history.back();'); 

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

                $gbook['cs_cid'] = 0;
                $gbook['CS_Userb']=$this->session->userdata('cs_name');
                $gbook['CS_AddTime']=date('Y-m-d H:i:s');
                $res=$this->CsdjDB->get_insert('gbook',$gbook);
                if($res>0){
                       $this->CsdjSkins->Msg_url('恭喜您，回复成功!',@$_SERVER['HTTP_REFERER']); 
                }else{
                       $this->CsdjSkins->Msg_url('抱歉，回复失败!','javascript:history.back();'); 
                }
        }

	public function del()
	{
                $id=intval($this->input->get('id', TRUE));   //id
                if($id==0) $this->CsdjSkins->Msg_url('抱歉，参数错误!','javascript:history.back();');  

                if($this->db->query("delete from ".CS_SqlPrefix."gbook where (cs_usera='".$this->session->userdata('cs_name')."' or cs_userb='".$this->session->userdata('cs_name')."') and cs_id='$id'")){
                    $this->db->query("delete from ".CS_SqlPrefix."gbook where cs_fid='$id'"); //删除回复
                }
                $this->CsdjSkins->Msg_url('恭喜您，删除成功!',@$_SERVER['HTTP_REFERER']);  
        }
}

