<?php
if (!defined('HUCART_ORG')) exit('HuCart.COM No direct script access allowed');
class User extends HC_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load_lang('adminsys/user');
		$this -> load('admin'); 
		$this -> admin -> online_admin();
	}
	
	function info_list(  )
	{
		
		$page_size = 20 ;		
		$keywordval 	= $this -> input ->  funpost("keyword");
		$page_num_val 	= $this -> input ->  req["page_num"];
		$searchtypeval 	= $this -> input ->  req["searchtype"];
		
		//var_dump( $searchtypeval );
		
		if( $page_num_val > 1 )
		{
			$page_num  = $page_num_val;
			$limit_num = ceil( ( $page_num_val -1 ) * $page_size );
		}else{
			$page_num  = 1;
			$limit_num = 0 ;
		}

		
		if($this -> input ->  get["keyword"] !='')
		{   
			$keywordval 	= $this -> input ->  get["keyword"];
			$keywordval  	= urldecode($keywordval); 
		}
		if($keywordval!='')
		{ 	
			//$endArray["keyword"] =$keywordval;
			$endurl 	   .= '&keyword='.urlencode($keywordval);
			
			//$whereadd .= " AND (  use_name LIKE  '%".$keywordval."%' )";
			
			if( $searchtypeval =='' )
			{
				$whereadd .= " AND (  use_name LIKE  '%".$keywordval."%' )"; 
				$searchtypeval['0'] = 'use_name';
				//$endArray["searchtype[0]"] = 'use_name';
			}else if($searchtypeval[0] !='' and $searchtypeval[1] !='')
			{
				$whereadd .= " AND  (  ".$searchtypeval[0]." LIKE  '%".$keywordval."%' OR ".$searchtypeval[1]." LIKE  '%".$keywordval."%' )";
				//$endArray["searchtype[0]"] =$searchtypeval[0];
				//$endArray["searchtype[1]"] =$searchtypeval[1];
			}else if($searchtypeval[0] !='' and $searchtypeval[1] =='')
			{
				$whereadd .= " AND  (  ".$searchtypeval[0]." LIKE  '%".$keywordval."%' )";
				//$endArray["searchtype[0]"] =$searchtypeval[0];
			}else if( $searchtypeval[0] =='' and $searchtypeval[1] !='')
			{
				$whereadd .= " AND  (  ".$searchtypeval[1]." LIKE  '%".$keywordval."%' )";
				//$endArray["searchtype[1]"] =$searchtypeval[1];
			}

			
		}
		
		
		//echo $whereadd;
		//exit();

		$listval  = $this -> input ->  get["listval"];
		$listtype = $this -> input ->  get["listtype"];

		$orderbyval = "";
		if($listval!='' and $listtype =='DESC' )
		{
			//$endArray["listtype"] = 'DESC';
			//$endArray["listval"]  = $listval;
			$orderbyval = $listval ."  DESC ";
			$listhtml = '';	//反向排序参数
			
		}elseif($listval!='' )
		{
			$orderbyval = $listval;
			$listhtml = 'DESC';
			
			//$endArray["listtype"] = '';
			//$endArray["listval"]  = $listval;

		}else{
			//$endArray["listtype"] = 'DESC';
			//$endArray["listval"]  = 'use_id';
			$orderbyval = " `use_id` DESC , `use_regtime` DESC ";	
		}
		

		//var_dump( $searchtypeval );

		 //["searchtype"]=> array(5) { [0]=> string(9) "user_name" [3]=> string(10) "userd_nick" [4]=> string(8) "userd_qq" [5]=> string(12) "userd_mobile" [6]=> string(5) "phone" }
		
		//帐号 	真实姓名 	Email地址 	登录次数 	加入时间 	最后登录时间
		
		$sql_query = " SELECT use_id, use_name,  use_email, use_locked, use_logcount ,use_regtime ,use_logintime  FROM  ".$this -> db -> prefix."users  WHERE 1  $whereadd  GROUP BY use_id ORDER BY  $orderbyval LIMIT  $limit_num , $page_size ";
		
		$info = $this -> db -> select_info($sql_query);
		
		$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."user  WHERE 1 $whereadd  ORDER BY `use_id`  ";
		
		$infocount = $this -> db -> select_one($sql_query);
		
		$page_str = $this -> page ->get_admin_page( $url = '?load=user&act=info_list', $page_num  , $page_size  , $result_count = $infocount["CNUM"], $endArray );
		
		
		$this -> view -> assign('searchtype',$searchtypeval);
		
		$this -> view -> assign('listval',$listval);
		$this -> view -> assign('listtype',$listtype);
 
		$this -> view -> assign('keywordval',$keywordval);
		$this -> view -> assign('page_str',$page_str);
		$this -> view -> assign('info',$info);
		$this -> view -> display('user.tpl');
		
		
	}
	
	function add(  )
	{
		//$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24

		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=user&act=add_info");
		$this -> view -> display('useradd.tpl');
	}
	
	function add_info()
	{
		$use_nameval 		= $this -> input ->  funpost("use_name");
		
		$use_emailval 		= $this -> input ->  funpost("use_email");
		$use_pwdval 		= $this -> input ->  funpost("use_pwd");
		$reuse_pwdval 		= $this -> input ->  funpost("reuse_pwd");

		$use_regtimeval 	= time();
		
		if($use_nameval !='' and $use_emailval !='' and $use_pwdval !='' and $reuse_pwdval == $use_pwdval)
		{
			$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."user  WHERE use_name = '".$use_nameval."' ORDER BY `use_id` ";
			
			$info = $this -> db -> select_one( $sql_query );
			
			if( $info["CNUM"] < 1 )
			{
				//MD5加密码！！
				$md5use_pwdval = md5($use_pwdval);
				
				$insertuser ="INSERT INTO `".$this -> db -> prefix."user` (`use_name` , `use_email` , `use_pwd` , `use_regtime` ) VALUES (  '$use_nameval',  '$use_emailval', '$md5use_pwdval', '$use_regtimeval')";
				$userid = $this -> db -> insertSql($insertuser);
				
				$usd_truenameval 	= $this -> input ->  funpost("usd_truename");
				$usd_sexval 		= $this -> input ->  funpost("usd_sex");
				$usd_nickval 		= $this -> input ->  funpost("usd_nick");
				$usd_birthdayval 	= $this -> input ->  funpost("usd_birthday");
				$usd_saltval 		= $this -> input ->  funpost("usd_salt");
				$usd_msnval 		= $this -> input ->  funpost("usd_msn");
				$usd_qqval 			= $this -> input ->  funpost("usd_qq");
				$usd_officephoneval = $this -> input ->  funpost("usd_officephone");
				$usd_homephoneval 	= $this -> input ->  funpost("usd_homephone");
				$usd_mobileval 		= $this -> input ->  funpost("usd_mobile");
				
				$usd_provinceval 	= $this -> input ->  funpost("usd_province");
				
				$usd_provinceval 	= $this -> input ->  funpost("idprovince");
				$usd_cityval 		= $this -> input ->  funpost("idcity");
				$usd_districtval 	= $this -> input ->  funpost("iddistrict");
				$usd_pcdallval		= $this -> input ->  funpost("hiprovince").$this -> input ->  funpost("hicity").$this -> input ->  funpost("hidistrict");
				
				$usd_addressval 	= $this -> input ->  funpost("usd_address");				

				$insertuserdetail ="INSERT INTO `".$this -> db -> prefix."userdetail` (`use_id` , `usd_truename` , `usd_sex` , `usd_nick`,`usd_birthday`,`usd_salt`,`usd_msn` ,`usd_qq`,`usd_officephone`,`usd_homephone`,`usd_mobile`,`usd_province`,`usd_city`,`usd_district`,`usd_address`) VALUES (  '$userid',  '$usd_truenameval', '$usd_sexval', '$usd_nickval', '$usd_birthdayval', '$usd_saltval', '$usd_msnval', '$usd_qqval', '$usd_officephoneval', '$usd_homephoneval', '$usd_mobileval', '$usd_provinceval', '$usd_cityval', '$usd_districtval', '$usd_addressval')";
				$this -> db -> insertSql($insertuserdetail);
				
				
				$this -> view ->  assign('info_title',$this->lang['add_success']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=user&act=info_list");
				$this -> view ->  display('suggestion.tpl');	
			}else{
				
				$this -> view ->  assign('info_title',$this->lang['err_info_exist']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=user&act=add");
				$this -> view ->  display('suggestion.tpl');	
			}
			
			
		}else{
			
			$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=user&act=add");
			$this -> view ->  display('suggestion.tpl');		}
			//var_dump($_REQUEST);
	}
	
	function del( )
	{
		$infoid = $this -> input ->  get["id"];
		
		//查询一下是否超级帐号
		$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."user  WHERE `use_id` in ( $infoid ) and use_name = '".EMPLOYEE_BASEUSER."'   ";
			
		$infocnum = $this -> db -> select_one( $sql_query );

		
		if($infocnum["CNUM"]> 0 )
		{
			$this -> view ->  assign('info_title',$GLOBALS['_LANG']['bd_deladminerror']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=user&act=info_list");
			$this -> view ->  display('suggestion.tpl');
			
		}else{
			$deletesql ="DELETE FROM `".$this -> db -> prefix."user` WHERE `use_id` in ( $infoid )  ";
			$this -> db -> query_sql($deletesql);
			
			$deletesqldetail ="DELETE FROM `".$this -> db -> prefix."userdetail` WHERE `use_id` in ( $infoid )  ";
			$this -> db -> query_sql( $deletesqldetail );
			
			$this -> view ->  assign('info_title',$this->lang['del_success']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=user&act=info_list");
			$this -> view ->  display('suggestion.tpl');
		}
		

		
	}
	
	function info(  )
	{
		$infoid = $this -> input ->  get["id"];
		
		$sql_query = " SELECT u.use_id ,u.use_name, u.use_email ,d.usd_truename,d.usd_sex,d.usd_nick,d.usd_birthday,d.usd_salt ,d.usd_msn,d.usd_qq,d.usd_officephone,d.usd_homephone ,d.usd_mobile,d.usd_province,d.usd_city,d.usd_district,d.usd_address  FROM  ".$this -> db -> prefix."user as u  left join  ".$this -> db -> prefix."userdetail as d  on d.use_id = u.use_id  WHERE u.use_id = '".$infoid."' ORDER BY u.`use_id` ";
		$info = $this -> db -> select_one($sql_query);
		
		//var_dump( $info );
		
		//$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24 
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=user&act=edit_info");
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('useredit.tpl');
	
	}
	
	function infopwd(  )
	{
		$infoid = $this -> input ->  get["id"];
		
		$sql_query = " SELECT use_id ,use_name,use_type  FROM  ".$this -> db -> prefix."user   WHERE use_id = '".$infoid."' ORDER BY `use_id` ";
		
		$info = $this -> db -> select_one($sql_query);
		
		if(EMPLOYEE_BASEUSER == $info["use_name"])
		{
			$this -> view ->  assign('info_title',$GLOBALS['_LANG']['db_adminbaseuser']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=user&act=info_list");
			$this -> view ->  display('suggestion.tpl');

		}else{
		
		//$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24 
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=user&act=edit_infopwd");
		$this -> view ->  assign('info',$info);
		
		$this -> view ->  display('usereditpwd.tpl');
		}
	}
	
	function edit_info( )
	{	
	
		$use_idval 	= $this -> input ->  funpost("use_id");
				
		//$use_nameval 	= $this -> input ->  funpost("use_name"); //不可修改
		
		$use_emailval 	 = $this -> input ->  funpost("use_email");


		$addsetUp = '';
		if($use_idval !='' and $use_emailval !='' )
		{
		
			$updateuser = "UPDATE `".$this -> db -> prefix."user` SET    use_email = '".$use_emailval."'   WHERE  use_id = '".$use_idval."' ";
			
			$this -> db -> query_sql( $updateuser );
			
			$usd_truenameval 	= $this -> input ->  funpost("usd_truename");
			$usd_sexval 	 	= $this -> input ->  funpost("usd_sex");
			
			$usd_nickval 	 	= $this -> input ->  funpost("usd_nick");
			$usd_birthdayval 	= $this -> input ->  funpost("usd_birthday");
			
			$usd_saltval 	 	= $this -> input ->  funpost("usd_salt");
			$usd_msnval 	 	= $this -> input ->  funpost("usd_msn");

			$usd_qqval 			= $this -> input ->  funpost("usd_qq");
			$usd_officephoneval = $this -> input ->  funpost("usd_officephone");
			
			$usd_homephoneval 	= $this -> input ->  funpost("usd_homephone");
			$usd_mobileval 		= $this -> input ->  funpost("usd_mobile");
			
			$usd_provinceval 	= $this -> input ->  funpost("usd_province");
			$usd_addressval  	= $this -> input ->  funpost("usd_address");
			
			$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."userdetail  WHERE use_id = '".$use_idval."' ";
			
			$infoNUM = $this -> db -> select_one($sql_query);
			
			if( $infoNUM["CNUM"] < 1 )
			{
				$insertsql ="INSERT INTO `".$this -> db -> prefix."userdetail` (`use_id` , `usd_truename` , `usd_sex` , `usd_nick` , `usd_birthday` , `usd_salt` , `usd_msn` , `usd_qq` , `usd_officephone` , `usd_homephone` , `usd_mobile` , `usd_province` , `usd_address`  ) VALUES (  '$use_idval', '$usd_truenameval', '$usd_sexval', '$usd_nickval', '$usd_birthdayval', '$usd_saltval', '$usd_msnval', '$usd_qqval', '$usd_officephoneval', '$usd_homephoneval', '$usd_mobileval','$usd_provinceval','$usd_addressval')";
				$this -> db -> query_sql( $insertsql );	
			}else{
				$updateuserdetail = "UPDATE `".$this -> db -> prefix."userdetail` SET    usd_truename = '".$usd_truenameval."',usd_sex = '".$usd_sexval."',usd_nick = '".$usd_nickval."',usd_birthday = '".$usd_birthdayval."',usd_salt = '".$usd_saltval."',usd_msn = '".$usd_msnval."',usd_qq = '".$usd_qqval."',usd_officephone = '".$usd_officephoneval."',usd_homephone = '".$usd_homephoneval."',usd_mobile = '".$usd_mobileval."',usd_province = '".$usd_provinceval."',usd_address = '".$usd_addressval."'   WHERE  use_id = '".$use_idval."' ";
				$this -> db -> query_sql( $updateuserdetail );
			}
			
			

			$this -> view ->  assign('info_title',$this->lang['edit_success']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=user&act=info_list");
			$this -> view ->  display('suggestion.tpl');
				
		}else{
		
			$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=user&act=info&id=".$use_idval);
			$this -> view ->  display('suggestion.tpl');	
		}
		
	}

	function edit_infopwd( )
	{	
	
		$use_idval 		 = $this -> input ->  funpost("use_id");		
		$use_pwdval 	 = $this -> input ->  funpost("use_pwd");
		$use_repwdval 	 = $this -> input ->  funpost("use_repwd");

		$addsetUp = '';
		
		if($use_idval !='' and $use_pwdval !='' and  $use_repwdval !='' )
		{
			
			if($use_pwdval == $use_repwdval)
			{
				$updateuser = "UPDATE  `".$this -> db -> prefix."user` SET    use_pwd = '".md5($use_pwdval)."'   WHERE  use_id = '".$use_idval."' ";
			
				$this -> db -> query_sql( $updateuser );
				
				$this -> view ->  assign('info_title',$this->lang['edit_success']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=user&act=info_list");
				$this -> view ->  display('suggestion.tpl');		
					
			}else{
				
				$this -> view ->  assign('info_title',$GLOBALS['_LANG']['js_repwd']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=user&act=infopwd&id=".$use_idval);
				$this -> view ->  display('suggestion.tpl');	

			}

		}else{
		
			$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=user&act=infopwd&id=".$use_idval);
			$this -> view ->  display('suggestion.tpl');	
		}
		
	}


	
	function pwd()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
		//$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24	
		
		$this -> view ->  assign('use_name',$_SESSION["admin"]["adm_user"]);
		$this -> view ->  assign('act_url',ADMIN_WEBURL."/?load=user&act=act_pwd");
		$this -> view ->  display('pwd.tpl');	
		//exit();
	}
	
	function act_pwd()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
		$this -> load_db();
		
		$oldpwdval  	= $this -> input ->  post["oldpwd"];
		$use_pwdval		= $this -> input ->  post["use_pwd"];
		$reuse_pwdval 	= $this -> input ->  post["reuse_pwd"];
		
		$use_idval 		= $_SESSION["admin"]["adm_id"];
		
		$sql = " SELECT adm_id  FROM ".$this -> db -> prefix."admin WHERE adm_id= '".$use_idval."' AND  adm_pwd = '".md5($oldpwdval)."' LIMIT 1 ";
		$admin = $this -> db -> select_one($sql);

		if($use_pwdval <> $reuse_pwdval){

			//修改有误!
			$this -> view ->  assign('info_title',$this->lang['js_repwd']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/?load=user&act=pwd");	
			$this -> view ->  display('suggestion.tpl');
			
		}else if($admin["adm_id"]!='')
		{

			//修改成功！
			$sqlup = " UPDATE ".$this -> db -> prefix."admin  set  adm_pwd = '".md5($use_pwdval)."'  WHERE  adm_id= ".$use_idval;
			$this -> db -> query_sql($sqlup);
			$this -> view ->  assign('info_title',$this->lang['LA_pwdsusse']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/?load=user&act=pwd");
			$this -> view ->  display('suggestion.tpl');
			//exit();
		}else{
			//修改有误!
			$this -> view ->  assign('info_title',$this->lang['LA_pwderror']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/?load=user&act=pwd");	
			$this -> view ->  display('suggestion.tpl');
			
		}
		
		//exit();
	}
	
}


?>