<?php
/**
 *----------------------------------------------------------------------------*
 * 版权所有 2010-2016 HuCart.COM，并保留所有权利。
 * 此程序是HuCart的一部分。  
 * 根据MIT许可证授权(在软件和软件的所有副本中都必须包含版权声明和许可声明。)
 * 官方网址:http://Www.HuCart.Com
 * 技术支持:http://Www.cheermi.com
 * 对于完整的版权和许可证信息，请参阅MIT-LICENSE.TXT
 * 程序的再发布必须保留以上版权声明。
 *
 *@author pgmk<pgmk@hucart.com>
 *@copyright pgmk<pgmk@hucart.com>
 *@link http://www.hucart.com
 *@license The MIT License.
 *----------------------------------------------------------------------------*
**/
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Message  extends HC_Controller
{
	//注意无须构造函数,使用了构造函数得重构父类 parent::__construct();
	function __construct()
	{
		parent::__construct();
		$this->load_lang('user/comment');
		$this -> load('user'); 
		$this -> user -> online_user();
		$this -> load_db();
		
		$this -> load_model('web_config'); 	//加载模型
		$web_config = $this -> web_config_mod -> get_sys_config();
		//$this -> set_view_dir( $web_config['temp'] -> dir );
		$this -> view -> assign('web_config', $web_config);

		$this -> load('helper'); //加载辅助类
		$user_menu = $this -> web_config_mod ->  get_user_menu();
		$get_menu  = $this -> web_config_mod -> get_web_menu();
		$get_link  = $this -> web_config_mod -> get_link();
		$get_user_link = $this -> session->get_user_link( $this->lang['user_link'] );
		
		$this -> view -> assign('user_menu',$user_menu);
		$this -> view -> assign('get_menu',$get_menu);
		$this -> view -> assign('get_link',$get_link);
		$this -> view -> assign('get_user_link',$get_user_link);
	}
 
 
	function send_info()
	{
		$user_message = $this -> user_message();
		$this -> view -> assign('user_message',$user_message);
		
		$webtitle = '发件箱 - 用户中心';
		
		//处理你现在的位置
		$now_address = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/user/">用户中心</a> > 发件箱';
		$this -> view -> assign('webtitle',$webtitle);
		$this -> view -> assign('now_address',$now_address);

		$ckeditorLoad = $this -> helper -> load_ckeditor( 'mes_content','','Basic',  '#D3D3D3',   '580' );	//加载文本编辑器
		$this -> view -> assign('ckeditorLoad',$ckeditorLoad);
		$this -> view -> assign('act_url',BASE_WEBURL."/user/?load=message&act=add_message");
		
		$this -> view -> display('message.tpl');
		
		
	}
	
	function add_message()
	{
		$mes_fus_idval 			= $_SESSION["user"]["use_id"];
		$mes_titleval 			= $this -> input -> req["mes_title"];
		$mes_contentval 		= $this -> input -> req["mes_content"];	
		$use_nameval 			= $this -> input -> req["use_name"];
		

		
		$sql_query = " SELECT use_id  FROM  ".$this -> db -> prefix."users WHERE use_name = '".$use_nameval."' ORDER BY `use_id` ";
		$info = $this -> db -> select_one($sql_query);
		
		if($info["use_id"] > 0 )
		{
			$mes_tus_idval   = $info["use_id"];
			//$use_email_val 	= $info["use_email"];
			//$mes_add_timeval = time();
			$insertsql = "INSERT INTO `".$this -> db -> prefix."message` (  `mes_tus_id`,`mes_fus_id`, `mes_title` , `mes_content` , `mes_add_time`  )
VALUES ( $mes_tus_idval , '$mes_fus_idval', '$mes_titleval', '$mes_contentval', '".time()."' )";
		
			$this -> db -> query_sql( $insertsql );
			$this -> view -> assign('info_title',$this->lang["addsend"]);
			$this -> view -> assign('URL_TIME',URL_TIME);
			$this -> view -> assign('get_url',BASE_WEBURL."/user/index.php?load=message&act=send_info");
			$this -> view -> display('suggestion.tpl');	
		}else{
			$this -> view -> assign('info_title',$this->lang["senderror"]);
			$this -> view -> assign('URL_TIME',URL_TIME);
			$this -> view -> assign('get_url',BASE_WEBURL."/user/index.php?load=message&act=send_info");
			$this -> view -> display('suggestion.tpl');	
		}
		
	
	}

	function replay_edit_info()
	{
		$mes_fus_idval 			= $_SESSION["user"]["use_id"];
		$mes_parent_idval 		= $this -> input -> req["mes_id"];
		$mes_titleval 			= $this -> input -> req["mes_title"];
		$mes_contentval 		= $this -> input -> req["mes_content"];	
		$use_nameval 			= $this -> input -> req["use_name"];
		$mes_tus_idval 			= $this -> input -> req["mes_tus_id"];

		$sql_query = " SELECT use_id,use_email  FROM  ".$this -> db -> prefix."users WHERE use_name = '".$use_nameval."' ORDER BY `use_id` ";
		$info = $this -> db -> select_one($sql_query);
		
		if($info["use_id"] > 0 )
		{
			$mes_tus_idval 	 = $info["use_id"];
			$use_emailval 	 = $info["use_email"];
			$mes_add_timeval = time();
			$insertsql = "INSERT INTO `".$this -> db -> prefix."message` (  `mes_tus_id`,`mes_parent_id`,`mes_fus_id`, `mes_title` , `mes_content` , `mes_add_time` )
VALUES ( $mes_tus_idval , '$mes_parent_idval','$mes_fus_idval', '$mes_titleval', '$mes_contentval', '$mes_add_timeval' )";
		
			$this -> db -> query_sql( $insertsql );
			$this -> view -> assign('info_title',$this->lang["addsend"]);
			$this -> view -> assign('URL_TIME',URL_TIME);
			$this -> view -> assign('get_url',BASE_WEBURL."/user/index.php?load=message&act=send_info");
			$this -> view -> display('suggestion.tpl');	
			
		}else{
			$this -> view -> assign('info_title',$this->lang["senderror"]);
			$this -> view -> assign('URL_TIME',URL_TIME);
			$this -> view -> assign('get_url',BASE_WEBURL."/user/index.php?load=message&act=send_info");
			$this -> view -> display('suggestion.tpl');	
		}
		
	
	}

	function user_message()
	{

		$sqladdress = " SELECT m.*, u.use_name  FROM  ".$this -> db -> prefix."message as m ,".$this -> db -> prefix."users as u   WHERE m.mes_tus_id = u.use_id and  m.mes_fus_id = ".$_SESSION["user"]["use_id"]."  GROUP BY m.mes_id ORDER BY m.`mes_id` LIMIT 0 , 1000";
		
		return  $this -> db -> select_info( $sqladdress );

	}
	
	function info()
	{
		$webtitle = '站内信息 - 用户中心';
		//处理你现在的位置
		$now_address = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/user/">用户中心</a> > 站内信息';
		
		$this -> view -> assign('webtitle',$webtitle);
		
		$this -> view -> assign('now_address',$now_address);
		
		
		$infoid = intval(trim($_REQUEST["id"]));

		$sql_query = " SELECT m.* ,u.use_name FROM ".$this -> db -> prefix."message as m , ".$this -> db -> prefix."users as u  WHERE m.mes_tus_id = u.use_id and  m.mes_id =  ".$infoid." and m.mes_fus_id = ".$_SESSION["user"]["use_id"] ;
		
		$info = $this -> db -> select_one($sql_query);
		
		$ckeditorLoad = $this -> helper -> load_ckeditor( 'mes_content',$info["mes_content"],'Basic', '#D3D3D3', '580' );	//加载文本编辑器
		$this -> view -> assign('ckeditorLoad',$ckeditorLoad);
		
		$this -> view ->assign('act_url',BASE_WEBURL."/user/index.php?load=message&act=edit_info");
		$this -> view -> assign('info',$info);
		$this -> view -> display('message_edit.tpl');
	
	}
	
	function read_info()
	{
		$infoid = intval(trim($_REQUEST["id"]));
		
		//读取信息
		$mes_readtimeval = time();
		$updatesql = "UPDATE `".$this -> db -> prefix."message` SET  mes_is_read = '1' ,  mes_read_time = '".$mes_readtimeval."' WHERE  mes_id = ".$infoid." and mes_tus_id = ".$_SESSION["user"]["use_id"] ;
			
		$this -> db -> query_sql( $updatesql );
		
		
		

		$sql_query = " SELECT m.* ,u.use_name FROM ".$this -> db -> prefix."message as m , ".$this -> db -> prefix."users as u  WHERE m.mes_tus_id = u.use_id and  m.mes_id =  ".$infoid." and m.mes_tus_id = ".$_SESSION["user"]["use_id"] ;
		
		$info = $this -> db -> select_one($sql_query);
		
		$ckeditorLoad = $this -> helper -> load_ckeditor( 'mes_content','','Basic', '#D3D3D3', '580' );	//加载文本编辑器
		$this -> view -> assign('ckeditorLoad',$ckeditorLoad);
		
		$this -> view ->assign('act_url',BASE_WEBURL."/user/index.php?load=message&act=replay_edit_info");
		$this -> view -> assign('info',$info);
		$this -> view -> display('message_read.tpl');
	
	}
	
	function receive_info()
	{
		$mes_tus_idval = $_SESSION["user"]["use_id"] ;
		
		$sqladdress = " SELECT m.*, u.use_name  FROM  ".$this -> db -> prefix."message as m ,".$this -> db -> prefix."users as u   WHERE m.mes_fus_id = u.use_id and  m.mes_tus_id = ".$mes_tus_idval."  GROUP BY m.mes_id ORDER BY m.`mes_id` LIMIT 0 , 1000";
		
		$receiveInfo = $this -> db -> select_info( $sqladdress );
				
		$this -> view -> assign('receiveInfo',$receiveInfo);
		
		$this -> view ->assign('act_url',BASE_WEBURL."/user/index.php?load=message&act=edit_info");
		
		$this -> view -> assign('info',$info);
		
		$webtitle = '收件箱 - 用户中心';
		//处理你现在的位置
		$now_address = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/user/">用户中心</a> > 收件箱';
		
		$this -> view -> assign('webtitle',$webtitle);
		
		$this -> view -> assign('now_address',$now_address);
		
		$this -> view -> display('message_replay.tpl');
		
	}
	
	
	function edit_info()
	{
		
		$mes_idval 				= $this -> input -> req["mes_id"];

		$mes_fus_idval 				= $_SESSION["user"]["use_id"];
		$mes_titleval 			= $this -> input -> req["mes_title"];
		$mes_contentval 		= $this -> input -> req["mes_content"];	
		$use_nameval 			= $this -> input -> req["use_name"];
		
		$sql_query = " SELECT use_id,use_email  FROM  ".$this -> db -> prefix."users WHERE use_name = '".$use_nameval."' ORDER BY `use_id` ";
		$info = $this -> db -> select_one($sql_query);


		if($info["use_id"] > 0 )
		{
			$mes_tus_idval = $info["use_id"];
			$use_emailval = $info["use_email"];
			$mes_add_timeval = time();
			$updatesql = "UPDATE `".$this -> db -> prefix."message` SET  mes_tus_id = '".$mes_tus_idval."' ,  mes_title = '".$mes_titleval."' , mes_content = '".$mes_contentval."'   WHERE  mes_id = '".$mes_idval."'  and mes_fus_id =  '".$mes_fus_idval."'";
			
			$this -> db -> query_sql( $updatesql );
			$this -> view -> assign('info_title',$this->lang["editsusse"]);
			$this -> view -> assign('URL_TIME',URL_TIME);
			$this -> view -> assign('get_url',BASE_WEBURL."/user/index.php?load=message&act=send_info");
			$this -> view -> display('suggestion.tpl');
			
			
		}else{
			$this -> view -> assign('info_title',$this->lang["senderror"]);
			$this -> view -> assign('URL_TIME',URL_TIME);
			$this -> view -> assign('get_url',BASE_WEBURL."/user/index.php?load=message&act=send_info");
			$this -> view -> display('suggestion.tpl');
		
		}
	
			
		
	}
	
	function del( )
	{
		$infoid = intval(trim($_REQUEST["id"]));
		
		$insertip 	= "  DELETE FROM `".$this -> db -> prefix."message` WHERE `mes_id` in ( $infoid ) and mes_fus_id = ".$_SESSION["user"]["use_id"] ;
		$this -> db -> query_sql($insertip);
		$this -> view -> assign('info_title',$this->lang['bd_delsusse']);
		$this -> view -> assign('URL_TIME',URL_TIME);
		$this -> view -> assign('get_url',BASE_WEBURL."/user/index.php?load=message&act=send_info");
		$this -> view -> display('suggestion.tpl');
	}
	
	function short_info()
	{
		$sql_short = " SELECT * FROM  ".$this -> db -> prefix."short_info as s   WHERE   sho_tus_id = ".$_SESSION["user"]["use_id"]."  GROUP BY sho_id ORDER BY `sho_id` LIMIT 0 , 100";
		
		$shortInfo = $this -> db -> select_info( $sql_short );
			
		$webtitle = '短信提醒 - 用户中心';
		//处理你现在的位置
		$now_address = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/user/">用户中心</a> > 短信提醒';
		
		$this -> view -> assign('webtitle',$webtitle);
		
		$this -> view -> assign('now_address',$now_address);
				
		$this -> view -> assign('shortInfo',$shortInfo);	
		$this -> view -> display('short_info.tpl');
		
	}
	
	function del_short( )
	{
		$infoid = intval(trim($_REQUEST["id"]));
		
		$insertip 	= "  DELETE FROM `".$this -> db -> prefix."short_info` WHERE `sho_id` in ( $infoid ) and mes_tus_id = ".$_SESSION["user"]["use_id"] ;
		$this -> db -> query_sql($insertip);
		$this -> view -> assign('info_title',$this->lang['bd_delsusse']);
		$this -> view -> assign('URL_TIME',URL_TIME);
		$this -> view -> assign('get_url',BASE_WEBURL."/user/index.php?load=message&act=short_info");
		$this -> view -> display('suggestion.tpl');
	}
	
	function read_short_info(  )
	{
		$infoid = intval(trim($_REQUEST["id"]));
		
		//读取信息
		$sho_readtimeval = time();
		$updatesql = "UPDATE `".$this -> db -> prefix."short_info` SET  sho_isread = '1' ,  sho_readtime = '".$sho_readtimeval."' WHERE sho_id = ".$infoid." and mes_tus_id = ".$_SESSION["user"]["use_id"] ;
		$this -> db -> query_sql( $updatesql );

		$sql_query = " SELECT * FROM ".$this -> db -> prefix."short_info   WHERE  sho_id =  ".$infoid." and mes_tus_id = ".$_SESSION["user"]["use_id"] ;
		
		$info = $this -> db -> select_one($sql_query);

		$this -> view -> assign('info',$info);
		$this -> view -> display('short_info_read.tpl');
	
	}
	
	function book()
	{
		//现推荐8个最新商品
		
		$sql_query 	= " SELECT pro_id, pro_name ,pro_img, pro_up_time   FROM  ".$this -> db -> prefix."product   WHERE pro_enabled = 1 $addsql  GROUP BY  pro_id ORDER BY pro_up_time DESC , `pro_id` LIMIT  0 , 8 ";
		$info 		= $this -> db ->  select_info( $sql_query );
		//var_dump($info);
		$this -> view -> assign('info',$info);
		
		$webtitle = '我的订阅 - 用户中心';
		//处理你现在的位置
		$now_address = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/user/">用户中心</a> > 我的订阅';
		
		$this -> view -> assign('webtitle',$webtitle);
		
		$this -> view -> assign('now_address',$now_address);
		
		$this -> view -> display('book_list.tpl');
			
	}
}

 