<?php 
class CActionConfigUser extends CActionBase
{
	function doAction()
	{
		$ret = 0;
		$job = $_POST["job"];
		switch($job)
		{
			case "add":
				$ret = $this->Add();
			break;
			case "mod":
				$ret = $this->Mod();
			break;
			case "del":
				$ret = $this->Del();
			break;
			case "photo":
				$ret = $this->Photo();
			break;
			case "mod_passwd":
				$ret = $this->ModPasswd();
			break;
			case "mod_info":
				$ret = $this->ModInfo();
			break;
		}
		
		return $ret;
	}	
	
	function Add()
	{
		$user_name = $_POST["user_name"];
		$user_pwd	 = $_POST["user_pwd"];
		$real_name = $_POST["real_name"];
		$email		 = $_POST["email"];
		$ip				 = $_POST["ip"];
		$phone 		 = $_POST["phone"];
		$photo		 = $_POST["photo"];
		$dept_id	 = $_POST["dept_id"];
		$role_id 	 = $_POST["role_id"];
		$user_state= $_POST["user_state"];
		
		$nCount = $this->db->GetCount('tb_user', 'user_name', $user_name);
		if($nCount > 0) return 2; //用户名称相同
		
		$user_id = $this->db->GetMaxField('tb_user', 'user_id')+1;
		$sql = "insert into tb_user (user_id,user_name,user_pwd,real_name,dept_id,role_id,email,ip,photo,phone,state,create_time) values (";
		$sql = $sql . $user_id . ",";
		$sql = $sql . "'" . $user_name . "'," ;
		$sql = $sql . "'" . $user_pwd . "',"	;
		$sql = $sql . "'" . $real_name . "'," ;
		$sql = $sql . $dept_id .","	  ;
		$sql = $sql . $role_id."," 	  ;
		$sql = $sql . "'".$email."',"		  ;
		$sql = $sql . "'".$ip."',"				;
		$sql = $sql . "'".$photo."',"		  ;
		$sql = $sql . "'".$phone."',"		  ;
		$sql = $sql . $user_state. ",'now()'" ;
		$sql = $sql . ")";
		
		$this->db->ExeSql($sql);
		return 0;
	}
	
	function Mod()
	{
		$user_id = $_POST["user_id"];
		$user_name = $_POST["user_name"];
		$user_pwd	 = $_POST["user_pwd"];
		$real_name = $_POST["real_name"];
		$email		 = $_POST["email"];
		$ip				 = $_POST["ip"];
		$phone 		 = $_POST["phone"];
		$photo		 = $_POST["photo"];
		$dept_id	 = $_POST["dept_id"];
		$role_id 	 = $_POST["role_id"];
		$user_state= $_POST["user_state"];
		
		$sql = "update tb_user set ";
		$sql = $sql . "user_pwd ='".$user_pwd."',";
		$sql = $sql . "real_name='".$real_name."',";
		$sql = $sql . "dept_id=".$dept_id.",";
		$sql = $sql . "role_id=".$role_id.",";
		$sql = $sql . "email='".$email."',";
		$sql = $sql . "ip='".$ip."',";
		$sql = $sql . "photo='".$photo."',";
		$sql = $sql . "phone='".$phone."',";
		$sql = $sql . "state=".$user_state;
		$sql = $sql . " where user_id=".$user_id;
		
		$this->db->ExeSql($sql);
		return 0;
	}
	
	function Del()
	{
		$user_id = $_POST["user_id"];
		$sql = "delete from tb_user where user_id=".$user_id;
		$this->db->ExeSql($sql);
		return 0;
	}
	
	function Photo()
	{
		global $UserInfo, $FileInfo;
		$user_id = $UserInfo['UID'];
		$user_name = $UserInfo["UNAME"];
		
		$file_error = $_FILES["attach_photo"]["error"];
		if($file_error != UPLOAD_ERR_OK)
			return "<script>alert('上传相片失败')</script>";
		
		$file_name = $_FILES["attach_photo"]["name"];
		$file_tmp = $_FILES["attach_photo"]["tmp_name"];
		$file_size = $_FILES["attach_photo"]["size"];
		$file_type = strtolower(substr($file_name, strlen($file_name)-3, 3));
		require_once("../common/class.upload.php");
		$up = new CUpload();
		if(!$up->Execute($file_tmp, $FileInfo["photo_path"],$file_size, $file_type))
				return "<script> alert('".$up->GetError()."'); </script>";
				
		$sql = "update tb_user set photo='".$up->file_name ."' where user_id=".$user_id;
		$this->db->ExeSql($sql);
		return "<script>parent.user_photo.src='".$up->file_name."'</script>";
	}
	
	function ModPasswd()
	{
		global $UserInfo;
		$user_id = $UserInfo['UID'];
		$passwd = $_POST['passwd'];
		
		$sql = "update tb_user set user_pwd='".$passwd."' where user_id=".$user_id;
		$this->db->ExeSql($sql);
		return 0;
	}
	
	function ModInfo()
	{
		global $UserInfo;
		$user_id = $UserInfo['UID'];
		$real_name = $_POST['real_name'];
		$email = $_POST['email'];
		$ip = $_POST['ip'];
		$phone = $_POST['phone'];
		$dept_id = $_POST['dept_id'];
		
		$sql = "update tb_user set real_name = '".$real_name."', ";
		$sql = $sql . " email = '".$email."', ";
		$sql = $sql . " ip = '".$ip."', ";
		$sql = $sql . " phone = '".$phone."', ";
		$sql = $sql . " dept_id = ".$dept_id." ";
		$sql = $sql . " where user_id=".$user_id;
		$this->db->ExeSql($sql);
		return 0;
	}
	
};
?>
